RAG · Lesson 56 of 120
Reciprocal Rank Fusion
Merging ranked lists.
Keyword scores and embedding scores are on different scales, so we can't just add them. Reciprocal rank fusion ignores the scores, and uses only each chunk's position in each list.

- Different score scales
- Use positions
- Not scores
The formula
Each list gives a chunk one divided by sixty plus its rank. First place gives about point zero one six. Tenth place gives a bit less. Add up a chunk's points from every list. Chunks near the top of both lists win.
| key | value |
|---|---|
| Points | 1 / (60 + rank) |
| 1st place | 0.0164 |
| 10th place | 0.0143 |
| Total | sum across lists |
RRF by hand
RRF loops over each ranked list, and adds one over sixty plus rank to each item. We merge two small lists of page names. Run it. Returns is near the top of both lists, so it wins overall.
rrf.pypython
def rrf(*ranked_lists, k=60):
scores = {}
for ranked in ranked_lists:
for rank, item in enumerate(ranked):
scores[item] = scores.get(item, 0) + 1 / (k + rank + 1)
return sorted(scores, key=lambda item: -scores[item])
meaning = ["warranty", "returns", "shipping"]
keywords = ["returns", "stores", "warranty"]
print(rrf(meaning, keywords))Output
['returns', 'warranty', 'stores', 'shipping']
Summary
Reciprocal rank fusion merges ranked lists by position, so different score scales don't matter. Next, reranking.
Key takeaways- 1 / (60 + rank)
- Add across lists
- Top of both wins
Next up: Reranking — a second, closer look