# Sort Order with Matching Values

**URL:** <https://discuss.elastic.co/t/sort-order-with-matching-values/13124>\
**Category:** Elasticsearch\
**Created:** [August 8, 2013, 9:13am UTC](https://discuss.elastic.co/t/sort-order-with-matching-values/13124 "2013-08-08T09:13:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jack\_Pearkes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jack_pearkes/32/2184_2.png) [@Jack\_Pearkes](https://discuss.elastic.co/u/Jack_Pearkes)\
**Post date:** [August 8, 2013, 9:13am UTC](https://discuss.elastic.co/t/sort-order-with-matching-values/13124/1 "2013-08-08T09:13:07Z")

</div>

Hello,

I have a document with an attribute called "rating", which is a manually  
defined attribute created by the end-user. "rating" is indexed by  
Elasticsearch as an integer. Sometimes, "rating" can be identical in  
multiple documents. I sort the return of a search across those documents by  
their "rating", descending.

So, unfortunately, Elasticsearch seems to sort documents with the same  
"rating" differently across multiple queries. This is causing some user  
experience issues.

In my first query, the document order may be:

{"name": "doca", "rating": 5}  
{"name": "docb", "rating": 4}  
{"name": "docc", "rating": 4}  
{"name": "docd", "rating": 3}

And the second query:

{"name": "doca", "rating": 5}  
{"name": "docc", "rating": 4}  
{"name": "docb", "rating": 4}  
{"name": "docd", "rating": 3}

Notice the position of docb and docc, in this case.

Can anyone suggest a way to return results in a consistent order in a  
situation like this?

Thanks for your time.

Best,

Jack

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Boaz\_Leskes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/boaz_leskes/32/723_2.png) [@Boaz\_Leskes](https://discuss.elastic.co/u/Boaz_Leskes)\
**Post date:** [August 8, 2013, 11:47am UTC](https://discuss.elastic.co/t/sort-order-with-matching-values/13124/2 "2013-08-08T11:47:07Z")

</div>

Hi Jack,

Sorting of document with equal sorting values is undefined (it depends on  
the current state of lucene's segment which go through continuos merging)  
.You will need to add a tie-breaking field for those situation. You can use  
any field in your data or \_score for the score of the document.  
See [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/sort/) for more  
information.

Cheers,  
Boaz

On Thursday, August 8, 2013 11:13:07 AM UTC+2, Jack Pearkes wrote:

> Hello,
> 
> I have a document with an attribute called "rating", which is a manually  
> defined attribute created by the end-user. "rating" is indexed by  
> Elasticsearch as an integer. Sometimes, "rating" can be identical in  
> multiple documents. I sort the return of a search across those documents by  
> their "rating", descending.
> 
> So, unfortunately, Elasticsearch seems to sort documents with the same  
> "rating" differently across multiple queries. This is causing some user  
> experience issues.
> 
> In my first query, the document order may be:
> 
> {"name": "doca", "rating": 5}  
> {"name": "docb", "rating": 4}  
> {"name": "docc", "rating": 4}  
> {"name": "docd", "rating": 3}
> 
> And the second query:
> 
> {"name": "doca", "rating": 5}  
> {"name": "docc", "rating": 4}  
> {"name": "docb", "rating": 4}  
> {"name": "docd", "rating": 3}
> 
> Notice the position of docb and docc, in this case.
> 
> Can anyone suggest a way to return results in a consistent order in a  
> situation like this?
> 
> Thanks for your time.
> 
> Best,
> 
> Jack

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Jack\_Pearkes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jack_pearkes/32/2184_2.png) [@Jack\_Pearkes](https://discuss.elastic.co/u/Jack_Pearkes)\
**Post date:** [August 8, 2013, 4:24pm UTC](https://discuss.elastic.co/t/sort-order-with-matching-values/13124/3 "2013-08-08T16:24:55Z")

</div>

Boaz,

Thank you kindly, that solution to tie-break with another field worked  
perfectly.

Best,

Jack

On Thu, Aug 8, 2013 at 1:47 PM, Boaz Leskes [b.leskes@gmail.com](mailto:b.leskes@gmail.com) wrote:

> Hi Jack,
> 
> Sorting of document with equal sorting values is undefined (it depends on  
> the current state of lucene's segment which go through continuos merging)  
> .You will need to add a tie-breaking field for those situation. You can use  
> any field in your data or \_score for the score of the document. See  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/sort/) for more  
> information.
> 
> Cheers,  
> Boaz
> 
> On Thursday, August 8, 2013 11:13:07 AM UTC+2, Jack Pearkes wrote:
> 
> > Hello,
> > 
> > I have a document with an attribute called "rating", which is a manually  
> > defined attribute created by the end-user. "rating" is indexed by  
> > Elasticsearch as an integer. Sometimes, "rating" can be identical in  
> > multiple documents. I sort the return of a search across those documents by  
> > their "rating", descending.
> > 
> > So, unfortunately, Elasticsearch seems to sort documents with the same  
> > "rating" differently across multiple queries. This is causing some user  
> > experience issues.
> > 
> > In my first query, the document order may be:
> > 
> > {"name": "doca", "rating": 5}  
> > {"name": "docb", "rating": 4}  
> > {"name": "docc", "rating": 4}  
> > {"name": "docd", "rating": 3}
> > 
> > And the second query:
> > 
> > {"name": "doca", "rating": 5}  
> > {"name": "docc", "rating": 4}  
> > {"name": "docb", "rating": 4}  
> > {"name": "docd", "rating": 3}
> > 
> > Notice the position of docb and docc, in this case.
> > 
> > Can anyone suggest a way to return results in a consistent order in a  
> > situation like this?
> > 
> > Thanks for your time.
> > 
> > Best,
> > 
> > Jack
> > 
> > --  
> > You received this message because you are subscribed to a topic in the  
> > Google Groups "elasticsearch" group.  
> > To unsubscribe from this topic, visit  
> > [https://groups.google.com/d/topic/elasticsearch/VsHojLsvTUs/unsubscribe](https://groups.google.com/d/topic/elasticsearch/VsHojLsvTUs/unsubscribe).  
> > To unsubscribe from this group and all its topics, send an email to  
> > [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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).  
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, 2:22am UTC](https://discuss.elastic.co/t/sort-order-with-matching-values/13124/4 "2017-07-06T02:22:04Z")

</div>


