liveBook · Manning

Reciprocal rank fusion

As explained in Retrieval Augmented Generation, The Foundational Ideas

Rank fusion is a retrieval technique that combines ranked result lists from multiple searches into a single ranking.

A single search can miss relevant documents because one phrasing captures only part of a user’s intent. Reciprocal rank fusion (RRF) combines ranked result lists from multiple searches into one list. In a RAG pipeline, those searches might use different query variants; the fused list is assembled before documents are passed to the answer generator. section 8.0 section 8.4

RRF ignores each search’s original scores. For every document, it adds 1 / (k + rank) for each list in which the document appears, then sorts documents by the total. A document found consistently across several lists can outrank one that tops only a single list. This avoids averaging scores that may have different scales, such as cosine similarity and BM25 scores. section 8.3

Listing 8.2 Reciprocal Rank Fusion algorithm.
from collections import defaultdict
from typing import List, Tuple, Dict
 
def reciprocal_rank_fusion(
    ranked_lists: List[List[str]], k: int = 60
) -> List:
    fused_scores: Dict[str, float] = defaultdict(float)  #A
    for ranked_list in ranked_lists:  #B
        for index, doc_id in enumerate(ranked_list):
            rank = index + 1  #C
            score = 1.0 / (k + rank)  #C
            fused_scores[doc_id] += score  #D
    sorted_docs = sorted(  #E
        fused_scores.items(),
        key=lambda item: item[1],
        reverse=True  #F
    )
 
    return sorted_docs
 

The Python implementation accumulates reciprocal-rank contributions by document ID and sorts the resulting totals; k defaults to 60. section 8.3

A product-search result that one query misses

A search for a warm winter jacket returns close matches to those words but few kinds of winter clothing. Separate searches for parkas, fleeces, and ski jackets produce different candidates. Fusing their lists brings a down parka and a fleece-lined ski jacket into the top results alongside a warm jacket. section 8.5

Choose RRF for its simple rank-based merge, but keep its limits in view:

  • It does not require score normalization, unlike score-summing methods whose inputs may be uncalibrated. section 8.3
  • It discards match strength: a first-place result with a weak raw score contributes as much as one with a strong raw score. Carry raw scores separately if a later stage needs confidence information. section 8.3
  • Its results are sensitive to k. In multi-query retrieval, extra variants also add cost, latency, and opportunities for noise; three to five queries with k = 60 are a starting point, not a guarantee. section 8.3 section 8.7

RRF also fits hybrid retrieval: a HyDE vector search and a lexical search can run in parallel, then merge their rankings. That retains semantic reach while giving exact terms—such as part numbers or drug names—a route into the final list. section 7.6.2

How Retrieval Augmented Generation, The Foundational Ideas covers it

How deep it goes
Works through the reciprocal-rank formula and Python implementation, then identifies score loss and k sensitivity.
Examples
Uses a Python fusion function and a simulated winter-clothing search to show how repeated appearances change the ranking.
What it assumes
Assumes searches can return ranked document lists; defines the fusion calculation without requiring calibrated retrieval scores.
What you can do after
Build it from scratch using ranked document IDs, or apply it within a multi-query RAG retrieval pipeline.
How it connects
Connects multi-query RAG, query expansion, BM25, HyDE, and hybrid search through a shared rank-based merge.

Also explained in

Compare how 6 books explain reciprocal rank fusion →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Retrieval Augmented Generation, The Foundational Ideas. The section links point to the parts of the book it draws on.