【问题标题】:ElasticSearch 5.0: aggregation on _score field?ElasticSearch 5.0:_score 字段的聚合?
【发布时间】:2016-12-17 10:00:39
【问题描述】:

我正在将我的 Elasticsearch 查询从 1.7 版本迁移到 5.0(目前是最新版本),但我在聚合方面遇到了一些问题。

我想对给定字段进行聚合并显示按分数排序的前 5 个文档。从我读过的内容来看,这曾经是这样完成的:

"aggs" : {
    "max_price" : { "max" : { "script" : "_score" } }
}

使用 Sense,我得到的答案是“[top_score] 中出现意外的令牌 VALUE_STRING [script]。”

有人经历过吗?

注意:对于旧版本,此处发布了答案:ElasticSearch: aggregation on _score field?

【问题讨论】:

    标签: elasticsearch aggregation


    【解决方案1】:

    如果您只需要按分数排名前 5 位的文档,总体而言,对于您的搜索词,您只需将查询的大小设置为 5,这样就可以解决问题(例如下面)

    { "size": 5, "query": { "bool": { "must": [ { "match": { "my_field": "whatever" } }, { "match": { "my_other_field": "whatever else" } } ], "should": [], "must_not": [], "filter": [] } }, "aggs": { "something": { "terms": { "field": "my_term" }, "aggs": { "field_stats": { "stats": { "field": "price" } } } } } }

    另一方面,如果您想要每个存储桶的前 5 个得分文档,您是否考虑过使用 top hits 聚合? https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html

    【讨论】:

      【解决方案2】:

      Elasticsearch 的默认脚本语言现在是 Painless 而不是 groovy。根据How to use scripts,你可以试试:

      "aggs" : {
          "max_price": {
              "max": {
                  "script": {
                      "lang": "groovy", 
                      "inline": "_score"
                  } 
              } 
          }
      }
      

      在 Elasticsearch 5.x 中更新,将会有弃用的日志,例如:

      [WARN][o.e.d.s.g.GroovyScriptEngineService] [groovy] 脚本已弃用,请改用 [painless] 脚本

      更正确的做法是:

      "aggs" : {
          "max_price": {
              "max": {
                  "script": {
                      "lang": "painless", 
                      "inline": "_score"
                  } 
              } 
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2014-08-24
        • 2015-07-17
        • 1970-01-01
        • 2020-02-18
        • 2016-11-23
        • 2015-07-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多