Skip to content

How does Reciprocal Rank Fusion (RRF) combine scores from sparse and dense retrievers?

Intermediate · 2 min read · RAG & Vector Databases

Open in study mode to save this question and track progress

Short answer: Reciprocal Rank Fusion (RRF) is an algorithm that merges search results from multiple independent retrieval systems (e.g. BM25 keyword search and dense vector search) by calculating a combined score based on each document's reciprocal rank position across query result lists, requiring zero score normalization.

Detail

Dense vector scores (cosine similarity [0,1][0, 1]) and BM25 sparse scores ([0,∞)[0, \infty)) operate on incompatible scale distributions. Direct score addition fails.

RRF Formula

RRF_Score(d∈D)=∑m∈M1k+rm(d)\text{RRF\_Score}(d \in D) = \sum_{m \in M} \frac{1}{k + r_m(d)}

Where:

  • MM is the set of retrieval algorithms (e.g. BM25, Vector Search).
  • rm(d)r_m(d) is the 1-based rank position of document dd in retriever mm's result list.
  • kk is a smoothing constant (standard default k=60k = 60).
Document A: BM25 Rank 1, Vector Rank 5  ──► RRF = 1/(60+1) + 1/(60+5) = 0.01639 + 0.01538 = 0.03177
Document B: BM25 Rank 20, Vector Rank 1 ──► RRF = 1/(60+20) + 1/(60+1) = 0.01250 + 0.01639 = 0.02889

Example

Python RRF merger implementation:

python
def reciprocal_rank_fusion(results_list: list[list[str]], k: int = 60) -> list[tuple[str, float]]:
    rrf_scores = {}
    for results in results_list:
        for rank, doc_id in enumerate(results, start=1):
            if doc_id not in rrf_scores:
                rrf_scores[doc_id] = 0.0
            rrf_scores[doc_id] += 1.0 / (k + rank)

    # Sort documents by combined RRF score descending
    sorted_docs = sorted(rrf_scores.items(), key=lambda item: item[1], reverse=True)
    return sorted_docs

bm25_res = ["doc1", "doc2", "doc3"]
vector_res = ["doc2", "doc1", "doc4"]
print("RRF Combined Rankings:", reciprocal_rank_fusion([bm25_res, vector_res]))

Interview tips

  • Explain why RRF is robust: it relies strictly on rank order rather than raw similarity score magnitudes, eliminating score scaling skew between different embedding models or search engines.
  • Highlight using RRF prior to passing candidates to a cross-encoder reranker.

Related Concepts


⬅ Back to RAG and Vector Databases · All topics