# Top hit aggregation with \_score sorting

**URL:** <https://discuss.elastic.co/t/top-hit-aggregation-with--score-sorting/38645>\
**Category:** Elasticsearch\
**Created:** [January 7, 2016, 4:13pm UTC](https://discuss.elastic.co/t/top-hit-aggregation-with--score-sorting/38645 "2016-01-07T16:13:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tom\_sharp](https://avatars.discourse-cdn.com/v4/letter/t/5daacb/32.png) [@tom\_sharp](https://discuss.elastic.co/u/tom_sharp)\
**Post date:** [January 7, 2016, 4:13pm UTC](https://discuss.elastic.co/t/top-hit-aggregation-with--score-sorting/38645/1 "2016-01-07T16:13:13Z")

</div>

Hi,

Is there a way to do a top hit aggregation ordering the buckets by max document score?

I saw you can do it using the script sorting ie: [http://stackoverflow.com/questions/26213271/elasticsearch-aggregation-order-by-top-hit-score](http://stackoverflow.com/questions/26213271/elasticsearch-aggregation-order-by-top-hit-score)

But I don't have access to scripting for some reason.. Any clue ?

---

<div class="post-metadata">

**Author:** ![polya](https://avatars.discourse-cdn.com/v4/letter/p/94ad74/32.png) [@polya](https://discuss.elastic.co/u/polya)\
**Post date:** [January 8, 2016, 12:00am UTC](https://discuss.elastic.co/t/top-hit-aggregation-with--score-sorting/38645/2 "2016-01-08T00:00:21Z")

</div>

I don't see an exact example and hence unable to give the right DSL but does the following construct work:

{  
"query":{  
"bool":{  
"should":[

```
     ]
  }

```

},  
"aggs":{  
"group\_by\_score: {  
"terms":{  
"field":"search\_field"  
},  
"aggs":{  
"score":{  
"max":{  
"field":"\_score"  
}  
}  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![tom\_sharp](https://avatars.discourse-cdn.com/v4/letter/t/5daacb/32.png) [@tom\_sharp](https://discuss.elastic.co/u/tom_sharp)\
**Post date:** [January 8, 2016, 7:54am UTC](https://discuss.elastic.co/t/top-hit-aggregation-with--score-sorting/38645/3 "2016-01-08T07:54:45Z")

</div>

Hi polya,

No it doesn't work. In the response it says "sort': [None]" - I assume it is because \_score is not really a field. I launched a new instance and this seems to work as I want but uses scripts.

[![](https://dl.dropboxusercontent.com/s/hu3uyzvigtlvghc/Screenshot%202016-01-08%2007.54.18.png?dl=0) ](https://dl.dropboxusercontent.com/s/hu3uyzvigtlvghc/Screenshot%202016-01-08%2007.54.18.png?dl=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:** [July 5, 2017, 11:26pm UTC](https://discuss.elastic.co/t/top-hit-aggregation-with--score-sorting/38645/4 "2017-07-05T23:26:08Z")

</div>


