# Sorting search hits based on an attribute value

**URL:** <https://discuss.elastic.co/t/sorting-search-hits-based-on-an-attribute-value/18428>\
**Category:** Elasticsearch\
**Created:** [July 2, 2014, 3:57am UTC](https://discuss.elastic.co/t/sorting-search-hits-based-on-an-attribute-value/18428 "2014-07-02T03:57:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bsa](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@bsa](https://discuss.elastic.co/u/bsa)\
**Post date:** [July 2, 2014, 3:57am UTC](https://discuss.elastic.co/t/sorting-search-hits-based-on-an-attribute-value/18428/1 "2014-07-02T03:57:24Z")

</div>

Hello All,

I want to sort the search hits based on two criteria - (1) value of an  
attribute, and (2) \_score. The first criteria is such that if the attribute  
has two values "open" and "closed", the search hits with value "open"  
should be ranked above those with value "closed". What is the best way to  
do this in ES?

One approach I tried was to invert the score for search hits with value  
"closed" using a function\_score query. Though this works, there are two  
issues - (1) search hits with value "closed" are ranked in the reverse  
order of their score, (2) this approach does not allow handling more than  
two values of the attribute. Is there a better way to do this in ES?

--  
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/5221fb7b-25bc-4065-9500-ad7f3bf2655e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5221fb7b-25bc-4065-9500-ad7f3bf2655e%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![vineeth\_mohan\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vineeth_mohan_2/32/747_2.png) [@vineeth\_mohan\_2](https://discuss.elastic.co/u/vineeth_mohan_2)\
**Post date:** [July 2, 2014, 4:15am UTC](https://discuss.elastic.co/t/sorting-search-hits-based-on-an-attribute-value/18428/2 "2014-07-02T04:15:41Z")

</div>

Hi ,

I feel you should write a script and use that in the function\_score.

So assuming \_score is normalized to a value between 0 and 1.

value of closed = 1  
value of open = 2

Score of a document = value of attribute + \_score.

Thanks  
Vineeth

On Wed, Jul 2, 2014 at 9:27 AM, bsa [shashi.b.anand@gmail.com](mailto:shashi.b.anand@gmail.com) wrote:

> Hello All,
> 
> I want to sort the search hits based on two criteria - (1) value of an  
> attribute, and (2) \_score. The first criteria is such that if the attribute  
> has two values "open" and "closed", the search hits with value "open"  
> should be ranked above those with value "closed". What is the best way to  
> do this in ES?
> 
> One approach I tried was to invert the score for search hits with value  
> "closed" using a function\_score query. Though this works, there are two  
> issues - (1) search hits with value "closed" are ranked in the reverse  
> order of their score, (2) this approach does not allow handling more than  
> two values of the attribute. Is there a better way to do this in ES?
> 
> --  
> 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/5221fb7b-25bc-4065-9500-ad7f3bf2655e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5221fb7b-25bc-4065-9500-ad7f3bf2655e%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/5221fb7b-25bc-4065-9500-ad7f3bf2655e%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/5221fb7b-25bc-4065-9500-ad7f3bf2655e%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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/CAGdPd5k\_2JwxncZd\_yfF4Cw2N9d7mnBWfSHDbQ24pCTRz\_C8Cg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAGdPd5k_2JwxncZd_yfF4Cw2N9d7mnBWfSHDbQ24pCTRz_C8Cg%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:18am UTC](https://discuss.elastic.co/t/sorting-search-hits-based-on-an-attribute-value/18428/3 "2017-07-06T01:18:35Z")

</div>


