---
title: Vector Search
noindex: true
---

<Danger>
  **Legacy Docs:** This page describes our legacy API. It will be deprecated in
  a future version. Please use the [v2 API](/) where possible.
</Danger>

## Basic Usage

<Note>
  Creating an [HNSW index](/legacy/similarity/index) over a table can
  significantly improve query times.
</Note>

Vectors can be searched using L2 distance, cosine distance, or inner product.

```sql
-- L2 distance
SELECT * FROM mock_items ORDER BY embedding <-> '[1,2,3]'::vector;

-- Cosine distance
SELECT * FROM mock_items ORDER BY embedding <=> '[1,2,3]'::vector;

-- Inner product
SELECT * FROM mock_items ORDER BY embedding <#> '[1,2,3]'::vector;
```

## Sparse Vector Search

The following code block demonstrates the equivalent for sparse vector search.

```sql
-- L2 distance
SELECT * FROM items ORDER BY embedding <-> '{1:3,3:1,5:2}/5' LIMIT 5;

-- Cosine distance
SELECT * FROM items ORDER BY embedding <=> '{1:3,3:1,5:2}/5' LIMIT 5;

-- Inner product
SELECT * FROM items ORDER BY embedding <#> '{1:3,3:1,5:2}/5' LIMIT 5;
```
