# Elastic 2.x function\_score week performance with script

**URL:** <https://discuss.elastic.co/t/elastic-2-x-function-score-week-performance-with-script/45665>\
**Category:** Elasticsearch\
**Created:** [March 29, 2016, 8:56am UTC](https://discuss.elastic.co/t/elastic-2-x-function-score-week-performance-with-script/45665 "2016-03-29T08:56:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Royi\_Pressburger](https://avatars.discourse-cdn.com/v4/letter/r/ce7236/32.png) [@Royi\_Pressburger](https://discuss.elastic.co/u/Royi_Pressburger)\
**Post date:** [March 29, 2016, 8:56am UTC](https://discuss.elastic.co/t/elastic-2-x-function-score-week-performance-with-script/45665/1 "2016-03-29T08:56:06Z")

</div>

Hi

We have just migrated from elastic version 1.7 to version 2.2, and we have encountered some extremely weird behavior.

We have a score function with a script:

`{ "script_score": { "params": { "fields": ["country_code_volumes/840", "country_code_volumes/826", "country_code_volumes/036", "country_code_volumes/124"], "div_f": "volume", "multiply": 1, "add": 0 }, script": "doc_v=doc[div_f].value; if (doc_v == 0) {return score;}; score=0; for(field in fields) { score += doc[field].value;}; score /= (doc_v*1d); ((score + add) * multiply)" } }`

we notice that taking this function with a query takes x10 times slower then if for example there is only one element in the `fields` array.

we tried to narrow it down more.. and we have come to a conclusion that the line inside the for loop:  
`score += doc[field].value` is the reason for the slow performance..  
for example if I change that line to: `score = doc[field].value` the query return 10 times faster then with the `+` operator..

Anyone have any idea why is that?

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [March 31, 2016, 9:08am UTC](https://discuss.elastic.co/t/elastic-2-x-function-score-week-performance-with-script/45665/2 "2016-03-31T09:08:48Z")

</div>

Not a groovy expert, but I suspect this has something to do with type promotion. Maybe groovy promotes you numbers to big decimals in order to perform the addition.

---

<div class="post-metadata">

**Author:** ![Royi\_Pressburger](https://avatars.discourse-cdn.com/v4/letter/r/ce7236/32.png) [@Royi\_Pressburger](https://discuss.elastic.co/u/Royi_Pressburger)\
**Post date:** [March 31, 2016, 9:28am UTC](https://discuss.elastic.co/t/elastic-2-x-function-score-week-performance-with-script/45665/3 "2016-03-31T09:28:53Z")

</div>

Not sure that is it because if we take the same script, and instead of preforming the for loop we do something like this:  
`score += doc[fields[0]].value; ..... ... score += doc[fields[last]].value;`  
The performance is still good.

---

<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 5, 2017, 11:03pm UTC](https://discuss.elastic.co/t/elastic-2-x-function-score-week-performance-with-script/45665/4 "2017-07-05T23:03:42Z")

</div>


