Cosine similarity vs. dot product for embedding search
Rank three vectors by cosine similarity and dot product, then normalize them. Learn why a longer embedding can win and how to check a search index migration.
- Author
- Salon Rai
- 3 min read
Cosine similarity and dot product give the same ranking for a fixed nonzero query when all candidate vectors have the same nonzero length. With unequal lengths, a long vector pointing slightly away from the query can outrank a short vector aligned exactly with it. A two-dimensional example makes it easy to diagnose why an embedding index changes its top result after a metric switch.
Rank three candidates by hand
Use the query vector (1, 0). Candidate A is (1, 0), candidate B is (3, 4) and candidate C is (0, 2). These are illustrative coordinates with no language model behind them. Because the query has only its first coordinate active, its dot product with each candidate is simply that candidate’s first coordinate.
| Candidate | Vector length | Dot product with query | Cosine similarity |
|---|---|---|---|
| A | 1 | 1 | 1 |
| B | 5 | 3 | 0.6 |
| C | 2 | 0 | 0 |
Raw dot product picks B. Cosine similarity divides the dot product by the product of the two vector lengths, so it picks A. B’s length of five makes its projection onto the query direction larger even though B points away from that direction. C is perpendicular to the query and scores zero either way. Google’s guide to similarity measures for embeddings
Reproduce the rankings without a vector database
This Python example checks vector dimensions explicitly. Ordinary zip silently stops at the shorter input, which can hide a mismatched embedding dimension in a home-grown diagnostic. Python 3.10 and newer supports the strict option used below. The zero vector has no direction, so the normalization function rejects it.
from math import sqrt
def dot(a, b):
return sum(x * y for x, y in zip(a, b, strict=True))
def unit(vector):
length = sqrt(dot(vector, vector))
if length == 0:
raise ValueError("Cosine similarity needs a nonzero vector")
return tuple(value / length for value in vector)
q = (1, 0)
docs = {"A": (1, 0), "B": (3, 4), "C": (0, 2)}
for normalize in (False, True):
query = unit(q) if normalize else q
scores = {
name: dot(query, unit(v) if normalize else v)
for name, v in docs.items()
}
print(sorted(scores.items(), key=lambda item: -item[1]))
After normalization, B becomes (0.6, 0.8). A’s normalized score of one beats 0.6. This equivalence applies to the math for the vectors supplied. A production search can introduce other differences through approximate retrieval, quantization, ties or filters. Compare an exact, unfiltered run first when diagnosing a change.
Normalize the side that affects ranking
If you normalize only the query, every dot product for that query is divided by the same positive constant. The order cannot change. In the example the query already has unit length, so applying that operation would leave B in first place.
Normalizing each candidate removes its individual length factor. For a fixed query this makes the dot-product ranking agree with cosine ranking. Normalizing the query as well makes the scores equal to cosine similarity, which matters if you reuse a score threshold across different queries. A queue triggered at 0.75 could behave differently even when the displayed top five documents stay in the same order.
Whether length carries useful information depends on how the embedding model was trained and how the index expects its vectors. Consult the model and database documentation before normalizing an existing collection. Changing a distance label in a user interface does not update previously stored embeddings. Faiss notes on inner product and cosine search
Audit a migration with one real query
Save a query vector, a small set of candidate IDs and their full-precision vectors from both sides of the migration. Check the model identifier, dimension, preprocessing and a sample of document lengths. Two models can produce vectors with the same dimension and entirely different meanings for the coordinates.
Compute an exact top list from those saved vectors. Compare the first document whose rank changes and record both its raw dot product and its length. If the disagreement disappears after normalization, you have explained that pair. If the local order agrees with the old index but differs from the new one, inspect distance versus similarity conventions: some interfaces return smaller-is-better distances rather than larger-is-better similarities. Then reintroduce approximate index settings and filters one change at a time.
Next steps
Keep the three-vector fixture as a contract test for the scoring function. For a live retrieval change, follow it with labeled queries and the application cutoff. A ranking change tells you that a metric mattered. Whether the change helps a reader find evidence is a separate measurement, illustrated in the Recall@k and MRR walkthrough.

Written by