DEV Community

Rajat Sharma
Rajat Sharma

Posted on

Understanding Reciprocal Rank Fusion (RRF) in Retrieval-Augmented Systems

In our last discussion, we explored how Retrieval-Augmented Generation (RAG) enhances large language models (LLMs) by fetching external information to improve their responses.

Today, let's dive deeper into a key retrieval technique used inside RAG systems: Reciprocal Rank Fusion (RRF).


โœจ What is Reciprocal Rank Fusion (RRF)?

Reciprocal Rank Fusion is a simple yet powerful algorithm used to combine search results from multiple queries.

Instead of depending on just a single query to retrieve documents, RRF:

  • Fans out multiple subqueries
  • Retrieves results separately for each
  • Merges them so that higher-ranked results are prioritized across the different searches

๐Ÿ”ฅ In short: RRF combines multiple search results intelligently so that the most relevant documents rise to the top.


๐Ÿค” Why Use RRF?

Sometimes a single query can't capture everything the user needs.

For example, if a user asks:

"What are the challenges of task decomposition?"

Depending on how it's phrased, you might miss out on some valuable documents!

โœ… By generating multiple sub-questions,

โœ… Retrieving documents for each sub-question, and

โœ… Merging the results using RRF,

โ€”you ensure broader coverage without losing precision.


๐Ÿ›  How Does RRF Work?

Let's break it down step-by-step:

Stage What Happens
Subquery Generation Create multiple focused sub-questions based on the main query.
Parallel Retrieval Retrieve relevant documents separately for each sub-question.
Reciprocal Rank Fusion Merge documents intelligently based on their ranks (using the RRF formula).
Final Selection Select top-scoring documents for context or generation.

๐Ÿงฎ The RRF Formula

Each document's RRF score is computed as:

RRF_score = โˆ‘ (1 / (k + rank))
Enter fullscreen mode Exit fullscreen mode
  • rank = Position of the document in the retrieval list (starting from 0)
  • k = A small constant (commonly 60) that dampens the impact of lower-ranked documents.

โšก Thus, documents that consistently rank higher across different queries will be scored higher and surfaced earlier!


๐Ÿ”ฅ Flowchart for Visual Understanding

Flow chart of Reciprocal Rank Fusion


๐Ÿงฉ Simple Code Snippet for Learning

Hereโ€™s a mini Python function showing how RRF merging happens:

def reciprocal_rank_fusion(subquestions, k=60):
    all_ranked_results = []

    # Retrieve chunks for each sub-question
    for subq in subquestions:
        chunks = retrieve_chunks(subq["question"])
        all_ranked_results.append(chunks)

    score_dict = {}

    # Apply RRF scoring
    for chunks in all_ranked_results:
        for rank, doc in enumerate(chunks):
            key = (doc.metadata.get("page"), doc.page_content.strip())
            if key not in score_dict:
                score_dict[key] = {"doc": doc, "score": 0}
            score_dict[key]["score"] += 1 / (k + rank)

    # Sort by score
    fused_docs = sorted(score_dict.values(), key=lambda x: x["score"], reverse=True)

    return [entry["doc"] for entry in fused_docs]
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“Œ Highlights:

  • Retrievals happen in parallel for each subquery.
  • Rank and reciprocal scores intelligently merge the results.
  • Final documents are sorted and fused based on their scores.

๐ŸŽฏ Conclusion

Reciprocal Rank Fusion (RRF) is a powerful technique that broadens context intelligently without introducing too much irrelevant information.

It ensures:

  • Broader and more accurate retrieval
  • Preference for high-quality, semantically rich documents
  • Better context for LLMs to generate superior outputs

๐ŸŒŸ What's Next?

Stay tuned!

In future articles, we'll cover more advanced retrieval techniques like:

๐Ÿ”น Step-Back Prompting
๐Ÿ”น Chain-of-Thought Retrieval
๐Ÿ”น Hybrid Search
๐Ÿ”น Hierarchical Decomposition (HyDE)


๐Ÿš€ Stay Curious. Keep Building!

Top comments (0)