Hi guys,
I have a question on how can I use aggregation filter from a top level aggregation using lower level aggregation result field ?
My data:
[ { id: 1, product_id: 1, price: 6 }, { id: 2, product_id: 2, price: 5 }, { id: 3, product_id: 1, price: 4 }, { id: 4, product_id: 2, price: 8 }, { id: 5, product_id: 3, price: 7 } ]
Query I would like to run:
{
"size": 0,
"aggs": {
"products": {
"filter": {
"range": {
"price_sum": { //Using lower level aggregation filed
"gte" : 10,
"lte" : 20
}
}
},
"aggs": {
"products_ids": {
"terms": {
"field": "product_id",
"size": 0
},
"aggs": {
"price_sum": {
"sum": {
"script": "doc['price'].value.toInteger()"
}
}
}
}
}
}
}
}
Result expected:
product_ids: [
{ key: 1, price_sum: 10 },
{ key: 2, price_sum: 13 }
]