Unable to get the position of a particular key in sorted bucket aggregation result

<GET index/_search
{
"query": {
"bool": {
"must": [
{
"terms": {
"myIds": [
"301",
"302",
"303"
]
}
}
]
}
},
"aggs": {
"orders_over_time": {
"date_histogram": {
"field": "order_date",
"interval": "month",
"keyed": true
},
"aggs": {
"total_sales": {
"sum": {
"field": "orderCount"
}
},
"orders": {
"terms": {
"field": "orderId",
"order": {
"sum_order_count": "asc"
}
},
"aggs": {
"sum_order_count": {
"sum": {
"field": "orderCount"
}
}
}
}
}
}
}
}/>

Current result:
<`"aggregations": {
"orders_over_time": {
"buckets": {
"2018-02-01T00:00:00.000Z": {
"key_as_string": "2018-02-01T00:00:00.000Z",
"key": 1517443200000,
"doc_count": 13,
"orders": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "301",
"doc_count": 4,
"sum_order_count": {
"value": 20
}
},
{
"key": "302",
"doc_count": 4,
"sum_order_count": {
"value": 20
}
},
{
"key": "303",
"doc_count": 5,
"sum_order_count": {
"value": 25
}
}
]
},
"total_sales": {
"value": 65
}
}
}
}
}
/>

Expected result:
<`"aggregations": {
"orders_over_time": {
"buckets": {
"2018-02-01T00:00:00.000Z": {
"key_as_string": "2018-02-01T00:00:00.000Z",
"key": 1517443200000,
"doc_count": 13,
"orders": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "301",
"doc_count": 4,
"rank":1,
"sum_order_count": {
"value": 20
}
},
{
"key": "302",
"doc_count": 4,
"rank":2,
"sum_order_count": {
"value": 20
}
},
{
"key": "303",
"doc_count": 5,
"rank":3,
"sum_order_count": {
"value": 25
}
}
]
},
"total_sales": {
"value": 65
}
}
}
}
}
/>

I want to get the position of only one key like 302 i.e. 2.

I don't think this is currently possible today, you'll just have to do the processing client side to find the position.

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