We have elastic search index with documents having the id and price of the stocks.
We want to build a system where the user gives stock id as the input and it gets back the rank of that document. I have two queries that can solve this:
Get the price of that id and then use percentile rank aggreagation like here. This returns percentile value i.e.
{ "size": 0, "aggs" : { "load_time_ranks" : { "percentile_ranks" : { "field" : "price", "values" : <price_of_given_id> } } } }
-
2.
Get the price of that id, filter car greater or equal to that price and then use value count aggreagation like here. This returns the exact rank i.e.
{
"size": 0,
"aggs": {
"sort_score_aggs": {
"filter": {
"range": {
"price": {
"gte": <price_of_given_id>
}
}
},
"aggs": {
"rank_count": {
"value_count": {
"field": "price"
}
}
}
}
}
}
In both above approaches, we have to make two elastic search request i.e. to fetch the price of that document id first and then query to get percentile or the rank. Is there any way to get rank in one query OR any better way to solve this problem?