# Custom\_score query + sort by different field possible?

**URL:** <https://discuss.elastic.co/t/custom-score-query-sort-by-different-field-possible/10972>\
**Category:** Elasticsearch\
**Created:** [March 2, 2013, 2:13am UTC](https://discuss.elastic.co/t/custom-score-query-sort-by-different-field-possible/10972 "2013-03-02T02:13:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yooz](https://avatars.discourse-cdn.com/v4/letter/y/13edae/32.png) [@Yooz](https://discuss.elastic.co/u/Yooz)\
**Post date:** [March 2, 2013, 2:13am UTC](https://discuss.elastic.co/t/custom-score-query-sort-by-different-field-possible/10972/1 "2013-03-02T02:13:53Z")

</div>

Hi,

I am using a field (\_score) that is generated dynamically using a  
custom\_score query (script + params passed). Would it be possible to sort  
on a different field while still calculating this score for the documents  
returned? I.e. can a field be generated via a script + params but not be  
used to sort the results?

Thanks,  
--young

--  
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:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [March 2, 2013, 3:12am UTC](https://discuss.elastic.co/t/custom-score-query-sort-by-different-field-possible/10972/2 "2013-03-02T03:12:37Z")

</div>

If you are not using \_score in your script, you can simply use script fields[http://www.elasticsearch.org/guide/reference/api/search/script-fields.html](http://www.elasticsearch.org/guide/reference/api/search/script-fields.html).  
It will be even faster since it will be calculated only for the first page  
of results instead of all results in your result list. However if you need  
the original score in your script, you can add \_score as the second sort  
order after the field that you actually want to sort on.

On Friday, March 1, 2013 9:13:53 PM UTC-5, Yooz wrote:

> Hi,
> 
> I am using a field (\_score) that is generated dynamically using a  
> custom\_score query (script + params passed). Would it be possible to sort  
> on a different field while still calculating this score for the documents  
> returned? I.e. can a field be generated via a script + params but not be  
> used to sort the results?
> 
> Thanks,  
> --young

--  
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:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [March 2, 2013, 5:40am UTC](https://discuss.elastic.co/t/custom-score-query-sort-by-different-field-possible/10972/3 "2013-03-02T05:40:39Z")

</div>

Also don't forget to enable track\_scores if you are sorting on a field that  
is not the score.

> **[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.

--  
Ivan

On Fri, Mar 1, 2013 at 7:12 PM, Igor Motov [imotov@gmail.com](mailto:imotov@gmail.com) wrote:

> If you are not using \_score in your script, you can simply use script  
> fields[http://www.elasticsearch.org/guide/reference/api/search/script-fields.html](http://www.elasticsearch.org/guide/reference/api/search/script-fields.html).  
> It will be even faster since it will be calculated only for the first page  
> of results instead of all results in your result list. However if you need  
> the original score in your script, you can add \_score as the second sort  
> order after the field that you actually want to sort on.
> 
> On Friday, March 1, 2013 9:13:53 PM UTC-5, Yooz wrote:
> 
> > Hi,
> > 
> > I am using a field (\_score) that is generated dynamically using a  
> > custom\_score query (script + params passed). Would it be possible to sort  
> > on a different field while still calculating this score for the documents  
> > returned? I.e. can a field be generated via a script + params but not be  
> > used to sort the results?
> > 
> > Thanks,  
> > --young
> > 
> > --  
> > 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).

--  
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:** ![Yooz](https://avatars.discourse-cdn.com/v4/letter/y/13edae/32.png) [@Yooz](https://discuss.elastic.co/u/Yooz)\
**Post date:** [March 4, 2013, 4:17pm UTC](https://discuss.elastic.co/t/custom-score-query-sort-by-different-field-possible/10972/4 "2013-03-04T16:17:49Z")

</div>

Awesome! thanks Ivan & Igor!

On Friday, March 1, 2013 9:40:39 PM UTC-8, Ivan Brusic wrote:

> Also don't forget to enable track\_scores if you are sorting on a field  
> that is not the score.
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/sort.html)
> 
> --  
> Ivan
> 
> On Fri, Mar 1, 2013 at 7:12 PM, Igor Motov \<[imo...@gmail.com](mailto:imo...@gmail.com) \<javascript:\>
> 
> > wrote:
> 
> > If you are not using \_score in your script, you can simply use script  
> > fields[http://www.elasticsearch.org/guide/reference/api/search/script-fields.html](http://www.elasticsearch.org/guide/reference/api/search/script-fields.html).  
> > It will be even faster since it will be calculated only for the first page  
> > of results instead of all results in your result list. However if you need  
> > the original score in your script, you can add \_score as the second sort  
> > order after the field that you actually want to sort on.
> > 
> > On Friday, March 1, 2013 9:13:53 PM UTC-5, Yooz wrote:
> > 
> > > Hi,
> > > 
> > > I am using a field (\_score) that is generated dynamically using a  
> > > custom\_score query (script + params passed). Would it be possible to sort  
> > > on a different field while still calculating this score for the documents  
> > > returned? I.e. can a field be generated via a script + params but not be  
> > > used to sort the results?
> > > 
> > > Thanks,  
> > > --young
> > > 
> > > --  
> > > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > > 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:48am UTC](https://discuss.elastic.co/t/custom-score-query-sort-by-different-field-possible/10972/5 "2017-07-06T02:48:32Z")

</div>


