0

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

例如(嘲笑):

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

For which {"grouping":1,"a":2,"b":5},{"grouping":1,"a":1,"b":10} will return (something like): {"grouped":1,"withMax":5},其中最大值来自第一个对象,因为"a"那里更高。

4

1 回答 1

0

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

{
  "size": 0,
  "aggs": {
    "grouped": {
      "terms": {
        "field": "grouping"
      },
      "aggs": {
          "maxByA": {
            "top_hits": {
            "sort": [
             {"a": {"order": "desc"}}
            ],
            "size": 1
          }
        }
      }
    }
  }
}
于 2015-12-08T16:26:23.937 回答