What is Reciprocal Rank Fusion?什么是倒数排名融合?
Reciprocal Rank Fusion (RRF) is a technique for combining multiple ranked lists of search results into a single ranking. Originally developed for information retrieval research, RRF has become essential in modern search systems to allow ranking across multiple retrieval methods, such as combining lexical and semantic search results in hybrid search applications.倒数排名融合(RRF)是一种将多个搜索结果排名列表合并为单一排名的技术。该技术最初为信息检索研究而开发,现已成为现代搜索系统中不可或缺的方法,允许跨多种检索方法进行排名,例如在混合搜索应用中结合词汇搜索和语义搜索结果。
RRF's elegance lies in its simplicity: rather than normalizing scores within each scoring system, it works directly with document positions in the ranked lists.RRF的精妙之处在于其简洁性:它不依赖于对每个评分系统内的分数进行归一化,而是直接利用文档在排名列表中的位置。
How RRF WorksRRF的工作原理
RRF operates on a simple principle: documents that appear highly ranked across multiple search methods are likely to be genuinely relevant.RRF基于一个简单的原则:在多种搜索方法中均排名靠前的文档很可能具有真正的相关性。
The standard RRF formula treats all ranking sources equally, but Weighted RRF allows you to assign different weights to each method.标准RRF公式对所有排名来源一视同仁,但加权RRF允许您为每种方法分配不同的权重。
For example, when combining BM25 and vector search you might assign weights of 1.0 and 0.7 respectively to show that you value lexical over semantic meaning.例如,在结合BM25和向量搜索时,您可以分别分配1.0和0.7的权重,以表明您更看重词汇意义而非语义意义。
WeightedRRF(d)=∑r∈R wr⋅1k+rankr(d)Where其中
- is a documentd 是一个文档
- is the set of ranked listsR 是排名列表的集合
- is the rank of document in ranking rankr(d) 是文档 d 在排名 r 中的位置
- is the weight for ranking (typically between 0 and 1). In non-weighted/traditional RRF this is always 1.wr 是排名 r 的权重(通常在0到1之间)。在非加权/传统RRF中,该值始终为1。
- is a constant (typically 60) that controls the fusion behaviork 是一个常数(通常为60),用于控制融合行为
The fusion process works as follows:融合过程如下:
- Start with multiple ranked lists from different retrieval methods (discard the actual ranking numbers).从不同检索方法获取多个排名列表(丢弃实际排名数值)。
- Calculate RRF scores for each document that appears in any list and multiply by the weight (if used).计算出现在任何列表中的每个文档的RRF分数,并乘以权重(如果使用)。
- Merge all documents and sort by their combined RRF scores合并所有文档,并按组合后的RRF分数排序。
Documents missing from a ranking contribute zero to that ranking's sum.缺失于某个排名的文档对该排名的总和贡献为零。
The constant is almost always used because it has been empirically shown to work well across different datasets.常数 k=60 几乎总是被使用,因为经验表明它在不同数据集上表现良好。
can be tuned based on your specific use case and data characteristics. Lower values (20-40) will give top results more influence, higher values (80-100) will give a more gradual contribution difference.k 可以根据您的具体用例和数据特征进行调整。较低的值(20-40)会使顶部结果具有更大影响力,较高的值(80-100)则会使贡献差异更平缓。
Why RRF Works WellRRF为何有效
RRF has three key strengths:RRF具有三个关键优势:
- Simplicity: No training data, complex optimization, or normalization required. Easy to implement and fast to compute.简洁性:无需训练数据、复杂优化或归一化。易于实现且计算快速。
- Robustness: Works with different score scales without normalization. Handles partial results gracefully.鲁棒性:无需归一化即可处理不同分数尺度。优雅地处理部分结果。
- Effectiveness: Often outperforms more sophisticated fusion techniques, especially when combining complementary methods.有效性:通常优于更复杂的融合技术,尤其是在结合互补方法时。
Research consistently shows RRF's effectiveness when combining different retrieval approaches like keyword search with semantic similarity.研究一致表明,RRF在结合不同检索方法(如关键词搜索与语义相似度)时非常有效。
When to Use RRF何时使用RRF
While RRF can be used any time there are multiple scoring systems which need to be combined in a single query it excels in several scenarios:虽然RRF可用于任何需要将多个评分系统合并到单个查询中的场景,但它在以下几种情况下尤为出色:
Hybrid Search Systems Combining lexical search (BM25) with semantic vector search:混合搜索系统 结合词汇搜索(BM25)与语义向量搜索:
- BM25 finds specific keywords and technical termsBM25查找特定关键词和技术术语
- Vector search captures conceptual similarity向量搜索捕捉概念相似性
- RRF harnesses both strengthsRRF利用两者的优势
Multi-Field Search Search across different document parts with separate rankings:多字段搜索 跨不同文档部分进行搜索,每个部分有独立排名:
- Title search results标题搜索结果
- Body content results正文内容结果
- Metadata results元数据结果
Personalization Merge general relevance with personalized signals based on user behavior or preferences.个性化 将通用相关性与基于用户行为或偏好的个性化信号合并。
Example: RRF in Action示例:RRF的实际应用
Consider a search for machine learning tutorial using both lexical and semantic search:考虑使用词汇搜索和语义搜索查找“机器学习教程”:
Lexical Search Results:词汇搜索结果:
- "Complete Machine Learning Tutorial Guide"“完整机器学习教程指南”
- "Tutorial: Introduction to ML Algorithms"“教程:ML算法入门”
- "Python Machine Learning Handbook"“Python机器学习手册”
Semantic Search Results:语义搜索结果:
- "AI and Deep Learning Fundamentals"“AI与深度学习基础”
- "Complete Machine Learning Tutorial Guide"
- "Beginner's Guide to Neural Networks"“神经网络初学者指南”
RRF Calculation (k=60):RRF计算(k=60):
- "Complete ML Tutorial" appears in both lists (rank 1 and 2):
1/61 + 1/62 = 0.0326“完整ML教程”出现在两个列表中(排名1和2):1/61 + 1/62 = 0.0326 - "AI and Deep Learning" appears only in semantic (rank 1):
1/61 = 0.0164“AI与深度学习”仅出现在语义中(排名1):1/61 = 0.0164 - "Tutorial: Intro to ML" appears only in lexical (rank 2):
1/62 = 0.0161“教程:ML入门”仅出现在词汇中(排名2):1/62 = 0.0161
Weights allow you to emphasize one retrieval method over another. For instance, in a hybrid setup:权重允许您强调一种检索方法优于另一种。例如,在混合设置中:
- BM25 might have a weight of 1.0 for lexical precision.BM25可能具有1.0的权重,用于词汇精确性。
- A vector search model might have 0.7 for semantic similarity.向量搜索模型可能具有0.7的权重,用于语义相似性。
The effect is simple but powerful: RRF still rewards agreement across rankers, but weights let you encode which signals you trust most. This makes weighted RRF especially useful in production systems where retrieval sources vary in quality or purpose.效果简单而强大:RRF仍然奖励排名器之间的一致性,但权重让您编码最信任的信号。这使得加权RRF在生产系统中特别有用,因为检索源的质量或目的各不相同。
Implementation in SQLSQL实现
A variety of databases and search engines have pre-implemented RRF, but it's a very simple formula and can be easily demonstrated in SQL as follows:多种数据库和搜索引擎已预实现RRF,但该公式非常简单,可以轻松在SQL中演示如下:
WITH fulltext AS (
SELECT id, RANK() OVER (ORDER BY score DESC) AS rank
FROM (
SELECT id, pdb.score(id) AS score
FROM mock_items
WHERE description @@@ 'keyboard'
ORDER BY pdb.score(id) DESC
LIMIT 20
)
),
--- Semantic search, using pgvector and cosine distance for ranking
semantic AS (
SELECT
id,
RANK() OVER (ORDER BY embedding <=> '[1,2,3]') AS rank
FROM mock_items
ORDER BY embedding <=> '[1,2,3]'
LIMIT 20
),
-- Calculate RRF contributions from each ranker
rrf AS (
SELECT id, 1.0 / (60 + rank) AS s FROM fulltext
UNION ALL
SELECT id, 1.0 / (60 + rank) AS s FROM semantic
)
-- Sum the RRF scores, order by them, and join back the original data
SELECT
m.id,
sum(s),
m.description
FROM rrf
JOIN mock_items AS m USING (id)
GROUP BY m.id, m.description
ORDER BY sum(s) DESC
LIMIT 5;
Summary总结
Reciprocal Rank Fusion provides an elegant solution for combining multiple search rankings without the complexity that comes from score based systems. Its simplicity, robustness, and proven effectiveness make it the standard approach for hybrid search systems that need to merge results from different retrieval techniques.倒数排名融合为组合多个搜索排名提供了一种优雅的解决方案,避免了基于分数的系统带来的复杂性。其简洁性、鲁棒性和经过验证的有效性使其成为需要合并不同检索技术结果的混合搜索系统的标准方法。