RRF Rank Fusion
🎯 Introduction
In “Keyword Search and Hybrid Retrieval,” we merged vector search and keyword matching results using a simple rule: put keyword matches first and other passages afterward.
That rule is not always appropriate.
For example, a user asks, “What documents do I need to request a refund?” The knowledge base contains a refund window policy and a passage listing required documents. Both mention refunds, but the latter actually answers the question.
If two retrieval methods rank these passages differently, can we consider both rankings to decide the final order, rather than always favoring one method?
This article introduces RRF rank fusion through that example. We will inspect the material and results, then verify the ranking with a short program. Finally, we will introduce content-based reranking without adding another model API.
🧱 Inspect the Material and the Ranking Problem
Suppose the knowledge base contains these three passages, labeled A, B, and C for comparison:
| Label | Material | Original text |
|---|---|---|
| A | Refund policy | You can request a refund within 7 days of purchase. Sale items are nonrefundable. |
| B | Refund requirements | Refund requests require an order number and proof of purchase. |
| C | Shipping instructions | Standard orders ship within 48 hours of payment. |
The user asks:
What documents do I need to request a refund?
Without running any code, we can see that B, Refund requirements, answers the question most directly: it explicitly lists an order number and proof of purchase.
Now suppose the two retrieval methods return different orders:
| Rank | Vector search | Keyword search |
|---|---|---|
| 1st | B · Refund requirements | A · Refund policy |
| 2nd | C · Shipping instructions | B · Refund requirements |
| 3rd | A · Refund policy | — |
These rankings are example data chosen to explain RRF, not actual Qdrant query results. The keyword column assumes a retriever that ranks candidates. The previous article’s includes() only checks for matches; it does not automatically produce relevance rankings like these.
Reusing keyword priority from the previous article would place A first, followed by B and C:
1. A · Refund policy
2. B · Refund requirements
3. C · Shipping instructions
But we already know B is more suitable for this question. The problem is that we follow only the keyword list’s order and ignore vector search ranking B first.
⚖️ How Does RRF Combine Rankings?
RRF (reciprocal rank fusion) offers a simple approach:
- A passage gets a higher score the closer it is to the top of a list.
- If it appears in both lists, its scores are added together.
- The final list is sorted by descending total score.
For example, B ranks first in vector search and second in keyword search, so both lists contribute to its score.
A ranks first in keyword search but only third in vector search, so its total score may be slightly lower than B’s.
With the RRF calculation used here, the result is:
| Material | Vector rank | Keyword rank | RRF rank |
|---|---|---|---|
| B · Refund requirements | 1st | 2nd | 1st |
| A · Refund policy | 3rd | 1st | 2nd |
| C · Shipping instructions | 2nd | Not present | 3rd |
The difference between the methods is now clear:
| Keyword priority | RRF ranking |
|---|---|
| 1. A · Refund policy | 1. B · Refund requirements |
| 2. B · Refund requirements | 2. A · Refund policy |
| 3. C · Shipping instructions | 3. C · Shipping instructions |
RRF does not know that B mentions proof of purchase. It only combines positions in the two lists, which happens to put B first here. Let’s verify this with code.
🛠 Implement RRF in JavaScript
The new topic is how to merge two ranked lists after retrieval. Qdrant retrieval was covered earlier, so we will use the three passages and example rankings above without creating another collection or calling an embedding model.
Create 10.rrf.mjs and prepare the data:
const docs = [
{ id: 'A', title: 'Refund policy', text: 'You can request a refund within 7 days of purchase. Sale items are nonrefundable.' },
{ id: 'B', title: 'Refund requirements', text: 'Refund requests require an order number and proof of purchase.' },
{ id: 'C', title: 'Shipping instructions', text: 'Standard orders ship within 48 hours of payment.' },
];
const byId = new Map(docs.map(doc => [doc.id, doc]));
// These rankings match the table above
const semantic = ['B', 'C', 'A'];
const keyword = ['A', 'B'];
A, B, and C identify passages whose contents we have already seen, rather than unexplained strings such as doc-a. semantic and keyword represent the order of the two retrieval result sets.
Append the RRF implementation:
function rrf(semantic, keyword) {
const scores = {};
// Score the vector search ranking
semantic.forEach((id, index) => {
scores[id] = (scores[id] ?? 0) + 1 / (60 + index + 1);
});
// Add scores from the keyword search ranking
keyword.forEach((id, index) => {
scores[id] = (scores[id] ?? 0) + 1 / (60 + index + 1);
});
// Return document IDs sorted by total score
return Object.keys(scores).sort((a, b) => scores[b] - scores[a]);
}
const ranked = rrf(semantic, keyword);
console.log('RRF ranking:');
ranked.forEach((id, index) => {
console.log(`${index + 1}. ${id} · ${byId.get(id).title}`);
});
The code performs the three steps described above:
- Score vector search ranks: B, first in
semantic, gets1 / 61; C, second, gets1 / 62; A, third, gets1 / 63. - Add keyword search scores: A ranks first and B second in
keyword, so each gets an additional contribution. C is absent from that list, so its score stays unchanged. - Sort by total score:
scoresstores each passage’s accumulated score, and the function returns IDs in descending score order.
Because index starts at 0, index + 1 gives the actual rank. 60 is a commonly used RRF parameter; we use it directly here without deriving the formula.
Run:
node 10.rrf.mjs
The expected output for this fixed example is:
RRF ranking:
1. B · Refund requirements
2. A · Refund policy
3. C · Shipping instructions
This matches the RRF table above: B ranks first. That completes the main exercise for this article.
🔍 How Do RRF and Reranking Differ?
Although the result matches our expectation, RRF never read the refund requirements. It only knows where B ranks in each list.
If the original rankings are poor, RRF can still place unsuitable material first.
Content-based reranking takes another approach: it compares the user’s question with candidate passages to assess their relevance. For “What documents do I need to request a refund?”, it can compare the 7-day refund window with the requirement for an order number and proof of purchase, then adjust the order.
| Method | Main signal |
|---|---|
| RRF | Ranks in multiple retrieval result lists |
| Reranking | Content relevance between the question and candidate passages |
Reranking also has limits: it can only reorder retrieved candidates, not recover documents absent from the candidate set. An additional reranking model may also add latency and cost. For now, focus on the distinction; we will not add another model call here.
❓ Knowledge Check
Q: Does RRF discard a passage that appears in vector search but not keyword results?
No. C appears only in vector search but still receives a score and remains in the final results. Passages like B, which rank highly in both lists, generally have a better chance of accumulating a high total score.
🧪 Exercise
Change the vector ranking in the code to:
const semantic = ['A', 'B', 'C'];
Keep keyword = ["A", "B"] unchanged and rerun the program. Observe whether A and B switch places and explain why both lists now place A before B.
Success criteria: Explain the RRF output using both input rankings, understand why keyword results need not always come first, and recognize that RRF cannot guarantee the best supporting material for every question.
🎉 Congratulations! You can now combine multiple retrieval rankings with RRF, interpret the resulting order, and explain the different roles of rank fusion and content-based reranking.
Hybrid Retrieval
Combine vector search and keyword matching in an existing RAG knowledge base, then merge and deduplicate the two result sets.
Rerankers
Continue with the refund knowledge base, learn how rerankers assess relevance, and call Alibaba Cloud Model Studio’s Qwen reranking model from Node.js to inspect the results.
