Groovy to Painless Script migration


We are upgrading our elastic search and we have found that most of our groovy scripts are not working with the new version.
How can I migrate all my scripts which are on groovy to painless ? Is there a tool or library which can make this task manageable as we have loads of scripts ?

If it has to be done manually then how can I do it, is there any good source / link for this ?
For ex, below is on the aggregation script we use :-

{"aggs": {"group_by_sl_met": {"scripted_metric": {"map_script": "if(_source['taskslatable_has_breached']=='false'||_source['taskslatable_has_breached']=='FALSE'){if(doc['exception'].value == 'F'){}else{}} else{if(doc['exception'].value == 'F'){}else{}}", "init_script": "_agg['map'] = [met:0, notMet:0]", "reduce_script": "return_map = [numerator:0, denominator:0, performance:0];for (a in{return_map.numerator += a.met; return_map.denominator += a.notMet+a.met;}; if(return_map.denominator!=0){return_map.performance =(float) (return_map.numerator*100)/return_map.denominator}else{return_map.performance =''}; return return_map"}}}, "query": {"bool": {"must": [{"match": {"childslaId": "childSLAId"}}, {"match": {"useInComputation": true}}]}}}

Can anyone help me to run it on new elastic search version, I have tried different things to run it but its not working.

There is no automated way of converting. Many scripts "just work". There is anevolving specification for the language you can read. In your case, it looks like the largest problem is _source and _agg. These need to be access through params, eg params._source and params._agg.

I think the main issue is we are upgrading from very old version 1.4.2 to 6.2.4 version.
So most of the scripts are not working.
Anyway I tried your suggestion but I am getting this error exception :-

{"error":{"root_cause":[{"type":"script_exception","reason":"compile error","script_stack":["params._agg['map'] = [met:0, notMet:0]"," ^---- HERE"],"script":"params._agg['map'] = [met:0, notMet:0]","lang":"painless"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"1639","node":"kPKwEQUkRdOJBQCjsWr8VQ","reason":{"type":"script_exception","reason":"compile error","script_stack":["params._agg['map'] = [met:0, notMet:0]"," ^---- HERE"],"script":"params._agg['map'] = [met:0, notMet:0]","lang":"painless","caused_by":{"type":"illegal_argument_exception","reason":"Variable [met] is not defined."}}}]},"status":500}

Please format your requests/responses using triple backticks, and format json with a tool like jq.

When you encounter an error with painless, start by looking at the "reason" in the response. In this case, that is:

"reason":"Variable [met] is not defined."

Looking at the code, I see a map initialization like this:

[met:0, notMet:0]

In painless, string literals need to be in quotes (either single or double). Try changing your map to:

['met':0, 'notMet':0]
1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.