AI Hints
Home›RAG Tutorial›Hybrid Search and Reranking
RAG · Lesson 56 of 120

Reciprocal Rank Fusion

Merging ranked lists.

⏱ 2 min read▶ Video at 55:41🎓 Free
Watch this lesson in the RAG courseWatch on YouTube ↗Full RAG course

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.

Reciprocal Rank Fusion
From the video: Reciprocal Rank Fusion
  • 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.

keyvalue
Points1 / (60 + rank)
1st place0.0164
10th place0.0143
Totalsum 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