# Custom Scoring Script Running Slow

**URL:** https://discuss.elastic.co/t/custom-scoring-script-running-slow/4642
**Category:** Elasticsearch
**Created:** [June 16, 2011, 7:03pm UTC](https://discuss.elastic.co/t/custom-scoring-script-running-slow/4642 "2011-06-16T19:03:17Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![thnguyen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thnguyen/32/3207_2.png) [@thnguyen](https://discuss.elastic.co/u/thnguyen)
#### Post date: [June 16, 2011, 7:03pm UTC](https://discuss.elastic.co/t/custom-scoring-script-running-slow/4642/1 "2011-06-16T19:03:17Z")

</div>

Hello,

I'm attempting to run a custom scoring search that includes a script  
that was ported over from a Solr boost function. Initial requests to  
Elastic never return with a response. I must wait a long time after  
sending the initial request before receiving a response from  
subsequent request. To me is seems like maybe there is some type of  
compilation and loading of the script that is causing that long wait  
(unfamiliar of ES internals). Here is a subset of the entire script  
that I want to run:

\_score + 0.4\*( -150.0\*pow(max(doc[\u0027daysSinceRelease\u0027].value

- -30000.0,0),0.000001)_pow(max(-1826.0-doc[\u0027daysSinceRelease  
\u0027].value,0),0.000001) + -100.0_pow(max(doc[\u0027daysSinceRelease  
\u0027].value - -1825.0,0),0.000001)_pow(max(-1096.0-  
doc[\u0027daysSinceRelease\u0027].value,0),0.000001) + (-100.0+  
(doc[\u0027daysSinceRelease\u0027].value - -1095.0)_(0.0 - -100.0)/  
(-548.0 - -1095.0))\*pow(max(doc[\u0027daysSinceRelease\u0027].value -  
-1095.0,0),0.000001)\*pow(max(-548.0-doc[\u0027daysSinceRelease  
\u0027].value,0),0.000001) )

What is causing the search to be so slow? BTW the "daysSinceRelease"  
field is mapped to the integer datatype and I have 146k documents.  
Also this search is super fast in Solr.

---

<div class="post-metadata">

### Author: ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)
#### Post date: [June 16, 2011, 7:37pm UTC](https://discuss.elastic.co/t/custom-scoring-script-running-slow/4642/2 "2011-06-16T19:37:13Z")

</div>

For a complex script like that, it might make sense to run it as a native script query: [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/modules/scripting.html). It will be much faster compared to anything you can do otherwise.

On Thursday, June 16, 2011 at 10:03 PM, thnguyen wrote:

> Hello,
> 
> I'm attempting to run a custom scoring search that includes a script  
> that was ported over from a Solr boost function. Initial requests to  
> Elastic never return with a response. I must wait a long time after  
> sending the initial request before receiving a response from  
> subsequent request. To me is seems like maybe there is some type of  
> compilation and loading of the script that is causing that long wait  
> (unfamiliar of ES internals). Here is a subset of the entire script  
> that I want to run:
> 
> \_score + 0.4\*( -150.0\*pow(max(doc[\u0027daysSinceRelease\u0027].value
> 
> - -30000.0,0),0.000001)_pow(max(-1826.0-doc[\u0027daysSinceRelease  
> \u0027].value,0),0.000001) + -100.0_pow(max(doc[\u0027daysSinceRelease  
> \u0027].value - -1825.0,0),0.000001)_pow(max(-1096.0-  
> doc[\u0027daysSinceRelease\u0027].value,0),0.000001) + (-100.0+  
> (doc[\u0027daysSinceRelease\u0027].value - -1095.0)_(0.0 - -100.0)/  
> (-548.0 - -1095.0))\*pow(max(doc[\u0027daysSinceRelease\u0027].value -  
> -1095.0,0),0.000001)\*pow(max(-548.0-doc[\u0027daysSinceRelease  
> \u0027].value,0),0.000001) )
> 
> What is causing the search to be so slow? BTW the "daysSinceRelease"  
> field is mapped to the integer datatype and I have 146k documents.  
> Also this search is super fast in Solr.

---

<div class="post-metadata">

### Author: ![thnguyen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thnguyen/32/3207_2.png) [@thnguyen](https://discuss.elastic.co/u/thnguyen)
#### Post date: [June 16, 2011, 8:41pm UTC](https://discuss.elastic.co/t/custom-scoring-script-running-slow/4642/3 "2011-06-16T20:41:39Z")

</div>

When extending AbstractSearchScript do all I need to do is place my  
script in the score() method?

On Jun 16, 12:37 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> For a complex script like that, it might make sense to run it as a native script query:[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/modules/scripting.html). It will be much faster compared to anything you can do otherwise.
> 
> On Thursday, June 16, 2011 at 10:03 PM, thnguyen wrote:
> 
> > Hello,
> 
> > I'm attempting to run a custom scoring search that includes a script  
> > that was ported over from a Solr boost function. Initial requests to  
> > Elastic never return with a response. I must wait a long time after  
> > sending the initial request before receiving a response from  
> > subsequent request. To me is seems like maybe there is some type of  
> > compilation and loading of the script that is causing that long wait  
> > (unfamiliar of ES internals). Here is a subset of the entire script  
> > that I want to run:
> 
> > \_score + 0.4\*( -150.0\*pow(max(doc[\u0027daysSinceRelease\u0027].value
> > 
> > - -30000.0,0),0.000001)_pow(max(-1826.0-doc[\u0027daysSinceRelease  
> > \u0027].value,0),0.000001) + -100.0_pow(max(doc[\u0027daysSinceRelease  
> > \u0027].value - -1825.0,0),0.000001)_pow(max(-1096.0-  
> > doc[\u0027daysSinceRelease\u0027].value,0),0.000001) + (-100.0+  
> > (doc[\u0027daysSinceRelease\u0027].value - -1095.0)_(0.0 - -100.0)/  
> > (-548.0 - -1095.0))\*pow(max(doc[\u0027daysSinceRelease\u0027].value -  
> > -1095.0,0),0.000001)\*pow(max(-548.0-doc[\u0027daysSinceRelease  
> > \u0027].value,0),0.000001) )
> 
> > What is causing the search to be so slow? BTW the "daysSinceRelease"  
> > field is mapped to the integer datatype and I have 146k documents.  
> > Also this search is super fast in Solr.

---

<div class="post-metadata">

### Author: ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)
#### Post date: [June 16, 2011, 9:59pm UTC](https://discuss.elastic.co/t/custom-scoring-script-running-slow/4642/4 "2011-06-16T21:59:09Z")

</div>

What you would want to do is extend AbstractFloatSearchScript, and override runAsFloat. Within the method, you can do:

long time = doc().numeric("field\_name").getLongValue();  
return // the formula you want to do goes here...

On Thursday, June 16, 2011 at 11:41 PM, thnguyen wrote:

> When extending AbstractSearchScript do all I need to do is place my  
> script in the score() method?
> 
> On Jun 16, 12:37 pm, Shay Banon \<[shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) ([http://elasticsearch.com](http://elasticsearch.com))\> wrote:
> 
> > For a complex script like that, it might make sense to run it as a native script query:[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/modules/scripting.html). It will be much faster compared to anything you can do otherwise.
> > 
> > On Thursday, June 16, 2011 at 10:03 PM, thnguyen wrote:
> > 
> > > Hello,
> > 
> > > I'm attempting to run a custom scoring search that includes a script  
> > > that was ported over from a Solr boost function. Initial requests to  
> > > Elastic never return with a response. I must wait a long time after  
> > > sending the initial request before receiving a response from  
> > > subsequent request. To me is seems like maybe there is some type of  
> > > compilation and loading of the script that is causing that long wait  
> > > (unfamiliar of ES internals). Here is a subset of the entire script  
> > > that I want to run:
> > 
> > > \_score + 0.4\*( -150.0\*pow(max(doc[\u0027daysSinceRelease\u0027].value
> > > 
> > > - -30000.0,0),0.000001)_pow(max(-1826.0-doc[\u0027daysSinceRelease  
> > > \u0027].value,0),0.000001) + -100.0_pow(max(doc[\u0027daysSinceRelease  
> > > \u0027].value - -1825.0,0),0.000001)_pow(max(-1096.0-  
> > > doc[\u0027daysSinceRelease\u0027].value,0),0.000001) + (-100.0+  
> > > (doc[\u0027daysSinceRelease\u0027].value - -1095.0)_(0.0 - -100.0)/  
> > > (-548.0 - -1095.0))\*pow(max(doc[\u0027daysSinceRelease\u0027].value -  
> > > -1095.0,0),0.000001)\*pow(max(-548.0-doc[\u0027daysSinceRelease  
> > > \u0027].value,0),0.000001) )
> > 
> > > What is causing the search to be so slow? BTW the "daysSinceRelease"  
> > > field is mapped to the integer datatype and I have 146k documents.  
> > > Also this search is super fast in Solr.

---

<div class="post-metadata">

### Author: ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)
#### Post date: [June 16, 2011, 10:00pm UTC](https://discuss.elastic.co/t/custom-scoring-script-running-slow/4642/5 "2011-06-16T22:00:30Z")

</div>

Off, and the score you get by calling score(), for example:

long time = doc().numeric("field\_name").getLongValue();  
return score() \* (...)

On Friday, June 17, 2011 at 12:59 AM, Shay Banon wrote:

> What you would want to do is extend AbstractFloatSearchScript, and override runAsFloat. Within the method, you can do:
> 
> long time = doc().numeric("field\_name").getLongValue();  
> return // the formula you want to do goes here...
> 
> On Thursday, June 16, 2011 at 11:41 PM, thnguyen wrote:
> 
> > When extending AbstractSearchScript do all I need to do is place my  
> > script in the score() method?
> > 
> > On Jun 16, 12:37 pm, Shay Banon \<[shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) ([http://elasticsearch.com](http://elasticsearch.com))\> wrote:
> > 
> > > For a complex script like that, it might make sense to run it as a native script query:[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/modules/scripting.html). It will be much faster compared to anything you can do otherwise.
> > > 
> > > On Thursday, June 16, 2011 at 10:03 PM, thnguyen wrote:
> > > 
> > > > Hello,
> > > 
> > > > I'm attempting to run a custom scoring search that includes a script  
> > > > that was ported over from a Solr boost function. Initial requests to  
> > > > Elastic never return with a response. I must wait a long time after  
> > > > sending the initial request before receiving a response from  
> > > > subsequent request. To me is seems like maybe there is some type of  
> > > > compilation and loading of the script that is causing that long wait  
> > > > (unfamiliar of ES internals). Here is a subset of the entire script  
> > > > that I want to run:
> > > 
> > > > \_score + 0.4\*( -150.0\*pow(max(doc[\u0027daysSinceRelease\u0027].value
> > > > 
> > > > - -30000.0,0),0.000001)_pow(max(-1826.0-doc[\u0027daysSinceRelease  
> > > > \u0027].value,0),0.000001) + -100.0_pow(max(doc[\u0027daysSinceRelease  
> > > > \u0027].value - -1825.0,0),0.000001)_pow(max(-1096.0-  
> > > > doc[\u0027daysSinceRelease\u0027].value,0),0.000001) + (-100.0+  
> > > > (doc[\u0027daysSinceRelease\u0027].value - -1095.0)_(0.0 - -100.0)/  
> > > > (-548.0 - -1095.0))\*pow(max(doc[\u0027daysSinceRelease\u0027].value -  
> > > > -1095.0,0),0.000001)\*pow(max(-548.0-doc[\u0027daysSinceRelease  
> > > > \u0027].value,0),0.000001) )
> > > 
> > > > What is causing the search to be so slow? BTW the "daysSinceRelease"  
> > > > field is mapped to the integer datatype and I have 146k documents.  
> > > > Also this search is super fast in Solr.

---

<div class="post-metadata">

### Author: ![thnguyen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thnguyen/32/3207_2.png) [@thnguyen](https://discuss.elastic.co/u/thnguyen)
#### Post date: [June 17, 2011, 12:02am UTC](https://discuss.elastic.co/t/custom-scoring-script-running-slow/4642/6 "2011-06-17T00:02:00Z")

</div>

Forgive me but I'm confused. Following your advise on using  
AbstractFloatSearchScript I have created the following class:  
[Elastic Search Custom Script · GitHub](https://gist.github.com/1030597). You then mentioned in your next  
reply the score() method. In my runAsFloat() method do I need to add  
score() to the value of my function? Does Elastic automatically call  
runAsFloat() to get the score when I run the following search query (I  
assume it's correct)?:

{  
"query":{  
"custom\_score":{  
"query":{  
"query\_string":{  
"default\_field":"title",  
"query":"grand theft auto"  
}  
},  
"lang":"native",  
"script":"CustomScript "  
}  
}  
}

On Jun 16, 3:00 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> Off, and the score you get by calling score(), for example:
> 
> long time = doc().numeric("field\_name").getLongValue();  
> return score() \* (...)
> 
> On Friday, June 17, 2011 at 12:59 AM, Shay Banon wrote:
> 
> > What you would want to do is extend AbstractFloatSearchScript, and override runAsFloat. Within the method, you can do:
> 
> > long time = doc().numeric("field\_name").getLongValue();  
> > return // the formula you want to do goes here...
> 
> > On Thursday, June 16, 2011 at 11:41 PM, thnguyen wrote:
> 
> > > When extending AbstractSearchScript do all I need to do is place my  
> > > script in the score() method?
> 
> > > On Jun 16, 12:37 pm, Shay Banon \<[shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) ([http://elasticsearch.com](http://elasticsearch.com))\> wrote:
> > > 
> > > > For a complex script like that, it might make sense to run it as a native script query:[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/modules/scripting.html). It will be much faster compared to anything you can do otherwise.
> 
> > > > On Thursday, June 16, 2011 at 10:03 PM, thnguyen wrote:
> > > > 
> > > > > Hello,
> 
> > > > > I'm attempting to run a custom scoring search that includes a script  
> > > > > that was ported over from a Solr boost function. Initial requests to  
> > > > > Elastic never return with a response. I must wait a long time after  
> > > > > sending the initial request before receiving a response from  
> > > > > subsequent request. To me is seems like maybe there is some type of  
> > > > > compilation and loading of the script that is causing that long wait  
> > > > > (unfamiliar of ES internals). Here is a subset of the entire script  
> > > > > that I want to run:
> 
> > > > > \_score + 0.4\*( -150.0\*pow(max(doc[\u0027daysSinceRelease\u0027].value
> > > > > 
> > > > > - -30000.0,0),0.000001)_pow(max(-1826.0-doc[\u0027daysSinceRelease  
> > > > > \u0027].value,0),0.000001) + -100.0_pow(max(doc[\u0027daysSinceRelease  
> > > > > \u0027].value - -1825.0,0),0.000001)_pow(max(-1096.0-  
> > > > > doc[\u0027daysSinceRelease\u0027].value,0),0.000001) + (-100.0+  
> > > > > (doc[\u0027daysSinceRelease\u0027].value - -1095.0)_(0.0 - -100.0)/  
> > > > > (-548.0 - -1095.0))\*pow(max(doc[\u0027daysSinceRelease\u0027].value -  
> > > > > -1095.0,0),0.000001)\*pow(max(-548.0-doc[\u0027daysSinceRelease  
> > > > > \u0027].value,0),0.000001) )
> 
> > > > > What is causing the search to be so slow? BTW the "daysSinceRelease"  
> > > > > field is mapped to the integer datatype and I have 146k documents.  
> > > > > Also this search is super fast in Solr.

---

<div class="post-metadata">

### Author: ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)
#### Post date: [June 17, 2011, 4:31pm UTC](https://discuss.elastic.co/t/custom-scoring-script-running-slow/4642/7 "2011-06-17T16:31:18Z")

</div>

It uses the score you return in your function. If you want to use the current calculated score (the one computed based on matching the query, as you did in your script, then you can use the score() function to get it).

On Friday, June 17, 2011 at 3:02 AM, thnguyen wrote:

> Forgive me but I'm confused. Following your advise on using  
> AbstractFloatSearchScript I have created the following class:  
> [Elastic Search Custom Script · GitHub](https://gist.github.com/1030597). You then mentioned in your next  
> reply the score() method. In my runAsFloat() method do I need to add  
> score() to the value of my function? Does Elastic automatically call  
> runAsFloat() to get the score when I run the following search query (I  
> assume it's correct)?:
> 
> {  
> "query":{  
> "custom\_score":{  
> "query":{  
> "query\_string":{  
> "default\_field":"title",  
> "query":"grand theft auto"  
> }  
> },  
> "lang":"native",  
> "script":"CustomScript "  
> }  
> }  
> }
> 
> On Jun 16, 3:00 pm, Shay Banon \<[shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) ([http://elasticsearch.com](http://elasticsearch.com))\> wrote:
> 
> > Off, and the score you get by calling score(), for example:
> > 
> > long time = doc().numeric("field\_name").getLongValue();  
> > return score() \* (...)
> > 
> > On Friday, June 17, 2011 at 12:59 AM, Shay Banon wrote:
> > 
> > > What you would want to do is extend AbstractFloatSearchScript, and override runAsFloat. Within the method, you can do:
> > 
> > > long time = doc().numeric("field\_name").getLongValue();  
> > > return // the formula you want to do goes here...
> > 
> > > On Thursday, June 16, 2011 at 11:41 PM, thnguyen wrote:
> > 
> > > > When extending AbstractSearchScript do all I need to do is place my  
> > > > script in the score() method?
> > 
> > > > On Jun 16, 12:37 pm, Shay Banon \<[shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) ([http://elasticsearch.com](http://elasticsearch.com))\> wrote:
> > > > 
> > > > > For a complex script like that, it might make sense to run it as a native script query:[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/modules/scripting.html). It will be much faster compared to anything you can do otherwise.
> > 
> > > > > On Thursday, June 16, 2011 at 10:03 PM, thnguyen wrote:
> > > > > 
> > > > > > Hello,
> > 
> > > > > > I'm attempting to run a custom scoring search that includes a script  
> > > > > > that was ported over from a Solr boost function. Initial requests to  
> > > > > > Elastic never return with a response. I must wait a long time after  
> > > > > > sending the initial request before receiving a response from  
> > > > > > subsequent request. To me is seems like maybe there is some type of  
> > > > > > compilation and loading of the script that is causing that long wait  
> > > > > > (unfamiliar of ES internals). Here is a subset of the entire script  
> > > > > > that I want to run:
> > 
> > > > > > \_score + 0.4\*( -150.0\*pow(max(doc[\u0027daysSinceRelease\u0027].value
> > > > > > 
> > > > > > - -30000.0,0),0.000001)_pow(max(-1826.0-doc[\u0027daysSinceRelease  
> > > > > > \u0027].value,0),0.000001) + -100.0_pow(max(doc[\u0027daysSinceRelease  
> > > > > > \u0027].value - -1825.0,0),0.000001)_pow(max(-1096.0-  
> > > > > > doc[\u0027daysSinceRelease\u0027].value,0),0.000001) + (-100.0+  
> > > > > > (doc[\u0027daysSinceRelease\u0027].value - -1095.0)_(0.0 - -100.0)/  
> > > > > > (-548.0 - -1095.0))\*pow(max(doc[\u0027daysSinceRelease\u0027].value -  
> > > > > > -1095.0,0),0.000001)\*pow(max(-548.0-doc[\u0027daysSinceRelease  
> > > > > > \u0027].value,0),0.000001) )
> > 
> > > > > > What is causing the search to be so slow? BTW the "daysSinceRelease"  
> > > > > > field is mapped to the integer datatype and I have 146k documents.  
> > > > > > Also this search is super fast in Solr.

---

<div class="post-metadata">

### Author: ![diemacht](https://avatars.discourse-cdn.com/v4/letter/d/b5e925/32.png) [@diemacht](https://discuss.elastic.co/u/diemacht)
#### Post date: [June 6, 2013, 9:46am UTC](https://discuss.elastic.co/t/custom-scoring-script-running-slow/4642/8 "2013-06-06T09:46:52Z")

</div>

Would marking the fields used in the custom score script as "store" : "yes" in the mapping help?  
Would ES in this case access to the data faster?

---

<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:32am UTC](https://discuss.elastic.co/t/custom-scoring-script-running-slow/4642/9 "2017-07-06T02:32:37Z")

</div>


