Problem:
Do aggregations over nested objects.
Solution Summary:
Prerequisites:
Setup department mapping and then add department documents as described here. The "employees" is a nested array within department.
Solution Steps:
Case 1 - Find average employee age across departments
GET department/_search
{
"aggs": {
"employees": {
"nested": {
"path": "employees"
},
"aggs": {
"average_age": {
"avg": {
"field": "employees.age"
}
}
}
}
},
"size": 0
}
Response contains:
...
"hits": {
"total": 2,
"max_score": 0,
"hits": []
},
"aggregations": {
"employees": {
"doc_count": 4,
"average_age": {
"value": 31.75
}
}
}
}
Recent comments