Area 02 · RAG · Concept 09 of 20

Hybrid Search

Merge keyword and vector results (for example with reciprocal rank fusion) to find more of the right chunks.

01–10 · The basePhase 2 · Weeks 7–10≈ 1 hour

The idea

Run a vector search and a keyword search for the same question, then merge the two result lists. Vector search brings paraphrases; keyword search brings exact terms. Together they find more relevant chunks than either alone.

Scores from the two methods are on different scales, so merge by rank instead. Reciprocal Rank Fusion (RRF) gives each chunk 1 / (k + rank) from each list (k is usually 60) and sums them. Chunks that rank well in both lists rise to the top.

Retrieve generously at this stage, for example the top 20–50 from each, then let a reranker choose the final few for the prompt.

Why it matters

It is the most reliable single upgrade to a basic RAG pipeline and is used in nearly every production system.

Picture it

Words to know

  • RRFReciprocal Rank Fusion: merge ranked lists by summing 1/(k+rank).

Try it without code

Merge two lists by hand

  1. Write two ranked lists of 5 doc IDs with some overlap.
  2. Score each ID with 1/(60+rank) per list and sum.
  3. See which IDs win.

Use it in code

Reciprocal rank fusion over any number of ranked ID lists.

python
from collections import defaultdict

def rrf(*ranked_lists, k=60):
    scores = defaultdict(float)
    for lst in ranked_lists:
        for rank, doc_id in enumerate(lst, start=1):
            scores[doc_id] += 1 / (k + rank)
    return sorted(scores, key=scores.get, reverse=True)

vector_hits  = [12, 7, 33, 4, 9]
keyword_hits = [7, 51, 12, 2, 33]
print(rrf(vector_hits, keyword_hits)[:5])   # 7 and 12 rise to the top
PRACTICE · TICK IT AFTER THIS

Implement hybrid search over your pgvector table and compare Recall@5 against vector-only on your test questions.

Common mistakes

  • Adding raw scores from different methods.
  • Retrieving only 5 from each list, leaving nothing to rerank.

Go deeper