Hi, William.
Thanks for your reply.
Here are the calculations I'm doing:
GET my_index_0.0.22/_search
{
"size": 0,
"aggs": {
"Total number of each component": {
"scripted_metric": {
"init_script": "state.counts = [:]",
"map_script": """
for (component in params._source.slowest_components_total) {
state.counts[component] = state.counts.containsKey(component) ? state.counts[component] + 1 : 1;
}
""",
"combine_script": "return state.counts",
"reduce_script": """
Map finalCounts = [:];
for (state in states) {
for (entry in state.entrySet()) {
finalCounts[entry.getKey()] = finalCounts.containsKey(entry.getKey()) ?
finalCounts[entry.getKey()] + entry.getValue() : entry.getValue();
}
}
return finalCounts;
"""
}
}
}
}
A sample document:
{
"_index": "my_index_0.0.22",
"_type": "_doc",
"_id": "Mf-8W5UBwBI085POA_tP",
"_version": 1,
"_score": 1,
"_source": {
"dvideo_quality": "100",
"feature": "ABC",
"start_timestamp": "1665709037.75",
"scenario_duration": 4.28,
"query_start": "2022-10-14T02:57:17.750000Z",
"query_stop": "2022-10-14T02:57:22.030000Z",
"init_start": "2025-03-03T11:17:37.583979Z",
"init_stop": "2025-03-03T11:17:58.118608Z",
"buffer_start": "2025-03-03T11:18:11.110742Z",
"buffer_stop": "2025-03-03T11:20:02.990229Z",
"init_duration": 20.534629,
"buffer_duration": 111.879487,
"slowest_components_total": [
"AAA",
"AAA",
"AAA",
"AAA",
"BBB",
"BBB"
],
"id": "e506d477-f138-4553-9b63-dc8a95d65b54",
"timestamp": "2025-03-03T11:20:02.997252Z",
"total_files_size": 113.30000000000001
},
"fields": {
"start_timestamp": [
"1665709037.75"
],
"event_type": [
"event"
],
"dvideo_quality": [
"100"
],
"id": [
"e506d477-f138-4553-9b63-dc8a95d65b54"
],
"node": [
"105"
],
"cpu_usage": [
0.028979167
],
"timestamp": [
"2025-03-03T11:20:02.997Z"
],
"dvideo_quality.keyword": [
"100"
]
}
}
I simply want to count the number of occurrences of each component under slowest_componens_total
, so I need an end count as "AAA":4, "BBB":2
.
Any help would be great. Thanks a lot 