Sub topic 11
Combining lexical and dense retrieval before reranking
This chapter assumes you can run Python, know what a bi-encoder embedding is, and have built the retrieve-then-rerank pipeline from earlier chapters. Here you will improve the candidate pool that the cross-encoder sees.
Why one retriever is not enough
A reranker can only reorder what it receives. If the right passage is missing from the candidate pool, no reranker can recover it. So the first stage must have high recall@k.
Lexical retrieval (for example BM25) matches exact words. It handles error codes, product names, and rare terms well. It fails when the query and passage use different words for the same idea.
Dense retrieval (a bi-encoder) matches meaning. It handles paraphrases well. It can miss exact identifiers such as ERR_4021 because the embedding blurs rare tokens.
The two methods fail on different queries. Running both and merging the results gives a pool with better recall than either alone. This is called hybrid retrieval.
The merging problem
BM25 scores and cosine similarities live on different scales. BM25 might return 14.2 and cosine might return 0.71. Adding them directly lets one method dominate. You could normalize each score list, but the result is sensitive to outliers and to the score distribution of each query.
Reciprocal rank fusion (RRF) avoids the problem by 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 advantage of the top positions.
def reciprocal_rank_fusion(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)
lexical = ["d2", "d5", "d1"]
dense = ["d1", "d2", "d8"]
print(reciprocal_rank_fusion([lexical, dense]))This prints ['d2', 'd1', 'd5', 'd8']. Documents d2 and d1 appear in both lists, so they rise above d5 and d8, which appear in only one. Document d2 edges out d1 because it ranks first and second, while d1 ranks third and first.
Where fusion sits in the pipeline
The fused list becomes the candidate pool. You then cut it to the candidate depth you chose earlier and pass it to the cross-encoder.
Fusion does not need to be precise. Its job is to put relevant passages somewhere in the top N. The cross-encoder does the fine ordering.
Practical advice
- Retrieve a similar number from each retriever, such as 50 each, then fuse and keep the top 50 to 100.
- Measure recall@N of the fused pool against each single retriever on your own queries. Keep hybrid only if it helps.
- Because the cross-encoder ignores first-stage scores, fusion only changes which passages get scored, not how they are ordered at the end.
- Deduplicate by document ID before reranking so you do not pay twice for the same passage.
Exercise
With k=60, a document is ranked 1st in the lexical list and absent from the dense list. Another document is ranked 5th in both lists. Which has the higher RRF score?
Show answer
The first document scores 1/61 ≈ 0.0164. The second scores 2 × 1/65 ≈ 0.0308. The document ranked 5th in both lists wins, because RRF rewards agreement between retrievers.