# Painless flatten

**URL:** <https://discuss.elastic.co/t/painless-flatten/155582>\
**Category:** Elasticsearch\
**Created:** [November 6, 2018, 4:19pm UTC](https://discuss.elastic.co/t/painless-flatten/155582 "2018-11-06T16:19:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![HU\_Jun](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hu_jun/32/37186_2.png) [@HU\_Jun](https://discuss.elastic.co/u/HU_Jun)\
**Post date:** [November 6, 2018, 4:19pm UTC](https://discuss.elastic.co/t/painless-flatten/155582/1 "2018-11-06T16:19:25Z")

</div>

Working on a watcher, similar to [Flatten nested aggregation](https://discuss.elastic.co/t/flatten-nested-aggregation/125602)

but as I am still using ES 5.5, no composition agg avaiable ☹

in my query, i have a cascade of aggregations:

```
{
    aggs: {
        A 
        aggs: {
             B
               aggs: {
                     C
               
               }
        }
    }
}

```

In my watcher transform, I want to flatten the result to a list in case that buckets is not empty:

```
[ { A1.key, B1.key, C1.key },
  { A1.key, B1.key, C2.key },
  { A2.key, B2.key, C1.key } ... ]

```

want to find an easy way instead of loop the for, any suggestion, thank you in advance.

---

<div class="post-metadata">

**Author:** ![HU\_Jun](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hu_jun/32/37186_2.png) [@HU\_Jun](https://discuss.elastic.co/u/HU_Jun)\
**Post date:** [November 8, 2018, 9:01pm UTC](https://discuss.elastic.co/t/painless-flatten/155582/2 "2018-11-08T21:01:41Z")

</div>

Finally, found a solution based on flatMap and map

aggs.stream().flatMap(a -\> a.stream().flatMap( b-\>b.stream().map( c-\> -\> ['A': a.key, 'B': b.key, 'C': c.key] ) ) ).collect(Collectors.toList())

---

<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:** [December 6, 2018, 9:01pm UTC](https://discuss.elastic.co/t/painless-flatten/155582/3 "2018-12-06T21:01:53Z")

</div>

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