# Unable to order by top hits results

**URL:** https://discuss.elastic.co/t/unable-to-order-by-top-hits-results/216530
**Category:** Kibana
**Created:** [January 25, 2020, 4:44pm UTC](https://discuss.elastic.co/t/unable-to-order-by-top-hits-results/216530 "2020-01-25T16:44:40Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![tomeri](https://avatars.discourse-cdn.com/v4/letter/t/71c47a/32.png) [@tomeri](https://discuss.elastic.co/u/tomeri)
#### Post date: [January 25, 2020, 4:44pm UTC](https://discuss.elastic.co/t/unable-to-order-by-top-hits-results/216530/1 "2020-01-25T16:44:40Z")

</div>

Hi guys,  
I'm trying to create a pie chart that will show the most recent docs using the Top Hit aggregation, then aggregate them by some field and sort them by the results of the Top Hit. Here's what I did:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/6/2/62d89083b353e83919bf85945da6f9a9777d03a2.png)

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/3/d/3d379be3f974364f44a335440eda70182ef51489.png)

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/2/d/2d77d5b75378864164259c1d65dbae0ffea92cd4.png)

As you can see above, the sort is not correct, this is because i sort by the max value of the **totalSoreSize** field, over the last 7d, but I was expecting to have it sorted based on the returned results from the Top Hit.

Is there any way to make this work?

---

<div class="post-metadata">

### Author: ![Marius\_Dragomir](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marius_dragomir/32/42087_2.png) [@Marius\_Dragomir](https://discuss.elastic.co/u/Marius_Dragomir)
#### Post date: [January 28, 2020, 3:53pm UTC](https://discuss.elastic.co/t/unable-to-order-by-top-hits-results/216530/2 "2020-01-28T15:53:14Z")

</div>

I think the sort is done by the last Split aggregation that you have added. Can you try and switch them around to see if it works?

---

<div class="post-metadata">

### Author: ![tomeri](https://avatars.discourse-cdn.com/v4/letter/t/71c47a/32.png) [@tomeri](https://discuss.elastic.co/u/tomeri)
#### Post date: [January 29, 2020, 10:54am UTC](https://discuss.elastic.co/t/unable-to-order-by-top-hits-results/216530/3 "2020-01-29T10:54:15Z")

</div>

I use only 1 split (Terms), the other one is just the metric based on Top hits, i cannot switch between them

---

<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: [February 26, 2020, 10:54am UTC](https://discuss.elastic.co/t/unable-to-order-by-top-hits-results/216530/4 "2020-02-26T10:54:18Z")

</div>

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