Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 36 ยท Embeddings and Similarity

Similarity Metrics

Compare cosine similarity, dot product and Euclidean distance, see when they agree and when they differ, and learn which metric to configure in a vector database.

A vector database answers one question over and over: which stored vectors are closest to this query vector? "Closest" needs a definition. The three you will meet everywhere are cosine similarity, the dot product and Euclidean distance. They look different, but for the unit-length vectors that most text embedding models return, they all produce the same ranking. Knowing why (and when that stops being true) saves you from the classic bug of configuring an index with the wrong metric.

This lesson defines each metric, shows the relationships between them in code, and ends with a short rule for choosing.

The three metrics

Take two vectors a and b with the same number of dimensions.

  • Dot product: multiply matching coordinates and add them up, aยทb = a1*b1 + a2*b2 + .... It grows when the vectors point the same way and when they are long.
  • Cosine similarity: the dot product divided by both lengths, cos = aยทb / (|a| |b|). It measures only the angle between the vectors. It ranges from -1 (opposite) through 0 (unrelated, at a right angle) to 1 (same direction).
  • Euclidean (L2) distance: the straight-line distance, sqrt((a1-b1)^2 + (a2-b2)^2 + ...). Smaller means closer, and 0 means identical.
Python The three metrics by hand

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice a versus b: they point in exactly the same direction, so cosine is 1.00, yet their L2 distance is 5 because b is longer, and the dot product (50) is large because of that length. Against c (a right angle) cosine and dot product are both 0 but L2 is about 7.07. Each metric answers a slightly different question.

Why length matters (and when it does not)

For text, the length of an embedding is usually not meaningful: a long paragraph and a short sentence about the same topic should count as similar. Cosine ignores length by design. Many embedding models go one step further and return vectors that are already normalised to length 1.

If both vectors have length 1, then:

  • cosine = dot product (the division by 1 does nothing), and
  • L2 distance squared = 2 - 2 * cosine, so smaller distance is exactly larger cosine.

So all three give the same ranking. You can verify it:

Python On unit vectors the rankings agree

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

All three lists match. This is why your vector database's default does not matter much if you normalise, and why it matters a lot if you do not.

When the rankings disagree

Now remove the normalisation. Suppose a document vector has a large length. Its dot product with any query that leans the same way gets inflated, so it can win even when another document is the better directional match. L2 has the opposite problem: a perfectly aligned but long vector looks "far away".

Python Length changes the winner

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The dot product picks the long vector that is 30 degrees off, cosine picks the one that points exactly the same way, and L2 also picks the short aligned one because the long vector sits 4.16 units away. Dot product and the other two disagree as soon as lengths vary.

A very common production bug: the model returns normalised vectors, the index is created with Euclidean distance, and someone later switches to a model that does not normalise. Rankings quietly change. Always check the model's documentation, or normalise yourself and index with inner product.

Other metrics you will see

  • Manhattan (L1) distance: the sum of absolute differences. Rare for text embeddings, sometimes used for sparse features.
  • Hamming distance: the number of differing bits between binary vectors. It is extremely fast and is used with binary-quantised embeddings (compression).
  • Jaccard similarity: overlap of two sets. Used for deduplication, not for dense embeddings (ingestion).
flowchart TD
    Q["Are your vectors unit length?"] -->|yes| A["Cosine, dot and L2 rank the same: use the fastest (inner product)"]
    Q -->|no| B["Does length carry meaning?"]
    B -->|no, it is just noise| C["Use cosine, or normalise then use inner product"]
    B -->|yes, e.g. popularity or confidence| D["Use dot product, deliberately"]

Choosing a metric.

Metric names in databases

Databases use different names for the same ideas, and some store distance (lower is better) instead of similarity (higher is better):

Idea Common names
Cosine cosine, cosine_distance (= 1 - cosine), COSINE
Dot product dot, ip (inner product), DOT_PRODUCT
Euclidean l2, euclidean, EUCLID

Remember that a "distance" form flips the sorting direction. If your scores look backwards, check whether you are reading a similarity or a distance.

A practical default

  1. Read the embedding model's documentation to learn whether its vectors are normalised and which metric it was trained with.
  2. If you are unsure, normalise to unit length yourself and use inner product (or cosine). It is the fastest correct option.
  3. Use the same metric for the index, for thresholds (score thresholds) and for evaluation. A threshold of 0.75 means something only for one metric and one model.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Two vectors point in exactly the same direction but one is twice as long. What is their cosine similarity?

    Cosine measures only the angle. Same direction means an angle of zero and a cosine of 1, regardless of length.
  2. 2. For unit-length vectors, how do cosine, dot product and L2 distance rank results?

    With length 1, cosine equals the dot product and squared L2 equals 2 minus 2 times cosine, so all orderings match.
  3. 3. Which metric can be inflated by a document vector with a very large length?

    The raw dot product grows with vector length, so long vectors can outrank better directional matches.
  4. 4. A database reports cosine_distance. What does a smaller value mean?

    Cosine distance is 1 minus cosine similarity, so lower means closer.

Summary

  • Dot product rewards direction and length, cosine rewards direction only, L2 measures straight-line gap.
  • On normalised vectors the three metrics produce identical rankings, so inner product is a fast, safe choice.
  • On unnormalised vectors they can disagree; confirm how your model behaves and configure the index to match.
  • Distances sort ascending, similarities sort descending. Thresholds belong to one metric and one model.

Next: Embedding Models and Dimensions