liveBook · Manning

Reciprocal rank fusion

As explained in AI-Powered Search

Reciprocal rank fusion is a rank-based result-combination algorithm that merges rankings from multiple retrieval systems using reciprocal-rank contributions.

Reciprocal rank fusion (RRF) combines ranked results from multiple retrieval systems by summing each document’s reciprocal-rank contributions, then sorting by the total. It is useful when lexical and vector search provide complementary results. section 15.5.1

The calculation proceeds as follows:

  • For every ranked result list, add 1 / (k + rank) to the document’s score. section 15.5.1
  • Aggregate contributions for documents appearing in multiple lists, then sort scores in descending order. section 15.5.1
  • Increase k to reduce the influence of an isolated high rank; k=60 is a commonly used default in the implementation. section 15.5.1

Lexical and vector retrieval

For the query "singin' in the rain", run lexical and vector searches independently, then pass both ranked document lists to a hybrid search configured with RRF and k=60. section 15.5.1

Listing 15.17 RRF for combining multiple sets of search results
def reciprocal_rank_fusion(search_results, k=None):
  if k is None: k = 60
  scores = {}
  for ranked_docs in search_results:  #1
    for rank, doc in enumerate(ranked_docs, 1):  #2
      scores[doc["id"]] = (scores.get(doc["id"], 0) +  #3
                           (1.0 / (k + rank)))  #3
  sorted_scores = dict(sorted(scores.items(), #4
    key=lambda item: item[1], reverse=True))  #4
  return sorted_scores  #4

Inspect the Python implementation that iterates through ranked lists, accumulates reciprocal-rank scores, and returns sorted results. section 15.5.1

How AI-Powered Search covers it

How deep it goes
Explains the scoring loop, aggregation, sorting, and k trade-off, while stopping short of formal evaluation.
Examples
Uses the queries "singin' in the rain" and "the hobbit" with lexical, vector, and hybrid result comparisons.
What it assumes
Assumes ranked lexical and vector search results, while defining reciprocal-rank scoring from the algorithm itself.
What you can do after
Apply it via a Python function or hybrid-search API to merge ranked retrieval results.
How it connects
Connects reciprocal rank fusion with lexical search, vector search, hybrid search, and ranked retrieval.

Also explained in

Compare how 3 books explain reciprocal rank fusion →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI-Powered Search. The section links point to the parts of the book it draws on.