# How to rank bucket sort results?

**URL:** <https://discuss.elastic.co/t/how-to-rank-bucket-sort-results/222061>\
**Category:** Elasticsearch\
**Created:** [March 4, 2020, 11:24am UTC](https://discuss.elastic.co/t/how-to-rank-bucket-sort-results/222061 "2020-03-04T11:24:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![farazirfan](https://avatars.discourse-cdn.com/v4/letter/f/b9bd4f/32.png) [@farazirfan](https://discuss.elastic.co/u/farazirfan)\
**Post date:** [March 4, 2020, 11:24am UTC](https://discuss.elastic.co/t/how-to-rank-bucket-sort-results/222061/1 "2020-03-04T11:24:15Z")

</div>

I am creating a leaderboard query in elastic search, I have documents with parent-child relationship,  
Parent Doc: Player  
Child Doc: Team  
A player can be in multiple teams.

I am calculating total score for each team and sorting the results using bucket sort, and I am getting correct sorted results.

i want to get a rank of each team in sorted results, for example in a 1000 teams totals results in each bucket value i should get a key something like Rank: 1 which is a index of that value in 1000 sorted results;

Here is my current attempt:

`{ "size": 0, "query": { "bool": { "must": [{ "term": { "type": "contest_teams" } }, { "term": { "challenge_id": 212 } }] } }, "aggs": { "team": { "terms": { "field": "user_id" }, "aggs": { "team_score": { "parent": { "type": "team_player" }, "aggs": { "total_points": { "sum": { "field": "points" } } } }, "team_points_bucket_sort": { "bucket_sort": { "sort": [{ "team_score.total_points": { "order": "desc" } }], "size": 2, "from": 0 } } } } } }`

---

<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:** [April 1, 2020, 11:24am UTC](https://discuss.elastic.co/t/how-to-rank-bucket-sort-results/222061/2 "2020-04-01T11:24:23Z")

</div>

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