# Sorting facets on score

**URL:** <https://discuss.elastic.co/t/sorting-facets-on-score/4445>\
**Category:** Elasticsearch\
**Created:** [May 20, 2011, 12:38pm UTC](https://discuss.elastic.co/t/sorting-facets-on-score/4445 "2011-05-20T12:38:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yannick\_Smits\_2](https://avatars.discourse-cdn.com/v4/letter/y/a4c791/32.png) [@Yannick\_Smits\_2](https://discuss.elastic.co/u/Yannick_Smits_2)\
**Post date:** [May 20, 2011, 12:38pm UTC](https://discuss.elastic.co/t/sorting-facets-on-score/4445/1 "2011-05-20T12:38:28Z")

</div>

Instead of sorting facets by document count is it possible to sort facets by  
document score?

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [May 20, 2011, 2:59pm UTC](https://discuss.elastic.co/t/sorting-facets-on-score/4445/2 "2011-05-20T14:59:44Z")

</div>

You can use terms stats, and provide a value\_script with "doc.score".  
On Friday, May 20, 2011 at 3:38 PM, Yannick Smits wrote:

> Instead of sorting facets by document count is it possible to sort facets by document score?

---

<div class="post-metadata">

**Author:** ![Yannick\_Smits\_2](https://avatars.discourse-cdn.com/v4/letter/y/a4c791/32.png) [@Yannick\_Smits\_2](https://discuss.elastic.co/u/Yannick_Smits_2)\
**Post date:** [May 26, 2011, 8:58am UTC](https://discuss.elastic.co/t/sorting-facets-on-score/4445/3 "2011-05-26T08:58:48Z")

</div>

Thanks, got it to work with this syntax:

"facets" : {

"tag\_book\_stats" : {

"terms\_stats" : {

"key\_field" : "book.name",

"value\_script"" : "doc.score"

}

}

}

But it shows me the score and the count now but still sorts on count instead of score.

From: Shay Banon [[mailto:shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)]  
Sent: vrijdag 20 mei 2011 17:00  
To: [users@elasticsearch.com](mailto:users@elasticsearch.com)  
Subject: Re: sorting facets on score

You can use terms stats, and provide a value\_script with "doc.score".

On Friday, May 20, 2011 at 3:38 PM, Yannick Smits wrote:

Instead of sorting facets by document count is it possible to sort facets by document score?

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [May 26, 2011, 10:12am UTC](https://discuss.elastic.co/t/sorting-facets-on-score/4445/4 "2011-05-26T10:12:39Z")

</div>

Check the ordering option in terms stats: [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/terms-stats-facet.html).

On Thursday, May 26, 2011 at 11:58 AM, Yannick Smits wrote:

> Thanks, got it to work with this syntax:
> 
> "facets" : {
> 
> "tag\_book\_stats" : {
> 
> "terms\_stats" : {
> 
> "key\_field" : "book.name ([http://book.name](http://book.name))",
> 
> "value\_script"" : "doc.score"
> 
> }
> 
> }
> 
> }
> 
> But it shows me the score and the count now but still sorts on count instead of score.
> 
> From: Shay Banon [[mailto:shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)]  
> Sent: vrijdag 20 mei 2011 17:00  
> To: [users@elasticsearch.com](mailto:users@elasticsearch.com) ([mailto:users@elasticsearch.com](mailto:users@elasticsearch.com))  
> Subject: Re: sorting facets on score
> 
> You can use terms stats, and provide a value\_script with "doc.score".  
> On Friday, May 20, 2011 at 3:38 PM, Yannick Smits wrote:
> 
> > Instead of sorting facets by document count is it possible to sort facets by document score?

---

<div class="post-metadata">

**Author:** ![Yannick\_Smits\_2](https://avatars.discourse-cdn.com/v4/letter/y/a4c791/32.png) [@Yannick\_Smits\_2](https://discuss.elastic.co/u/Yannick_Smits_2)\
**Post date:** [May 26, 2011, 10:19am UTC](https://discuss.elastic.co/t/sorting-facets-on-score/4445/5 "2011-05-26T10:19:11Z")

</div>

Works perfectly!  
I was staring on the word “sort” instead of “order”.

Thanks!

From: Shay Banon [[mailto:shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)]  
Sent: donderdag 26 mei 2011 12:13  
To: [users@elasticsearch.com](mailto:users@elasticsearch.com)  
Subject: Re: sorting facets on score

Check the ordering option in terms stats: [http://www.elasticsearch.org/guide/reference/api/search/facets/terms-stats-facet.html](http://www.elasticsearch.org/guide/reference/api/search/facets/terms-stats-facet.html).

On Thursday, May 26, 2011 at 11:58 AM, Yannick Smits wrote:

Thanks, got it to work with this syntax:

"facets" : {

"tag\_book\_stats" : {

"terms\_stats" : {

"key\_field" : "book.name",

"value\_script"" : "doc.score"

}

}

}

But it shows me the score and the count now but still sorts on count instead of score.

From: Shay Banon [[mailto:shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com)]  
Sent: vrijdag 20 mei 2011 17:00  
To: [users@elasticsearch.com](mailto:users@elasticsearch.com)  
Subject: Re: sorting facets on score

You can use terms stats, and provide a value\_script with "doc.score".

On Friday, May 20, 2011 at 3:38 PM, Yannick Smits wrote:

Instead of sorting facets by document count is it possible to sort facets by document score?

---

<div class="post-metadata">

**Author:** ![dburge](https://avatars.discourse-cdn.com/v4/letter/d/a88e57/32.png) [@dburge](https://discuss.elastic.co/u/dburge)\
**Post date:** [September 4, 2012, 3:29pm UTC](https://discuss.elastic.co/t/sorting-facets-on-score/4445/6 "2012-09-04T15:29:04Z")

</div>

Hello Yannick Smits-2,

I'm having a problem setting the ordering on doc.score. Could you please post the code that finally worked for you ?

Thanks,  
Davdi

---

<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, 3:14am UTC](https://discuss.elastic.co/t/sorting-facets-on-score/4445/7 "2017-07-06T03:14:10Z")

</div>


