# Groovy to Painless Script migration

**URL:** https://discuss.elastic.co/t/groovy-to-painless-script-migration/132421
**Category:** Elasticsearch
**Created:** [May 18, 2018, 6:52am UTC](https://discuss.elastic.co/t/groovy-to-painless-script-migration/132421 "2018-05-18T06:52:03Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![avinashchauhan](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@avinashchauhan](https://discuss.elastic.co/u/avinashchauhan)
#### Post date: [May 18, 2018, 6:52am UTC](https://discuss.elastic.co/t/groovy-to-painless-script-migration/132421/1 "2018-05-18T06:52:03Z")

</div>

Hi,

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'){\_agg.map.met++}else{\_agg.map.notMet++}} else{if(doc['exception'].value == 'F'){\_agg.map.notMet++}else{\_agg.map.met++}}", "init\_script": "\_agg['map'] = [met:0, notMet:0]", "reduce\_script": "return\_map = [numerator:0, denominator:0, performance:0];for (a in \_aggs.map){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.  
Thanks

---

<div class="post-metadata">

### Author: ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)
#### Post date: [May 18, 2018, 6:04pm UTC](https://discuss.elastic.co/t/groovy-to-painless-script-migration/132421/2 "2018-05-18T18:04:10Z")

</div>

There is no automated way of converting. Many scripts "just work". There is an[evolving specification](https://www.elastic.co/guide/en/elasticsearch/painless/master/painless-lang-spec.html) 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`.

---

<div class="post-metadata">

### Author: ![avinashchauhan](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@avinashchauhan](https://discuss.elastic.co/u/avinashchauhan)
#### Post date: [May 19, 2018, 3:53am UTC](https://discuss.elastic.co/t/groovy-to-painless-script-migration/132421/3 "2018-05-19T03:53:26Z")

</div>

Hi,  
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}

---

<div class="post-metadata">

### Author: ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)
#### Post date: [May 19, 2018, 5:47am UTC](https://discuss.elastic.co/t/groovy-to-painless-script-migration/132421/4 "2018-05-19T05:47:45Z")

</div>

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:

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

```

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

```auto
[met:0, notMet:0]

```

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

```auto
['met':0, 'notMet':0]

```

---

<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: [June 16, 2018, 5:47am UTC](https://discuss.elastic.co/t/groovy-to-painless-script-migration/132421/5 "2018-06-16T05:47:47Z")

</div>

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