HI, is it possible to filter and sort the docs returned from top hits aggregation?
lets say I have a simple product index for docs like this:
{
"product_name": "some_product",
"category": "some_cotegory",
"price": "200"
"sold_times": "5",
"store": "store1"
}
and I want to get the most expensive products in their category and per store that have been sold less than 3 times and I want them to be ordered by store, category and price.
I can use two terms aggregations and top hits aggregation (with size=1) to get the most expensive products in their category per store, but how can I sort and filter these top hits results? I really need to filter the results after the top hits agg is performed, so the filter query is not the solution. How can I do this? Thx
EDIT:
the query to get the most expensive products in their directory per store is as follows:
POST /products/_search?size=0
{
"aggs": {
"by_store": {
"terms": {
"field": "store"
},
"aggs": {
"by_category": {
"terms": {
"field": "category"
},
"aggs": {
"most_expensive": {
"top_hits": {
"sort": [
{
"price": {
"order": "desc"
}
}
],
"size" : 1
}
}
}
}
}
}
}
}
This will return docs (as part of aggregations results) and I want to filter them by sold_times < 3 and sort them by store and category and price. So lets say that the most expensive product in category "accessories" is "charger" but this product was sold 5 times, so this most expensive product must be filtered out of the results.