WRITE · TRACE · UNDERSTAND

Fuse retriever rankings without comparing incompatible scores

Implement rrf(rankings,k=60) returning all ids sorted by descending sum 1/(k+rank), then id.

Read lesson: Fuse retriever rankings without comparing incompatible scores →

Your experiment

Implement rrf(rankings,k=60) returning all ids sorted by descending sum 1/(k+rank), then id. Rank unique ids within each list starting at 1, ignoring repeated appearances without consuming another rank. Require k>0.

Python runs in your browser. Your draft stays on this device.

What to look for

RRF uses ranking evidence instead of uncalibrated score scales. Tune and evaluate it on representative relevance judgments, keeping final evaluation queries separate from parameter selection.

Watch your code work

Ready to experiment

Write a solution or load the worked answer, then choose Trace & check.

Step through arrays, variables and training curves produced by your own Python.

About this algorithm

How Fuse retriever rankings without comparing incompatible scores works

The idea

Reciprocal rank fusion, RRF, merges several ranked lists into one. Every list gives an item 1 / (k + rank) points, and rrf adds the points up across lists.

Scores from different retrievers cannot be compared: a BM25 score of 12 and a cosine of 0.8 sit on unrelated scales. Positions can be compared, so the method ignores the scores and keeps only the ranks. An item that several retrievers place near the top beats one that a single retriever puts first. The constant k, 60 by default, flattens the curve so that rank 1 is worth only a little more than rank 2. In this Lab a repeated id inside one list counts once and does not use up a rank.

How it works

  1. Raise ValueError unless k > 0.
  2. For each ranking, drop repeated ids with dict.fromkeys, which keeps the first position of each.
  3. Number the remaining ids from rank 1 and add 1 / (k + rank) to that id's total in scores.
  4. An id that a ranking does not contain gets nothing from that ranking.
  5. Return every id sorted by descending total, with equal totals ordered by the id itself.

What it costs

Time
O(L + m log m)
Space
O(m)

L is the total number of entries across all rankings and m the number of distinct ids; each entry is read once and the m totals are sorted.

Where it is used

  • Elasticsearch has an rrf retriever that combines lexical and vector result sets, with a rank_constant that defaults to 60.
  • Azure AI Search uses Reciprocal Rank Fusion to merge the text and vector results of a hybrid query.
  • Cormack, Clarke and Büttcher introduced RRF at SIGIR 2009, where it beat Condorcet fusion and individual learning-to-rank methods on TREC data.

Watch out for

  • Adding the retrievers' raw scores instead of using ranks, which lets the retriever with the largest score scale decide the order.
  • Starting ranks at 0: the formula uses rank 1 for the first item, so every contribution comes out too large.
  • Counting a repeated id twice within one list, or letting the repeat consume a rank and push later ids down.