# Bucket sort may be wrong

**URL:** <https://discuss.elastic.co/t/bucket-sort-may-be-wrong/307745>\
**Category:** Elasticsearch\
**Created:** [June 21, 2022, 9:35am UTC](https://discuss.elastic.co/t/bucket-sort-may-be-wrong/307745 "2022-06-21T09:35:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![NathBabs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nathbabs/32/105378_2.png) [@NathBabs](https://discuss.elastic.co/u/NathBabs)\
**Post date:** [June 21, 2022, 9:35am UTC](https://discuss.elastic.co/t/bucket-sort-may-be-wrong/307745/1 "2022-06-21T09:35:14Z")

</div>

I am trying to use bucket\_sort, but i am not sure if it's working correctly.

```auto
		"aggs": {
			"group_by_phone": {
				"terms": {
					"field": "phoneNumber.keyword"
				},
				"aggs": {
					"totalTnxAmount": {
						"sum": {
							"field": "amount"
						}
					},
					"sales_bucket_sort": {
						"bucket_sort": {
							"sort": [
								{
									"totalTnxAmount": {
										"order": "desc"
									}
								}
							]
						}
					},
					"aggs": {
						"top_hits": {
							"_source": [
								"firstName",
								"lastName"
							],
							"size": 1
						}
					}
				}
			}
		}

```

Please can anyone tell me if anything is wrong with this aggregation

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [June 21, 2022, 12:38pm UTC](https://discuss.elastic.co/t/bucket-sort-may-be-wrong/307745/2 "2022-06-21T12:38:49Z")

</div>

Hi @NathBabs .  
What's the problem with the query? Doesn't return the results it expects?

---

<div class="post-metadata">

**Author:** ![NathBabs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nathbabs/32/105378_2.png) [@NathBabs](https://discuss.elastic.co/u/NathBabs)\
**Post date:** [June 21, 2022, 1:39pm UTC](https://discuss.elastic.co/t/bucket-sort-may-be-wrong/307745/3 "2022-06-21T13:39:35Z")

</div>

@RabBit_BR Apparently it should sort based on total transactions over a period of time , Top performance - descending or Bottom performance - ascending.  
When testing it, I have just little data to test with and it seems to work.  
But when deployed to production, I have been getting complaints that the bottom performing list is just a reverse of the top performing list. So it's actually not even showing the bottom performance, but yet i don't seem to see anything wrong with my query if I change the order to either any one of "desc" or "asc"

---

<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:** [July 19, 2022, 1:39pm UTC](https://discuss.elastic.co/t/bucket-sort-may-be-wrong/307745/4 "2022-07-19T13:39:46Z")

</div>

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