New Tool Radar

AI Concepts

How to Pick the Right Distance Metric for Vector Search

Use cosine for direction, dot product for magnitude-aware ranking, and Euclidean only when the scale of the vectors matters.

Schematic with three steps: Check whether your vectors are normalised; Decide whether length means anything; Test the metric on your own queries.

A vector database applies a similarity metric to find the vector most similar to the query, and the three common choices are cosine similarity, dot product and Euclidean distance. Cosine measures the angle between vectors, so it compares direction and ignores length. Dot product multiplies direction and length together. Euclidean measures straight-line distance, so it is sensitive to both. OpenAI recommends cosine similarity for its embeddings, and notes that its embeddings are normalised to length 1, which makes cosine and Euclidean rank identically.

What you need

  • The embedding model you use and whether its vectors are normalised
  • Whether vector length carries meaning in your data
  • A test set of queries with known correct results

Step 1: Check whether your vectors are normalised

If the model normalises vectors to length 1, cosine similarity can be computed as a dot product, and cosine and Euclidean give identical rankings. In that case the choice barely matters.

Step 2: Decide whether length means anything

If longer vectors mean stronger or more confident content, dot product preserves that signal and cosine throws it away. If length is an artefact of text length, cosine is the safer choice.

Step 3: Test the metric on your own queries

Run the same test questions under each metric and compare the top results. The metric that returns the correct documents most often on your data is the right one, whatever the general advice says.

Worked example: synthetic metric comparison on 3 queries

The table below is a synthetic example. It shows the expected result of comparing metrics; no search was run.

Query Cosine top result Dot product top result
Refund window Refund policy Refund policy
Long legal doc Refund policy Legal terms – longer doc wins
Pricing tiers Pricing page Pricing page

Expected result (UNTESTED). cosine and dot product agree on 2 of 3 queries, and dot product is misled by document length on the third. This check was not run live; the table shows the expected result for the synthetic example.

Limits

  • OpenAI recommends cosine similarity, and says the choice of distance function typically does not matter much.
  • The metric must match the one the index was built with; changing it later means rebuilding the index.
  • Normalising vectors before storage is a separate step from choosing the metric; check both.
  • A metric cannot fix a bad embedding model; if all metrics fail, the embeddings are the problem.

Sources

See all