gpt4 book ai didi

Elasticsearch:在文档 pt.2 中使用自定义分数字段进行影响评分

转载 作者:行者123 更新时间:2023-12-02 22:16:34 25 4
gpt4 key购买 nike

拥有这些文件:

{
"created_at" : "2017-07-31T20:30:14-04:00",
"description" : null,
"height" : 3213,
"id" : "1",
"tags" : [
{
"confidence" : 65.48948436785749,
"tag" : "beach"
},
{
"confidence" : 57.31950504425406,
"tag" : "sea"
},
{
"confidence" : 43.58207236617374,
"tag" : "coast"
},
{
"confidence" : 35.6857910950816,
"tag" : "sand"
},
{
"confidence" : 33.660057321079655,
"tag" : "landscape"
},
{
"confidence" : 32.53252312423727,
"tag" : "sky"
}
],
"width" : 5712,
"color" : "#0C0A07",
"boost_multiplier" : 1
}

{
"created_at" : "2017-07-31T20:43:17-04:00",
"description" : null,
"height" : 4934,
"id" : "2",
"tags" : [
{
"confidence" : 84.09123410403951,
"tag" : "mountain"
},
{
"confidence" : 56.412795342449456,
"tag" : "valley"
},
{
"confidence" : 48.36547551196872,
"tag" : "landscape"
},
{
"confidence" : 40.51100450186575,
"tag" : "mountains"
},
{
"confidence" : 33.14263528292239,
"tag" : "sky"
},
{
"confidence" : 31.064394646169404,
"tag" : "peak"
},
{
"confidence" : 29.372,
"tag" : "natural elevation"
}
],
"width" : 4016,
"color" : "#FEEBF9",
"boost_multiplier" : 1
}

我想根据每个标签的置信度值计算 _score。例如,如果您搜索“mountain”,它应该只返回 id 为 1 的文档,如果您搜索“landscape”,则 2 的分数应该高于 1,因为 landscape 在 2 中的置信度高于 1(48.36 对 33.66)。如果搜索“coast landscape”,此时1分应该高于2分,因为doc 1的tags数组中同时有coast和landscape。我还想将分数与“boost_multiplier”相乘,以提升某些文档对其他文档的影响。

我在 SO, Elasticsearch: Influence scoring with custom score field in document 中找到了这个问题

但是当我尝试接受的解决方案时(我在我的 ES 服务器中启用了脚本),它返回的两个文档都具有 _score 1.0,无论搜索词是什么。这是我尝试过的查询:

{
"query": {
"nested": {
"path": "tags",
"score_mode": "sum",
"query": {
"function_score": {
"query": {
"match": {
"tags.tag": "coast landscape"
}
},
"script_score": {
"script": "doc[\"confidence\"].value"
}
}
}
}
}
}

我也尝试了@yahermann 在评论中的建议,将“script_score”替换为“field_value_factor”:{“field”:“confidence”},结果仍然相同。知道为什么会失败,或者有更好的方法吗?

为了获得完整的图片,这里是我使用的映射定义:

{
"mappings": {
"photo": {
"properties": {
"created_at": {
"type": "date"
},
"description": {
"type": "text"
},
"height": {
"type": "short"
},
"id": {
"type": "keyword"
},
"tags": {
"type": "nested",
"properties": {
"tag": { "type": "string" },
"confidence": { "type": "float"}
}
},
"width": {
"type": "short"
},
"color": {
"type": "string"
},
"boost_multiplier": {
"type": "float"
}
}
}
},
"settings": {
"number_of_shards": 1
}
}

更新按照下面@Joanna 的回答,我尝试了查询,但事实上,无论我在匹配查询、coast、foo、bar 中输入什么,它总是返回两个文档的 _score 1.0,我在 elasticsearch 2.4 上尝试过。 Docker 中的 6、5.3、5.5.1。这是我得到的回复:

HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 1635

{"took":24,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":2,"max_score":1.0,"hits":[{"_index":"my_index","_type":"my_type","_id":"2","_score":1.0,"_source":{
"created_at" : "2017-07-31T20:43:17-04:00",
"description" : null,
"height" : 4934,
"id" : "2",
"tags" : [
{
"confidence" : 84.09123410403951,
"tag" : "mountain"
},
{
"confidence" : 56.412795342449456,
"tag" : "valley"
},
{
"confidence" : 48.36547551196872,
"tag" : "landscape"
},
{
"confidence" : 40.51100450186575,
"tag" : "mountains"
},
{
"confidence" : 33.14263528292239,
"tag" : "sky"
},
{
"confidence" : 31.064394646169404,
"tag" : "peak"
},
{
"confidence" : 29.372,
"tag" : "natural elevation"
}
],
"width" : 4016,
"color" : "#FEEBF9",
"boost_multiplier" : 1
}
},{"_index":"my_index","_type":"my_type","_id":"1","_score":1.0,"_source":{
"created_at" : "2017-07-31T20:30:14-04:00",
"description" : null,
"height" : 3213,
"id" : "1",
"tags" : [
{
"confidence" : 65.48948436785749,
"tag" : "beach"
},
{
"confidence" : 57.31950504425406,
"tag" : "sea"
},
{
"confidence" : 43.58207236617374,
"tag" : "coast"
},
{
"confidence" : 35.6857910950816,
"tag" : "sand"
},
{
"confidence" : 33.660057321079655,
"tag" : "landscape"
},
{
"confidence" : 32.53252312423727,
"tag" : "sky"
}
],
"width" : 5712,
"color" : "#0C0A07",
"boost_multiplier" : 1
}
}]}}

UPDATE-2我在 SO 上找到了这个:Elasticsearch: "function_score" with "boost_mode":"replace" ignores function score

它基本上是说,如果函数不匹配,它返回 1。这是有道理的,但我正在为相同的文档运行查询。这令人困惑。

最终更新终于找到问题了,傻我。 ES101,如果您向搜索 api 发送 GET 请求,它会返回所有得分为 1.0 的文档 :) 您应该发送 POST 请求...非常感谢@Joanna,它完美地工作!!!

最佳答案

您可以尝试这个查询 - 它结合了评分与:confidenceboost_multiplier 字段:

{
"query": {
"function_score": {
"query": {
"bool": {
"should": [{
"nested": {
"path": "tags",
"score_mode": "sum",
"query": {
"function_score": {
"query": {
"match": {
"tags.tag": "landscape"
}
},
"field_value_factor": {
"field": "tags.confidence",
"factor": 1,
"missing": 0
}
}
}
}
}]
}
},
"field_value_factor": {
"field": "boost_multiplier",
"factor": 1,
"missing": 0
}
}
}
}

当我用 coast 词搜索时 - 它返回:

  • id=1 的文档,因为只有这个有这个词,得分是 "_score": 100.27469

当我使用 landscape 术语进行搜索时 - 它会返回两个文档:

  • id=2 且得分为“_score”的文档:85.83046
  • id=1 且得分为“_score”的文档:59.7339

由于 id=2 的文档具有更高的 confidence 字段值,因此它获得更高的得分。

当我使用 coast landscape 术语进行搜索时 - 它会返回两个文档:

  • id=1 且得分为“_score”的文档:160.00859
  • id=2 且得分为“_score”的文档:85.83046

虽然 id=2 的文档有更高的 confidence 字段值,但是 id=1 的文档有两个匹配词,所以它得到更多更高的得分。通过更改 "factor": 1 参数的值,您可以决定置信度对结果的影响有多大。

boost_muliplier 字段

当我索引一个新文档时会发生更有趣的事情:假设它与具有 id=2 的文档几乎相同,但我设置了 "boost_multiplier": 4“id”:3:

{
"created_at" : "2017-07-31T20:43:17-04:00",
"description" : null,
"height" : 4934,
"id" : "3",
"tags" : [
...
{
"confidence" : 48.36547551196872,
"tag" : "landscape"
},
...
],
"width" : 4016,
"color" : "#FEEBF9",
"boost_multiplier" : 4
}

使用 coast landscape 术语运行相同的查询会返回三个文档:

  • id=3 且得分为“_score”的文档:360.02664
  • id=1 且得分为“_score”的文档:182.09859
  • id=2 且得分为“_score”的文档:90.00666

虽然 id=3 的文档只有一个匹配词(landscape),但它的 boost_multiplier 值大大提高了得分。在这里,使用 "factor": 1,您还可以决定这个值应该增加多少得分,使用 "missing": 0 决定如果没有这样的字段应该发生什么索引。

关于Elasticsearch:在文档 pt.2 中使用自定义分数字段进行影响评分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45460296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com