# How to add a script\_fields to score calculation?

**URL:** <https://discuss.elastic.co/t/how-to-add-a-script-fields-to-score-calculation/8387>\
**Category:** Elasticsearch\
**Created:** [July 11, 2012, 6:01pm UTC](https://discuss.elastic.co/t/how-to-add-a-script-fields-to-score-calculation/8387 "2012-07-11T18:01:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sebastien\_Lavoie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sebastien_lavoie/32/1971_2.png) [@Sebastien\_Lavoie](https://discuss.elastic.co/u/Sebastien_Lavoie)\
**Post date:** [July 11, 2012, 6:01pm UTC](https://discuss.elastic.co/t/how-to-add-a-script-fields-to-score-calculation/8387/1 "2012-07-11T18:01:33Z")

</div>

Hi,

I understand I can use a custom\_score[http://www.elasticsearch.org/guide/reference/query-dsl/custom-score-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/custom-score-query.html) with  
script and that I can 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) to  
calculate fields.

Now, what I want to do is to reuse the value generated with the  
script\_field to use in score calculation. The script I use is quite heavy  
so I don’t really want to duplicate it.

Basically, I want something like this:

{  
"query": {  
"custom\_score" : {  
"query" : {  
"match\_all" : {}  
},  
"script" : "\_score \* (1 + my\_complex\_field \* 0.2)"  
}  
},  
"script\_fields" : {  
"my\_complex\_field" : {  
"script" : "my\_complex\_script",  
"params": {  
"max\_date": 1342017125,  
"min\_date": 1340289125  
}  
}  
}  
}

Any ideas ?

Thanks

---

<div class="post-metadata">

**Author:** ![Sebastien\_Lavoie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sebastien_lavoie/32/1971_2.png) [@Sebastien\_Lavoie](https://discuss.elastic.co/u/Sebastien_Lavoie)\
**Post date:** [July 11, 2012, 9:53pm UTC](https://discuss.elastic.co/t/how-to-add-a-script-fields-to-score-calculation/8387/2 "2012-07-11T21:53:48Z")

</div>

Otherwise, is it possible to define a MVEL function on a global level (or  
in the index) and simply call this function everywhere ?

On Wednesday, July 11, 2012 2:01:33 PM UTC-4, Sebastien Lavoie wrote:

> Hi,
> 
> I understand I can use a custom\_score[http://www.elasticsearch.org/guide/reference/query-dsl/custom-score-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/custom-score-query.html) with  
> script and that I can 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) to  
> calculate fields.
> 
> Now, what I want to do is to reuse the value generated with the  
> script\_field to use in score calculation. The script I use is quite heavy  
> so I don’t really want to duplicate it.
> 
> Basically, I want something like this:
> 
> {  
> "query": {  
> "custom\_score" : {  
> "query" : {  
> "match\_all" : {}  
> },  
> "script" : "\_score \* (1 + my\_complex\_field \* 0.2)"  
> }  
> },  
> "script\_fields" : {  
> "my\_complex\_field" : {  
> "script" : "my\_complex\_script",  
> "params": {  
> "max\_date": 1342017125,  
> "min\_date": 1340289125  
> }  
> }  
> }  
> }
> 
> Any ideas ?
> 
> Thanks

---

<div class="post-metadata">

**Author:** ![micherr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/micherr/32/1908_2.png) [@micherr](https://discuss.elastic.co/u/micherr)\
**Post date:** [November 18, 2013, 10:54am UTC](https://discuss.elastic.co/t/how-to-add-a-script-fields-to-score-calculation/8387/3 "2013-11-18T10:54:11Z")

</div>

Hi!

If there any updates on the subject? I`m facing the same issue.

---

<div class="post-metadata">

**Author:** ![Sebastien\_Lavoie1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sebastien_lavoie1/32/1971_2.png) [@Sebastien\_Lavoie1](https://discuss.elastic.co/u/Sebastien_Lavoie1)\
**Post date:** [November 19, 2013, 1:13am UTC](https://discuss.elastic.co/t/how-to-add-a-script-fields-to-score-calculation/8387/4 "2013-11-19T01:13:42Z")

</div>

I am sorry, I was never able to make it work, I had to use it twice.

--  
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:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [November 21, 2013, 9:03am UTC](https://discuss.elastic.co/t/how-to-add-a-script-fields-to-score-calculation/8387/5 "2013-11-21T09:03:07Z")

</div>

Hey,

defining global scripts will not help your problem, but at least make it  
easier to execute/maintain, more about that

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

iirc it is not possible to access the script fields in your score. I am  
wondering, if you cant just use the score to go on?  
Also you might want to use the function\_score query,

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

--Alex

On Tue, Nov 19, 2013 at 2:13 AM, Sébastien Lavoie [sebastien@lavoie.sl](mailto:sebastien@lavoie.sl)wrote:

> I am sorry, I was never able to make it work, I had to use it twice.
> 
> --  
> 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:** ![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:05am UTC](https://discuss.elastic.co/t/how-to-add-a-script-fields-to-score-calculation/8387/6 "2017-07-06T02:05:46Z")

</div>


