Stage 2 of the RAG funnel: Reciprocal Rank Fusion, explained with real numbers

Stage 2 of the RAG funnel: Reciprocal Rank Fusion, explained with real numbers

Part 4 of 7 in the RAG in Production series: engineer-level, on Azure, mapped to Microsoft exam AI-103.

TL;DR for managers. Keyword search and vector search score results on completely different scales, so you can't just add the scores together. Reciprocal Rank Fusion ignores the scores and uses only each result's position in each list. Results that both methods rank highly rise to the top. It needs almost no tuning, and Azure AI Search applies it automatically to every hybrid query.

After stage 1 you have two ranked lists for the same question: one from BM25, one from the vector index. Stage 2 turns them into one list.

Why you can't add the scores

In Azure AI Search, a BM25 @search.score has no upper limit (a strong match might score 12.7, or 48.3 in a longer field). A cosine-similarity vector score runs from 0.333 to 1.00. Adding 12.7 to 0.83 lets BM25 decide everything. Min-max normalising each list to 0 to 1 looks like a fix, but one outlier squashes everyone else, and the scale changes from query to query.

RRF in one line

Reciprocal Rank Fusion comes from Cormack, Clarke and Büttcher (SIGIR 2009). For each document, add up one small number per list it appears in:

RRF(d) = Σ over lists L of  1 / (k + rank_L(d))        with k = 60

Rank 1 contributes 1/61, rank 2 contributes 1/62, and so on. A document missing from a list gets nothing from it. The constant k=60 is what the paper found worked best, and it's what Azure uses.

Worked example

BM25 returns A, B, C, D. Vector search returns E, C, A, F.

Doc BM25 rank Vector rank Calculation RRF score Final
A 1 3 1/61 + 1/63 0.03227 1
C 3 2 1/63 + 1/62 0.03200 2
E (none) 1 1/61 0.01639 3
B 2 (none) 1/62 0.01613 4
D 4 (none) 1/64 0.015625 5=
F (none) 4 1/64 0.015625 5=

Two things to notice. A and C appear in both lists, so they roughly double their score and lead by a wide margin. And E, first in only one list, still beats B, second in only one list: a strong single vote keeps a document in play for the re-ranker.

What k actually controls

k sets how much you trust agreement against a single strong vote. Take doc X, ranked 1st by BM25 and absent from the vector list, and doc Y, ranked 4th in both lists.

k = 1 k = 60
X (1st in one list) 1/2 = 0.500 1/61 = 0.0164
Y (4th in both) 1/5 + 1/5 = 0.400 1/64 + 1/64 = 0.0313
Winner X Y

With a small k, rank 1 is worth far more than rank 4, so one retriever's favourite wins. With k=60 the curve is flat, so agreement wins. For RAG, where the next stage re-ranks anyway, the flat curve is what you want.

A detail in Azure's scores worth knowing

In hybrid results, @search.score is the RRF score. If a document is first in both the text and the vector list, its score is 1/61 + 1/61 = 0.0328. If you see a top hybrid score of about 0.0328, both legs agreed on it. Microsoft's own documentation example shows a hybrid score of 0.032786883 for exactly this reason. More legs (several vector fields, several vector queries) raise the maximum, because each leg adds up to about 1/61. So never apply a fixed threshold to RRF scores across queries with different numbers of legs.

Implement it yourself (10 lines)

You need your own RRF when you fuse results that don't come from one Azure query: two indexes, a SQL source, or a second search service.

from collections import defaultdict

def rrf(*ranked_lists, k=60, weights=None):
    """Reciprocal Rank Fusion. Each list is doc ids, best first."""
    weights = weights or [1.0] * len(ranked_lists)
    scores = defaultdict(float)
    for w, ranked in zip(weights, ranked_lists):
        for rank, doc_id in enumerate(ranked, start=1):
            scores[doc_id] += w / (k + rank)
    return sorted(scores.items(), key=lambda kv: kv[1], reverse=True)

bm25   = ["A", "B", "C", "D"]
vector = ["E", "C", "A", "F"]
for doc, score in rrf(bm25, vector):
    print(doc, round(score, 5))
# A 0.03227
# C 0.032
# E 0.01639
# B 0.01613
# D 0.01562
# F 0.01562

Tuning: vector weights

If your evaluation shows one leg is consistently better for a type of content, Azure lets you weight a vector query in the fusion. A weight multiplies that leg's RRF contribution. For a support index full of error codes you might favour BM25 by down-weighting the vector leg:

{
  "search": "error 0x80070005 installing Teams",
  "vectorQueries": [
    { "kind": "text", "text": "error 0x80070005 installing Teams",
      "fields": "content_vector", "k": 50, "weight": 0.5 }
  ],
  "top": 50
}

Change weights only with an evaluation set in place (part 7). Without one, you are moving numbers based on the last three questions someone complained about.

When RRF is not enough

  • RRF scores are not probabilities. Don't use them to decide "no relevant answer exists". Use the re-ranker score for that (part 5).
  • RRF only re-orders what stage 1 found. A chunk outside both lists stays outside.
  • RRF can't tell that a document at rank 2 in both lists is a near-duplicate of rank 1. Deduplicate at ingestion.

Learning this for AI-103? Microsoft's AI-103 exam (Azure AI Apps and Agents Developer Associate) asks you to implement RAG, configure semantic, hybrid and vector search for grounding, and monitor relevance and grounding quality. Our live online AI-103 course builds these pipelines hands-on in Microsoft Foundry and Azure AI Search, on UK time, with mock assessments before you book the exam.

The RAG in Production series

  1. Part 1: RAG or fine-tuning?
  2. Part 2: The RAG retrieval funnel
  3. Part 3: Stage 1: ANN + BM25 hybrid search
  4. Part 4: Stage 2: Reciprocal Rank Fusion (you are here)
  5. Part 5: Stage 3: re-ranking with cross-encoders
  6. Part 6: Scaling: 10,000 vs 10 million documents
  7. Part 7: Evaluating RAG: retrieval vs generation
Back to blog

Ready to take the first step?

Live online courses on UK time, mapped to the current Microsoft AI exams. No coding needed for AI at work.

Explore AI coursesAsk us a question