Similarity: Cosine, Dot Product & Euclidean
Similarity metrics are the math for "how close are these two vectors?" — cosine looks at direction, dot product mixes in magnitude, and Euclidean measures straight-line distance.
Explain like I'm new to AI
Embeddings put meaning into vectors. But to actually use them, you need to measure how close two vectors are. That's what a similarity metric does — it turns "are these two things similar?" into a single number.
There are three you'll meet constantly. Play with them — adjust the angle and length of vector B and watch each metric react:
Somewhat similar
Try it: change only the length of B — cosine stays the same (it measures direction), while dot product and Euclidean distance both change.
Notice the key lesson: when you change only the length of B, cosine stays the same but the others change. Cosine cares about direction; the others care about size too.
Mental model
Think of two arrows from the same starting point:
- Cosine asks: are they pointing the same way? (ignores how long they are)
- Dot product asks: same way AND how strong? (longer arrows score higher)
- Euclidean asks: how far apart are the arrowheads? (straight-line gap)
How it works
- Cosine similarity ranges from −1 (opposite) through 0 (unrelated) to 1 (identical direction). It's
dot(A, B) / (|A| × |B|)— the angle between vectors. - Dot product multiplies matching components and sums them; it grows with both alignment and magnitude.
- Euclidean distance is the straight-line distance; smaller = more similar (opposite direction from the others).
cosine(A,B) = (A · B) / (|A| |B|) # −1 … 1, higher = closer
dot(A,B) = A · B # unbounded, higher = closer
euclidean(A,B) = |A − B| # 0 … ∞, lower = closerReal-world example
Most semantic search uses cosine similarity, because you care whether two texts mean the same thing, not whether one is longer. A short question and a long paragraph can be a perfect match in direction even though their magnitudes differ wildly.
Technical explanation
Here's the crucial optimization: if all vectors are normalized to length 1 ("unit vectors"), then cosine similarity and dot product become identical, and cosine ranking matches Euclidean ranking. That's why many embedding models output normalized vectors and vector databases default to dot product on them — it's cheaper to compute while giving cosine's behavior. Choosing a metric usually means: use the one the embedding model was trained/recommended for (often cosine). Mixing metrics inconsistently between indexing and querying silently ruins results.
Common mistakes
Forgetting that for Euclidean distance, smaller means more similar, while for cosine and dot product, larger means more similar. Sorting the wrong direction returns the least relevant results.
- Comparing vectors with a different metric than the model expects.
- Using dot product on un-normalized vectors and being surprised that long documents dominate.
When to use it
- Cosine: default for semantic text search (meaning over magnitude).
- Dot product: fast equivalent to cosine on normalized vectors; used when magnitude is meaningful.
- Euclidean: clustering and some vision tasks where absolute distance matters.
When NOT to use it
- Don't switch metrics between building the index and querying it — keep them consistent.
Alternatives
- Manhattan distance and others exist, but cosine/dot/Euclidean cover almost all embedding use.
Quick quiz
Related concepts
- What are Embeddings? — Embeddings turn information into numerical vectors where similar meanings sit close together, so software can compare meaning with math.
- What is Vector Search? — Vector search finds the stored items whose embeddings are closest to a query embedding — the nearest-neighbor operation behind semantic search.
Last reviewed: 2026-09-01 · Written by ByHeart AI · Reviewed by ByHeart AI