Similarity Metrics: Cosine, Euclidean, and Dot Product
You'll learn how vector databases measure "closeness" between embeddings to find the most relevant documents.
The Core Problem
When a RAG system retrieves documents, it must decide which embedding vectors are most similar to your query embedding. But vectors are just lists of numbers—how do you measure if two vectors are "close"? That's where similarity metrics come in.
Three Essential Metrics
Cosine Similarity
Measures the angle between two vectors, ignoring their magnitude. Think of two arrows: are they pointing in the same direction?
- Range: -1 (opposite) to 1 (identical direction)
- Best for: Text embeddings where direction matters more than length
- Formula captures orientation, not distance
Analogy: Two people walking—cosine asks "are they heading the same way?" not "how far apart are they?"
Euclidean Distance
Measures the straight-line distance between vector endpoints in space, like measuring with a ruler.
- Range: 0 (identical) to ∞ (very different)
- Best for: When absolute positioning matters
- Sensitive to vector magnitude
Analogy: Physical distance between two cities on a map.
Dot Product
Multiplies corresponding elements and sums them—combines both direction AND magnitude.
- Range: -∞ to ∞
- Best for: When both alignment and strength matter
- Computationally fastest
Analogy: How much two forces reinforce each other when applied together.
Why This Matters for RAG
Your vector database uses one of these metrics to rank documents. Cosine similarity dominates text retrieval because sentence embeddings are normalized—we care if concepts align, not if documents are "longer." Euclidean works better for image embeddings or when scale matters.
Key Takeaway: Cosine similarity measures directional alignment (preferred for text), Euclidean measures absolute distance, and dot product combines both—choosing the right metric determines which documents your RAG system retrieves.