Sub topic 11
Merging the results of many searches
This chapter assumes you have read the chapters on query fan-out and on hybrid retrieval. You should know that one prompt can become several narrower searches, and that each search returns a ranked list of passages. The code uses basic Python, but you can follow the ideas without running it.
The problem fan-out creates
Fan-out gives a system several ranked lists instead of one. Take the prompt "best running shoes for flat feet". One sub-query may return a buyer's guide first. Another may return a podiatrist's article first. A third may put a retailer's category page first. The system cannot show all of these lists to the user. It has to produce one ordered set of candidates for the next stage, which is reranking.
Merging, often called rank fusion, is that step. It sits between retrieval and reranking, and it decides which candidates the reranker gets to see.
Why not just average the scores?
The obvious approach is to add up each document's relevance scores across the lists. That works poorly for two reasons.
- Scores are not comparable across lists. A keyword-matching score and an embedding similarity score sit on different scales. Even two embedding searches can have different score distributions, because a narrow sub-query and a broad one behave differently.
- Scores are not always exposed. Some retrieval components return only an ordering.
Rank-based fusion avoids both problems. It ignores the raw scores and uses only each document's position in each list.
Reciprocal rank fusion
Reciprocal rank fusion (RRF) is a published method, described by Cormack, Clarke and Buettcher in 2009. It is widely used, and several search engines and vector databases offer it as a built-in option. Each list gives a document a score of 1 / (k + rank), and the scores are summed across lists. The constant k is commonly set to 60. It stops a single first-place result from dominating.
def reciprocal_rank_fusion(rankings, k=60):
"""Merge several ranked lists of document IDs into one list."""
scores = {}
for ranking in rankings:
for rank, doc in enumerate(ranking, start=1):
scores[doc] = scores.get(doc, 0) + 1 / (k + rank)
return sorted(scores, key=scores.get, reverse=True)
results_per_sub_query = [
["page_a", "page_b", "page_c"],
["page_b", "page_d", "page_a"],
["page_e", "page_b"],
]
print(reciprocal_rank_fusion(results_per_sub_query))This prints ['page_b', 'page_a', 'page_e', 'page_d', 'page_c']. Here is the arithmetic:
| Page | Positions | Score |
|---|---|---|
| page_b | 2nd, 1st, 2nd | 1/62 + 1/61 + 1/62 ≈ 0.0487 |
| page_a | 1st, 3rd | 1/61 + 1/63 ≈ 0.0323 |
| page_e | 1st in one list | 1/61 ≈ 0.0164 |
| page_d | 2nd in one list | 1/62 ≈ 0.0161 |
| page_c | 3rd in one list | 1/63 ≈ 0.0159 |
pageb never ranks first, yet it wins. It appears in all three lists, and consistent mid-to-high placement adds up faster than a single top spot. pagee ranks first for one sub-query and still finishes third.
What the method rewards
Three properties follow from the formula. They are properties of RRF itself. We do not know that any particular AI search product uses it.
- Breadth beats a single peak. A document that is a good answer to several sub-queries tends to beat one that is the best answer to one sub-query.
- Early ranks matter most, with diminishing differences. Moving from 1st to 2nd costs more than moving from 11th to 12th, but the gap is small compared with being absent from a list.
- Absence costs nothing directly, but it forfeits points. A page missing from a list is not penalised. It just earns nothing from that list.
A short experiment shows the second and third properties together:
def rrf_scores(rankings, k=60):
scores = {}
for ranking in rankings:
for rank, doc in enumerate(ranking, start=1):
scores[doc] = scores.get(doc, 0) + 1 / (k + rank)
return scores
lists = [
["specialist", "generalist"],
["other_1", "other_2", "generalist"],
["other_3", "other_4", "generalist"],
]
scores = rrf_scores(lists)
for doc, score in sorted(scores.items(), key=lambda item: -item[1])[:3]:
print(doc, round(score, 4))The output is generalist 0.0481, specialist 0.0164 and other_1 0.0164. The generalist page never ranks first anywhere. It still beats the specialist page, which ranks first once. A page that covers several angles but is never the best answer to any one of them can therefore outrank a specialist.
Practical details that change the outcome
The clean example hides several real-world complications.
- Fusion happens on passages, not pages. As the chunking chapter explained, retrieval returns passages. A system may fuse passage IDs and then group them by URL, or it may fuse at page level. Either choice changes whether several strong passages on one page add up or compete with each other. This is an open question for outsiders.
- Duplicates must be resolved. The same URL can come back under different sub-queries, and the same text can appear on several URLs. Fusion only works if the system recognises these as one item. Syndicated copies of your content may split the score.
- Ties are broken arbitrarily. In the Python above, tied documents keep the order in which they were first seen, because
sortedis stable. A real system may break ties differently. - Weights are possible. A system could weight the original prompt more than its expansions, or weight keyword results differently from embedding results. Weighted variants of RRF exist. Whether any product uses them is not documented.
- List length matters. If each sub-query only keeps its top 10 or 20 results, a page ranking 25th contributes nothing. Fusion cannot rescue a page that retrieval never returned.
- Fusion is not the final word. The merged list usually goes to a reranker. Fusion decides who gets an audition, and the reranker decides who gets cited.
What this means for marketers
The mechanism suggests some cautious conclusions. They are inferences, not documented rules.
- Being retrievable for several related sub-queries is worth more than being the top result for one. A page that answers the main question and its comparison, constraint and expert-angle variants has more chances to collect points.
- One page covering many angles is not automatically better than several focused pages. Fusion adds scores per document or passage. A very long page can dilute each passage, which hurts retrieval. Several focused pages can each rank well for one sub-query but lose the breadth effect. The right balance depends on the topic and the system, and you can only find it by testing.
- Ranking in classic search is a poor proxy. A page ranking 4th for the head term but absent from every narrower sub-query list may lose to a page ranking 6th across many.
- Missing from the candidate pool means invisible. Fix retrieval problems, such as crawl access, indexing and passage clarity, before worrying about fusion.
Exercise
Three sub-queries return these ranked lists: [A, B, C], [D, A, E], [F, G, A]. Using RRF with k = 60, which page ranks first, and why can it beat pages that are first in a list?
Show answer
Page A ranks first. It scores 1/61 + 1/62 + 1/63 ≈ 0.0484. Pages D and F are each first in one list but score only 1/61 ≈ 0.0164. RRF sums reciprocal-rank scores across lists, so appearing in every list outweighs a single top position.
Exercise
Name two reasons a system might fuse by rank position instead of by raw relevance score, and one reason fusion cannot help a page you want to be cited.
Show answer
Raw scores from different retrieval methods and sub-queries are on different scales, and some components return only an ordering. Fusion cannot help a page that no sub-query's retrieval returned, because only documents already in the lists can earn points.