根据 Elasticsearch 中函数分数查询的结果分数计算最终脚本分数
我正在尝试构建一个 Elasticsearch 查询,该查询应执行以下操作: 计算得分 A,该得分将是具有恒定权重的多个函数的总和,如下
"functions": [
{
"filter": {
"term": {
"field1": "value1"
}
},
"weight": 10
},
{
"filter": {
"match": {
"field2": "value2"
}
},
"weight": 15
}
]
所示,假设本例中 A=25。现在我想使用上述函数的分数来计算总体分数,如下所示
score = A * W1 + doc['field3'] * W2 + doc['field4'] * W3
我尝试了以下方法
{
"explain": true,
"sort": [
{
"_score": "desc"
}
],
"size": 10,
"query": {
"function_score": {
"query": {
"bool": {
"must": {
"match_all": {}
}
}
},
"functions": [
{
"filter": {
"term": {
"field1": "value1"
}
},
"weight": 15
},
{
"filter": {
"term": {
"field2": "value2"
}
},
"weight": 10
},
{
"script_score": {
"script": {
"source": "_score * params.w1 + doc['field3'].value * params.w2 + doc['field4'].value * params.w3",
"params": {
"w1": 0.4,
"w2": 0.1,
"w3": 0.2
}
}
}
}
],
"score_mode": "sum",
"boost_mode": "replace"
}
}
}
但是根据我的观察,最后一个 script_score 中的 _score 似乎不是前两个函数的值,因此最终的得分值是不同的。如何根据其他两个函数的结果计算 script_score ?
我在我的用例中使用elasticsearch 7.12。
I'm trying to build an Elasticsearch query that should do following:
Calculate a score A which is going to be the sum of multiple functions with constant weights as below
"functions": [
{
"filter": {
"term": {
"field1": "value1"
}
},
"weight": 10
},
{
"filter": {
"match": {
"field2": "value2"
}
},
"weight": 15
}
]
Suppose A=25 in this case. Now I want to compute an overall score using the score from the above functions as below
score = A * W1 + doc['field3'] * W2 + doc['field4'] * W3
I've tried the following
{
"explain": true,
"sort": [
{
"_score": "desc"
}
],
"size": 10,
"query": {
"function_score": {
"query": {
"bool": {
"must": {
"match_all": {}
}
}
},
"functions": [
{
"filter": {
"term": {
"field1": "value1"
}
},
"weight": 15
},
{
"filter": {
"term": {
"field2": "value2"
}
},
"weight": 10
},
{
"script_score": {
"script": {
"source": "_score * params.w1 + doc['field3'].value * params.w2 + doc['field4'].value * params.w3",
"params": {
"w1": 0.4,
"w2": 0.1,
"w3": 0.2
}
}
}
}
],
"score_mode": "sum",
"boost_mode": "replace"
}
}
}
But from what I've observed, the _score in the last script_score doesn't seem to the value from the previous two functions, hence the final score value is different. How can I compute the script_score on the result of the other two functions?
I'm using elasticsearch 7.12 for my use case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论