# ElasticSearch use aggregation over another aggregation result

**URL:** <https://discuss.elastic.co/t/elasticsearch-use-aggregation-over-another-aggregation-result/223579>\
**Category:** Elasticsearch\
**Created:** [March 13, 2020, 9:15pm UTC](https://discuss.elastic.co/t/elasticsearch-use-aggregation-over-another-aggregation-result/223579 "2020-03-13T21:15:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Linu\_Radu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/linu_radu/32/64098_2.png) [@Linu\_Radu](https://discuss.elastic.co/u/Linu_Radu)\
**Post date:** [March 13, 2020, 9:15pm UTC](https://discuss.elastic.co/t/elasticsearch-use-aggregation-over-another-aggregation-result/223579/1 "2020-03-13T21:15:11Z")

</div>

Having an elasticsearch aggregation result, I want to collect all duplicated values and to do a count on them.

**The desired result should be something similar to this:**

```auto
{
  [
    {"[root, A, A.3, A.3.1, A.3.1.1]": 1}, --> key and count
    {"[root, B, B.2, B.2.1, B.2.1.1]": 1}, 
    {"[root, A, A.2, A.2.1, A.2.1.2]": 1}, 
    {"[root, A, A.3, A.3.1, A.3.1.2]": 1}
  ]
}

```

**My current query:**

```auto
{
    "size": 0,
    "aggs": {
        "conversationsPerYear": {
            "date_histogram" : {
                "field" : "@timestamp",
                "interval" : "1y"
            },
            "aggs": {
                "conversationsAgg": {
                    "terms": {
                        "field": "context.conversationId"
                    },
                    "aggs": {
                        "intentPathMetricAgg": {
                            "scripted_metric": {
                                "init_script": "state.intentsMap = new TreeMap();",
                                "map_script": "state.intentsMap.put(doc['@timestamp'].value.toString(), doc['context.action'].value);",  
                                "combine_script": "return state",
                                "reduce_script": "SortedMap intentsMap = new TreeMap(); for (state in states) { intentsMap.putAll(state.intentsMap) } /* Next will reduce to one element, every duplicated sequence */ Iterator iterator = intentsMap.values().iterator(); String previousValue = null; while(iterator.hasNext()) { String currentValue = iterator.next(); if(currentValue.equals(previousValue)){ iterator.remove(); } previousValue = currentValue; } return intentsMap.values().toString()"
                            }
                        }
                    }
                }
            }
        }
    }
} 

```

**The current result:**

```auto
{    
    ...
    ...
    "aggregations": {
        "conversationsPerYear": {
            "buckets": [
                {
                    "key_as_string": "1577836800000",
                    "key": 1577836800000,
                    "doc_count": 38,
                    "conversationsAgg": {
                        "doc_count_error_upper_bound": 0,
                        "sum_other_doc_count": 0,
                        "buckets": [
                            {
                                "key": "conversation_1584025560104",
                                "doc_count": 13,
                                "intentPathMetricAgg": {
                                    "value": "[root, A, A.3, A.3.1, A.3.1.1]"
                                }
                            },
                            {
                                "key": "conversation_1584025607802",
                                "doc_count": 12,
                                "intentPathMetricAgg": {
                                    "value": "[root, B, B.2, B.2.1, B.2.1.1]"
                                }
                            },
                            {
                                "key": "conversation_1584025340698",
                                "doc_count": 7,
                                "intentPathMetricAgg": {
                                    "value": "[root, A, A.2, A.2.1, A.2.1.2]"
                                }
                            },
                            {
                                "key": "conversation_1584025600464",
                                "doc_count": 6,
                                "intentPathMetricAgg": {
                                    "value": "[root, A, A.3, A.3.1, A.3.1.2]"
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}

```

In case you want to see the mapping and sample document please check my other [post](https://stackoverflow.com/questions/60650823).

---

<div class="post-metadata">

**Author:** ![Linu\_Radu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/linu_radu/32/64098_2.png) [@Linu\_Radu](https://discuss.elastic.co/u/Linu_Radu)\
**Post date:** [March 17, 2020, 10:58am UTC](https://discuss.elastic.co/t/elasticsearch-use-aggregation-over-another-aggregation-result/223579/2 "2020-03-17T10:58:55Z")

</div>

I resolved the issue and here is the answer: [https://stackoverflow.com/questions/60662222](https://stackoverflow.com/questions/60662222)

---

<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 14, 2020, 10:59am UTC](https://discuss.elastic.co/t/elasticsearch-use-aggregation-over-another-aggregation-result/223579/3 "2020-04-14T10:59:03Z")

</div>

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