title: ElasticSearch use aggregation over another aggregation result
body: <p>Having an elasticsearch aggregation result, I want to collect all duplicated values and to do a count on them.</p>
  
  <p><strong>The desired result should be something similar to this:</strong></p>
  
  <pre><code>{
    [
      {"[root, A, A.3, A.3.1, A.3.1.1]": 1}, --&gt; 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}
    ]
  }
  </code></pre>
  
  <p><strong>My current query:</strong></p>
  
  <pre><code>{
      "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()"
                              }
                          }
                      }
                  }
              }
          }
      }
  } 
  </code></pre>
  
  <p><strong>The current result:</strong></p>
  
  <pre><code>{    
      ...
      ...
      "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]"
                                  }
                              }
                          ]
                      }
                  }
              ]
          }
      }
  }
  </code></pre>
  
  <p>In case you want to see the mapping and sample document please check my other <a href="https://stackoverflow.com/questions/60650823/elasticsearch-mapping-the-result-of-collapse-do-operations-on-a-grouped-docume">post</a>.</p>
  
  <p>Any hint is more than welcome!</p>