# Need help with painless scripting

**URL:** <https://discuss.elastic.co/t/need-help-with-painless-scripting/130059>\
**Category:** Elasticsearch\
**Created:** [April 30, 2018, 7:18pm UTC](https://discuss.elastic.co/t/need-help-with-painless-scripting/130059 "2018-04-30T19:18:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![elasticuser1](https://avatars.discourse-cdn.com/v4/letter/e/a3d4f5/32.png) [@elasticuser1](https://discuss.elastic.co/u/elasticuser1)\
**Post date:** [April 30, 2018, 7:18pm UTC](https://discuss.elastic.co/t/need-help-with-painless-scripting/130059/1 "2018-04-30T19:18:30Z")

</div>

Hi, I have data like this -

> curl -H "content-type: application/json" -XPUT "[http://xxx:9200/currencylookup/inr/usd](http://xxx:9200/currencylookup/inr/usd)" -d'  
> {  
> "currency": "usd",  
> "currencyname": "US Dollar",  
> "units\_per\_inr": 0.016155969,  
> "inr\_per\_unit": 61.89662756  
> }'
> 
> curl -H "content-type: application/json" -XPUT "[http://xxx:9200/expenses/overseas/1](http://xxx:9200/expenses/overseas/1)" -d'  
> { "amount":100, "currency":"usd", "location":"USA" }'

And I am trying to join these two indexes using scripted metric aggregation like below -

> curl -H "content-type: application/json" -XPOST [http://xxx:9200/expenses,currencylookup/\_search?size=0](http://xxx:9200/expenses,currencylookup/_search?size=0) -d '{  
> "aggs": {  
> "results": {  
> "scripted\_metric": {  
> "init\_script" : "params.\_agg.exp = ; params.\_agg.cur = ; ",  
> "map\_script" : "if (doc.\_type.value == \u0027inr\u0027) { params.\_agg.cur.add([doc.currency.value, doc.inr\_per\_unit.value]); } else { params.\_agg.exp.add([doc.currency.value, doc.amount.value]); }",  
> "reduce\_script" : "params.exp = ; params.cur = ; params.results = ; for (item in params.\_aggs) { params.exp.add(item.exp); params.cur.add(item.cur); } for (c in params.cur) { for (e in params.exp) { if (e[0] == c[0]) { params.results.add(e[1]\*c[1]); } } } return params.results;"  
> }  
> }  
> }  
> }'

This is returning index out of bounds exception -

> {"error":{"root\_cause":,"type":"search\_phase\_execution\_exception","reason":"","phase":"fetch","grouped":true,"failed\_shards":,"caused\_by":{"type":"script\_exception","reason":"runtime error","script\_stack":["java.util.ArrayList.rangeCheck(ArrayList.java:653)","java.util.ArrayList.get(ArrayList.java:429)","if (e[0] == c[0]) { "," ^---- HERE"],"script":"params.exp = ; params.cur = ; params.results = ; for (item in params.\_aggs) { params.exp.add(item.exp); params.cur.add(item.cur); } for (c in params.cur) { for (e in params.exp) { if (e[0] == c[0]) { params.results.add(e[1]\*c[1]); } } } return params.results;","lang":"painless","caused\_by":{"type":"index\_out\_of\_bounds\_exception","reason":"Index: 0, Size: 0"}}},"status":503}

Not sure how to fix this. Please help. Thanks

---

<div class="post-metadata">

**Author:** ![elasticuser1](https://avatars.discourse-cdn.com/v4/letter/e/a3d4f5/32.png) [@elasticuser1](https://discuss.elastic.co/u/elasticuser1)\
**Post date:** [May 1, 2018, 4:55pm UTC](https://discuss.elastic.co/t/need-help-with-painless-scripting/130059/2 "2018-05-01T16:55:13Z")

</div>

Anyone there? thank you

---

<div class="post-metadata">

**Author:** ![pjanzen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pjanzen/32/13756_2.png) [@pjanzen](https://discuss.elastic.co/u/pjanzen)\
**Post date:** [May 1, 2018, 5:15pm UTC](https://discuss.elastic.co/t/need-help-with-painless-scripting/130059/3 "2018-05-01T17:15:49Z")

</div>

In your script you have 2 lists. e and c. The error is that you are referencing a list item that is not there. You need to test if the lists are filled before you de-reference them.

This might help:

- [Painless link 1](https://discuss.elastic.co/t/painless-if-doc-field-value-in-list/126598)
- [main docs.](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/modules-scripting-painless-syntax.html)
- [This a good howto](https://www.compose.com/articles/how-to-script-painless-ly-in-elasticsearch/)

---

<div class="post-metadata">

**Author:** ![elasticuser1](https://avatars.discourse-cdn.com/v4/letter/e/a3d4f5/32.png) [@elasticuser1](https://discuss.elastic.co/u/elasticuser1)\
**Post date:** [May 1, 2018, 8:41pm UTC](https://discuss.elastic.co/t/need-help-with-painless-scripting/130059/4 "2018-05-01T20:41:22Z")

</div>

Thanks @pjanzen. How to reference the list item? Can you please look into my code? I am not clear.. Any help is appreciated..

---

<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:** [May 29, 2018, 8:41pm UTC](https://discuss.elastic.co/t/need-help-with-painless-scripting/130059/5 "2018-05-29T20:41:27Z")

</div>

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