# Update or remove object from array not working in elastic search

**URL:** https://discuss.elastic.co/t/update-or-remove-object-from-array-not-working-in-elastic-search/20132
**Category:** Elasticsearch
**Created:** [October 8, 2014, 7:18am UTC](https://discuss.elastic.co/t/update-or-remove-object-from-array-not-working-in-elastic-search/20132 "2014-10-08T07:18:50Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Rajit\_Garg](https://avatars.discourse-cdn.com/v4/letter/r/8edcca/32.png) [@Rajit\_Garg](https://discuss.elastic.co/u/Rajit_Garg)
#### Post date: [October 8, 2014, 7:18am UTC](https://discuss.elastic.co/t/update-or-remove-object-from-array-not-working-in-elastic-search/20132/1 "2014-10-08T07:18:50Z")

</div>

_I have read the nested docs and create mapping and get mapping as follow_

"fields": {  
"type": "nested",  
"properties": {  
"\_id": {  
"type": "string"  
},  
"name": {  
"type": "string"  
}  
}  
}

_where fields is my array contain the following_

"fields": [  
{  
"\_id": "1",  
"name": "naveen"  
},  
{  
"\_id": "2",  
"name": "rajit"  
},  
{  
"\_id": "3",  
"name": "ashu"  
}  
]

_I want to update and remove object from this array, and I am doing this,_

client.update({"index": "daffosw","type": "daffo","id": "2501","body": {  
"script" : "for (int i = 0; i \< ctx.\_source.fields.size(); i++){  
if(ctx.\_source.fields[i]\_id == id){  
ctx.\_source.fields[i].name = 'Updated John';  
}  
}",  
"params" : {  
"id" : "1"  
}  
}}

_and I am getting this error_

VerifyError[Bad type on operand stack  
Exception Details:  
Location:

ASMAccessorImpl\_18539991071412750271920.getValue(Ljava/lang/Object;Ljava/lang/Object;Lorg/elasticsearch/common/mvel2/integration/VariableResolverFactory;)Ljava/lang/Object;  
@49: invokeinterface  
Reason:  
Type 'java/lang/Object' (current frame, stack[1]) is not assignable to  
integer  
Current Frame:  
bci: @49  
flags: { }  
locals: { 'ASMAccessorImpl\_18539991071412750271920',  
'java/lang/Object', 'java/lang/Object',  
'org/elasticsearch/common/mvel2/integration/VariableResolverFactory' }  
stack: { 'java/util/List', 'java/lang/Object' }............  
.............................................................  
.........................................................

_then I have tried this_

client.update({"index": "daffosw","type": "daffo","id": "2501","body": {  
"script" : "for (int i = 0; i \< ctx.\_source.fields.size(); i++){  
if(ctx.\_source.fields[i]\_id == id){  
ctx.\_source.fields.remove(i);  
}  
}",  
"params" : {  
"id" : "1"  
}  
}}

_and this is giving the same type of error_

_then I have tried_

client.update({"index": "daffosw","type": "daffo","id": "2501","body": {  
"script" : "for (element : doc['fields'].values){  
if(element.\_id == id){  
element.name = 'Updated John';  
}  
}",  
"params" : {  
"id" : "1"  
}  
}}

_which gives this error_

_elasticsearchIllegalArgumentException[failed to execute script]; nested:  
PropertyAccessException[[Error: unresolvable property or identifier: doc]_

please help....

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/1c65f529-6e6b-4415-8f93-662acbac9929%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1c65f529-6e6b-4415-8f93-662acbac9929%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Rajit\_Garg](https://avatars.discourse-cdn.com/v4/letter/r/8edcca/32.png) [@Rajit\_Garg](https://discuss.elastic.co/u/Rajit_Garg)
#### Post date: [October 8, 2014, 11:35am UTC](https://discuss.elastic.co/t/update-or-remove-object-from-array-not-working-in-elastic-search/20132/2 "2014-10-08T11:35:42Z")

</div>

Hey

I figured it out, I am able to update but I am not able to remove object  
that satisfies the condition

PUT twitter/twit/1  
{  
"list": [  
{  
"tweet\_id": "1",  
"a": "b"  
},  
{  
"tweet\_id": "123",  
"a": "f"  
}  
]  
}

POST /twitter/twit/1/\_update  
{  
"script": "foreach (item : ctx.\_source.list) {if (item['tweet\_id'] ==  
tweet\_id) { item['new\_field'] = 'ghi'; } }",  
"params": {  
"tweet\_id": "123"  
}  
}  
this is working

for remove i am doing this

POST /twitter/twit/1/\_update  
{  
"script": "foreach (item : ctx.\_source.list) {if (item['tweet\_id'] ==  
tweet\_id) {ctx.\_source.list.remove(item); } }",  
"params": {  
"tweet\_id": "123"  
}  
}

but this is not working and giving this error,

ElasticsearchIllegalArgumentException[failed to execute script]; nested:  
ConcurrentModificationException;  
Error: ElasticsearchIllegalArgumentException[failed to execute script];  
nested: ConcurrentModificationException;  
..................................  
...............................

please help........

On Wednesday, October 8, 2014 12:48:50 PM UTC+5:30, Rajit Garg wrote:

> _I have read the nested docs and create mapping and get mapping as follow_
> 
> "fields": {  
> "type": "nested",  
> "properties": {  
> "\_id": {  
> "type": "string"  
> },  
> "name": {  
> "type": "string"  
> }  
> }  
> }
> 
> _where fields is my array contain the following_
> 
> "fields": [  
> {  
> "\_id": "1",  
> "name": "naveen"  
> },  
> {  
> "\_id": "2",  
> "name": "rajit"  
> },  
> {  
> "\_id": "3",  
> "name": "ashu"  
> }  
> ]
> 
> _I want to update and remove object from this array, and I am doing this,_
> 
> client.update({"index": "daffosw","type": "daffo","id": "2501","body": {  
> "script" : "for (int i = 0; i \< ctx.\_source.fields.size(); i++){  
> if(ctx.\_source.fields[i]\_id == id){  
> ctx.\_source.fields[i].name = 'Updated John';  
> }  
> }",  
> "params" : {  
> "id" : "1"  
> }  
> }}
> 
> _and I am getting this error_
> 
> VerifyError[Bad type on operand stack  
> Exception Details:  
> Location:
> 
> ASMAccessorImpl\_18539991071412750271920.getValue(Ljava/lang/Object;Ljava/lang/Object;Lorg/elasticsearch/common/mvel2/integration/VariableResolverFactory;)Ljava/lang/Object;  
> @49: invokeinterface  
> Reason:  
> Type 'java/lang/Object' (current frame, stack[1]) is not assignable to  
> integer  
> Current Frame:  
> bci: @49  
> flags: { }  
> locals: { 'ASMAccessorImpl\_18539991071412750271920',  
> 'java/lang/Object', 'java/lang/Object',  
> 'org/elasticsearch/common/mvel2/integration/VariableResolverFactory' }  
> stack: { 'java/util/List', 'java/lang/Object' }............  
> .............................................................  
> .........................................................
> 
> _then I have tried this_
> 
> client.update({"index": "daffosw","type": "daffo","id": "2501","body": {  
> "script" : "for (int i = 0; i \< ctx.\_source.fields.size(); i++){  
> if(ctx.\_source.fields[i]\_id == id){  
> ctx.\_source.fields.remove(i);  
> }  
> }",  
> "params" : {  
> "id" : "1"  
> }  
> }}
> 
> _and this is giving the same type of error_
> 
> _then I have tried_
> 
> client.update({"index": "daffosw","type": "daffo","id": "2501","body": {  
> "script" : "for (element : doc['fields'].values){  
> if(element.\_id == id){  
> element.name = 'Updated John';  
> }  
> }",  
> "params" : {  
> "id" : "1"  
> }  
> }}
> 
> _which gives this error_
> 
> _elasticsearchIllegalArgumentException[failed to execute script]; nested:  
> PropertyAccessException[[Error: unresolvable property or identifier: doc]_
> 
> please help....

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/6c3b31f6-2611-4e32-9a8d-a03b01854685%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/6c3b31f6-2611-4e32-9a8d-a03b01854685%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 12:57am UTC](https://discuss.elastic.co/t/update-or-remove-object-from-array-not-working-in-elastic-search/20132/3 "2017-07-06T00:57:42Z")

</div>


