Lesson 277 of 296
Hybrid search, reranking and evaluating RAG
What you will learn
- Why vector search alone misses things
- Keyword search and BM25
- Hybrid search with reciprocal rank fusion (RRF)
- Reranking
- Measuring retrieval: recall@k and MRR
- Evaluating answers and catching regressions
On this page
Why vector search alone is not enough
Vector search is good at meaning, but weak at exact terms: product codes ("SKU-4471"), error codes ("ERR_CONN_RESET"), names and rare words. Two codes can look the same in "meaning" but be completely different things.
Keyword search is the opposite: great for exact terms, weak when the user uses different words from the document.
Keyword search and BM25
BM25 is the standard keyword ranking formula used by search engines. In simple words, a document scores higher when:
- it contains the search words,
- those words are rare across all documents (a rare word tells you more than "the"),
- the document is not padded with lots of other text.
PostgreSQL full-text search, Elasticsearch and OpenSearch all give you keyword search without writing BM25 yourself.
Hybrid search with reciprocal rank fusion
Hybrid search runs both searches and merges the results. The scores from the two systems are on different scales, so instead of adding scores we combine ranks.
Reciprocal rank fusion (RRF) gives each document a score of 1 / (k + rank) from each list it appears in, and adds them up. The original 2009 paper by Cormack, Clarke and Büttcher used k = 60, and 60 is still the common default. A document that ranks well in both lists wins.
function rrf(lists: string[][], k = 60): string[] {
const scores = new Map<string, number>();
for (const list of lists) {
list.forEach((id, index) => {
const rank = index + 1;
scores.set(id, (scores.get(id) ?? 0) + 1 / (k + rank));
});
}
return [...scores.entries()].sort((a, b) => b[1] - a[1]).map(([id]) => id);
}
const vectorResults = ["doc-a", "doc-b", "doc-c"];
const keywordResults = ["doc-c", "doc-a", "doc-d"];
console.log(rrf([vectorResults, keywordResults])); // [ 'doc-a', 'doc-c', 'doc-b', 'doc-d' ]Reranking
Retrieval is fast but rough. A reranker is a second, slower model that looks at the question and each candidate chunk together and gives a more accurate relevance score.
A common pattern:
- Retrieve 50 candidates with hybrid search (fast).
- Rerank those 50 (slower, more accurate).
- Send the top 5 to the LLM.
This improves answer quality and keeps prompts small and cheap.
Measuring retrieval
If the right chunk is not retrieved, the model cannot answer correctly. So measure retrieval separately from the final answer.
Build an evaluation set: real questions, each with the id of the chunk that contains the answer. Then measure:
| Metric | Meaning |
|---|---|
| Recall@k | In what share of questions was the right chunk in the top k results? |
| MRR (mean reciprocal rank) | On average, how high was the right chunk? Rank 1 scores 1, rank 2 scores 0.5, rank 3 scores 0.33; missing scores 0 |
type EvalCase = { expected: string; results: string[] };
function recallAtK(cases: EvalCase[], k: number): number {
const hits = cases.filter((c) => c.results.slice(0, k).includes(c.expected)).length;
return hits / cases.length;
}
function mrr(cases: EvalCase[]): number {
const total = cases.reduce((sum, c) => {
const index = c.results.indexOf(c.expected);
return sum + (index === -1 ? 0 : 1 / (index + 1));
}, 0);
return total / cases.length;
}
const cases: EvalCase[] = [
{ expected: "returns", results: ["returns", "delivery"] },
{ expected: "delivery", results: ["jobs", "delivery"] },
{ expected: "jobs", results: ["returns", "delivery"] },
];
console.log(recallAtK(cases, 1).toFixed(2)); // 0.33
console.log(recallAtK(cases, 2).toFixed(2)); // 0.67
console.log(mrr(cases).toFixed(2)); // 0.50Evaluating answers
For the final answer, check things such as:
| Check | Question |
|---|---|
| Faithfulness | Is every claim supported by the retrieved sources? |
| Relevance | Does it answer the actual question? |
| Correctness | Does it match the known right answer in the evaluation set? |
| Format | Does it follow the required format and cite sources? |
Ways to check: exact or rule-based checks with code where possible, human review of a sample, and LLM-as-a-judge (a second model grades answers against a rubric). An LLM judge is useful but can be wrong or biased, so check it against human grades first.
Catching regressions
Treat prompts, chunk sizes and models like code:
- Keep the evaluation set in the repo.
- Run it in CI when you change a prompt, a model or the retrieval settings.
- Fail the build if recall@k or answer scores drop below a set level.
- In production, log questions, retrieved ids and user feedback (thumbs up or down) to grow the evaluation set.
How to say it in an interview
"Vector search is good at meaning but misses exact terms like product codes, so I use hybrid search: vector plus BM25 keyword search, merged with reciprocal rank fusion, then a reranker on the top candidates before sending a few chunks to the model. I measure retrieval separately with recall@k and MRR on an evaluation set of real questions, and check answers for faithfulness and correctness with code checks, human review and an LLM judge. The evaluation runs in CI so prompt or model changes cannot silently make results worse."
Key words
| Word | Meaning |
|---|---|
| BM25 | A standard keyword ranking formula |
| Hybrid search | Combining keyword and vector search |
| RRF | Merging ranked lists by adding 1 / (k + rank) |
| Reranker | A slower, more accurate model that reorders candidates |
| Recall@k | Share of questions where the right item is in the top k |
| MRR | Average of 1 / rank of the right item |
| LLM-as-a-judge | Using a model to grade outputs against a rubric |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.