Need help with painless scripting

Hi, I have data like this -

curl -H "content-type: application/json" -XPUT "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" -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 -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

Anyone there? thank you

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:

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

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