Vector Similarity Search: Metrics & Approximate Methods › Distance Metrics

Michael BrenndoerferJanuary 25, 202617 min read
Language AI HandbookAll chapters
Part 44 of 67Retrieval-Augmented Generation
Chapter 338Vector Similarity Search: Metrics & Approximate Methods › Distance Metrics
Reading section 2 of 11 · Distance Metrics · 17 minSection 2 of 11 · 17 min

Book contents

Section 2 of 11 · 17 min read

Vector Similarity Search: Metrics & Approximate Methods › Distance MetricsOne section · Sections in this chapter2 / 11 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Before you can search for the "most similar" vectors, you need to define a measure of similarity. This decision shapes the entire retrieval pipeline. The metric you choose determines which documents appear at the top of your search results, how your index structures organize the vector space, and ultimately how effective your retrieval pipeline will be. Different metrics capture different geometric relationships between vectors, and understanding these relationships in detail is needed for building high-quality search systems.

At a high level, the question of similarity can be split into two perspectives. One perspective asks: "Are these two vectors pointing in roughly the same direction?" This is a question about orientation, about the angle between the arrows in vector space. The other perspective asks: "How far apart are these two vectors as points in space?" This is a question about position, about the absolute gap between them. These perspectives can yield different answers for the same pair of vectors. Knowing which question matters for your application helps in choosing the right metric.

Before examining each metric in detail, it helps to build intuition for the geometry of high-dimensional spaces. In two or three dimensions, vectors are easy to visualize as arrows. In 768 dimensions, you cannot draw the picture, but the same geometric relationships hold. A vector is still a direction and a magnitude. Two vectors still form an angle. The distance between their tips is still well-defined. Every piece of geometry we develop here applies exactly in high dimensions, even when our visual intuition fails.

Cosine Similarity

Cosine similarity measures the angle between two vectors, ignoring their magnitudes entirely. The core intuition is geometric: imagine two arrows emanating from the origin in a high-dimensional space. If the arrows point in the same direction, regardless of whether one is short and the other is long, we consider them maximally similar. If they point in perpendicular directions, they share no similarity at all. And if they point in opposite directions, they are maximally dissimilar. This is precisely what the cosine of the angle between two vectors captures.

For vectors a\mathbf{a} and b\mathbf{b} in Rd\mathbb{R}^d, cosine similarity is defined as:

cos_sim(a,b)=a⋅b∥a∥ ∥b∥=∑i=1daibi∑i=1dai2 ∑i=1dbi2\text{cos\_sim}(\mathbf{a}, \mathbf{b}) = \frac{\mathbf{a} \cdot \mathbf{b}}{\|\mathbf{a}\| \, \|\mathbf{b}\|} = \frac{\sum_{i=1}^{d} a_i b_i}{\sqrt{\sum_{i=1}^{d} a_i^2} \, \sqrt{\sum_{i=1}^{d} b_i^2}}

where:

  • a,b\mathbf{a}, \mathbf{b}: the input vectors being compared
  • dd: the dimensionality of the vector space
  • ai,bia_i, b_i: the scalar components of vectors a\mathbf{a} and b\mathbf{b} at dimension ii
  • a⋅b\mathbf{a} \cdot \mathbf{b}: the dot product, representing unnormalized alignment
  • ∥a∥\|\mathbf{a}\|: the Euclidean length (L2L_2 norm) of vector a\mathbf{a}
  • ∑i=1d\sum_{i=1}^{d}: the summation across all dimensions

To understand what this formula is really doing, let's break it apart. The numerator, the dot product a⋅b\mathbf{a} \cdot \mathbf{b}, computes a raw measure of how well the two vectors align. It multiplies corresponding components and sums the results. When both vectors have large positive values in the same dimensions, the dot product grows large and positive. When one vector is positive where the other is negative, those dimensions contribute negative terms, pulling the sum down. The dot product, in isolation, reflects both alignment and magnitude: longer vectors produce larger dot products even at the same angle.

The denominator corrects for this magnitude dependence. By dividing by the product of the two vector norms, we effectively scale the dot product so that it reflects only the angular relationship. Geometrically, this is equivalent to first projecting both vectors onto the unit sphere (normalizing them to length 1) and then computing their dot product. The result is the cosine of the angle θ\theta between them.

The result ranges from −1-1 (pointing in opposite directions) through 00 (orthogonal) to +1+1 (pointing in the same direction). In practice, most embedding models produce vectors that occupy a relatively narrow region of the space, so similarity values for relevant documents often fall between 0.5 and 0.95. Two completely unrelated documents might still achieve cosine similarities around 0.2-0.4 simply because language embeddings cluster in a particular region of the high-dimensional sphere, a phenomenon called anisotropy that we discussed in the chapter on BERT representations.

Why is cosine similarity the default for text embeddings? Because it focuses on the direction of the vector rather than its length. Two documents about the same topic should be considered similar even if one has a larger-magnitude embedding. By normalizing out magnitude, cosine similarity captures pure semantic orientation. Consider two sentences like "The cat sat on the mat" and "A feline rested upon the rug." An embedding model should map these to vectors pointing in nearly the same direction. This reflects their shared meaning. Even if the model produces vectors of slightly different lengths for these two inputs, cosine similarity will correctly identify them as highly similar because their angular separation is small.

The metric is also well-suited to the query-document asymmetry that characterizes retrieval. Query vectors often represent short, terse expressions of information need, while document vectors represent long, rich passages of text. The absolute magnitude of these vectors might differ substantially, but if they cover the same topic, their directions should align. Normalizing out magnitude makes this comparison fair.

Cosine Distance

Search systems often need a distance (where smaller is better) rather than a similarity (where larger is better). Cosine distance is simply 1−cos_sim(a,b)1 - \text{cos\_sim}(\mathbf{a}, \mathbf{b}), converting the similarity into a value between 0 and 2 where 0 means identical direction.

Dot Product (Inner Product)

The dot product is the simplest and most computationally inexpensive of all vector similarity measures. It requires no normalization step: you simply multiply corresponding components and sum:

dot(a,b)=a⋅b=∑i=1daibi\text{dot}(\mathbf{a}, \mathbf{b}) = \mathbf{a} \cdot \mathbf{b} = \sum_{i=1}^{d} a_i b_i

where:

  • a,b\mathbf{a}, \mathbf{b}: the input vectors
  • dd: the dimensionality of the vector space
  • ai,bia_i, b_i: the scalar components of vectors a\mathbf{a} and b\mathbf{b} at dimension ii
  • ∑i=1d\sum_{i=1}^{d}: the summation across all dd dimensions
  • a⋅b\mathbf{a} \cdot \mathbf{b}: the aggregate alignment score, which increases with both directional similarity and vector length

Unlike cosine similarity, the dot product does account for vector magnitude. This means a longer vector can score higher even if its direction is slightly less aligned. To see why, think of it geometrically: the dot product of two vectors equals ∥a∥ ∥b∥ cos⁡θ\|\mathbf{a}\| \, \|\mathbf{b}\| \, \cos\theta. When the norms are not divided out, a vector that is twice as long contributes twice as much to the final score, all else being equal. This makes the dot product sensitive to both how well two vectors agree in direction and how "strong" or "confident" each vector's representation is.

When would you want this behavior? When magnitude carries information. Some embedding models and retrieval systems train embeddings so that the vector norm encodes confidence or relevance strength. For example, a model might learn to produce longer vectors for documents that are more central to a topic and shorter vectors for documents that touch on the topic only tangentially. In these cases, dot product is the correct metric because it rewards both directional alignment and strong confidence signals. A highly relevant document with a long vector and good directional alignment will outscore a marginally relevant document, even if the marginal document happens to have a slightly better angle.

There is an important relationship between these two metrics in practice. If your vectors are L2L_2-normalized (i.e., ∥a∥=∥b∥=1\|\mathbf{a}\| = \|\mathbf{b}\| = 1), then the dot product and cosine similarity are identical:

a⋅b=∥a∥ ∥b∥ cos⁡θ(geometric definition)=1⋅1⋅cos⁡θ(substitute unit norms)=cos⁡θ(simplify)\begin{aligned} \mathbf{a} \cdot \mathbf{b} &= \|\mathbf{a}\| \, \|\mathbf{b}\| \, \cos\theta && \text{(geometric definition)} \\ &= 1 \cdot 1 \cdot \cos\theta && \text{(substitute unit norms)} \\ &= \cos\theta && \text{(simplify)} \end{aligned}

where:

  • a,b\mathbf{a}, \mathbf{b}: the normalized input vectors
  • ∥a∥,∥b∥\|\mathbf{a}\|, \|\mathbf{b}\|: the lengths of the vectors (both equal to 1)
  • θ\theta: the angle between vectors a\mathbf{a} and b\mathbf{b}

The derivation proceeds by recalling that the geometric definition of the dot product relates it to the product of the vector magnitudes and the cosine of the angle between them. When both vectors lie on the unit sphere, their magnitudes are exactly 1, and the product collapses to the cosine of the angle alone.

This is why many systems pre-normalize their embeddings. It lets you use the cheaper dot product operation while getting cosine similarity results, and it opens the door to optimized index structures that assume unit-norm vectors. The dot product avoids the division and square root operations required by cosine similarity, which makes it faster in tight inner loops where billions of comparisons are performed. By paying the one-time cost of normalization at indexing time, you gain this computational advantage at every subsequent query.

The computational savings of the dot product over cosine similarity might seem trivial, but at scale they matter. For 10 million vectors at dimension 768, each dot product requires 768 multiply-add operations. Cosine similarity requires those same operations plus two norm computations (each requiring 768 squarings, a sum, and a square root). When you are running this computation billions of times, the accumulated savings are significant.

Euclidean Distance

Euclidean distance (L2L_2 distance) takes an entirely different perspective from the angle-based metrics we have discussed so far. Instead of asking how well two vectors align directionally, it asks: how far apart are these two points in space? It measures the straight-line distance between two points, the length of the shortest path connecting them:

dL2(a,b)=∥a−b∥=∑i=1d(ai−bi)2d_{\text{L2}}(\mathbf{a}, \mathbf{b}) = \|\mathbf{a} - \mathbf{b}\| = \sqrt{\sum_{i=1}^{d} (a_i - b_i)^2}

where:

  • a,b\mathbf{a}, \mathbf{b}: the input vectors
  • dd: the dimensionality of the vector space
  • ai,bia_i, b_i: the scalar components of vectors a\mathbf{a} and b\mathbf{b} at dimension ii
  • ai−bia_i - b_i: the difference between the scalar components along dimension ii
  • (ai−bi)2(a_i - b_i)^2: the squared difference, which makes negative differences positive and penalizes large outliers
  • ∑i=1d\sum_{i=1}^{d}: the sum of squared differences across all dimensions
  • …\sqrt{\dots}: the square root, which converts the sum of squares back to the original units (linear distance)

This is the most intuitive metric: it corresponds to the physical distance you would measure with a ruler. It generalizes the familiar Pythagorean theorem from two or three dimensions to arbitrarily high-dimensional spaces. In two dimensions, the distance between points (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2) is (x1−x2)2+(y1−y2)2\sqrt{(x_1-x_2)^2 + (y_1-y_2)^2}. The formula above extends this same idea to dd dimensions, summing the squared differences along every axis and taking the square root of the total.

Euclidean distance is sensitive to both direction and magnitude. Two vectors can have high cosine similarity (similar direction) but large Euclidean distance (very different magnitudes). Consider a query vector q\mathbf{q} and a document vector v=10q\mathbf{v} = 10\mathbf{q}. Their cosine similarity is a perfect 1.0, since they point in exactly the same direction. But their Euclidean distance is 9∥q∥9\|\mathbf{q}\|, which could be quite large. This illustrates the basic distinction: cosine similarity sees these as identical, while Euclidean distance sees them as far apart. Which perspective is "correct" depends entirely on whether magnitude carries meaningful information in your embedding space.

Another important property of Euclidean distance is its sensitivity to outlier dimensions. Because differences are squared before summing, a single dimension with a large discrepancy can dominate the total distance. For example, if two 768-dimensional vectors agree perfectly in 767 dimensions but differ by 10 in the remaining dimension, the squared Euclidean distance is 102=10010^2 = 100, equivalent to disagreeing by 0.36 across all 768 dimensions. This squaring effect means Euclidean distance penalizes concentrated disagreement more heavily than diffuse disagreement. Whether this is desirable depends on the structure of your embedding space.

For normalized vectors, Euclidean distance and cosine similarity are monotonically related. This connection is one of the most important results in vector similarity search, because it means that, under normalization, the choice between these two metrics does not change the ranking of results. The derivation proceeds by expanding the squared Euclidean distance:

∥a−b∥2=(a−b)⋅(a−b)(expand squared norm)=∥a∥2+∥b∥2−2a⋅b(distribute terms)=1+1−2cos⁡θ(substitute unit norms)=2−2cos⁡θ(simplify)\begin{aligned} \|\mathbf{a} - \mathbf{b}\|^2 &= (\mathbf{a} - \mathbf{b}) \cdot (\mathbf{a} - \mathbf{b}) && \text{(expand squared norm)} \\ &= \|\mathbf{a}\|^2 + \|\mathbf{b}\|^2 - 2\mathbf{a} \cdot \mathbf{b} && \text{(distribute terms)} \\ &= 1 + 1 - 2\cos\theta && \text{(substitute unit norms)} \\ &= 2 - 2\cos\theta && \text{(simplify)} \end{aligned}

where:

  • ∥a−b∥2\|\mathbf{a} - \mathbf{b}\|^2: the squared Euclidean distance
  • a,b\mathbf{a}, \mathbf{b}: the normalized input vectors (length of 1)
  • θ\theta: the angle between vectors a\mathbf{a} and b\mathbf{b}

The key step is the expansion in the second line. When you compute (a−b)⋅(a−b)(\mathbf{a} - \mathbf{b}) \cdot (\mathbf{a} - \mathbf{b}), you are effectively computing a⋅a−2a⋅b+b⋅b\mathbf{a} \cdot \mathbf{a} - 2\mathbf{a} \cdot \mathbf{b} + \mathbf{b} \cdot \mathbf{b}, which is ∥a∥2−2a⋅b+∥b∥2\|\mathbf{a}\|^2 - 2\mathbf{a} \cdot \mathbf{b} + \|\mathbf{b}\|^2. For unit vectors, the squared norms are both 1, and the dot product is cos⁡θ\cos\theta, giving us the clean relationship 2−2cos⁡θ2 - 2\cos\theta.

So dL2=2−2cos⁡θd_{\text{L2}} = \sqrt{2 - 2\cos\theta}. Higher cosine similarity means smaller Euclidean distance. This means that for normalized vectors, searching by minimum Euclidean distance produces the same ranking as searching by maximum cosine similarity. The two metrics induce identical orderings over all candidate vectors, differing only in the numerical values they assign. In practice, many libraries use squared Euclidean distance (dL22d_{\text{L2}}^2) to avoid the square root computation, since it preserves the same ordering. The square root is a monotonically increasing function, so removing it does not affect which vector is closest or the relative ranking of candidates.

Manhattan Distance

Manhattan distance (L1L_1 distance) takes yet another approach to measuring separation between points, one that treats each dimension independently and sums their contributions linearly rather than quadratically:

dL1(a,b)=∑i=1d∣ai−bi∣d_{\text{L1}}(\mathbf{a}, \mathbf{b}) = \sum_{i=1}^{d} |a_i - b_i|

where:

  • a,b\mathbf{a}, \mathbf{b}: the input vectors
  • dd: the dimensionality of the vector space
  • ai,bia_i, b_i: the scalar components of vectors a\mathbf{a} and b\mathbf{b} at dimension ii
  • ∣ai−bi∣|a_i - b_i|: the absolute difference along dimension ii
  • ∑i=1d\sum_{i=1}^{d}: the sum of these linear differences, representing the total "grid-based" distance

Named after the grid-like street layout of Manhattan, where travel between two points must follow the rectangular street grid rather than cutting diagonally, this metric captures a different notion of separation. Imagine traveling from one intersection to another: you can only move north-south or east-west, never diagonally. The total distance you travel is the sum of horizontal and vertical displacements, which is exactly what the L1L_1 norm computes.

This metric is less common for dense text embeddings but appears in some specialized contexts. It is less sensitive to outliers in individual dimensions than Euclidean distance, since it does not square the differences. Recall that in Euclidean distance, a single dimension with a discrepancy of 10 contributes 100 to the sum, whereas in Manhattan distance, it contributes only 10. This means Manhattan distance treats every unit of disagreement equally, regardless of whether it is concentrated in one dimension or spread across many. For representations where individual dimensions can occasionally take on extreme values, such as certain sparse or quantized embeddings, this reduced sensitivity can be advantageous.

Manhattan distance also has a useful computational property: on hardware with SIMD (Single Instruction Multiple Data) vector extensions, computing absolute values and summing them is often slightly faster than computing squared differences, because the squaring step is replaced by an absolute value operation. For very tight performance budgets, this can matter.

Hamming Distance and Binary Embeddings

A final metric worth mentioning, though it occupies a specialized niche, is Hamming distance. Used with binary vector representations, it counts the number of bit positions where two vectors differ:

dHamming(a,b)=∑i=1d1[ai≠bi]d_{\text{Hamming}}(\mathbf{a}, \mathbf{b}) = \sum_{i=1}^{d} \mathbb{1}[a_i \neq b_i]

where:

  • a,b\mathbf{a}, \mathbf{b}: binary vectors with elements in {0,1}\{0, 1\}
  • 1[⋅]\mathbb{1}[\cdot]: the indicator function, equal to 1 when the condition is true and 0 otherwise
  • ∑i=1d\sum_{i=1}^{d}: the count of positions where the two vectors disagree

Hamming distance is important because modern CPUs can compute it extremely efficiently using the POPCNT instruction (population count, which counts set bits). By representing vectors as bit arrays and computing Hamming distance via XOR followed by POPCNT, you can compare thousands of binary vectors in the time it takes to compare a handful of float32 vectors. This enables retrieval systems that sacrifice some precision for dramatic throughput gains. Techniques like binary neural hashing learn to project continuous embeddings into binary codes that preserve approximate similarity under Hamming distance.

Choosing the Right Metric

The "correct" metric depends on how your embeddings were trained. This is a point worth emphasizing: the metric is not a free parameter you can tune independently. During training, the embedding model learns to arrange vectors in space so that a specific notion of similarity (defined by the training loss) corresponds to semantic relatedness. If the model was trained to make relevant document-query pairs have high cosine similarity, then the learned geometry of the embedding space is shaped around angular relationships. Using a different metric at search time than the one used during training will degrade retrieval quality, because you would be measuring a geometric property that the model did not optimize for.

Most modern sentence embedding models, including those we discussed in the previous chapter on embedding models, are trained with cosine similarity as the objective and expect you to use cosine similarity (or equivalently, dot product on normalized vectors) at search time. Always check the documentation for your specific model.

The key guidelines are:

  • Cosine similarity / normalized dot product: Default choice for most sentence embedding models (Sentence-BERT, E5, GTE, etc.)
  • Dot product: Use when your model explicitly trains with dot product and encodes meaningful information in vector magnitude
  • Euclidean distance: Common in computer vision embeddings and some specialized models; equivalent to cosine for normalized vectors
  • Manhattan distance: Rarely the best choice for text embeddings, but useful for certain sparse or quantized representations
  • Hamming distance: For binary embeddings where extreme throughput is required and some accuracy loss is acceptable
Out[3]:
Visualization
Radar chart comparing cosine, dot product, Euclidean, and Manhattan metrics across five properties.
Properties of four common vector similarity metrics across five criteria. Each axis shows a different property rated from 0 (poor) to 5 (excellent). Cosine similarity and normalized dot product are nearly identical in profile, while Euclidean distance differs mainly in its sensitivity to magnitude, and Manhattan distance trades recall quality for outlier robustness.