【问题标题】:Is there a way to set a score range (or a max score) for a query有没有办法为查询设置分数范围(或最大分数)
【发布时间】:2017-12-11 10:42:53
【问题描述】:

我有一个非常简单的查询:

match: {
  field => {
    boost: 4,
    query: term,
    fuzziness: 'AUTO',
  }
}

由几个(大约 10 个)其他查询组成,其中大多数使用 constant_score。问题是,在特定条件下,我的查询得分太大,会取消所有其他查询结果。

这里是解释的一部分:

"details" => [
[0] {
      "value" => 63.656006,
"description" => "sum of:",
    "details" => [
    [0] {
              "value" => 63.656006,
        "description" => "weight(title.de:kandinsky in 1694239) [PerFieldSimilarity], result of:",
            "details" => [
            [0] {
                      "value" => 63.656006,
                "description" => "score(doc=1694239,freq=1.0 = termFreq=1.0\n), product of:",
                    "details" => [
                    [0] {
                              "value" => 4.0,
                        "description" => "boost",
                            "details" => []
                    },
                    [1] {
                              "value" => 11.3820715,
                        "description" => "idf, computed as log(1 + (docCount - docFreq + 0.5) / (docFreq + 0.5)) from:",
[...]

你看到了吗,由于 IDF,我得到了 11.38 的分数。 我的其他查询(分数在 1 到 3 之间)完全没用。

我的问题是:

如何设置查询的最大可能分数?

或者,更好的是,我可以为我的查​​询设置分数范围吗?

我想避免对该字段进行 constant_score 查询,我需要一些 TF/IDF 和该字段的分数概念,但不是那么强。

我试过这个:

function_score: {
  query: { match: {
    field => term,
  }},
  score_mode: :avg,
  script_score: {
    script: {
      inline: "4 * (1 + Math.log(2 + _score))",
    }
  },
}

更好,但在某些情况下仍然可以取得很高的分数。

【问题讨论】:

  • 您好,您找到答案了吗?我想弄清楚我必须做些什么来解决这个问题,因为我希望我的分数在 0 到 1 之间。谢谢!
  • @christouandr7,我使用了函数分数和脚本分数,使用了1 - (1/x) script_score。我将在下面发布完整答案
  • 感谢您的更新!我有另一个问题。由于我得到的一些分数小于 1,因此使用此评分函数会产生问题。用你刚才写的函数,如果分数小于1,那么最后的分数就是负数!你知道如何处理吗?
  • @christouandr7 你可以这样调整函数:1 - (1/( 1 +x))
  • 我已将该函数调整为 1 - (1 / (10x))。它不是很敏感,但我的工作已经完成!也谢谢你的回答!

标签: ruby-on-rails elasticsearch elasticsearch-ruby


【解决方案1】:

最后,我在script_score 中使用1 - (1/x) 函数将函数得分与脚本得分一起使用

GET _search
{
  "query": {
    "function_score": {
      "query": {
        "match": {
          "postgresql.log.message": "alter"
        }
      },
      "script_score" : {
                "script" : {
                    "params": {
                        "max_score": 5
                    },
                    "source": "params.max_score * (1 - 1 / _score)" 
                }
            }
    }
  }
}

这样,我的分数会在 0 到接近 5 之间(max_score)。

您可以尝试here 与单词alter(得分3.9150627)或alter table pgbench_branches add primary key (bid)(得分4.8539715)。

您可以调整1 - (1/x) 函数以更快地接近渐近线。

【讨论】:

  • 对于较小的分数值,您的最​​高分数不会总是接近 5。就像假设特定查询的实际最高分数是 2,那么您将获得等于 2.5 而不是 5 的最高分数。
【解决方案2】:

您是否尝试过使用功能分数查询? 这是相同的链接 https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html

【讨论】:

  • 是的,我已经尝试过了(我刚刚更新了 OP)。它不允许我创建一个分数范围,只是为了限制一些分数值。
  • 最后,我使用了函数分数和脚本分数,使用 1 - (1/x) script_score。请参阅下面的完整答案
猜你喜欢
  • 1970-01-01
  • 2017-12-19
  • 1970-01-01
  • 1970-01-01
  • 2011-02-28
  • 2019-12-19
  • 2012-02-16
  • 1970-01-01
相关资源
最近更新 更多