Sub topic 10
Combining keyword and vector search
This chapter assumes you can read basic Python and know that an embedding is a vector, and that vector search ranks passages by similarity. You should also know how top-k retrieval works from the previous chapters.
Why one retriever is not enough
Vector search matches meaning. It finds "how do I reset my password" when the document says "account credential recovery." It is weak on exact strings, though. Product codes, error numbers, function names, and rare surnames often get blurred in an embedding.
Keyword search (for example BM25, a scoring formula based on term frequency) has the opposite profile. It matches exact tokens well and misses paraphrases. Hybrid search runs both retrievers and merges their results.
| Query | Better retriever | Reason |
|---|---|---|
| "ERR_4012 on login" | Keyword | The exact token matters. |
| "why can't I sign in" | Vector | The wording differs from the document. |
| "ERR_4012 meaning" | Both | Needs the exact code and the explanation. |
The score problem
You can't simply add the two scores. BM25 scores are unbounded and depend on the corpus. Cosine similarity falls between -1 and 1. Adding them lets one retriever dominate. You could normalize each score, but outliers make that fragile.
Reciprocal rank fusion (RRF) avoids the issue by ignoring scores and using only ranks. Each document earns 1 / (k + rank) from every list it appears in, and the scores are summed. The constant k (commonly 60) softens the gap between the first and second places.
def reciprocal_rank_fusion(rankings, k=60):
# rankings is a list of ranked lists of document ids, best first
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_ranking = ["doc_7", "doc_2", "doc_9"]
vector_ranking = ["doc_2", "doc_4", "doc_7"]
print(reciprocal_rank_fusion([keyword_ranking, vector_ranking]))This prints ['doc_2', 'doc_7', 'doc_4', 'doc_9']. Both doc_2 and doc_7 appear in both lists. doc_2 wins because its ranks (2 and 1) sum to a higher fused score than the ranks of doc_7 (1 and 3). Documents found by only one retriever still appear, lower down.
Practical points
- Fetch more candidates from each retriever than you finally need, for example 20 each to keep 5. Fusion can only reorder what it receives.
- Apply metadata filters inside both retrievers, not after fusion. Otherwise filtering can leave you with too few results.
- RRF treats both lists equally. To favor one, multiply its contribution by a weight.
- Use the same document ids in both indexes. If chunk ids differ, the same passage is counted twice.
- Compare hybrid against each retriever alone using recall at k. Hybrid helps most on corpora with codes, names, or jargon, and gives little on plain prose.
Exercise
A query returns docA at rank 1 in the keyword list only, and docB at rank 3 in both lists. With k=60, which document ranks higher after RRF?
Show answer
docB. Its score is 2 × 1/63 ≈ 0.0317, while docA scores 1/61 ≈ 0.0164. Appearing in both lists outweighs a top rank in just one.