# Order the bucket by max score of document in Aggregations V1.0.RC2

**URL:** https://discuss.elastic.co/t/order-the-bucket-by-max-score-of-document-in-aggregations-v1-0-rc2/15652
**Category:** Elasticsearch
**Created:** [February 7, 2014, 1:57am UTC](https://discuss.elastic.co/t/order-the-bucket-by-max-score-of-document-in-aggregations-v1-0-rc2/15652 "2014-02-07T01:57:01Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![toanluu](https://avatars.discourse-cdn.com/v4/letter/t/ad7895/32.png) [@toanluu](https://discuss.elastic.co/u/toanluu)
#### Post date: [February 7, 2014, 1:57am UTC](https://discuss.elastic.co/t/order-the-bucket-by-max-score-of-document-in-aggregations-v1-0-rc2/15652/1 "2014-02-07T01:57:01Z")

</div>

Hi all,

Have you ever tried to order the bucket by field \_score of documents given  
a query?

I took example from [http://blog.qbox.io/elasticsearch-aggregations](http://blog.qbox.io/elasticsearch-aggregations) and make  
the query:  
curl -XPOST "[http://localhost:9200/sports/athlete/\_search](http://localhost:9200/sports/athlete/_search)" -d '{  
"query" : {  
"match" : { "sport" : "baseball" }  
},

"size": 0,  
"aggs": {  
"sport": {  
"terms": {  
"field": "sport",  
"size": 5,  
"order" : { "max\_score" : "desc" }  
},  
"aggs" : {  
"max\_score" : { "max" : { "field" : "\_score" } }  
}  
}  
}  
}'

However in the response i see:

```
"aggregations": {
    "sport": {
        "buckets": [{
            "key": "baseball",
            "doc_count": 16,
            "max_score": {
                "value": null
            }
        }]
    }
}

```

I know that the 1st document in result list has score 2.098612 for that  
query so why max\_score is null in aggregations?

Thanks for your help.  
Toan.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAPUM1c21LCuLaveBXS38kd3fSmw-PENKgSqu9z3fQmd5L2C6JQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAPUM1c21LCuLaveBXS38kd3fSmw-PENKgSqu9z3fQmd5L2C6JQ%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)
#### Post date: [February 7, 2014, 1:59pm UTC](https://discuss.elastic.co/t/order-the-bucket-by-max-score-of-document-in-aggregations-v1-0-rc2/15652/2 "2014-02-07T13:59:56Z")

</div>

Hi,

The score is not a field of a document but is computed dynamically, you  
need to use a script to get it. So replacing `{ "field" : "_score" }` with  
`{ "script" : "_doc.score" }` should have the desired effect.

On Fri, Feb 7, 2014 at 2:57 AM, Toan V Luu [luuvinhtoan@gmail.com](mailto:luuvinhtoan@gmail.com) wrote:

> Hi all,
> 
> Have you ever tried to order the bucket by field \_score of documents  
> given a query?
> 
> I took example from [http://blog.qbox.io/elasticsearch-aggregations](http://blog.qbox.io/elasticsearch-aggregations) and  
> make the query:  
> curl -XPOST "[http://localhost:9200/sports/athlete/\_search](http://localhost:9200/sports/athlete/_search)" -d '{  
> "query" : {  
> "match" : { "sport" : "baseball" }  
> },
> 
> "size": 0,  
> "aggs": {  
> "sport": {  
> "terms": {  
> "field": "sport",  
> "size": 5,  
> "order" : { "max\_score" : "desc" }  
> },  
> "aggs" : {  
> "max\_score" : { "max" : { "field" : "\_score" } }  
> }  
> }  
> }  
> }'
> 
> However in the response i see:
> 
> ```
> "aggregations": {
> "sport": {
> "buckets": [{
> "key": "baseball",
> "doc_count": 16,
> "max_score": {
> "value": null
> }
> }]
> }
> }
> 
> ```
> 
> I know that the 1st document in result list has score 2.098612 for that  
> query so why max\_score is null in aggregations?
> 
> Thanks for your help.  
> Toan.
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/CAPUM1c21LCuLaveBXS38kd3fSmw-PENKgSqu9z3fQmd5L2C6JQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAPUM1c21LCuLaveBXS38kd3fSmw-PENKgSqu9z3fQmd5L2C6JQ%40mail.gmail.com)  
> .  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
Adrien Grand

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j5nZoO5L81LJq5MKsTSynZaSZCsv68oPGvAg3GA6\_8%2B3g%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j5nZoO5L81LJq5MKsTSynZaSZCsv68oPGvAg3GA6_8%2B3g%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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 6, 2017, 1:51am UTC](https://discuss.elastic.co/t/order-the-bucket-by-max-score-of-document-in-aggregations-v1-0-rc2/15652/3 "2017-07-06T01:51:43Z")

</div>


