在 Elastic Search 中组合聚合

Combining aggregations in Elastic Search

我有一些对象按 'masterID' 分组。我需要一个 aggregation/query 结果显示每个对象组 'relevance' 最高的对象 'masterID'.

通过术语 'masterID' 的聚合,我可以获得每个 'masterID' 的桶。但是如何获得每个桶中最高的 'relevance' 对象?

目前的查询是:

curl -XGET 'http://localhost:9200/pwo/_search?search_type=count&size=0&pretty=true' -d '{
  "aggregations": {
    "masterIDs": {
      "terms": {
        "field": "masterID",
        "size": 0
      }
    }
  }
}
'

curl -XGET 'http://localhost:9200/pwo/_search?size=0&pretty=true' -d '{
  "aggregations": {
    "relevance": {
      "max": {
        "field": "relevance"
      }
    }
  }
}
'

有没有办法通过一个查询来解决这个问题?

您可以在 masterIDs 块中添加相关性聚合块,如下所示:

curl -XGET 'http://localhost:9200/pwo/_search?size=0&pretty=true' -d '{
  "aggregations": {
    "masterIDs": {
      "terms": {
        "field": "masterID",
        "size": 0
      },
      "aggregations": {
        "relevance": {
          "max": { "field" : "relevance" }
        },
        "aggregations": {
          "top_hits": {
            "size": 1
          }
        }
      }
    }
  }
}
'