【问题标题】:Elasticsearch minByElasticsearch minBy
【发布时间】:2016-01-26 13:37:48
【问题描述】:

弹性搜索中有没有办法从包含最大值的文档中获取字段? (基本上与 scala 中的maxBy 类似)

例如(嘲笑):

{
    "aggregations": {
        "grouped": {
            "terms": {
                "field": "grouping",
                "order": {
                    "docWithMin": "asc"
                }
            },
            "aggregations": {
                "withMax": {
                    "max": {
                        "maxByField": "a",
                        "field": "b"
                    }
                }
            }
        }
    }
}

为此{"grouping":1,"a":2,"b":5},{"grouping":1,"a":1,"b":10} 将返回(类似于):{"grouped":1,"withMax":5},其中最大值来自第一个对象,因为"a" 在那里更高。

【问题讨论】:

  • 如果你有 {"grouping":1,"a":2,"b":5}{"grouping":1,"a":2,"b":4} 会发生什么?即相同的grouping,相同的a,应该返回b的哪个值?
  • 任何一个都可以接受

标签: elasticsearch elasticsearch-api elasticsearch-aggregation


【解决方案1】:

假设您只想要 a 为最大值的文档,您可以这样做:

{
  "size": 0,
  "aggs": {
    "grouped": {
      "terms": {
        "field": "grouping"
      },
      "aggs": {
          "maxByA": {
            "top_hits": {
            "sort": [
             {"a": {"order": "desc"}}
            ],
            "size": 1
          }
        }
      }
    }
  }
}

【讨论】:

    猜你喜欢
    • 2017-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-28
    • 2016-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多