# Update array on source

**URL:** <https://discuss.elastic.co/t/update-array-on-source/44951>\
**Category:** Elasticsearch\
**Created:** [March 21, 2016, 6:49am UTC](https://discuss.elastic.co/t/update-array-on-source/44951 "2016-03-21T06:49:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gpong](https://avatars.discourse-cdn.com/v4/letter/g/d6d6ee/32.png) [@gpong](https://discuss.elastic.co/u/gpong)\
**Post date:** [March 21, 2016, 6:49am UTC](https://discuss.elastic.co/t/update-array-on-source/44951/1 "2016-03-21T06:49:38Z")

</div>

Hi guys, I use Elasticsearch 2.2.0 and I tried to update array inside a source  
here is my source  
{  
"list":[  
{  
"tweet\_id":"1",  
"a":"b"  
},  
{  
"tweet\_id":"123",  
"a":"f"  
}  
]  
}

and i use this script to update the data

{  
"script":"foreach (item : ctx.\_source.list) {if (item['tweet\_id'] ==tweet\_id) { item['new\_field'] = 'ghi'; } }",  
"params":{  
"tweet\_id":"123"  
}  
}

but the result is error  
here is the error

{  
"error": {  
"root\_cause": [  
{  
"type": "remote\_transport\_exception",  
"reason": "[node1\_ES2-2][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": "failed to run inline script [foreach (item : ctx.\_source.list) {if (item['tweet\_id'] ==tweet\_id) { item['new\_field'] = 'ghi'; } }] using lang [groovy]",  
"caused\_by": {  
"type": "missing\_method\_exception",  
"reason": "No signature of method: 1a98d9460b8843033b48ef3d6cf1537914f98f76.foreach() is applicable for argument types: (java.util.LinkedHashMap, 1a98d9460b8843033b48ef3d6cf1537914f98f76$\_run\_closure1) values: [[item:[[tweet\_id:1, a:b], [tweet\_id:123, a:f]]], 1a98d9460b8843033b48ef3d6cf1537914f98f76$\_run\_closure1@74f92d43] Possible solutions: each(groovy.lang.Closure)"  
}  
}  
},  
"status": 400  
}

Can anyone tell me what is wrong here?

Thanks

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [March 21, 2016, 8:12am UTC](https://discuss.elastic.co/t/update-array-on-source/44951/2 "2016-03-21T08:12:36Z")

</div>

Hey,

you tried to write javascript as your scripting language (at least that's what I am interpreting here), but you need to use groovy here. Groovy does not have a concept called `foreach`, you may want to check out some groovy examples...

As an example (without knowing any of your data structure or having tested this)

```auto
ctx.source.list.each { elem -> if (elem.tweetId == tweet_id) { ctx.source.new_field = 'ghi' } }

```

Hope this helps...

--Alex

---

<div class="post-metadata">

**Author:** ![gpong](https://avatars.discourse-cdn.com/v4/letter/g/d6d6ee/32.png) [@gpong](https://discuss.elastic.co/u/gpong)\
**Post date:** [March 21, 2016, 9:25am UTC](https://discuss.elastic.co/t/update-array-on-source/44951/3 "2016-03-21T09:25:38Z")

</div>

Hi Alexander

Thanks for the answer. But when I use the script from above , it show error "Cannot get property 'list' on null object". But I have value on list array. Can you tell me what is going wrong here?? thank a lot

here is my properties setting  
"list": {  
"properties": {  
"a": {  
"type": "string"  
},  
"tweet\_id": {  
"type": "string"  
}  
}  
},

and the error is :  
{  
"error": {  
"root\_cause": [  
{  
"type": "remote\_transport\_exception",  
"reason": "[node1\_ES2-2][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": "failed to run inline script [ctx.source.list.each { }] using lang [groovy]",  
"caused\_by": {  
"type": "null\_pointer\_exception",  
"reason": "Cannot get property 'list' on null object"  
}  
}  
},  
"status": 400  
}

---

<div class="post-metadata">

**Author:** ![gpong](https://avatars.discourse-cdn.com/v4/letter/g/d6d6ee/32.png) [@gpong](https://discuss.elastic.co/u/gpong)\
**Post date:** [March 21, 2016, 9:49am UTC](https://discuss.elastic.co/t/update-array-on-source/44951/4 "2016-03-21T09:49:33Z")

</div>

I found out what the problem is. use \_source instead of source.

thanks

---

<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:** [July 5, 2017, 11:06pm UTC](https://discuss.elastic.co/t/update-array-on-source/44951/5 "2017-07-05T23:06:40Z")

</div>


