Basics
The whole idea first, then the terms the rest of this page uses.
1.1Whole idea
Keyword and vector search each produce a ranking, on incompatible scales. RRF ignores scores and uses ranks: each list adds 1 / (k + rank) to a document. Documents high on several lists rise to the top.
Merges BM25 and vector rankings without normalising scores that live on different scales.
1.2Key concepts
One idea per card, in plain words.
Runs keyword and vector retrieval together: exact terms from one, paraphrases from the other.
A BM25 score of 12.4 and a cosine of 0.83 cannot be compared. Positions can.
Dampens the gap between top ranks. The paper's value of 60 works widely.
Question
What exactly you must answer, and what to settle before you start.
2.1Question
Keyword search finds exact product codes; vector search finds paraphrases. Merge both into one list, given that BM25 scores 12.4 where cosine scores 0.83.
Detailed answer
How it works, and how to build or explain it.
3.1Step by step
The mechanism, in the order you would explain it.
- Fetch both lists
Take 50–100 from each retriever. Shallow lists leave RRF nothing to fuse.
- Add reciprocal ranks
For each list and document, add weight / (k + rank).
- Sort and cut
Sort by the sum and send the top results on to the reranker.
- Tune weights last
Keep weights at 1 until an eval set shows a reason to change them.
3.2Code
Working code for the core of it.
Merging rankings with RRF
Keyword and vector scores aren't comparable. Ranks are.
1export interface RrfOptions {2 k?: number;3 weights?: number[];4 top?: number;5}6 7/** Reciprocal rank fusion: each list adds weight / (k + rank). Raw scores never enter the sum. */8export function rrf(lists: string[][], { k = 60, weights, top }: RrfOptions = {}): { id: string; score: number }[] {9 const scores = new Map<string, number>();10 lists.forEach((list, l) =>11 list.forEach((id, i) => scores.set(id, (scores.get(id) ?? 0) + (weights?.[l] ?? 1) / (k + i + 1)))12 );13 const ranked = [...scores].map(([id, score]) => ({ id, score })).sort((a, b) => b.score - a.score);14 return top === undefined ? ranked : ranked.slice(0, top);15}/ (k + i + 1)- Only position counts. An item near the top of both lists beats one at the top of just one.
k = 60- Softens the gap between top ranks. 60 is the value from the original paper.
Tested by npm run test:modules.
Q&A
What interviewers ask next. Answer out loud, then open.
4.1Interview questions
Why k = 60?
Why not normalise and add scores?
What about a document in one list only?
Extra depth
Edge cases, counterpoints and what to read next.
5.1Edge cases and counterpoints
Includes the strongest argument against this approach.
Show detailsHide details
- Queries and fusion
- Some queries suit one retriever. Query classification can adjust weights per query.
- Counterpoint
- With labelled data, learned fusion often beats RRF. RRF wins as the zero-configuration default.