Reranking and hybrid search
1. WHY do we need this at all?
A naive RAG pipeline does: embed query → nearest neighbours in vector DB → feed top-k to LLM. Two failure modes:
- Dense (vector) search misses exact terms. Embeddings capture meaning, so a query for the error code
"ERR_4092"or the drug"Metformin"may retrieve semantically "nearby" but literally wrong chunks. It has no notion of an exact keyword match. - Sparse (keyword) search misses meaning. A search for
"car"will not match a doc that only says"automobile".
The 80/20 insight: retrieval quality — not prompt tweaking — is usually the single biggest lever on RAG accuracy. Hybrid + rerank often beats any prompt engineering.
2. The two retrievers — derived from first principles
2.1 Sparse: BM25 (WHAT and WHY)
We want to score how relevant a document is to a query with terms . Intuition to build in:
- A term that appears more often in should raise the score (term frequency, TF) — but with diminishing returns (the 50th "cat" tells us little beyond the 5th).
- A term that is rare across the whole corpus is more discriminating (inverse document frequency, IDF). "The" is useless; "photosynthesis" is gold.
- Long documents get a term-count advantage unfairly, so we normalise by length.
Putting these three ideas into a formula:
Why this shape?
- The fraction saturates: as the score , giving diminishing returns. That's the "50th cat" effect.
- controls how fast it saturates; controls how strongly length normalisation kicks in ( ⇒ ignore length, ⇒ full normalisation).
- , with = total docs, = docs containing — rare terms get a big multiplier.
2.2 Dense: cosine similarity of embeddings
Embed query and doc into vectors with a bi-encoder (each encoded independently), score by: Because documents are embedded offline once, this is fast (approximate nearest neighbour search) — but the query and doc never "see" each other, so subtle interactions are lost.
3. Merging the two lists — Reciprocal Rank Fusion (RRF)
The scores from BM25 and cosine are on incomparable scales. We can't just add them. RRF sidesteps this by using only the rank of each document in each list.
Why ? A document ranked #1 contributes ; ranked #100 contributes . So top positions dominate, and a doc that appears in both lists (even at middling rank) beats a doc that appears in only one. The constant softens the difference between rank 1 and rank 2 so it isn't winner-take-all.

4. Reranking with a cross-encoder (the precision step)
The winning architecture: retrieve broadly and cheaply (bi-encoder + BM25 → top-100), then rerank precisely and expensively (cross-encoder → top-5). You pay the expensive cost only on 100 items, not the whole corpus.
5. Common mistakes (steel-manned)
6. Feynman
Recall Explain to a 12-year-old
Imagine finding a fact in a huge library. One helper (keyword search) looks for the exact words you said. Another helper (meaning search) looks for books that are about the same idea even with different words. You ask both so nothing good is missed — that's hybrid search. They hand you a messy pile of 100 books. Then a super-careful reader (the reranker) actually reads your question and each book together and puts the best 5 on top. You only give those 5 to the answer-writer, so it doesn't get confused by a big messy pile.
7. Flashcards
What problem does hybrid search solve that pure vector search cannot?
What are the two component retrievers in hybrid search?
Why does BM25's term-frequency term saturate?
What do the BM25 parameters and control?
Give the RRF formula.
Why use rank-based RRF instead of adding raw scores?
Difference between a bi-encoder and a cross-encoder?
Why can't a cross-encoder be used to search the whole corpus?
Hybrid search improves which metric, and reranking improves which?
Why not just feed a large top-k to the LLM instead of reranking?
What is the typical two-stage retrieve-then-rerank sizing?
Can reranking recover a relevant doc missing from the candidate set?
8. Connections
- Vector Databases and ANN Search — supplies the dense candidate list.
- BM25 and TF-IDF — the sparse retriever's scoring foundation.
- Embeddings and Bi-encoders — how dense vectors are produced.
- Cross-encoders and Sentence Transformers — the reranking model.
- Retrieval-Augmented Generation (RAG) — the pipeline this feeds.
- Lost in the Middle - LLM context limits — why small top-k matters.
- Evaluation - Recall@k and MRR — how to measure retrieval quality.
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
RAG me sabse bada problem yeh hai: agar sahi document retrieve hi nahi hua, toh LLM kabhi sahi jawab nahi de sakta — woh hallucinate karega. Isliye retrieval quality par focus karna 80/20 move hai. Ab do tarike ke search hote hain. Dense (vector) search meaning samajhta hai — "car" aur "automobile" ko same maanta hai — par exact keyword ya error code jaise "ERR_4092" ko miss kar deta hai. Sparse search (BM25) exact words dhoondhta hai par meaning nahi samajhta. Hybrid search dono ko mila deta hai, taaki koi bhi accha document chhoote na — isse recall badhta hai.
BM25 ka logic simple hai: jo term document me zyada baar aaye woh important (TF), par bahut zyada baar aane se fayda kam hota jaata hai (saturation). Jo term poore corpus me rare ho woh zyada discriminating (IDF). Aur lambe documents ko length se normalise karte hain taaki unko unfair advantage na mile. Dono retrievers ki scores alag-alag scale par hoti hain, isliye directly add nahi kar sakte. Solution: RRF — sirf rank use karo, score nahi. Jo document dono lists me aata hai woh upar chadh jaata hai, kyunki do gawaah ek se behtar hote hain.
Fir aata hai reranking. Retrieval ke baad hamare paas top-100 candidates hote hain, thodi si messy list. Ab ek cross-encoder query aur har document ko ek saath padhta hai (dono ke tokens ek dusre ko attend karte hain), isliye woh bahut accurate score deta hai — par mehenga hota hai. Isliye ise sirf 100 candidates par lagate hain, poore corpus par nahi. Yeh top-5 ko upar le aata hai, aur wahi 5 LLM ko dete hain. Yaad rakho: hybrid recall badhata hai, rerank precision badhata hai — pehle wide net dalo, fir best fish rakho. Aur ek cheez: rerank sirf order badal sakta hai, agar sahi doc top-100 me hai hi nahi toh rerank kuch nahi kar payega — isliye recall pehle important hai.