Skip to content

Courses / RAG Systems / Module 6

Module 6: Hybrid search and re-ranking: BM25 + vectors, RRF, cross-encoders, late interaction

12 h · theory 4 · hands-on 5 · project 2 · revision 1

You will be able to

  • Combine BM25 and vector results with reciprocal rank fusion
  • Re-rank with a cross-encoder and measure the gain
  • Know when late interaction (ColBERT) is worth it
  • Budget re-ranking latency

Capstone increment: Hybrid retriever with re-ranking that beats the baseline on the golden set.

Job-description skills: Hybrid search, Re-ranking, Search engines · Lab tools: opensearch-py, bm25s, sentence-transformers, rerankers

1. Why one retriever isn’t enough

BM25 wins on exact terms: error codes, product names, people, clause numbers, rare words. Dense retrieval wins on meaning: paraphrases and questions phrased in users’ words rather than the document’s. Their failures are largely different, so running both and combining the results recovers documents that either alone would miss.

Production pattern: run BM25 and vector search in parallel, fuse the two lists, re-rank the top few dozen with a cross-encoder, and send the best five or so to the model.

Queryafter rewritingBM25exact terms, codesVector searchmeaning, paraphraseRRF fusionΣ 1 / (60 + rank)Re-rank top 30cross-encoderTop 5to the promptTwo retrievers, one ranked listin parallel
Fusion recovers what either retriever misses; re-ranking puts the best of them first. Budget re-ranking by how many candidates you score.

2. Fuse ranked lists with reciprocal rank fusion

BM25 scores are unbounded and depend on the corpus; cosine similarities cluster in a narrow band. Adding raw scores lets one retriever dominate. Reciprocal rank fusion (Cormack, Clarke and Büttcher, 2009) ignores scores and uses ranks: each document gets the sum of 1 / (k + rank) over the lists it appears in, with k = 60 as the usual constant. It needs no calibration and is hard to beat.

Weighted score fusion after min-max normalisation can do slightly better when tuned, but it is sensitive to outliers and must be re-tuned when the corpus or models change. Qdrant, OpenSearch, Elasticsearch and Weaviate all offer hybrid search natively; you can also fuse in your own code in a few lines.

Hybrid search and re-rankingSix queries over a help centre. Pick how the results are ranked and compare mean recall@5 and MRR across all six.

All six queries: recall@5 0.67 · MRR 0.77 · re-ranker calls per query 0
This query: recall@5 1.00 · reciprocal rank 1.00

#DocumentBM25 rankVector rankCross-encoder
1Error E-4012: card declined on retry ✓160.96
2Payments: retry schedule for failed charges 220.55
3Payment errors: general troubleshooting 310.62
4Billing FAQ 440.18
5Handling throttling with backoff 530.20
6API overview and authentication 690.07
7Refund policy 770.10
8Annual plans: invoices and billing cycle 8–0.05

✓ = judged relevant by a person. A line below rank 5 doesn’t count for recall@5.

Guided experiments
  1. Compare BM25 and vectors on “error E-4012” and on “how do I get my money back”. Why does each retriever win one and lose the other?
  2. Switch to hybrid (RRF). Is mean recall@5 better than either retriever alone? Look at “how do I get my money back”: what happens to a document only one retriever found?
  3. Turn on re-ranking of the top 5. Recall@5 doesn’t move but MRR does. Why can re-ranking the top 5 never change recall@5? Now re-rank the top 10.
  4. Move the RRF constant from 60 down to 1. Small k trusts the very top of each list more. Is the difference worth tuning, or is 60 a safe default?

3. Re-rank with a cross-encoder

A bi-encoder embeds query and document separately. A cross-encoder reads them together, so every query word can attend to every document word; it is far more accurate at judging relevance and far too slow to run over the whole corpus. Use it on the top 20 to 100 candidates from the first stage.

Open options include the BGE rerankers and the MS MARCO MiniLM cross-encoders; hosted rerankers are available from Cohere, Voyage, Jina and others. The rerankers library gives one interface to most of them, so you can compare on your golden set by changing a string.

Common misconception: “Re-ranking improves recall@5.” Re-ranking only reorders what it is given. If you re-rank the top 5 into the top 5, recall@5 can’t change; only depth beyond k lets it pull in documents.

Deep dive

Deep dive: bi-encoders and cross-encoders

A bi-encoder encodes the query and each document separately, into one vector each. Documents are encoded once at indexing time, so a query costs one encoding plus a nearest-neighbour search, which scales to billions of documents. The price is that the query never “sees” the document: everything about the document has to be squeezed into one vector in advance.

A cross-encoder feeds “[query] [SEP] [document]” through one transformer, so every query token attends to every document token, and a small head outputs one relevance score. It is much more accurate, but nothing can be precomputed: one query against a million documents is a million forward passes. So it only re-ranks the bi-encoder’s top 20–100.

Worked example: where the time goes in two-stage retrieval (rough orders of magnitude)

stage 1: encode the query + ANN search            → tens of milliseconds for 100 candidates
stage 2: cross-encoder on 50 pairs, batched        → tens of ms on a GPU, hundreds on a CPU
cross-encoder over 1 million documents instead     → 1 million forward passes per query
so: recall comes from stage 1, precision at the top from stage 2
QuerytextEncoderonce per queryVectore.g. 1,024-dANN indexdocs pre-encodedTop 100millisecondsPairquery [SEP] docTransformerfull attentionScoreone per pairRe-ranktop 20–50 onlyTo the LLMbest 5–8 chunksStage 1, bi-encoder: query and documents encoded separatelyStage 2, cross-encoder: each pair read togethercandidates
The bi-encoder can search millions because documents are encoded ahead of time; the cross-encoder is far more accurate because query words attend to document words, but it must run once per pair at query time.

4. Late interaction: ColBERT

ColBERT keeps a vector for every token. A query scores a document by matching each query token to its most similar document token (MaxSim) and summing. That captures much of a cross-encoder’s precision while letting documents be encoded in advance. The price is storage: dozens to hundreds of vectors per chunk instead of one, reduced by compression in ColBERTv2 and PLAID.

Use late interaction as a re-ranker over first-stage candidates, or as the first stage when you can afford the index. RAGatouille makes ColBERT easy to try; the same MaxSim idea powers ColPali for page images in Module 12.

Deep dive

Deep dive: late interaction (MaxSim), token by token

ColBERT sits between the two. It stores one small vector (128-d) per document token, computed offline like a bi-encoder. At query time each query token finds its most similar document token, and the score is the sum of those best matches: score(q, d) = Σ over query tokens i of max over document tokens j of q_i · d_j.

Because every query word gets its own best match, a long document that holds the answer among other topics isn’t diluted the way one averaged vector is. The cost is storage: a vector per token instead of one per chunk, which ColBERTv2 shrinks with centroid-plus-residual compression.

Worked example: MaxSim for “refund window laptops” against the long policy in the tool

refund  → best match “refundable”   1.00
window  → best match “14 days”      1.00
laptops → best match “laptops”      1.00
MaxSim  = 1.00 + 1.00 + 1.00 = 3.00
the same document as one averaged vector: cosine 0.31, ranked last
One vector or one per token?Query: “refund window laptops”. The token vectors use five readable features (refund, time limit, laptop, shipping, warranty) so you can follow the scores.
RankSingle vector (mean of tokens, cosine)Late interaction (MaxSim)
1Electronics refund window 0.92Complete returns, shipping and warranty policy 3.00
2Laptop warranty and repairs 0.50Electronics refund window 2.93
3Complete returns, shipping and warranty policy 0.31Laptop warranty and repairs 2.32
Query tokenshippingcourierPIN codewarrantyrepairinvoicelaptopsrefundable14 daysdeliverypackagingservicemax
refund0.000.000.000.000.000.430.001.000.000.000.000.001.00
window0.000.000.000.000.000.000.000.001.000.000.000.001.00
laptops0.000.000.000.000.110.001.000.000.000.000.000.001.00

MaxSim = sum of each row’s best match = 3.00. The single vector scores the same document 0.31, because averaging every token into one vector blurs what the document is about: in the long policy, nine tokens about shipping and warranty bury the three that answer the question.

What it costs to store

One 1,024-d float vector each: 4.1 GB · one 128-d fp16 vector per token: 51 GB · ColBERTv2 residual compression (≈36 bytes per vector): 7 GB

Guided experiments
  1. Compare the two rankings. Why does the single-vector search put the complete policy last, even though it holds the exact answer (“laptops refundable 14 days”)?
  2. Open the matrix for the policy, then for “Laptop warranty and repairs”. Which query token fails to find a good match in the warranty document? That is the signal MaxSim keeps and averaging throws away.
  3. A cross-encoder goes further: it reads query and document together, so “window” can attend to “14 days” directly. Why is it then too slow to run over the whole index, and fine for re-ranking the top 20–50?
  4. Switch to ColPali at 5 million pages. Why do teams pair multi-vector retrieval with compression, or use it only to re-rank candidates from a cheaper first stage?

5. Budget and measure

BM25 and vector search each take a few milliseconds; re-ranking 30 candidates with a small cross-encoder takes tens of milliseconds on a GPU and more on a CPU. Decide the re-rank depth from where relevant documents sit in your first-stage ranking: if they are almost always in the top 20, re-ranking 100 buys latency and nothing else.

Report recall@5, MRR and nDCG@10 for BM25 alone, vectors alone, hybrid, and hybrid plus re-ranking, and split the numbers by query segment (codes and names versus natural-language questions). The segment view tells you why the combination wins.

In production: Run the two first-stage searches concurrently and put a timeout on the re-ranker with a fallback to the fused order. A slow re-ranker should degrade quality slightly, not fail the request.

Hands-on: coding window

The coding window and the full VS Code lab are available after you sign in.

Sign in to code

Labs

Lab 6.1: Hybrid retrieval

Index the capstone chunks in BM25 (bm25s or OpenSearch) and Qdrant, fuse with RRF, and compare against each alone on the golden set, split by query segment.

Expected: A table of recall@5, MRR and nDCG@10 for three configurations and two segments.

Lab 6.2: Re-ranking depth

Re-rank the fused list with bge-reranker-base through the rerankers library at depths 10, 30 and 100. Measure quality and p95 latency on CPU and, if available, GPU.

Expected: A chosen depth with the quality and latency numbers that justify it.

Lab 6.3: Hybrid retriever (capstone increment)

Add src/retrieval/hybrid.py with parallel BM25 and vector search, RRF, a re-ranker with timeout and fallback, and config for depth and k.

Expected: The capstone beats the Module 0 baseline on recall@5 and MRR, reported in docs/baseline.md.

Go deeper

Videos

Further reading

GitHub repositories

Checkpoint quiz

1. Why not just add BM25 and cosine scores?
2. In RRF, a document ranked 1st in one list and absent from the other scores…
3. Re-ranking the top 5 with a cross-encoder changes…
4. What does ColBERT store per document?
5. Relevant documents are almost always in your first-stage top 20. Re-rank depth?
6. Why can’t a cross-encoder replace the first-stage retriever?

Interview questions

  1. Why does hybrid search beat dense retrieval alone? Give query examples.
  2. Explain reciprocal rank fusion. Why is k = 60 a reasonable default?
  3. How do you choose the re-ranking depth and model under a 300 ms latency budget?
  4. Bi-encoder, cross-encoder and late interaction: when would you use each?
  5. Your re-ranker made recall@5 worse on one segment. How do you investigate?

Ask the professor

Sign in to ask questions about this module.

Discussion

Community discussion appears here once giscus is configured (see README).