根据 Elasticsearch 中函数分数查询的结果分数计算最终脚本分数

发布于 2025-01-13 03:32:32 字数 2043 浏览 2 评论 0原文

我正在尝试构建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文