Elasticsearch:倒数排序融合 – Reciprocal rank fusion

警告:此功能处于技术预览阶段,可能会在未来版本中更改或删除。 Elastic 将尽最大努力修复任何问题,但技术预览中的功能不受官方 GA 功能的支持 SLA 约束。

倒数排序融合(RRF)是一种将具有不同相关性指标的多个结果集组合成单个结果集的方法。 RRF 无需调优,不同的相关性指标也不必相互关联即可获得高质量的结果。该方法的优势在于不利用相关分数,而仅靠排名计算。相关分数存在的问题在于不同模型的分数范围差。

使用 Reciprocal Rank Fusion (RRF) 的简化混合搜索

通常,最好的排名是通过组合多种排名方法来实现的,例如 BM25 和生成密集向量嵌入的 ML 模型。 在实践中,将结果集组合成一个单一的组合相关性排名结果集被证明是非常具有挑战性的。 当然,理论上你可以将每个结果集的分数归一化(因为原始分数在完全不同的范围内),然后进行线性组合,根据每个排名的分数加权和排序最终结果集方法。 只要你提供正确的权重,Elasticsearch 就支持它并且运行良好。 为此,你需要了解环境中每种方法得分的统计分布,并有条不紊地优化权重。 实际上,这超出了绝大多数用户的能力。

另一种方法是 RRF 算法,它提供了出色的排序方法零样本混合,正如学术研究所证明的那样。 如果你不了解不同方法中排名分数的确切分布,这不仅是最好的混合方式,而且客观上也是混合排名方法的好方法 —— 即使你知道如何归一化及分数的分布情况,也很难被击败。 基本概念是结果的组合顺序由每个结果集中每个文档的位置(和存在)定义。 因此可以方便地忽略排名分数。

Elastic 8.8 支持具有多个密集向量查询和在倒排索引上运行的单个查询的 RRF。 在不久的将来,我们希望支持来自 BM25 和 Elastic 的检索模型(两者都是稀疏向量)的混合结果,从而产生同类最佳的零样本集(无域内训练)排名方法。

  • D – 文档集
  • R – 一组排名作为 1..|D| 的排列
  • K – 通常默认设置为 60

RRF 使用以下公式来确定对每个文档进行排名的分数:





















1.  score = 0.0
2.  for q in queries:
3.      if d in result(q):
4.          score += 1.0 / ( k + rank( result(q), d ) )
5.  return score

7.  # where
8.  # k is a ranking constant
9.  # q is a query in the set of queries
10.  # d is a document in the result set of q
11.  # result(q) is the result set of q
12.  # rank( result(q), d ) is d's rank within the result(q) starting from 1



倒数排序融合 API

你可以将 RRF 用作搜索的一部分,以使用来自:

  • 1 个查询(query)和 1 个或多个 kNN 搜索
  • 2 个或更多 kNN 搜索

rrf 参数是一个可选对象,定义为搜索请求 rank parameter 的一部分。 rrf 对象包含以下参数:

条目 描述
rank_constant (可选,整数)此值确定每个查询的单个结果集中的文档对最终排名结果集的影响程度。 较高的值表示排名较低的文档具有更大的影响力。 此值必须大于或等于 1。默认为 60。
window_size (可选,整数)此值确定每个查询的单个结果集的大小。 较高的值将以性能为代价提高结果相关性。 最终排名的结果集被修剪为搜索请求的 <<search-size-param, size>。 window_size 必须大于或等于 size 且大于或等于 1。默认为 100。

使用 RRF 的示例请求:





















1.  GET example-index/_search

2.  {




3.      "query": {
4.          "term": {
5.              "text": "shoes"
6.          }
7.      },
8.      "knn": {
9.          "field": "vector",
10.          "query_vector": [1.25, 2, 3.5],
11.          "k": 50,
12.          "num_candidates": 100
13.      },
14.      "rank": {
15.          "rrf": {
16.              "window_size": 50,
17.              "rank_constant": 20
18.          }
19.      }
20.  }


在上面的示例中,我们首先执行 kNN 搜索以获取其全球前 50 名的结果。 然后我们执行查询以获取其全球前 50 名的结果。 之后,在一个协调节点上,我们将 knn 搜索结果与查询结果结合起来,根据 RRF 方法对它们进行排序,得到最终的 top 10 结果。

请注意,如果来自 knn 搜索的 k 大于 window_size,则结果将被截断为 window_size。 如果 k 小于 window_size,则结果为 k 大小。

倒数排序融合支持的功能

RRF 确实支持:

RRF 目前不支持:

使用不支持的功能作为使用 RRF 的搜索的一部分将导致异常。

倒数排序融合完整示例

我们首先为具有文本字段、向量字段和整数字段的索引创建映射,同时索引多个文档。 对于这个例子,我们将使用一个只有一个维度的向量来使排名更容易解释。





















1.  PUT example-index
2.  {




3.    "mappings": {
4.      "properties": {
5.        "text": {
6.          "type": "text"
7.        },
8.        "vector": {
9.          "type": "dense_vector",
10.          "dims": 1,
11.          "index": true,
12.          "similarity": "l2_norm"
13.        },
14.        "integer": {
15.          "type": "integer"
16.        }
17.      }
18.    }
19.  }

21.  PUT example-index/_doc/1
22.  {
23.      "text" : "rrf",
24.      "vector" : [5],
25.      "integer": 1
26.  }

28.  PUT example-index/_doc/2
29.  {
30.      "text" : "rrf rrf",
31.      "vector" : [4],
32.      "integer": 2
33.  }

35.  PUT example-index/_doc/3
36.  {
37.      "text" : "rrf rrf rrf",
38.      "vector" : [3],
39.      "integer": 1
40.  }

42.  PUT example-index/_doc/4
43.  {
44.      "text" : "rrf rrf rrf rrf",
45.      "integer": 2
46.  }

48.  PUT example-index/_doc/5
49.  {
50.      "vector" : [0],
51.      "integer": 1
52.  }


54.  POST example-index/_refresh



现在,我们使用带有查询、kNN 搜索和术语聚合的 RRF 执行搜索。





















1.  GET example-index/_search

2.  {




3.    "query": {

4.      "term": {

5.        "text": "rrf"

6.      }

7.    },
8.    "knn": {
9.      "field": "vector",
10.      "query_vector": [
11.        3
12.      ],
13.      "k": 5,
14.      "num_candidates": 5
15.    },
16.    "rank": {
17.      "rrf": {
18.        "window_size": 5,
19.        "rank_constant": 1
20.      }
21.    },
22.    "size": 3,
23.    "aggs": {
24.      "int_count": {
25.        "terms": {
26.          "field": "integer"
27.        }
28.      }
29.    }
30.  }


我们收到带有排名 hits 和术语聚合结果的响应。 请注意,_score 为 null,我们改为使用 _rank 来显示排名靠前的文档。





















1.  {
2.    "took": 16,
3.    "timed_out": false,
4.    "_shards": {
5.      "total": 1,
6.      "successful": 1,
7.      "skipped": 0,
8.      "failed": 0
9.    },
10.    "hits": {
11.      "total": {
12.        "value": 5,
13.        "relation": "eq"
14.      },
15.      "max_score": null,
16.      "hits": [
17.        {
18.          "_index": "example-index",
19.          "_id": "3",
20.          "_score": null,
21.          "_rank": 1,
22.          "_source": {
23.            "text": "rrf rrf rrf",
24.            "vector": [
25.              3
26.            ],
27.            "integer": 1
28.          }
29.        },
30.        {
31.          "_index": "example-index",
32.          "_id": "2",
33.          "_score": null,
34.          "_rank": 2,
35.          "_source": {
36.            "text": "rrf rrf",
37.            "vector": [
38.              4
39.            ],
40.            "integer": 2
41.          }
42.        },
43.        {
44.          "_index": "example-index",
45.          "_id": "4",
46.          "_score": null,
47.          "_rank": 3,
48.          "_source": {
49.            "text": "rrf rrf rrf rrf",
50.            "integer": 2
51.          }
52.        }
53.      ]
54.    },
55.    "aggregations": {
56.      "int_count": {
57.        "doc_count_error_upper_bound": 0,
58.        "sum_other_doc_count": 0,
59.        "buckets": [
60.          {
61.            "key": 1,
62.            "doc_count": 3
63.          },
64.          {
65.            "key": 2,
66.            "doc_count": 2
67.          }
68.        ]
69.      }
70.    }
71.  }


让我们分解一下这些点击率是如何排名的。 我们首先分别运行查询和 kNN 搜索,以收集它们各自的命中率。

首先,我们查看查询的 hits。





















1.  GET example-index/_search?filter_path=**.hits

2.  {




3.    "query": {

4.      "term": {

5.        "text": "rrf"

6.      }

7.    }
8.  }


上面的命令显示的结果为:





















1.  {

2.    "hits": {

3.      "hits": [

4.        {

5.          "_index": "example-index",

6.          "_id": "4",
7.          "_score": 0.16152832,         [1]
8.          "_source": {

9.            "text": "rrf rrf rrf rrf",
10.            "integer": 2
11.          }
12.        },
13.        {
14.          "_index": "example-index",
15.          "_id": "3",
16.          "_score": 0.15876243,         [2]
17.          "_source": {
18.            "text": "rrf rrf rrf",
19.            "vector": [
20.              3
21.            ],
22.            "integer": 1
23.          }
24.        },
25.        {
26.          "_index": "example-index",
27.          "_id": "2",
28.          "_score": 0.15350538,         [3]
29.          "_source": {
30.            "text": "rrf rrf",
31.            "vector": [
32.              4
33.            ],
34.            "integer": 2
35.          }
36.        },
37.        {
38.          "_index": "example-index",
39.          "_id": "1",
40.          "_score": 0.13963442,         [4]
41.          "_source": {
42.            "text": "rrf",
43.            "vector": [
44.              5
45.            ],
46.            "integer": 1
47.          }
48.        }
49.      ]
50.    }
51.  }



  • [1]  rank 1, _id 4
  • [2]  rank 2, _id 3
  • [3]  rank 3, _id 2
  • [4]  rank 4, _id 1

请注意,我们的第一个命中没有 vector 字段的值。 现在,我们查看 kNN 搜索的结果。





















1.  GET example-index/_search?filter_path=**.hits

2.  {




3.    "knn": {
4.      "field": "vector",
5.      "query_vector": [
6.        3
7.      ],
8.      "k": 5,
9.      "num_candidates": 5
10.    }
11.  }



上面搜索的结果为:





















1.  {

2.    "hits": {

3.      "hits": [

4.        {

5.          "_index": "example-index",

6.          "_id": "3",                      [1]
7.          "_score": 1,
8.          "_source": {

9.            "text": "rrf rrf rrf",
10.            "vector": [
11.              3
12.            ],
13.            "integer": 1
14.          }
15.        },
16.        {
17.          "_index": "example-index",
18.          "_id": "2",                     [2]
19.          "_score": 0.5,
20.          "_source": {
21.            "text": "rrf rrf",
22.            "vector": [
23.              4
24.            ],
25.            "integer": 2
26.          }
27.        },
28.        {
29.          "_index": "example-index",
30.          "_id": "1",                     [3]
31.          "_score": 0.2,
32.          "_source": {
33.            "text": "rrf",
34.            "vector": [
35.              5
36.            ],
37.            "integer": 1
38.          }
39.        },
40.        {
41.          "_index": "example-index",
42.          "_id": "5",                     [4]
43.          "_score": 0.1,
44.          "_source": {
45.            "vector": [
46.              0
47.            ],
48.            "integer": 1
49.          }
50.        }
51.      ]
52.    }
53.  }



  • [1]  rank 1, _id 3
  • [2]  rank 2, _id 2
  • [3]  rank 3, _id 1
  • [4]  rank 4, _id 5

我们现在可以获取两个单独排名的结果集并将 RRF 公式应用于它们以获得我们的最终排名。





















1.  # doc  | query     | knn       | score
2.  _id: 1 = 1.0/(1+4) + 1.0/(1+3) = 0.4500
3.  _id: 2 = 1.0/(1+3) + 1.0/(1+2) = 0.5833
4.  _id: 3 = 1.0/(1+2) + 1.0/(1+1) = 0.8333
5.  _id: 4 = 1.0/(1+1)             = 0.5000
6.  _id: 5 =             1.0/(1+4) = 0.2000


我们根据 RRF 公式对文档进行排名,其中 window_size 为 5,截断 RRF 结果集中底部的 2 个文档,大小为 3。我们以 _id: 3 作为 _rank: 1, _id: 2 作为 _rank: 2, 及 _id: 4 作为 _rank: 3。这个排名符合预期的原始 RRF 搜索的结果集。

© 版权声明
THE END
喜欢就支持一下吧
点赞0

Warning: mysqli_query(): (HY000/3): Error writing file '/tmp/MYh79EKP' (Errcode: 28 - No space left on device) in /www/wwwroot/583.cn/wp-includes/class-wpdb.php on line 2345
admin的头像-五八三
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

图形验证码
取消
昵称代码图片