# Flatten nested aggregation

**URL:** <https://discuss.elastic.co/t/flatten-nested-aggregation/125602>\
**Category:** Elasticsearch\
**Created:** [March 26, 2018, 12:45pm UTC](https://discuss.elastic.co/t/flatten-nested-aggregation/125602 "2018-03-26T12:45:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![denismaciel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/denismaciel/32/31555_2.png) [@denismaciel](https://discuss.elastic.co/u/denismaciel)\
**Post date:** [March 26, 2018, 12:45pm UTC](https://discuss.elastic.co/t/flatten-nested-aggregation/125602/1 "2018-03-26T12:45:32Z")

</div>

Hi, I am trying to flatten out the aggregation below:

```auto
GET _search
{
  "size": 0,
  "aggs": {
    "ACTION": {
      "terms": {
        "field": "action.keyword"
      },
      "aggs": {
        "MONTH": {
          "date_histogram": {
            "field": "@timestamp",
            "interval": "month"
          }
        }
      }
    }
  }

```

The current result is:

```auto
  "aggregations": {
    "ACTION": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 985,
      "buckets": [
        {
          "key": "index",
          "doc_count": 8289,
          "MONTH": {
            "buckets": [
              {
                "key_as_string": "2018-02-01T00:00:00.000Z",
                "key": 1517443200000,
                "doc_count": 4723
              },
              {
                "key_as_string": "2018-03-01T00:00:00.000Z",
                "key": 1519862400000,
                "doc_count": 3566
              }
            ]
          }
        } 
        ...

```

I would like to flatten it and get something like:

```auto
[{
   "key_ACTION": "index",
   "key_MONTH": "2018-02-01",
   "doc_count": 4723
   }, {
   "key_ACTION": "index",
   "key_MONTH": "2018-03-01",
   "doc_count": 3566
  }, { ... }
]

```

I figured there must be a built-in alternative to that already implemented... I am also open to alternative ways of handling it.

My final goal is to plot it in Kibana with the Vega plugin.

---

<div class="post-metadata">

**Author:** ![nyuriks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nyuriks/32/26171_2.png) [@nyuriks](https://discuss.elastic.co/u/nyuriks)\
**Post date:** [March 26, 2018, 6:47pm UTC](https://discuss.elastic.co/t/flatten-nested-aggregation/125602/2 "2018-03-26T18:47:14Z")

</div>

@denismaciel you can use [ES composite aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-composite-aggregation.html) to generate a flat result, or if you are using Kibana 6.0 or 6.1 (not supported for 6.2 yet), you can install [Vega plugin](https://github.com/nyurik/kibana-vega-vis) and use [Vega flatten transformation](https://vega.github.io/vega/docs/transforms/flatten/)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 23, 2018, 6:47pm UTC](https://discuss.elastic.co/t/flatten-nested-aggregation/125602/3 "2018-04-23T18:47:24Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
