Nested document updation using painless script

POST test/b/1
{
    "name": "andrew",
    "b": [{"x":"c1", "y": 0}, {"x":"c2", "y": 0}]
}
GET test/b/1

POST test/b/1/_update
{
  "script": {
    "lang": "painless",
    "source": "Map item; for (int i=0;i<ctx._source.b.size();i++) { item=ctx._source.b[i]; if (item['x'] == x_id) { ctx._source.b[i] = newobj} }",
    "params": {
      "x_id": "c1",
      "newobj": {
        "x": "c1",
        "y": 4222
      }
    }
  }
}

is throwing an error

{
  "error": {
    "root_cause": [
      {
        "type": "remote_transport_exception",
        "reason": "[RiyoP2B][10.65.5.170:9300][indices:data/write/update[s]]"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "failed to execute script",
    "caused_by": {
      "type": "script_exception",
      "reason": "compile error",
      "script_stack": [
        "... e.b[i]; if (item['x'] == x_id) { ctx._source.b[i]  ...",
        "                             ^---- HERE"
      ],
      "script": "Map item; for (int i=0;i<ctx._source.b.size();i++) { item=ctx._source.b[i]; if (item['x'] == x_id) { ctx._source.b[i] = newobj} }",
      "lang": "painless",
      "caused_by": {
        "type": "illegal_argument_exception",
        "reason": "Variable [x_id] is not defined."
      }
    }
  },
  "status": 400
}

What is the mistake in this script

ES version: 5.6.3

x_id is part of the params map. Try doing params.x_id

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