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

**URL:** <https://discuss.elastic.co/t/unable-to-get-the-position-of-a-particular-key-in-sorted-bucket-aggregation-result/131033>\
**Category:** Elasticsearch\
**Created:** [May 8, 2018, 3:28pm UTC](https://discuss.elastic.co/t/unable-to-get-the-position-of-a-particular-key-in-sorted-bucket-aggregation-result/131033 "2018-05-08T15:28:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![garima\_khaneja](https://avatars.discourse-cdn.com/v4/letter/g/90ced4/32.png) [@garima\_khaneja](https://discuss.elastic.co/u/garima_khaneja)\
**Post date:** [May 8, 2018, 3:28pm UTC](https://discuss.elastic.co/t/unable-to-get-the-position-of-a-particular-key-in-sorted-bucket-aggregation-result/131033/1 "2018-05-08T15:28:58Z")

</div>

\<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.

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [May 8, 2018, 4:00pm UTC](https://discuss.elastic.co/t/unable-to-get-the-position-of-a-particular-key-in-sorted-bucket-aggregation-result/131033/2 "2018-05-08T16:00:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 5, 2018, 4:00pm UTC](https://discuss.elastic.co/t/unable-to-get-the-position-of-a-particular-key-in-sorted-bucket-aggregation-result/131033/3 "2018-06-05T16:00:15Z")

</div>

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