# Sortting based on combination of ES score and two fields

**URL:** <https://discuss.elastic.co/t/sortting-based-on-combination-of-es-score-and-two-fields/18826>\
**Category:** Elasticsearch\
**Created:** [July 23, 2014, 2:29pm UTC](https://discuss.elastic.co/t/sortting-based-on-combination-of-es-score-and-two-fields/18826 "2014-07-23T14:29:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![M\_20](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/m_20/32/1371_2.png) [@M\_20](https://discuss.elastic.co/u/M_20)\
**Post date:** [July 23, 2014, 2:29pm UTC](https://discuss.elastic.co/t/sortting-based-on-combination-of-es-score-and-two-fields/18826/1 "2014-07-23T14:29:21Z")

</div>

Hi,

How can I sort the result based on summation of several fields?  
For my application, I want to sort the results based on ( ElasticSearch  
score + field\_1 + field\_2 )  
In fact:  
Final Score = ElasticSearch score + field\_1 + field\_2  
It seems kinda recursive, but I was wondering ES is able to do so.

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/6eac0f4f-655f-4b12-840e-162276e454fd%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/6eac0f4f-655f-4b12-840e-162276e454fd%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![M\_20](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/m_20/32/1371_2.png) [@M\_20](https://discuss.elastic.co/u/M_20)\
**Post date:** [July 23, 2014, 2:35pm UTC](https://discuss.elastic.co/t/sortting-based-on-combination-of-es-score-and-two-fields/18826/2 "2014-07-23T14:35:41Z")

</div>

It seems this is what I need

"script\_score" : {  
"script" : "\_score + doc['my\_numeric\_field\_1'].value + doc['my\_numeric\_field\_2'].value"  
}

Am I right??

On Wednesday, July 23, 2014 9:29:22 AM UTC-5, M\_20 wrote:

> Hi,
> 
> How can I sort the result based on summation of several fields?  
> For my application, I want to sort the results based on ( Elasticsearch  
> score + field\_1 + field\_2 )  
> In fact:  
> Final Score = Elasticsearch score + field\_1 + field\_2  
> It seems kinda recursive, but I was wondering ES is able to do so.
> 
> 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/7686b437-a86e-4617-a4f5-378a84bb0ce9%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/7686b437-a86e-4617-a4f5-378a84bb0ce9%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![M\_20](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/m_20/32/1371_2.png) [@M\_20](https://discuss.elastic.co/u/M_20)\
**Post date:** [July 23, 2014, 4:40pm UTC](https://discuss.elastic.co/t/sortting-based-on-combination-of-es-score-and-two-fields/18826/3 "2014-07-23T16:40:23Z")

</div>

I wrote a code ( based on ES documentation

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

), but it doesn't work.  
Here are the code and the error message. Any suggestion?

///// journalscorenormal --\> the field's name

String script = "doc['journalscorenormal'].value";  
ScriptSortBuilder sortSc = SortBuilders.scriptSort(script,"float");  
SearchResponse response = client  
.prepareSearch(index)  
.setTypes(MyVars.index.getType())  
.setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH)  
.setQuery(  
QueryBuilders.fuzzyLikeThisQuery(field)  
.likeText(searchTerm)).setFrom(0)  
.setSize(60)  
.addSort(sortSc)  
.setExplain(true).execute().actionGet();

Failed to parse source  
[{"from":0,"size":60,"query":{"flt":{"fields":["sentence"],"like\_text":"disease"}},"explain":true,"  
_sort":[{"\_script":{"script":"doc['journalscorenormal'].value","type":"float"}}]}]]_];  
nested: ScriptException[dynamic scripting disabled]; }  
at  
org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.onFirstPhaseResult(TransportSearchTypeAction.java:233)  
at  
org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction$1.onFailure(TransportSearchTypeAction.java:179)  
at  
org.elasticsearch.search.action.SearchServiceTransportAction$23.run(SearchServiceTransportAction.java:523)  
at  
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)  
at  
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)  
at java.lang.Thread.run(Thread.java:744)

On Wednesday, July 23, 2014 9:35:41 AM UTC-5, M\_20 wrote:

> It seems this is what I need
> 
> "script\_score" : {  
> "script" : "\_score + doc['my\_numeric\_field\_1'].value + doc['my\_numeric\_field\_2'].value"  
> }
> 
> Am I right??
> 
> On Wednesday, July 23, 2014 9:29:22 AM UTC-5, M\_20 wrote:
> 
> > Hi,
> > 
> > How can I sort the result based on summation of several fields?  
> > For my application, I want to sort the results based on ( Elasticsearch  
> > score + field\_1 + field\_2 )  
> > In fact:  
> > Final Score = Elasticsearch score + field\_1 + field\_2  
> > It seems kinda recursive, but I was wondering ES is able to do so.
> > 
> > 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/33a3db5a-04b6-4ae6-a60f-e015e0d17ccd%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/33a3db5a-04b6-4ae6-a60f-e015e0d17ccd%40googlegroups.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:13am UTC](https://discuss.elastic.co/t/sortting-based-on-combination-of-es-score-and-two-fields/18826/4 "2017-07-06T01:13:51Z")

</div>


