Retrieval Algorithms
After this lesson, you will be able to:
- Compare dense (cosine on embeddings) vs sparse (BM25/TF-IDF) retrieval, and explain why hybrid search beats either alone in production
- Use Reciprocal Rank Fusion (RRF) to combine results from multiple retrievers without scoring-scale headaches
- Apply Maximal Marginal Relevance (MMR) to balance relevance with diversity — preventing all top-k results from being near-duplicates
- Pick the right retrieval algorithm for your task — semantic similarity, keyword precision, multi-vector retrieval, or hybrid
Before you start
Open the /play/hybrid-search playground to watch BM25, dense embeddings, and RRF-fused results re-rank live as you change the query. Try "GPT-4 model" (BM25 wins on the exact identifier) then "OpenAI's flagship LLM" (dense wins on the paraphrase). Fusion is the one that wins both.
#The Two Families: Dense vs Sparse
#BM25: The Battle-Tested Sparse Baseline
BM25 (Robertson & Sparck Jones 1976, refined 1994) is a probabilistic ranking function. Despite being 50 years old, it's still the strongest sparse retriever and a staple of every production search system.
Why BM25 still wins: exact identifier match, robust to OOD terms, no embedding model required, cheap to run on very large corpora with inverted indices.
Three pieces of the formula are results rather than definitions. Here n is the number of documents that contain the term t and N is the number of documents in the corpus, and the lesson's code uses IDF(t) = ln(1 + (N - n + 0.5)/(n + 0.5)). The next three blocks open to show where each piece comes from. The first explains the fraction with k_1 and b, the second explains the IDF, and the third explains why an inverted index makes lookups cheap.
#Reciprocal Rank Fusion (RRF): The Default Combiner
Production stacks (Pinecone hybrid search, Weaviate, Vespa) implement RRF natively.
#Mixing Scores Directly: Normalise First
RRF throws the scores away. The other common fusion keeps them and mixes with a weight alpha between 0 and 1: score = alpha times dense + (1 minus alpha) times sparse (the weighted form is stated in Query Rewriting & Advanced Retrieval, and RRF is derived there). It only works if each score is first rescaled to a common range. Open the derivation to see why min-max rescaling does that, and why it makes the result independent of the units of each retriever.
#MMR: Adding Diversity
A subtle bug in pure top-k retrieval: results often duplicate each other. If your corpus has three near-identical paraphrases of the same fact, top-3 returns all three — wasting context window and starving the LLM of variety.
Before the decision rubric, let's get hands-on with BM25's parameters. Knowing how k1 and b actually behave will save you a lot of time tuning production retrieval.
A BM25 retriever indexes a corpus where document lengths vary from 50 tokens (FAQ entries) to 5000 tokens (full articles). Long articles dominate the rankings even when shorter docs are more relevant. Which parameter should you tune?
#Decision Rubric
Your RAG system retrieves documents about a specific machine learning model with version numbers (GPT-4o-2024-11-20). Dense retrieval misses some queries that include exact version numbers. Best fix?
The answer: hybrid retrieval with RRF. Dense embeddings often fail on rare exact tokens (version numbers, product IDs, unusual acronyms). BM25 handles these natively. Fusion gives you both worlds.
| Use case | Best retrieval |
|---|---|
| General Q&A on diverse text | Hybrid (dense + BM25 + RRF) |
| Code search, identifier-heavy | BM25 leads, dense as backup |
| Conversational paraphrase | Dense leads, BM25 as backup |
| Highly diverse top-k needed | Dense + MMR (λ=0.7) |
| Largest corpora (100M+) | BM25 first-stage, dense rerank |
Multi-vector retrieval (ColBERT, ColPali) keeps one vector per query word and one per document token or image patch, and scores a page as the sum over query words of its best-matching patch. Flip the toggle to see the same three pages ranked by that MaxSim sum and by one averaged vector, then drag a word's angle to move its target.
#The Math of Hybrid Search
The whole hybrid retrieval story is one equation per piece — BM25 for sparse, cosine for dense, RRF for fusion, MMR for diversity. The playground below implements all four end-to-end on a small corpus so you can sweep parameters and watch what happens.
#Hands-On
Tests: Verify BM25 returns docs containing 'pressure sensor'; verify hybrid (RRF) merges results from both retrievers; verify MMR top-4 includes more diverse docs than pure dense top-4.
#Key Takeaways
- Dense and sparse retrieval are complementary. Dense excels at paraphrase, sparse at exact-match; hybrid search via RRF wins production
- BM25 is still the sparse baseline. 50 years old, still hard to beat for keyword-precision tasks; tune k1=1.2, b=0.75 by default
- Reciprocal Rank Fusion (RRF, k=60) is the standard combiner — parameter-free, scale-invariant, production default
- MMR enforces diversity in top-k results — prevents redundant paraphrases from filling your context window
- Decision rubric: identifiers/code → BM25 leads; paraphrase Q&A → dense leads; production → hybrid + reranker
#Quick Check
Why doesn't direct score-summing (BM25 score + cosine score) work for combining dense + sparse retrieval?