【问题标题】:Can we Round off the score in Elasticsearch我们可以四舍五入 Elasticsearch 中的分数吗
【发布时间】:2017-09-21 03:47:46
【问题描述】:

我正在实施一个项目,其中将根据分数对结果进行排序,如果分数相同,则根据日期字段对结果集进行排序。 当分数相差 0.00001 即第 5 位或第 6 位小数位时,就会出现问题。有什么方法可以将 Elasticsearch 中得出的分数四舍五入到小数点后 4 位,以便二次排序可以处理它。 如果没有任何解决方法可以实现这一点。

谢谢 阿什特

【问题讨论】:

  • 但 elastericsearch 会根据分数按排序顺序给出。
  • @AnkurJyotiPhukan 是的,我知道 .. 我们在 ES 中也有二级排序属性,当它们具有相同的 _score 值时,想要对记录进行二级排序
  • 使用script sorting 来操纵分数。

标签: elasticsearch elasticsearch-plugin


【解决方案1】:

这是一个使用script score 对实际分数进行四舍五入的可行解决方案

GET /post/_search
{
    "query": {
        "function_score": {
            "query": {
                "match": {"title": "example"}
            },
            "script_score" : {
                "script" : {
                  "source": "Math.round(_score)"
                }
            }
        }
    },
    "sort": [
        "_score",
        {"postdate": "desc"}
    ]
}

在这个搜索示例中,我们首先按四舍五入的 _score 排序,然后按降序排序 post.postdate。

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,我想先将分数四舍五入,然后按分数排序。 我也使用了 Mad 的答案,带有 function_score,但在昏迷后我仍然有带有数字的分数。

    根据documentation,我修改了boost_mode以获得一个整数:

    GET /_search
    {
      "query": {
        "function_score": {
          "query": { "match_all": {} },
          "boost_mode": "replace",
          "script_score" : {
            "script" : {
              "source": "Math.round(_score)"
              }
            }
         }
       },
      "sort": [
        "_score",
        {"postdate": "desc"}
       ]
     }
    

    使用这个 boost_mode 帮助我获得整数作为分数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-18
      • 1970-01-01
      • 2011-12-05
      • 1970-01-01
      • 2011-12-02
      • 2015-11-12
      相关资源
      最近更新 更多