Sparse and dense retrievers fail on different questions, so a production system runs both. This is hybrid retrieval, and Reciprocal Rank Fusion (RRF) is the usual way to merge the 2 ranked lists.
Why rank and not score
The scores of the 2 retrievers cannot be compared, for example a BM25 score of 8.2 against a cosine of 0.71. RRF ignores the scores and uses positions in each list.
Parts of the formula:
- is the position of passage in the list of retriever , where 1 is the top, and a passage missing from a list earns 0 from it
- gives points for that position, highest for rank 1 and shrinking down the list
- the sum over retrievers means a passage that both retrievers like rises to the top
- evens out the gap between neighbouring ranks, so agreement counts for more than one first place
Worked example
Dense ranking is [C3, C1, C7] and BM25 ranking is [C1, C9, C3].
| Passage | Points | Total |
|---|---|---|
| C1 | 0.03252 | |
| C3 | 0.03227 | |
| C9 | 0.01613 | |
| C7 | 0.01587 |
The fused order is C1, C3, C9, C7. C3 is the favourite of the dense retriever but only third for BM25, while C1 is near the top of both lists, so it wins. A passage that both retrievers like is a safer bet than the favourite of either one.
Score fusion as an alternative
- min-max normalise both scores, then combine them as
- the value of must be tuned for each collection
- RRF is easier to use because it has no scale to match and almost no tuning
Code
def rrf(rankings, k=60):
"""Reciprocal Rank Fusion: every list awards 1/(k + rank) points, and the points are summed."""
points = {}
for ranking in rankings: # one ranked list per retriever
for rank, doc in enumerate(ranking, start=1):
points[doc] = points.get(doc, 0.0) + 1.0 / (k + rank)
return sorted(points, key=points.get, reverse=True), points
order, points = rrf([["C3", "C1", "C7"], ["C1", "C9", "C3"]])
print(order) # ['C1', 'C3', 'C9', 'C7']
def hybrid_search(query, k=50):
"""Run both retrievers and fuse their ranked lists with RRF."""
order, _ = rrf([sparse_search(query), dense_search(query)])
return order[:k]Notes:
enumerate(..., start=1)makes the top passage rank 1, not rank 0sparse_searchanddense_searchare the sparse and dense search functions from the retrieval steps- hybrid retrieval keeps the strength of BM25 on exact IDs and most of the strength of the dense retriever on paraphrases, so it gives the best overall MRR, but it is not perfect on every question
The fused list is still only a first look, and a more careful second look comes from Re-ranking.