Dense Retrieval: Semantic Search & Bi-Encoder Implementation › Worked Example: Semantic Similarity in Action
Language AI HandbookAll chapters Dense Retrieval: Semantic Search & Bi-Encoder Implementation › Worked Example: Semantic Similarity in ActionOne section · Sections in this chapter7 / 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
Let's trace through a concrete numerical example to build intuition for how dense retrieval works from query input to ranked output. This example makes the abstract concepts concrete and shows exactly what happens at each step.
Setup: We have a three-dimensional toy embedding space. Query = "How do plants make food?" has been encoded to the vector . We have three candidate documents with pre-computed embeddings:
- Document A: "Photosynthesis turns light energy into chemical energy in plants." Embedding:
- Document B: "Plants produce glucose from carbon dioxide and sunlight." Embedding:
- Document C: "My grandmother makes delicious food using fresh vegetables." Embedding:
Documents A and B are about plant biology and photosynthesis; Document C is about cooking. The embeddings reflect this: A and B have high values in dimensions 1 and 2 (which we can think of as capturing "biology" and "plant science"), while C has a high value in dimension 3 (capturing "food/cooking").
Step 1: Normalize all embeddings. Before computing similarities, we normalize each vector to unit length:
Similarly normalizing: , , .
Step 2: Compute cosine similarities (dot products of normalized vectors).
Step 3: Rank by similarity. The ranking is: Document A (0.984) > Document B (0.941) > Document C (0.737).
Step 4: Interpret the result. Despite Document C containing the word "food" (which appears in the query "How do plants make food?"), it ranks last. Documents A and B, which describe photosynthesis without using the words "make" or "food," rank first and second. The embedding space has captured the semantic intent of the query: we are asking about plant biology, and the model correctly identifies plant biology documents as most relevant, while recognizing that "making food" in the cooking sense is semantically different.
Now consider what BM25 would do. The query contains terms: "how," "do," "plants," "make," "food." Document A contains "plants" once. Document B contains "Plants" once. Document C contains "food" once and is about cooking with vegetables from a garden (semantically related to "plants" in a different sense). BM25 might rank Document C highest due to the "food" term match, which appears to be a highly specific query term. Document A and B each only match "plants," giving them lower BM25 scores. This illustrates the vocabulary mismatch problem directly: the lexical method is misled by surface-level matches, while the semantic method captures true intent.
The key insight from this numerical example is that dense retrieval's power comes entirely from the quality of the embedding space. The toy embeddings above were constructed to make the example clear, but real embedding spaces are 768-dimensional and learned from billions of text examples. The geometric relationships in those learned spaces capture fine-grained semantic associations that no human could engineer by hand.
