Loop over a map in Painless

I would like to loop over the entries in the map using the Painless script for updating by query...

"script": {
"inline" : "for (int i = 0; i < ctx._source['Items'].length; ++i) { if (ctx._source['Items'][i].id == params.id) { for (entry : params.changes.entrySet()) { ctx._source['Items'][i][entry.getKey()] = entry.getValue() } } }",
"lang" : "painless",
"params": {
"id": "a7f168ce207080641b87098708433c4b",
"changes" : {
"Key0": "val00",
"Key1": "val01",
"Key2": "val02",
"Key3": "val03",
}
}
}

Does not seem to work though. Get error like:

"reason" : "invalid sequence of tokens near [':'].",

Does not seem to like the looping part it looks like

Can 'params' be treated as a Java HashMap? Can 'params' have nested objects within.

Soon after I posted I found the issue. It was complaining about ':' in the loop, rightly so I guess in an overall json doc. Replaced it with 'in' and it worked.

for (entry in params.changes.entrySet())

is the correct syntax to use

5 Likes

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