RRF (Reciprocal Rank Fusion)
A Python library implementing various Reciprocal Rank Fusion strategies for RAG systems.
What it solves
RAG systems often search two ways at once, by keywords and by meaning, and then merge the two ranked lists. Reciprocal Rank Fusion is the usual way to merge them: each result scores 1 / (k + rank), and the scores add up.
But retrievers return chunks, not documents. A long document can appear several times in one list, and if every chunk counts, it can beat a short document that both retrievers ranked first:
- 1Rotation guide
- 2Platform handbook · part 3
- 3Platform handbook · part 9
- 4other result
- 5Platform handbook · part 14
- 1Rotation guide
- 2other result
- 3other result
- Rotation guide: short, on topic
- Platform handbook: long, mentions keys in passing
Count every chunk
- 10.0474
- 20.0328
The handbook wins on volume.
Count each document once per list
- 10.0328
- 20.0161
The guide wins on relevance.
This library fuses at the document level and offers five strategies for handling those repeats.
Where it fits
Fusion is cheap and needs no training, and it decides which candidates the more expensive reranker gets to see.
Try it
Two retrievers have answered the same question. Pick a strategy and watch the fused ranking change; hover a document to see where its chunks came from.
Every hit adds 1 / (k + rank), and repeats add up. At the default k, B wins on volume: four low-ranked keyword hits.
Lower k makes the top ranks count for more.
What each retriever returned
Query: How do I rotate API keys without downtime?
- 1Glossary: API key
- 2Key rotation runbook
- 3REST API reference
- 4Zero-downtime deploys
- 5Credential lifecycle guide
- 6REST API reference
- 7REST API reference
- 8REST API reference
- 1Credential lifecycle guide
- 2Credential lifecycle guide
- 3Key rotation runbook
- 4Key rotation runbook
- 5Zero-downtime deploys
- 6Credential lifecycle guide
- 7REST API reference
- 8Security FAQ
Fused ranking
Top result: REST API reference
- 3Key rotation runbook0.04763 chunks
- 1REST API reference0.07565 chunks
- 5Glossary: API key0.01641 chunk
- 6Security FAQ0.01471 chunk
- 2Credential lifecycle guide0.06314 chunks
- 4Zero-downtime deploys0.03102 chunks
The five strategies
One document, found at ranks 1, 3 and 8 in the same list. How much each hit counts:
Diminishing returns
RecommendedEvery hit counts, each less than the last.
At the default α = 0.5, all the repeats together add less than the best hit alone, so depth helps but never outweighs it.
λ · Σ α^j / (k + r_j)rank 1× 1rank 3× 0.5rank 8× 0.25Standard RRF
Every hit counts in full.
Σ 1 / (k + r)rank 1× 1rank 3× 1rank 8× 1Best rank
One vote per list, at its best rank.
Σ_lists 1 / (k + r_best)rank 1× 1rank 3not countedrank 8not countedMax + bonus
The best hit counts in full; the rest add a bonus.
1 / (k + r_best) + λ · Σ_rest 1 / (k + r)rank 1× 1rank 3× 0.3rank 8× 0.3Soft dedup
Every hit counts, but repeats are pushed down their list first.
r′ = r · (1 + β · j)rank 1× 1rank 3→ 3.3 · × 0.995rank 8→ 9.6 · × 0.98
Library defaults: k = 60, α = 0.5, λ = 0.3, β = 0.1. Diminishing returns also scales the total by λ.