I need to make an aggregation per category on the following example that I've taken from a similar issue
{
"category": 1,
"name": "apple",
"price": 3,
},
{
"category": 2,
"name": "orange",
"price": 2,
},
{
"category": 3,
"name": "tomato",
"price": 1,
}
I've tried to do an aggregation per category, adding the price for all documents, using this aggregation :
"aggs": {
"1": {
"global": {
},
"aggs": {
"2": {
"terms": {
"field": category,
"size": 10000
},
"aggs": {
"3": {
"sum" : {
"field" : "price"
}
}
}
}
}
}
}
I expected something like this (for 3 documents having the same information):
{
"category": 1,
"sum": 9,
},
{
"category": 2,
"sum": 6,
},
{
"category": 3,
"sum": 3,
}
But instead it's adding all the values in all the documents. The value 18 it's (3+2+1) * 3. What is wrong with the query I'm executing?
"buckets" : [
{
"key" : 1,
"doc_count" : 3,
"3" : {
"value" : 18.0
}
},
{
"key" : 2,
"doc_count" : 3,
"3" : {
"value" : 18.0
}
},
{
"key" : 3,
"doc_count" : 3,
"3" : {
"value" : 18.0
}