Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 36 ยท Retrieval Strategies

Hybrid Search and Reciprocal Rank Fusion

Learn hybrid search for RAG: run keyword and vector retrieval together and merge the rankings with reciprocal rank fusion or weighted score fusion, in Python.

You now have two retrievers with opposite strengths. BM25 nails exact tokens; embeddings nail paraphrase. Hybrid search runs both and merges the results into one list. In most published benchmarks and in practice, a well-built hybrid retriever beats either alone, and it is the default recommendation for production RAG.

The hard part is the merge. A BM25 score might be 11.7 and a cosine similarity 0.43: the scales are unrelated, so you cannot simply add them. This lesson covers the two standard solutions.

The "dense" side in these examples is LSA: TF-IDF followed by a truncated SVD from scikit-learn. It produces genuinely dense vectors that capture some word co-occurrence, but it is far weaker than a neural embedding model. It is a browser-friendly stand-in; the fusion code is identical with real embeddings.

Reciprocal rank fusion (RRF)

RRF ignores the raw scores and uses only ranks. Each retriever gives every document a rank (1 is best). A document's fused score is the sum over retrievers of 1 / (c + rank), where c is a smoothing constant, commonly 60.

  • A document that is high in both lists wins.
  • A document that is first in one list and absent from the other still gets a decent score.
  • No normalization, no tuning of weights, and it works even when one retriever returns a very different number of results.
Python RRF in a few lines

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Documents A and C appear near the top of both lists, so they lead. E is only in the dense list, D only in the keyword list; both still appear, below the shared winners.

A full hybrid retriever

Below, a BM25 index and an LSA index search the same support corpus. The query "I want my money back" has no word overlap with the refund document, while "E-4012" is a pure keyword case.

Python BM25 + LSA + RRF

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Look at which list contributes the winner for each query. The point is not that LSA is great, it is that fusion lets each retriever cover the other's blind spot without you deciding in advance which query type you have.

Weighted score fusion

Sometimes you want control: "trust keywords twice as much". Then you must normalize each score list to a common range before mixing. Min-max normalization is the simplest; it rescales each list to 0 to 1.

Python Normalized weighted fusion

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Weighted fusion is more tunable than RRF (several databases expose a single alpha parameter) but also more fragile: min-max depends on the best and worst result in each particular query, so a query with one great keyword hit and a flat dense list behaves differently from the reverse. A common approach is to start with RRF, then try weights only if evaluation shows a clear gain.

Where to do the fusion

Option Notes
In the database Weaviate, Qdrant, Elasticsearch, OpenSearch, Milvus and pgvector setups with FTS offer built-in hybrid queries. One round trip.
In application code Two queries plus the 10-line RRF above. Works with any stores.
Multi-field Store a sparse vector (BM25 or SPLADE) beside the dense vector in one record.

Whichever you use, fetch more candidates from each side than you will finally need (for example 20 to 50 each), fuse, then trim, and finally apply a re-ranker.

Common mistakes

  • Adding raw BM25 and cosine scores directly.
  • Using different chunk sets in the two indexes, so ids do not line up.
  • Forgetting that a document missing from one list should simply contribute nothing, not a penalty.
  • Skipping evaluation: confirm the hybrid beats both parts on your own questions (retrieval metrics).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why can't you simply add a BM25 score and a cosine similarity?

    BM25 is unbounded and corpus-dependent while cosine is bounded, so the raw sum is dominated by whichever is larger.
  2. 2. What does reciprocal rank fusion use as input?

    RRF sums 1/(c + rank) over retrievers and ignores raw scores.
  3. 3. In weighted fusion, why normalize scores first?

    Min-max or similar normalization makes the weights meaningful.
  4. 4. A query contains the exact product code 'X-770'. Which retriever most likely finds it?

    Exact rare tokens are the strength of keyword search; hybrid keeps that strength.

Summary

  • Hybrid search merges keyword and vector rankings; it covers each method's blind spots.
  • RRF uses ranks only, needs almost no tuning, and is a strong default (c about 60).
  • Weighted fusion needs normalization and gives an explicit keyword-versus-meaning dial.
  • Retrieve wide from each side, fuse, trim, then re-rank.

Next: Metadata Filtering and Query Transformation