How to aggregate sub documents

I have sub documents in the main document , below is the format:
"activities:[
{
"code": "CODE1",
"price":20
},
{
"code": "CODE2",
"price":24
}
]

I want to sum the price group by code. However, the term aggregates query give result for CODE1:44 instead 20. because the sum aggs is sum up the price field for all inner documents.

Any help will be appreciated.

May be use nested type for nested documents?
Have a look at Nested field type | Elasticsearch Guide [7.15] | Elastic

1 Like

Thanks @dadoonet . nested aggregation is the solution. Because the nested aggregation treats the sub documents as main document. This may be the reason.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.