Using an array as parameter for update script

I am using ElasticSearch 6.5, invoking commands by Kibana 6.5.
My documents have user field which is an array of user structures. I want to construct a script, that will update some of the structures in this array based on the identifier stored in the structure.
Code I came to so far:

POST test_idx1/_doc/1/_update
{
 "script": """ 
 for (int i = 0; i < params.src.size(); i++) {
        boolean f = false;
        for (int j = 0; j < ctx._source.user.size(); j++) {
            if (ctx._source.user[j].uid == params.src[i].uid) {
                ctx._source.user[j].first = params.src[i].first;
                ctx._source.user[j].last = params.src[i].last;
                f=true;
                break;
            }
        }
    if(!f){ctx._source.user.add(params.src[i]);}
    }""",
 "params": {
   "src": [
     {
       "uid" : 1,
       "first": "John_u",
       "last": "Smith_u"
     },
     {
       "uid" : 2,
       "first": "Bob_u",
       "last": "Jones_u"
     },
     {
       "uid" : 4,
       "first": "Anna_u",
       "last": "Right_u"
     }
   ]
 }
}

But it ends with an error:

{
  "error": {
    "root_cause": [
      {
        "type": "remote_transport_exception",
        "reason": "[T-Qm5ly][127.0.0.1:9300][indices:data/write/update[s]]"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "failed to execute script",
    "caused_by": {
      "type": "script_exception",
      "reason": "runtime error",
      "script_stack": [
        "i = 0; i < params.src.size(); i++) {\n        boolean ",
        "                     ^---- HERE"
      ],
      "script": " for (int i = 0; i < params.src.size(); i++) {\n        boolean f = false;\n        for (int j = 0; j < ctx._source.user.size(); j++) {\n            if (ctx._source.user[j].uid == params.src[i].uid) {\n                ctx._source.user[j].first = params.src[i].first;\n                ctx._source.user[j].last = params.src[i].last;\n                f=true;\n                break;\n            }\n        }\n    if(!f){ctx._source.user.add(params.src[i]);}\n    }",
      "lang": "painless",
      "caused_by": {
        "type": "null_pointer_exception",
        "reason": null
      }
    }
  },
  "status": 400
}

So it seems, like params.src is null, while I am providing values for it in params section.
What am I doing wrong?

Ok, I detected the issue. Script code and params section have to be nested inside the script section.
This code is working:

POST test_idx1/_doc/1/_update
{
    "script": {
        "source" : """ 
            for (int i = 0; i < params.src.size(); i++) {
                boolean f = false;
                for (int j = 0; j < ctx._source.user.size(); j++) {
                    if (ctx._source.user[j].uid == params.src[i].uid) {
                        ctx._source.user[j].first = params.src[i].first;
                        ctx._source.user[j].last = params.src[i].last;
                f=true;
                break;
            }
            }
            if(!f){ctx._source.user.add(params.src[i]);}
        }""",
        "lang" : "painless",
        "params": {
            "src": [
                {
                    "uid" : 1,
                    "first": "John_u",
                    "last": "Smith_u"
                },
                {
                    "uid" : 2,
                    "first": "Bob_u",
                    "last": "Jones_u"
                },
                {
                    "uid" : 4,
                    "first": "Anna_u",
                    "last": "Right_u"
                }
            ]
        }
    }
}

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