# How to configure elasticsearch to sort the scored documents on a field after score for documents is calculated?

**URL:** <https://discuss.elastic.co/t/how-to-configure-elasticsearch-to-sort-the-scored-documents-on-a-field-after-score-for-documents-is-calculated/15338>\
**Category:** Elasticsearch\
**Created:** [January 21, 2014, 9:30am UTC](https://discuss.elastic.co/t/how-to-configure-elasticsearch-to-sort-the-scored-documents-on-a-field-after-score-for-documents-is-calculated/15338 "2014-01-21T09:30:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dark\_shadow](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dark\_shadow](https://discuss.elastic.co/u/dark_shadow)\
**Post date:** [January 21, 2014, 9:30am UTC](https://discuss.elastic.co/t/how-to-configure-elasticsearch-to-sort-the-scored-documents-on-a-field-after-score-for-documents-is-calculated/15338/1 "2014-01-21T09:30:41Z")

</div>

Hi,

I want to configure the ElasticSearch so that it after calculating the  
score of each document corresponding to a user query, it sorts those score  
documents by some other field and then give the response. There may be a  
case in which some documents have same score. I want Elasticsearch to sort  
those same scoring documents on some other field. That's why needed this  
functionality. Can anyone tell me how can I add this functionality to my  
mapping ?

Thanks

--  
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/cc1f83fb-c00e-4b71-8e12-5b2c096714d0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/cc1f83fb-c00e-4b71-8e12-5b2c096714d0%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [January 21, 2014, 9:52pm UTC](https://discuss.elastic.co/t/how-to-configure-elasticsearch-to-sort-the-scored-documents-on-a-field-after-score-for-documents-is-calculated/15338/2 "2014-01-21T21:52:44Z")

</div>

You can sort on multiple fields for tie-breakers:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

If you are first sorting on a field, and not the score, you will need to  
enable track\_scores:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

Cheers,

Ivan

On Tue, Jan 21, 2014 at 1:30 AM, coder [mukulnitkkr@gmail.com](mailto:mukulnitkkr@gmail.com) wrote:

> Hi,
> 
> I want to configure the Elasticsearch so that it after calculating the  
> score of each document corresponding to a user query, it sorts those score  
> documents by some other field and then give the response. There may be a  
> case in which some documents have same score. I want Elasticsearch to sort  
> those same scoring documents on some other field. That's why needed this  
> functionality. Can anyone tell me how can I add this functionality to my  
> mapping ?
> 
> Thanks
> 
> --  
> 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/cc1f83fb-c00e-4b71-8e12-5b2c096714d0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/cc1f83fb-c00e-4b71-8e12-5b2c096714d0%40googlegroups.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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQCf3YsAYXqMR\_2S3p3Naw-mKZv7iPr9sW%3D37hzHsWHMvA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQCf3YsAYXqMR_2S3p3Naw-mKZv7iPr9sW%3D37hzHsWHMvA%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:** ![dark\_shadow](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dark\_shadow](https://discuss.elastic.co/u/dark_shadow)\
**Post date:** [January 22, 2014, 6:47am UTC](https://discuss.elastic.co/t/how-to-configure-elasticsearch-to-sort-the-scored-documents-on-a-field-after-score-for-documents-is-calculated/15338/3 "2014-01-22T06:47:10Z")

</div>

Hi Ivan,

Actually my code is in java. I have seen those links but not able to  
implement it in my code. I'm using customscorequerybuilder for building my  
query. How can I use this sort in my code. Is there any java api for this ?  
Also, Can I do this sort in my mapping itself ?

Thanks

On Wed, Jan 22, 2014 at 3:22 AM, Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com) wrote:

> You can sort on multiple fields for tie-breakers:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html)
> 
> If you are first sorting on a field, and not the score, you will need to  
> enable track\_scores:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#_track_scores)
> 
> Cheers,
> 
> Ivan
> 
> On Tue, Jan 21, 2014 at 1:30 AM, coder [mukulnitkkr@gmail.com](mailto:mukulnitkkr@gmail.com) wrote:
> 
> > Hi,
> > 
> > I want to configure the Elasticsearch so that it after calculating the  
> > score of each document corresponding to a user query, it sorts those score  
> > documents by some other field and then give the response. There may be a  
> > case in which some documents have same score. I want Elasticsearch to sort  
> > those same scoring documents on some other field. That's why needed this  
> > functionality. Can anyone tell me how can I add this functionality to my  
> > mapping ?
> > 
> > Thanks
> > 
> > --  
> > 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/cc1f83fb-c00e-4b71-8e12-5b2c096714d0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/cc1f83fb-c00e-4b71-8e12-5b2c096714d0%40googlegroups.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).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQCf3YsAYXqMR\_2S3p3Naw-mKZv7iPr9sW%3D37hzHsWHMvA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQCf3YsAYXqMR_2S3p3Naw-mKZv7iPr9sW%3D37hzHsWHMvA%40mail.gmail.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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAAVTvp44q9tj\_mvgbVqVUcXnY7QQZg9jSFj85ZsYf%3D-ME%3DGgjw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAAVTvp44q9tj_mvgbVqVUcXnY7QQZg9jSFj85ZsYf%3D-ME%3DGgjw%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:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [January 22, 2014, 7:21pm UTC](https://discuss.elastic.co/t/how-to-configure-elasticsearch-to-sort-the-scored-documents-on-a-field-after-score-for-documents-is-calculated/15338/4 "2014-01-22T19:21:15Z")

</div>

Some pseudo-ish code using the Java API:

SearchRequestBuilder builder = ...

SortBuilder sortBuidler = org.elasticsearch.search.sort.fieldSort(field);  
builder.setTrackScores(true);  
builder.addSort(sortBuidler);  
builder.addSort(scoreSort().order(SortOrder.DESC));

Hope this helps,

Ivan

On Tue, Jan 21, 2014 at 10:47 PM, Mukul Gupta [mukulnitkkr@gmail.com](mailto:mukulnitkkr@gmail.com) wrote:

> Hi Ivan,
> 
> Actually my code is in java. I have seen those links but not able to  
> implement it in my code. I'm using customscorequerybuilder for building my  
> query. How can I use this sort in my code. Is there any java api for this ?  
> Also, Can I do this sort in my mapping itself ?
> 
> Thanks
> 
> On Wed, Jan 22, 2014 at 3:22 AM, Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com) wrote:
> 
> > You can sort on multiple fields for tie-breakers:  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html)
> > 
> > If you are first sorting on a field, and not the score, you will need to  
> > enable track\_scores:  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#_track_scores)
> > 
> > Cheers,
> > 
> > Ivan
> > 
> > On Tue, Jan 21, 2014 at 1:30 AM, coder [mukulnitkkr@gmail.com](mailto:mukulnitkkr@gmail.com) wrote:
> > 
> > > Hi,
> > > 
> > > I want to configure the Elasticsearch so that it after calculating the  
> > > score of each document corresponding to a user query, it sorts those score  
> > > documents by some other field and then give the response. There may be a  
> > > case in which some documents have same score. I want Elasticsearch to sort  
> > > those same scoring documents on some other field. That's why needed this  
> > > functionality. Can anyone tell me how can I add this functionality to my  
> > > mapping ?
> > > 
> > > Thanks
> > > 
> > > --  
> > > 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/cc1f83fb-c00e-4b71-8e12-5b2c096714d0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/cc1f83fb-c00e-4b71-8e12-5b2c096714d0%40googlegroups.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).  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQCf3YsAYXqMR\_2S3p3Naw-mKZv7iPr9sW%3D37hzHsWHMvA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQCf3YsAYXqMR_2S3p3Naw-mKZv7iPr9sW%3D37hzHsWHMvA%40mail.gmail.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).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/CAAVTvp44q9tj\_mvgbVqVUcXnY7QQZg9jSFj85ZsYf%3D-ME%3DGgjw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAAVTvp44q9tj_mvgbVqVUcXnY7QQZg9jSFj85ZsYf%3D-ME%3DGgjw%40mail.gmail.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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQDrh%3D3Spe1ucfLzrK%2B9QQ6brhQ4\_M2gYVdGaa-ZnjkhYw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQDrh%3D3Spe1ucfLzrK%2B9QQ6brhQ4_M2gYVdGaa-ZnjkhYw%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:** ![dark\_shadow](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dark\_shadow](https://discuss.elastic.co/u/dark_shadow)\
**Post date:** [January 29, 2014, 8:41am UTC](https://discuss.elastic.co/t/how-to-configure-elasticsearch-to-sort-the-scored-documents-on-a-field-after-score-for-documents-is-calculated/15338/5 "2014-01-29T08:41:33Z")

</div>

Hi Ivan,

I tried doing the above thing in my code but it seems there is no  
scoreSort() method. Are you the last sorting needs to be done this way :  
addSort(scoreSort().  
order(SortOrder.DESC));

Thanks

On Thu, Jan 23, 2014 at 12:51 AM, Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com) wrote:

> Some pseudo-ish code using the Java API:
> 
> SearchRequestBuilder builder = ...
> 
> SortBuilder sortBuidler = org.elasticsearch.search.sort.fieldSort(field);  
> builder.setTrackScores(true);  
> builder.addSort(sortBuidler);  
> builder.addSort(scoreSort().order(SortOrder.DESC));
> 
> Hope this helps,
> 
> Ivan
> 
> On Tue, Jan 21, 2014 at 10:47 PM, Mukul Gupta [mukulnitkkr@gmail.com](mailto:mukulnitkkr@gmail.com)wrote:
> 
> > Hi Ivan,
> > 
> > Actually my code is in java. I have seen those links but not able to  
> > implement it in my code. I'm using customscorequerybuilder for building my  
> > query. How can I use this sort in my code. Is there any java api for this ?  
> > Also, Can I do this sort in my mapping itself ?
> > 
> > Thanks
> > 
> > On Wed, Jan 22, 2014 at 3:22 AM, Ivan Brusic [ivan@brusic.com](mailto:ivan@brusic.com) wrote:
> > 
> > > You can sort on multiple fields for tie-breakers:  
> > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html)
> > > 
> > > If you are first sorting on a field, and not the score, you will need to  
> > > enable track\_scores:  
> > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#_track_scores)
> > > 
> > > Cheers,
> > > 
> > > Ivan
> > > 
> > > On Tue, Jan 21, 2014 at 1:30 AM, coder [mukulnitkkr@gmail.com](mailto:mukulnitkkr@gmail.com) wrote:
> > > 
> > > > Hi,
> > > > 
> > > > I want to configure the Elasticsearch so that it after calculating the  
> > > > score of each document corresponding to a user query, it sorts those score  
> > > > documents by some other field and then give the response. There may be a  
> > > > case in which some documents have same score. I want Elasticsearch to sort  
> > > > those same scoring documents on some other field. That's why needed this  
> > > > functionality. Can anyone tell me how can I add this functionality to my  
> > > > mapping ?
> > > > 
> > > > Thanks
> > > > 
> > > > --  
> > > > 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/cc1f83fb-c00e-4b71-8e12-5b2c096714d0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/cc1f83fb-c00e-4b71-8e12-5b2c096714d0%40googlegroups.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).  
> > > To view this discussion on the web visit  
> > > [https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQCf3YsAYXqMR\_2S3p3Naw-mKZv7iPr9sW%3D37hzHsWHMvA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQCf3YsAYXqMR_2S3p3Naw-mKZv7iPr9sW%3D37hzHsWHMvA%40mail.gmail.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).  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/CAAVTvp44q9tj\_mvgbVqVUcXnY7QQZg9jSFj85ZsYf%3D-ME%3DGgjw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAAVTvp44q9tj_mvgbVqVUcXnY7QQZg9jSFj85ZsYf%3D-ME%3DGgjw%40mail.gmail.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).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQDrh%3D3Spe1ucfLzrK%2B9QQ6brhQ4\_M2gYVdGaa-ZnjkhYw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQDrh%3D3Spe1ucfLzrK%2B9QQ6brhQ4_M2gYVdGaa-ZnjkhYw%40mail.gmail.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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAAVTvp5GM3r\_LhSVMSYrj%2Bch20vcCp5k%3DMxW9wjHk1-8ryH94w%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAAVTvp5GM3r_LhSVMSYrj%2Bch20vcCp5k%3DMxW9wjHk1-8ryH94w%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:54am UTC](https://discuss.elastic.co/t/how-to-configure-elasticsearch-to-sort-the-scored-documents-on-a-field-after-score-for-documents-is-calculated/15338/6 "2017-07-06T01:54:15Z")

</div>


