Sub topic 8
Semantic retrieval: matching the meaning
This chapter assumes you know the retrieval-augmented generation (RAG) loop from earlier chapters: the system retrieves passages, then a language model writes an answer from them. It also assumes you have read the chapter on lexical retrieval. No programming is needed, though one short Python sketch is included.
What semantic retrieval does
Lexical retrieval asks which documents contain the words in the query. Semantic retrieval asks which passages mean something close to the query, even if they share few words with it.
A query such as "shoes for flat feet" can match a passage about "stability footwear for overpronation". The passage never says "flat feet", but the two ideas sit close together in how a language model represents text. That difference is why semantic retrieval is a core part of AI search.
Embeddings: text as coordinates
An embedding is a list of numbers, called a vector, that a trained model produces for a piece of text. Texts with similar meaning get vectors that point in similar directions. Real embeddings usually have hundreds or thousands of numbers. You can picture them as coordinates in a very large space where related ideas cluster.
The model that makes embeddings is trained on large amounts of text. It learns that "sofa" and "couch" appear in similar contexts, and that "bank" near "river" differs from "bank" near "loan". You do not control this model, and different systems use different ones.
How a semantic search runs
The process has an offline part and an online part.
- Offline, the system splits pages into passages (see the chapter on chunking) and embeds each passage.
- It stores the vectors in a vector index, a database built to find nearby vectors quickly.
- Online, it embeds the query, or each sub-query after fan-out, with the same model.
- It returns the passages whose vectors are closest to the query vector.
The query and the passages must be embedded by the same model, or one compatible with it. Vectors from different models are not comparable.
Measuring closeness
The most common measure is cosine similarity. It compares the angle between two vectors and ignores their length. A score near 1 means the vectors point the same way. A score near 0 means they are unrelated.
The sketch below uses invented three-number vectors so you can see the mechanism. It is an illustration, not output from any real system.
import math
def cosine_similarity(a, b):
dot = sum(x * y for x, y in zip(a, b))
norm_a = math.sqrt(sum(x * x for x in a))
norm_b = math.sqrt(sum(y * y for y in b))
return dot / (norm_a * norm_b)
# Invented vectors. Real embeddings have hundreds or thousands of dimensions.
passages = {
"Stability shoes add support for overpronation.": [0.9, 0.1, 0.2],
"Arch support helps people with low arches.": [0.8, 0.3, 0.1],
"Our store opens at 9am on weekdays.": [0.0, 0.1, 0.95],
}
query = [0.85, 0.2, 0.15] # pretend embedding of "shoes for flat feet"
ranked = sorted(
passages.items(),
key=lambda item: cosine_similarity(query, item[1]),
reverse=True,
)
for text, vector in ranked:
print(round(cosine_similarity(query, vector), 3), text)The two shoe passages score about 0.99 and the store-hours passage scores about 0.19. Neither shoe passage contains the word "flat", yet both rank above the unrelated one. That is the behaviour semantic retrieval adds over keyword matching.
Finding neighbours at scale
Comparing a query against billions of passages one by one is too slow. Vector indexes therefore use approximate nearest neighbour (ANN) search. They return passages that are very likely, but not guaranteed, to be the closest. This is a documented engineering trade-off in vector search generally. For you, it means retrieval is not perfectly repeatable or exhaustive. A good passage can occasionally be missed.
What semantic retrieval rewards in content
Some of the following is documented in how embedding search works. Some is inference about how AI search products use it. The chapter on uncertainty covers that boundary.
- Clear topical focus per passage. A passage that covers one idea gets a vector that represents that idea. A passage mixing pricing, history and a product pitch gets a blurred vector that sits near none of them.
- Natural wording of the concept. You can explain "overpronation" in the same passage that mentions "flat feet", so both phrasings land near each other. You do not need to stuff synonyms in.
- Self-contained passages. Embedding sees only the passage text. If a paragraph says "it costs more than the other one", the vector has little to anchor to. Name the subject in the passage itself.
- Direct answers to a question. Passages phrased like an answer to a likely sub-question tend to sit near the vector for that question.
Where semantic retrieval fails
Embeddings compress meaning, and compression loses detail. Know these failure modes:
| Weakness | Example | Why it happens |
|---|---|---|
| Exact identifiers | Model number "XT-4410" | Rare strings carry little meaning for the model, so near-miss codes look similar |
| Numbers and constraints | "under 100 dollars" vs "over 100 dollars" | The topic is the same, so the vectors are close |
| Negation | "not suitable for flat feet" | Text with and without "not" often embeds nearby |
| Very new terms | A brand launched last month | The model may not have seen the term in training |
| Long, mixed passages | A page section covering five topics | One vector cannot represent all five well |
These gaps are one reason many systems combine semantic retrieval with keyword matching, which the next chapter covers. A later reranking step can also catch some of these errors.
What you can and cannot know
The general technique is well documented in public research and in the documentation of vector databases and embedding providers. What is not public for most AI search products is which embedding model they use, how they chunk pages, how many passages they retrieve, and how semantic scores are weighed against other signals. Any claim that a specific engine "uses cosine similarity above 0.8" or "embeds only the first 500 words" is speculation unless the vendor has published it.
Practical checks
- Pick a sub-question you expect, such as "stability vs motion control shoes".
- Find the passage on your page that answers it.
- Confirm the passage names its subject, states one idea, and does not depend on surrounding paragraphs.
- Confirm that any exact details, such as names, numbers and prices, appear as literal text.
The next chapter shows how a system can combine this meaning-based signal with keyword matching, so these exact details are not lost.
Exercise
A page section says "It works better than the older one, and it costs less." Explain two reasons a semantic retriever may handle this passage poorly, and how you would fix it.
Show answer
First, the passage names no subject, so its embedding has little to anchor to and may sit near many unrelated comparisons. Second, it holds no concrete terms such as product names, which a lexical signal would need and which would also sharpen the embedding. The fix is to rewrite the passage so it stands alone, for example "The Model B stabilises the foot better than the Model A and costs 20 dollars less", keeping it focused on one comparison.
Exercise
Why must the query and the passages be embedded with the same model, and why is retrieval from a vector index not guaranteed to return the single closest passage?
Show answer
Each model has its own coordinate space, so vectors from different models cannot be compared meaningfully. Large vector indexes use approximate nearest neighbour search for speed, which returns passages that are very likely close but may miss the true nearest one.