Cosine similarity is a measure of how closely two vectors point in the same direction. In the context of text search, those vectors are embeddings - numeric representations of meaning - and a high cosine similarity score means two pieces of text are semantically close, regardless of whether they share any words.
Why it matters
When a semantic search system embeds your query and every passage in a document, it needs a way to decide which passages are most relevant. Cosine similarity provides that: it computes the angle between the query vector and each passage vector. Passages that sit close to the query in meaning - expressing the same idea, from different angles - score near 1.0; passages on unrelated topics score near 0.
The intuition is geometric. A passage about "terminating a contract" and a query for "cancelling an agreement" encode similar meanings, so their vectors point roughly the same way through the high-dimensional space embeddings live in. Cosine similarity captures that alignment directly, which is why it's the standard scoring function for vector-based retrieval.
Cosine similarity vs. Euclidean distance
Cosine similarity isn't the only way to compare two vectors - the other common option is Euclidean distance, the straight-line distance you'd measure with a ruler between two points. The two behave differently, and the difference is exactly why embeddings use cosine similarity almost universally.
| Cosine similarity | Euclidean distance | |
|---|---|---|
| What it measures | The angle between two vectors - direction, not magnitude | The straight-line distance between two points in space |
| Sensitive to vector length | No - a short vector and a long vector pointing the same way score identically | Yes - longer vectors can appear "farther away" even if they mean the same thing |
| Typical score range | -1 to 1 (in practice usually 0 to 1 for text embeddings) | 0 to unbounded - depends entirely on the scale of the vectors |
| Best suited for | Comparing meaning when only direction matters, e.g. text embeddings | Comparing points where absolute magnitude is meaningful, e.g. physical coordinates |
| Standard choice for semantic search | Yes | Rarely - length differences would distort the ranking |
The practical reason cosine wins for text: two embeddings can point in nearly the same direction - meaning nearly the same thing - while having different lengths, because embedding models don't guarantee every vector comes out the same magnitude. Cosine similarity ignores length entirely and looks only at direction, so it isn't fooled by that variation. Euclidean distance would be, penalizing a passage for having a "longer" vector even when its meaning is a near-perfect match.
For most retrieval systems, cosine similarity produces a good but imperfect first-pass ranking. That's where reranking comes in: a second-pass model re-scores the top candidates by reading query and passage together, correcting the cases where cosine similarity ranked a topically-adjacent-but-wrong passage ahead of the one that actually answers the question.
Together, cosine similarity and reranking form the relevance backbone that lets a system surface the right passage - and cite the sentence behind the answer. Neither step needs to be perfect on its own; cosine similarity only has to get the right passage into the shortlist, and reranking only has to sort a small shortlist correctly. Splitting the work this way is what makes retrieval both fast (a cheap vector comparison narrows millions of passages down to a handful) and accurate (a slower, smarter model makes the final call on that handful).