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.
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.
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
| # | Document | BM25 rank | Vector rank | Cross-encoder |
|---|---|---|---|---|
| 1 | Error E-4012: card declined on retry ✓ | 1 | 6 | 0.96 |
| 2 | Payments: retry schedule for failed charges | 2 | 2 | 0.55 |
| 3 | Payment errors: general troubleshooting | 3 | 1 | 0.62 |
| 4 | Billing FAQ | 4 | 4 | 0.18 |
| 5 | Handling throttling with backoff | 5 | 3 | 0.20 |
| 6 | API overview and authentication | 6 | 9 | 0.07 |
| 7 | Refund policy | 7 | 7 | 0.10 |
| 8 | Annual 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
- 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?
- 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?
- 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.
- 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.
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
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
| Rank | Single vector (mean of tokens, cosine) | Late interaction (MaxSim) |
|---|---|---|
| 1 | Electronics refund window 0.92 | Complete returns, shipping and warranty policy 3.00 |
| 2 | Laptop warranty and repairs 0.50 | Electronics refund window 2.93 |
| 3 | Complete returns, shipping and warranty policy 0.31 | Laptop warranty and repairs 2.32 |
| Query token | shipping | courier | PIN code | warranty | repair | invoice | laptops | refundable | 14 days | delivery | packaging | service | max |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| refund | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.43 | 0.00 | 1.00 | 0.00 | 0.00 | 0.00 | 0.00 | 1.00 |
| window | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 1.00 | 0.00 | 0.00 | 0.00 | 1.00 |
| laptops | 0.00 | 0.00 | 0.00 | 0.00 | 0.11 | 0.00 | 1.00 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 | 1.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
- 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”)?
- 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.
- 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?
- 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.
Hands-on: coding window
The coding window and the full VS Code lab are available after you sign in.
Sign in to codeLabs
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
- The Complete Guide to Hybrid Search in RAG (BM25 + Embeddings + Reranker) · Dave Ebbelaar · ~59 min. A complete build of BM25 plus embeddings plus a re-ranker, with the reasoning for each stage.
- What is reciprocal rank fusion in hybrid search? · Abhishek Thakur · ~8 min. Reciprocal rank fusion explained in eight minutes with a worked example.
- Improving RAG Systems with Hybrid Search and Re-ranking with LanceDB · Jason Liu · ~15 min. How hybrid search and re-ranking move metrics in practice, framed as systematic improvement.
- Supercharge Your RAG with Contextualized Late Interactions · Prompt Engineering · ~18 min. Late interaction and ColBERT compared with bi-encoders, with a working RAG example.
Further reading
- Paper ColBERT: Efficient and Effective Passage Search via Contextualized Late Interaction over BERT · Khattab and Zaharia, SIGIR 2020. Introduces late interaction and MaxSim, and compares it with bi-encoders and cross-encoders.
- Paper ColBERTv2: Effective and Efficient Retrieval via Lightweight Late Interaction · Santhanam et al., 2022. How residual compression shrinks the per-token index enough to use in production.
- Paper Reciprocal Rank Fusion outperforms Condorcet and individual Rank Learning Methods · Cormack, Clarke and Büttcher, SIGIR 2009. The two-page paper behind RRF and the k = 60 default.
- Docs Retrieve & Re-Rank · Sentence Transformers docs. The two-stage pipeline with working code and recommended cross-encoder models.
- Docs Cross-encoder usage · Sentence Transformers docs. How to score and rank pairs with a cross-encoder in a few lines.
GitHub repositories
- AnswerDotAI/rerankers: The unified Reranker interface; swap cross-encoders, ColBERT and hosted APIs with one line.
- stanford-futuredata/ColBERT: The ColBERTv2 and PLAID papers’ code: how MaxSim and residual compression work.
- AnswerDotAI/RAGatouille: The quickest way to index and search with ColBERT, or to use it as a re-ranker.
- opensearch-project/opensearch-py: Client examples for BM25 and the hybrid query with normalisation processors.
Checkpoint quiz
Interview questions
- Why does hybrid search beat dense retrieval alone? Give query examples.
- Explain reciprocal rank fusion. Why is k = 60 a reasonable default?
- How do you choose the re-ranking depth and model under a 300 ms latency budget?
- Bi-encoder, cross-encoder and late interaction: when would you use each?
- 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).