Elastic 2.x function_score week performance with script


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?

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.

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.