Similarity Search
Vector Search
Basic Usage
Creating an HNSW index over a table can significantly improve query times.
Vectors can be searched using L2 distance, cosine distance, or inner product.
-- 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.
-- 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;
Was this page helpful?