Sub topic 11
Hybrid search: keywords plus vectors
This chapter assumes you know what embeddings are, can read basic Python and SQL, and have seen how metadata filtering narrows a vector search. It covers why pure vector search sometimes fails, how keyword search complements it, and how to merge the two result lists.
Why vectors alone are not enough
Vector search finds items whose meaning is close to the query. It is weak on exact tokens. Product codes such as XR-7741, rare names, error messages and acronyms often have no useful position in embedding space, because the model never saw them often enough to place them well.
Keyword search has the opposite profile. It matches the literal terms, so XR-7741 finds the document containing XR-7741. It fails when the query and the document use different words for the same idea, such as "car" and "automobile".
Hybrid search runs both and combines the results.
| Property | Keyword (BM25) | Vector |
|---|---|---|
| Matches on | Exact terms | Meaning |
| Good at | IDs, names, rare terms | Paraphrase, synonyms |
| Weak at | Synonyms, typos in intent | Exact strings |
| Score scale | Unbounded, depends on corpus | Bounded by the metric |
BM25 is the standard keyword ranking function. It scores a document higher when it contains the query terms often, when those terms are rare across the corpus, and when the document is not unusually long.
The two-retriever pipeline
The common design sends the query down two paths. One path runs a keyword index. The other embeds the query and runs a nearest neighbor search. A fusion step merges the two ranked lists into one.
Combining scores
The two retrievers produce scores on different scales, so adding them directly is unreliable. A BM25 score of 14 and a cosine similarity of 0.82 are not comparable. There are two common fixes.
- Weighted sum after normalization. Rescale each score list to a common range, such as 0 to 1, then compute
alpha * vector + (1 - alpha) * keyword. This needs a tunedalpha, and min-max scaling is sensitive to outliers. - Reciprocal rank fusion (RRF). Ignore scores and use only ranks. Each document earns
1 / (k + rank)from each list, and the earned values are summed. The constantkis usually 60. RRF needs no normalization and works well as a default.
Reciprocal rank fusion in code
This example fuses two ranked lists of document IDs. A document that appears high in both lists wins.
def rrf(rankings, k=60):
scores = {}
for ranking in rankings:
for rank, doc_id in enumerate(ranking, start=1):
scores[doc_id] = scores.get(doc_id, 0.0) + 1.0 / (k + rank)
return sorted(scores, key=scores.get, reverse=True)
keyword_hits = ["d3", "d1", "d7", "d9"]
vector_hits = ["d1", "d4", "d3", "d8"]
fused = rrf([keyword_hits, vector_hits])
print(fused)d1 and d3 appear in both lists, so they rank first. d1 comes out ahead because its two ranks (2 and 1) sum to a larger score than the ranks for d3 (1 and 3).
Common errors
- Adding raw scores from the two systems without normalizing them. One scale dominates and the other contributes almost nothing.
- Retrieving too few candidates from each side. Fusion can only rank what it receives, so fetch more than the final count, for example 50 from each list to return 10.
- Using different text processing for indexing and querying on the keyword side, such as stemming at index time but not at query time.
- Tuning on a handful of queries. Measure with labeled queries, as described in the chapter on evaluation, and include ID-style and paraphrase-style queries.
- Assuming hybrid is always better. For purely conceptual queries, the keyword side can add noise, so check the results on your own data.
Exercise
A query for the error code "ECONNRESET" returns semantically related networking articles but not the page that contains the exact code. Which retrieval method is likely missing, and why does reciprocal rank fusion suit combining it with vector search?
Show answer
Keyword search (BM25) is missing, because it matches exact tokens that embeddings often represent poorly. RRF suits the combination because it uses only ranks, so it avoids comparing BM25 scores with vector similarity scores, which are on different scales.
Exercise
Using the rrf function above with k=60, what is the fused score of a document ranked 1st in one list and 3rd in the other?
Show answer
1/61 + 1/63, which is about 0.01639 + 0.01587 = 0.03226.