我正在查询

 aggs: {

            all_devices_types: {
              nested: {
                path: 'devices'
              },
              aggs: {
                devices_device_name: {
                  terms: {
                    field: 'devices.brand_name'
                  }
                },
                devices_brand_name: {
                  terms: {
                    field: 'devices.device_name'
                  }
                }
              }
            }
所有设备都有品牌,因此一个记录如下所示
{
"device_name": "R Max",
"device_id": "767",
"brand_name": "Addas",
"brand_id": "726"
}
在我进行聚合查询后,我得到了结果,例如。
"devices_device_name": {
"doc_count_error_upper_bound": 2,
"sum_other_doc_count": 81,
"buckets": [
{
"key": "R max ",
"doc_count": 1406
}
]
}
我需要在这个聚合中也有vlue“brand_name”,所以结果应该像
“devices_device_name”:{
“doc_count_error_upper_bound”:2
“sum_other_doc_count”:81,
“存储桶”:[
{
“brand_name”:“Addidass”
“key”:“R max”,
“doc_count”:1406
}
]
}
我怎样才能做到这一点?

最佳答案

您不能舍弃-至少不求助于scripting
可以做的是一个聚合中的一个聚合:

GET brands/_search
{
  "size": 0,
  "aggs": {
    "all_devices_types": {
      "nested": {
        "path": "devices"
      },
      "aggs": {
        "by_device_name": {
          "terms": {
            "field": "devices.device_name"
          },
          "aggs": {
            "by_brand_name": {
              "terms": {
                 "field": "devices.brand_name"
              }
            }
          }
        }
      }
    }
  }
}
这会导致给定brand_name中的device_name计数
[
  {
    "key":"R Max",
    "doc_count":1,
    "by_brand_name":{
      "doc_count_error_upper_bound":0,
      "sum_other_doc_count":0,
      "buckets":[
        {
          "key":"Addas",
          "doc_count":1
        }
      ]
    }
  }
]

关于elasticsearch - Elasticsearch 6:聚合结果中的其他信息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62834936/

10-15 23:16