---
title: Vector Search
---

## Basic Usage

<Note>
  Creating an [HNSW index](/documentation/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;
```
