Retrieval and RAGRanking, evidence and groundingStaff
When do cosine similarity and inner product produce different retrieval rankings?
By Amit Kumar2 min readWorked answer includedReviewed 28 Sept 2026
The question
Interview question
A vector index switches from cosine similarity to maximum inner product with the same model and corpus. Use a small vector example to explain a changed top result, identify normalization conditions for ranking versus score equality, and design an exact-search comparison before blaming approximate search.
Draft your approach first
Take a few minutes before you read the answer. Name the constraint that matters most, the failure you are designing for and how you would know it worked.
Reveal the worked answerWorked answer
Cosine compares direction after accounting for vector length. Dot product includes length. With query q = (1, 0), document A at (0.8, 0.6) has cosine 0.8 and dot product 0.8. Document B at (2, 2) has cosine about 0.707 and dot product 2. Cosine ranks A first, dot product ranks B first. The numbers are a toy example, but they show why unchanged embeddings can change a ranking. The query's norm is constant across candidates, so document norms are the part that can reorder them.
Faiss's metric notes say inner product is not cosine unless the vectors are normalized. If every stored document vector is normalized to unit length, maximum inner product gives the same ranking as cosine for one fixed nonzero query. Normalizing the query as well makes the scores equal to cosine, which is useful for consistent semantics. Zero vectors need an explicit policy. Floating-point ties, approximate index search and quantization can still produce differences near a boundary, but I would verify the exact metric difference first.
Inspect the vectors as stored, not just the embedding model's output in a notebook. Did one ingest path normalize and another skip it? Does the index normalize internally for its cosine mode but not its inner product mode? Did a backfill write raw vectors into an index built for normalized ones? Compare vector norms by source and time, then score a small exact-search fixture under both metrics. If the exact rankings differ as the formula predicts, ANN is not the root cause. If exact rankings agree but the production index changes, investigate its candidate search, metric setting and quantization.
Changing metric is a relevance migration. Hold a query set with answer-bearing documents and hard negatives, measure recall at k, the rank of the correct policy and final citation accuracy. Norm may carry useful signal for some embedding models, so I would not normalize reflexively either. The team should choose a metric compatible with how that model was trained and with observed retrieval quality. Version the metric and normalization recipe with the index, rebuild or safely transform vectors, and keep the previous generation for rollback. Do not compare a raw dot score to an old cosine threshold.
If the top result is restricted, authorization still happens regardless of metric. A ranking change can make a hidden permission bug show up in a new place, but it is a separate boundary. The mathematical question has a crisp answer: a fixed embedding model does not fix the ordering when you change the score function.
Related questions
Keep practising retrieval and rag
Follow another question in this area, or search the complete library.
Get my essays on AI engineering and distributed systems by email. Subscribe to ArchCrux