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:
{
[
{"[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:
{
"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:
{
...
...
"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.