recallA retrieval
field guide

Similarity Metrics

Build this skill

Compute and compare cosine similarity, dot product and Euclidean distance, and pick the measure a model expects.

What does “close” mean for a list of numbers?

Once texts are turned into embeddings, search becomes a question of finding the stored embeddings closest to the query's embedding. That raises a basic question: how do you measure closeness between two lists of numbers? There are three common answers, and you will meet all three in vector databases and embedding model documentation: cosine similarity, the dot product and Euclidean distance.

It helps to think of each embedding as an arrow (a vector) starting at the centre of the space and pointing somewhere. Every arrow has a direction (where it points) and a length (how far it reaches). The three measures differ in which of these they pay attention to.

Cosine similarity: compare directions

Cosine similarity measures the angle between two arrows and ignores their lengths.

  • Arrows pointing the same way: cosine = 1 (same meaning).
  • Arrows at right angles: cosine = 0 (unrelated).
  • Arrows pointing in opposite directions: cosine = −1.

In practice, text embeddings rarely point in opposite directions, so most scores you see fall between about 0 and 1. The formula multiplies the two lists position by position, adds the results, and divides by the two lengths:

\[\cos(A, B) = \frac{A \cdot B}{|A| \times |B|}\]

Ignoring length is useful for text. The length of an embedding can be affected by things like how long or how wordy the text was, which have nothing to do with what it is about. Direction carries the meaning. This is why cosine similarity is the default choice for most text search.

Dot product: direction and length together

The dot product is the top half of the cosine formula on its own: multiply the two lists position by position and add up the results. It grows when the arrows point the same way, and it also grows when the arrows are longer. So a longer arrow can score higher even at a slightly worse angle.

Some embedding models are trained so that length carries useful information, and their documentation recommends the dot product. For most others, the two measures give the same ranking anyway, for a reason explained below.

Euclidean distance: the gap between the tips

Euclidean distance is ordinary straight-line distance, the same as measuring with a ruler between the tips of the two arrows. Smaller means closer, so here a low number is good, which is the opposite of the other two measures. Vector databases often call it L2 distance.

Cosine similarityLooks at: angle onlyRange: −1 to 1Higher = more similarDefault for textDot productLooks at: angle + lengthRange: any numberHigher = more similarFast to computeEuclidean (L2)Looks at: gap between tipsRange: 0 and upLower = more similarCommon in vector DBs

All three compare the same two arrows. They differ in whether length matters and whether a high or low number means “close”.

Play with two arrows below. Change the angle and watch cosine similarity change. Then change only the lengths and notice which measures move and which stay still.

Normalised embeddings make the choice easy

Many systems normalise every embedding, which means scaling each arrow so its length is exactly 1 while keeping its direction. Once all arrows have length 1, something convenient happens:

  • The dot product becomes exactly equal to cosine similarity, because dividing by lengths of 1 changes nothing.
  • Euclidean distance gives the same ranking as cosine similarity, just in reverse order (smallest distance = highest cosine).

So for normalised embeddings, all three measures put results in the same order. Many popular embedding models already return normalised vectors, and many systems use the dot product because it is the cheapest of the three to calculate.

The code below checks this with two small vectors. Run it and compare the numbers before and after normalising.

python · runs in your browser

Expected output:

text
raw       : cosine=0.96  dot=24.00  euclidean=1.41
normalised: cosine=0.96  dot=0.96  euclidean=0.28

report works out all three measures for one pair of vectors. a / np.linalg.norm(a) is the normalising step: dividing a vector by its own length gives a vector of length 1 that points the same way.

The cosine stays at 0.96 either way, because normalising does not change direction. After normalising, the dot product equals the cosine exactly.

Which one should you use?

The practical rule is short: use the measure the embedding model was trained with, which its documentation will state. If it says the embeddings are normalised, cosine and dot product are interchangeable, and the dot product is slightly faster.

The more important point is to be consistent. The vector database index must be built for the same measure you search with. A pgvector index built for Euclidean distance, for example, will not be used by a query that asks for cosine distance, and searches can become dramatically slower without any error message.

Are the embeddingsnormalised?yesDot productsame ranking as cosine,fastestnoWhat does the model cardsay?cosineCosineusual for textdotDot productif length is meaningful

In most text search systems, embeddings are normalised and the choice hardly matters, as long as the index and the query use the same measure.

One last caution about the scores themselves. A cosine of 0.82 does not mean "82 percent relevant". Scores are only meaningful compared with other scores from the same model on the same collection. One model might give related passages 0.85 and unrelated ones 0.70, while another gives 0.45 and 0.10. Setting a fixed cut-off like "ignore anything below 0.8" only works after you have looked at real scores for your own model and data.

Follow your curiosity.

↑ ↓ to explore · Enter to open · Escape to close