Parent/Child Filter documents by using aggregation results

Hi,
I am new using ES and I am currently working on an interesting usage for ES, here a little of context: In my company we have two main business objects and they have a parent-child relationship. There are many children for each parent and the children are updated frequently, so we wanted to model our documents as parent/child, this using ElasticSearch 6.2.2.
The parent and child documents have many attributes, but for the use case we just need the price of the children and return all the attributes of the parent.

This is what we need to achieve: Get all the parent documents where the minimum price and maximum price of their children fall in a certain range.

I manage to perform the aggregations on the children and get the price ranges correctly, but I don't know how to filter the documents by using the aggregation results.

This is an example query for getting the prices of the children per parent:
{
"query": {
"bool": {
"must": [
{
"has_child": {
"type": "childrenType",
"query": {
"match_all": {}
}
}
}
]
}
},
"aggs": {
"parent_agg": {
"terms": {
"field" : "Id"
},
"aggs": {
"price_ranges": {
"children": {
"type": "childrenType"
},
"aggs": {
"max_price": {
"max": {
"field": "price"
}
},
"min_price": {
"min": {
"field": "price"
}
}
}
}
}
}
}
}

Is it possible to get the data we need for this use case using Elastic? Thanks in advance for all your feedback. Please let me know if you need more information.

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