'elasticsearch filters aggregation does not return array format

The filters aggregation returns bucket as object

      "buckets": {
        "errors": {
          "doc_count": 1
        },
        "warnings": {
          "doc_count": 2
        }
      }

But i would like to return a buckets array, like the terms aggregation

      "buckets": [
        { 
          "key": "errors",
          "doc_count": 1
        },
        { 
          "key": "warnings",
          "doc_count": 2
        }
      ]

Is this possible or any sort of data transformation can be done in the query to make it so?



Solution 1:[1]

You can do it by providing an array of filters, but in this case your buckets will be anonymous:

GET logs/_search
{
  "size": 0,
  "aggs" : {
    "messages" : {
      "filters" : {
        "filters" : [    <--- specify array
          { "match" : { "body" : "error"   }},
          { "match" : { "body" : "warning" }}
        ]
      }
    }
  }
}

The response will provide an array of resulting buckets in the same order

  "buckets": [
    {
      "doc_count": 1
    },
    {
      "doc_count": 2
    }
  ]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Val