How can I create an combined aggregation in Elasticsearch using multiple nested objects?
What I want to achieve is the SUM of quantity grouped by country in a mapping that looks like this
curl -XPUT 'localhost:9200' -d '{
"index": "test",
"type": "orders",
"body": {
"orders": {
"order_number": {
"type": "string",
"index": "not_analyzed"
},
"buyer": {
"type": "nested",
"properties": {
"country": {
"type": "string",
"index": "not_analyzed"
}
}
},
"products": {
"type": "nested",
"properties": {
"name": {
"type": "string",
"index": "not_analyzed"
},
"quantity": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}'
Using this test data
curl -XPOST 'localhost:9200/_bulk' -d '
{"index": {"_index": "test", "_type": "orders", "_id": 1}}
{"order_number": "1", "buyer": {"country": "US"}, "products": [{"name": "A product", "quantity": 10}]}
{"index": {"_index": "test", "_type": "orders", "_id": 2}}
{"order_number": "2", "buyer": {"country": "DE"}, "products": [{"name": "A product", "quantity": 10}]}
{"index": {"_index": "test", "_type": "orders", "_id": 3}}
{"order_number": "3", "buyer": {"country": "US"}, "products": [{"name": "A product", "quantity": 10}]}
'
The result should be that there was sold 10 "A product" in DE and 20 in US. How can I create this aggregation when the properties in question are on separate nested objects?