Overview

Vector similarity scores how close two embeddings are; search ranks stored vectors by it. Cosine similarity compares direction (1 means the same direction) and ignores magnitude. Dot product is the unnormalized inner product and equals cosine on unit-length vectors. Euclidean (L2) distance measures geometric distance, where smaller is closer.

Use the metric the embedding model was trained for, as its documentation states; most text embedding models are built for cosine. Normalize vectors if you switch to dot product, since a mismatch quietly degrades ranking.

Exact search compares the query with every vector, which is accurate but slow on large collections. Approximate nearest-neighbor indexes such as HNSW and IVFFlat trade a small recall loss for speed; use exact search when the collection is small or recall must be perfect. A reranker can refine the ranking afterward.

Example

-- pgvector: <=> is cosine distance, so 1 - distance is similarity
SELECT id, content, 1 - (embedding <=> $1) AS score
FROM documents
ORDER BY embedding <=> $1
LIMIT 10;