# Error while update using script for array kind of field

**URL:** <https://discuss.elastic.co/t/error-while-update-using-script-for-array-kind-of-field/91372>\
**Category:** Elasticsearch\
**Created:** [June 30, 2017, 5:48am UTC](https://discuss.elastic.co/t/error-while-update-using-script-for-array-kind-of-field/91372 "2017-06-30T05:48:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gautam\_Vanani](https://avatars.discourse-cdn.com/v4/letter/g/a9a28c/32.png) [@Gautam\_Vanani](https://discuss.elastic.co/u/Gautam_Vanani)\
**Post date:** [June 30, 2017, 5:48am UTC](https://discuss.elastic.co/t/error-while-update-using-script-for-array-kind-of-field/91372/1 "2017-06-30T05:48:14Z")

</div>

I have tags field in document, but while indxing I may not have values for `tags` field and when tries to add value in it, giving me `null pointer` error.

mapping for tags

```
{
   "type": "text",
    "fields": {
         "keyword": {
          "type": "keyword",
          "ignore_above": 256
       }
 }

```

update by query using below json

> ```
> {
> "script": {
> "inline": "ctx._source.tags.addAll(params.tags); ctx._source.updated_on=params.updated_on; "> "params": {
> "tags": [
> "test"
> ],
> "updated_on": "2017-06-30T05:06:37.933Z"
> },
> "lang": "painless"
> },
> "query": {
> "match": {
> "stone_id": "8822370208"
> }
> }
> }
> 
> ```

Is there any way that I can initialize it as an array  
I tried to init it using null\_value but not working for me

> "tags": {  
> "type": "keyword",  
> "ignore\_above": 256,  
> "null\_value" :   
> }

or  
Is there any way that I can check it is array before updating using script  
if not an array-\> init as array then use addAll()

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [July 10, 2017, 4:31pm UTC](https://discuss.elastic.co/t/error-while-update-using-script-for-array-kind-of-field/91372/2 "2017-07-10T16:31:38Z")

</div>

`null_value` is only applied at indexing time (after update scripts), and only if json `null` is the value of the field.

In your script, you need to check if the field exists. `ctx._source` is a Map. So you can do that with `ctx._source.containsKey('tags')`, and if that returns false, initialize the tags field in source with `ctx._source['tags'] = []`

---

<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:** [August 7, 2017, 4:31pm UTC](https://discuss.elastic.co/t/error-while-update-using-script-for-array-kind-of-field/91372/3 "2017-08-07T16:31:48Z")

</div>

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