Dense Retrieval: Semantic Search & Bi-Encoder Implementation › Embedding Similarity Metrics
Language AI HandbookAll chapters Dense Retrieval: Semantic Search & Bi-Encoder Implementation › Embedding Similarity MetricsOne section · Sections in this chapter4 / 12 ↓
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
Once we have query and document embeddings, we need a similarity function to rank documents. The three most common metrics are dot product, cosine similarity, and Euclidean distance. Each of these metrics captures a different notion of what it means for two vectors to be "similar," and understanding their geometric interpretations helps us choose the right metric for a given application.
Think of the embedding space as a high-dimensional room where every piece of text has a specific location. Similarity metrics are different ways to measure how "close" two locations are. The dot product measures closeness while also caring about how far each vector is from the origin. Cosine similarity measures only the angle between vectors, ignoring how far they are from the origin. Euclidean distance measures the straight-line distance between the two locations in the room. Each notion of closeness has its uses, and the right choice depends on how the embeddings were trained and what properties you want retrieval to have.
Dot Product
The dot product quantifies the similarity between two vectors by aggregating their aligned components. To understand this intuitively, imagine two vectors as arrows pointing in some direction in high-dimensional space. The dot product measures how much one vector "projects" onto another, combining information about both their directions and their lengths. For vectors and , the dot product is defined as:
where:
- is the similarity score
- are the dense vectors for the query and document
- is the dimensionality of the embedding space (e.g., 768)
- are the -th scalar components of each vector
- aggregates alignment across all dimensions
The geometric interpretation of this formula is instructive. Each dimension of the embedding space captures some aspect of meaning. When both and are large and positive, their product contributes positively to the similarity, showing that both the query and document exhibit that particular semantic feature strongly. Conversely, when one component is positive and the other is negative, their product is negative, reducing the overall similarity score. The dot product is therefore highest when the two vectors point in the same direction and have large magnitudes.
The dot product is computationally efficient and captures both the alignment of vectors (their angular similarity) and their magnitudes. Larger embeddings produce larger dot products, which can be useful when magnitude carries semantic meaning. For example, longer, more detailed documents might have larger embedding norms, and this property could be desirable if we want to favor complete, information-rich documents over brief ones.
Many dense retrieval models, including the original DPR (Dense Passage Retrieval), use dot product similarity because it can be computed extremely efficiently using matrix multiplication. Given a query vector and a matrix of document vectors, all similarity scores can be computed in a single matrix-vector multiplication using highly optimized BLAS libraries that run at near-peak hardware throughput.
Cosine Similarity
While the dot product captures both direction and magnitude, there are situations where we want to focus purely on semantic direction, ignoring how "long" the vectors are. Cosine similarity addresses this need by normalizing the dot product by the magnitudes of both vectors, measuring only the angular alignment:
where:
- is the cosine similarity score, ranging from to
- is the unnormalized dot product (raw alignment)
- are the Euclidean norms (lengths) of the vectors
- computes the vector's magnitude as the square root of the sum of squared components
The cosine similarity ranges from (vectors pointing in opposite directions) to (vectors pointing in the same direction), with showing orthogonality. By removing magnitude effects, cosine similarity focuses purely on semantic direction. This is particularly useful when we want to compare texts of different lengths on an equal footing, since a longer document might naturally produce a larger-magnitude embedding without being more relevant to any particular query.
The key insight is that by normalizing, we make the similarity score a function only of the angle between the vectors. Two documents that say exactly the same thing but one is much longer (perhaps with more examples and explanations) should have similar cosine similarity to a query, even if the longer document's raw embedding vector is larger in magnitude. This length-invariant behavior is often what we want in retrieval.
A useful equivalence emerges when embeddings are L2-normalized, meaning each vector is divided by its magnitude so that . In this case, cosine similarity equals the dot product:
Here, and are the unit-length vectors derived by dividing and by their respective norms. The dot product operation, applied to these normalized vectors, directly yields the cosine of the angle between the original vectors.
This equivalence is practically very useful: we can pre-normalize all document embeddings once during indexing, then use the faster dot product operation at query time while still computing cosine similarity. This is exactly what many production systems do, including most models in the Sentence Transformers library. Pre-normalizing once means we never need to compute norms at query time, keeping retrieval latency minimal.
Euclidean Distance
The third common metric takes a different perspective entirely. Rather than measuring how vectors align, Euclidean (L2) distance measures the straight-line distance between vectors in the embedding space:
where:
- is the Euclidean distance (lower means more similar)
- is the difference vector, computed element-wise
- is the squared difference in dimension , which is always non-negative
- converts the sum of squared differences to linear distance units
Unlike the similarity metrics above, smaller Euclidean distances indicate greater similarity. This is an important distinction to keep in mind when implementing retrieval systems: with Euclidean distance, we search for the nearest neighbors rather than the highest-scoring documents.
Euclidean distance is related to dot product for normalized vectors. When vectors have unit length, minimizing Euclidean distance is mathematically equivalent to maximizing the dot product. To see why, expand the distance formula:
Since and are constants, minimizing is equivalent to maximizing , which for normalized vectors equals cosine similarity. This relationship allows certain approximate nearest neighbor indexing algorithms designed for Euclidean distance to be adapted for dot product or cosine similarity retrieval, which is important for efficient large-scale search.
Using Python 3.11.14 environment at: /private/tmp/mb-language-ai-modern-plots/books/_quarto_language-ai-handbook/.venv Checked 2 packages in 3ms


Choosing a Metric
The choice of similarity metric should match the training objective used when the embedding model was created. This is not a trivial constraint: using the wrong metric can substantially degrade retrieval quality even for an otherwise excellent model.
- Models trained with dot product loss perform best with dot product similarity at inference time
- Models trained with cosine loss perform best with cosine similarity
- Many embedding models normalize their outputs, making the choice less necessary in practice
In practice, cosine similarity (or equivalently, dot product with normalized embeddings) is the most common choice because it is reliable to variations in embedding magnitude and provides easily interpretable scores. When similarity ranges from to , it becomes straightforward to set thresholds and compare scores across different queries and different corpora. A score of 0.8 means something consistent regardless of whether the query is long or short, whether the document is a sentence or a paragraph.
One subtle consideration is how the model was trained with respect to embedding norms. Some contrastive learning setups allow the model to encode confidence or importance in the embedding norm, making dot product the preferred metric. Others explicitly normalize embeddings during training, making cosine similarity the correct choice. Always check the documentation for any pre-trained embedding model to understand what metric it expects.