# Using an array as parameter for update script

**URL:** https://discuss.elastic.co/t/using-an-array-as-parameter-for-update-script/202998
**Category:** Elasticsearch
**Created:** [October 10, 2019, 10:04am UTC](https://discuss.elastic.co/t/using-an-array-as-parameter-for-update-script/202998 "2019-10-10T10:04:30Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![MMH](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mmh/32/54449_2.png) [@MMH](https://discuss.elastic.co/u/MMH)
#### Post date: [October 10, 2019, 10:04am UTC](https://discuss.elastic.co/t/using-an-array-as-parameter-for-update-script/202998/1 "2019-10-10T10:04:30Z")

</div>

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:

```auto
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:

```auto
{
  "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?

---

<div class="post-metadata">

### Author: ![MMH](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mmh/32/54449_2.png) [@MMH](https://discuss.elastic.co/u/MMH)
#### Post date: [October 10, 2019, 12:12pm UTC](https://discuss.elastic.co/t/using-an-array-as-parameter-for-update-script/202998/2 "2019-10-10T12:12:21Z")

</div>

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

```auto
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"
                }
            ]
        }
    }
}

```

---

<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: [November 7, 2019, 12:12pm UTC](https://discuss.elastic.co/t/using-an-array-as-parameter-for-update-script/202998/3 "2019-11-07T12:12:36Z")

</div>

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