Guide / cosine similarity in vector search
Cosine Similarity Explained for Vector Search
Cosine similarity measures direction between vectors. It is a useful ranking signal, not a correctness guarantee.
What cosine similarity measures
Cosine similarity compares the orientation of two vectors. If two normalized vectors point in a similar direction, their cosine similarity is high.
The common formula is cosine(a, b) = (a dot b) / (||a|| ||b||). Many embedding systems normalize vectors so cosine similarity and dot product become closely related.
Why direction can approximate semantic relatedness
Embedding models place texts with related statistical contexts near similar directions. That is why a query about login failure can retrieve a document about authentication errors.
The metric does not inspect truth, recency, permission, or operational safety. It only compares vectors produced by a model.
Cosine, dot product, and L2 distance
Cosine focuses on angle. Dot product can also include magnitude. L2 distance measures Euclidean distance. Which metric is appropriate depends on how the embedding model was trained and whether vectors are normalized.
Complexity Table
| Metric | Measures | Good for | Risk |
|---|---|---|---|
| Cosine similarity | Vector orientation | Normalized embeddings | Ignores magnitude and correctness |
| Dot product | Alignment plus magnitude | Models trained for dot product | Magnitude can dominate |
| L2 distance | Raw coordinate distance | Geometric nearest neighbors | Scale sensitivity |
When to Use This
- Use cosine similarity when the embedding model expects angular comparison or normalized vectors.
- Use it as candidate retrieval, then validate final quality with reranking and judged queries.
When Not to Use This
- Do not treat high cosine similarity as proof that an answer is true.
- Do not compare scores across different embedding models without calibration.
Production Failure Modes
High cosine similarity can retrieve text that is related but wrong for the user task. This is common with ambiguous terms, stale docs, and negated queries.
Thresholds can drift after an embedding model change. A score cutoff that worked on one model may fail after re-embedding.
Animated SVG Diagram
Next Topics
FAQ
What does cosine similarity mean in vector search?
It measures how closely two embedding vectors point in the same direction, usually as a proxy for semantic relatedness.
Is cosine similarity better than dot product?
Neither is universally better. Use the metric recommended for the embedding model and verify retrieval quality on your data.
Does high cosine similarity mean the answer is correct?
No. It means the vectors are close by the chosen metric. Correctness needs grounding, filters, freshness checks, and evaluation.