Cosine Similarity for Retrieval
Cosine similarity measures the angle between two vectors — the standard distance metric for semantic search.
Measuring Similarity
How close are two vectors? Three common distance metrics:
- Cosine similarity — angle between vectors
- Dot product — projection
- Euclidean (L2) distance — straight-line distance
For text embeddings, cosine is the default.
Cosine Similarity Formula
For vectors A and B:
cos(A, B) = (A . B) / (|A| * |B|)
Result is between -1 and 1:
- 1 = identical direction
- 0 = orthogonal (unrelated)
- -1 = opposite