Intermediate LevelAI, LLMs, Embeddings & MCP
What is Hybrid Search (Dense + Sparse) and Reciprocal Rank Fusion (RRF) in RAG?
❓ Interview Prompt: Explain combining dense vector similarity with sparse BM25 search using Reciprocal Rank Fusion (RRF).
Single retrieval strategies have distinct blind spots: dense vector search misses specific serial numbers/code terms, while BM25 keyword search misses semantic synonyms. Hybrid Search combines both methods.
Reciprocal Rank Fusion (RRF) Algorithm:
To merge ranked lists from dense and sparse search without needing to normalize disparate score scales, RRF calculates a combined score based on relative rank positions:
$$RRF\_Score(d) = \sum_{m \in M} \frac{1}{k + r_m(d)}$$
•
$r_m(d)$ is the rank position of document $d$ in retriever $m$.
•
$k$ is a constant smoothing parameter (typically $k = 60$).
Code Implementation Example
typescript
function reciprocalRankFusion(denseResults: string[], sparseResults: string[], k: number = 60): { id: string; score: number }[] {
const scores: Record<string, number> = {};
const addRanks = (list: string[]) => {
list.forEach((docId, rank) => {
scores[docId] = (scores[docId] || 0) + (1 / (k + (rank + 1)));
});
};
addRanks(denseResults);
addRanks(sparseResults);
return Object.entries(scores)
.map(([id, score]) => ({ id, score }))
.sort((a, b) => b.score - a.score);
}💡Reciprocal Rank Fusion (RRF) score aggregation algorithm.
🎯 Interview Key Takeaway
Hybrid search combines dense vector semantics with sparse BM25 keyword matching using Reciprocal Rank Fusion.
#Hybrid Search#RRF#BM25#RAG#Intermediate
Back to all AI, LLMs, Embeddings & MCP Questions→