Aggregate the results of all sub aggregations

Hello everyone,

Here is what I would like to do. I have an index with several
documents in Elasticsearch. In every document I have two field:
deviceField (name of the device) and pressionField (the value of the
pression periodically). I want to query in my index the average pression
per device and to do some operations on it. Then, i want to sum the result for all device.
I managed to do the operations on every device (code below) but not for the aggregation of the results of all sub-aggregation.

curl -XGET 'localhost:9200/mydata/_search?pretty' -d '
{
"aggs" : {
"devices" : {
"terms" : {
"field" : "deviceField"
},
"aggs" : {
"average_pression" : {
"sum" : {
"field" : "pressionField"
}
},
"count_frames" : {
"value_count" : {
"field" : "deviceField"
}
},
"per_percentage": {
"bucket_script": {
"buckets_path": {
"countFrames": "count_frames",
"average_pression": "average_pression"
},
"script": "average_pression*countFrames"
}
}
}
}
}
}'

Thank you very much for your attention and your help.

S

I finally found the answer.

here is the code
curl -XGET 'localhost:9200/mydata/_search?pretty' -d '
{
"aggs" : {
"devices" : {
"terms" : {
"field" : "deviceField"
},
"aggs" : {
"average_pression" : {
"sum" : {
"field" : "pressionField"
}
},
"count_frames" : {
"value_count" : {
"field" : "deviceField"
}
},
"per_percentage": {
"bucket_script": {
"buckets_path": {
"countFrames": "count_frames",
"average_pression": "average_pression"
},
"script": "average_pression*countFrames"
},
"avg_per_all_devices":{
"avg_bucket":{
"buckets_path":"devices>per_percentage"
}
}
}
}
}
}
}'

Good luck !
S

1 Like