# How to update a object within array in elastic search

**URL:** <https://discuss.elastic.co/t/how-to-update-a-object-within-array-in-elastic-search/20115>\
**Category:** Elasticsearch\
**Created:** [October 7, 2014, 1:38pm UTC](https://discuss.elastic.co/t/how-to-update-a-object-within-array-in-elastic-search/20115 "2014-10-07T13:38:52Z")\
**Posts on this page:** 10\
**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 7, 2014, 1:38pm UTC](https://discuss.elastic.co/t/how-to-update-a-object-within-array-in-elastic-search/20115/1 "2014-10-07T13:38:52Z")

</div>

Here I have a array type field in my document of elastic search, like  
below:-

"prog\_lists": [  
{  
"description": "engineer",  
"age": 25,  
"name": "ashu"  
},  
{  
"description": "programmer",  
"age": 26,  
"name": "rajit"  
},  
{  
"description": "designer",  
"age": 27,  
"name": "naveen"  
}  
]

I want to update only those objects which satisfy any condition, like I want to query where name equals to ashu update his age to 30.

please suggest something....

Thanks in advance....

--  
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/1ed154d1-a441-46c9-bc37-82cc360ef7aa%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1ed154d1-a441-46c9-bc37-82cc360ef7aa%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [October 7, 2014, 1:46pm UTC](https://discuss.elastic.co/t/how-to-update-a-object-within-array-in-elastic-search/20115/2 "2014-10-07T13:46:36Z")

</div>

Use nested documents.

See  
[http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/nested-objects.html#nested-objects](http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/nested-objects.html#nested-objects)  
[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-nested-type.html#mapping-nested-type](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-nested-type.html#mapping-nested-type)

--  
David Pilato | Technical Advocate | [elasticsearch.com](http://elasticsearch.com)  
[david.pilato@elasticsearch.com](mailto:david.pilato@elasticsearch.com)  
@dadoonet | @elasticsearchfr | @scrutmydocs

Le 7 octobre 2014 à 15:38:56, Rajit Garg ([rajit.garg@daffodilsw.com](mailto:rajit.garg@daffodilsw.com)) a écrit:

Here I have a array type field in my document of elastic search, like below:-

"prog\_lists": [  
{  
"description": "engineer",  
"age": 25,  
"name": "ashu"  
},  
{  
"description": "programmer",  
"age": 26,  
"name": "rajit"  
},  
{  
"description": "designer",  
"age": 27,  
"name": "naveen"  
}  
]

I want to update only those objects which satisfy any condition, like I want to query where name equals to ashu update his age to 30.

please suggest something....

Thanks in advance....

--  
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/1ed154d1-a441-46c9-bc37-82cc360ef7aa%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1ed154d1-a441-46c9-bc37-82cc360ef7aa%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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/etPan.5433eebd.2cd89a32.7bd4%40MacBook-Air-de-David.local](https://groups.google.com/d/msgid/elasticsearch/etPan.5433eebd.2cd89a32.7bd4%40MacBook-Air-de-David.local).  
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, 6:32am UTC](https://discuss.elastic.co/t/how-to-update-a-object-within-array-in-elastic-search/20115/3 "2014-10-08T06:32:01Z")

</div>

\*I have seen the nested docs, and create mapping and getting mapping as  
follow:- \*

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

where fields is my array containing

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

_Now I want to update and remove object from this array, and I am doing  
this as follow:-_

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"  
}  
}}

_but this will give this error_

VerifyError[Bad type on operand stack  
Exception Details:  
Location:  
ASMAccessorImpl\_19994590841412749253560.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\_19994590841412749253560', 'java/lang/Object', 'java/lang/Object', 'org/elasticsearch/common/mvel2/integration/VariableResolverFactory' }  
stack: { 'java/util/List', 'java/lang/Object' }  
Bytecode:  
0000000: 2d12 0eb9 0014 0200 b900 1901 00c0 001b  
0000010: 121d b900 2102 00c0 001b 1223 b900 2102  
0000020: 00c0 0025 2d12 27b9 0014 0200 b900 1901  
0000030: 00b9 002a 0200 c000 1b12 2cb9 0021 0200  
0000040: b0  
]  
Error: VerifyError[Bad type on operand stack..................

..............................

..................................

...................................

.....................................

................................

\*then I am doing this \*

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

_and this is giving this error_

ElasticsearchIllegalArgumentException[failed to execute script]; nested: PropertyAccessException[[Error: could not access: \_id; in class: java.lang.Integer]

_then I am doing this_

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" } }}

_and this is giving this error_

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

please help

On Tuesday, October 7, 2014 7:17:02 PM UTC+5:30, David Pilato wrote:

> Use nested documents.
> 
> See
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/nested-objects.html#nested-objects)
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-nested-type.html#mapping-nested-type)
> 
> [http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-nested-type.html#mapping-nested-type](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-nested-type.html#mapping-nested-type)
> 
> --  
> _David Pilato_ | Technical Advocate | _[elasticsearch.com](http://elasticsearch.com)  
> [http://elasticsearch.com](http://elasticsearch.com)_  
> [david....@elasticsearch.com](mailto:david....@elasticsearch.com) \<javascript:\>  
> @dadoonet [https://twitter.com/dadoonet](https://twitter.com/dadoonet) | @elasticsearchfr  
> [https://twitter.com/elasticsearchfr](https://twitter.com/elasticsearchfr) | @scrutmydocs  
> [http://twitter.com/scrutmydocs](http://twitter.com/scrutmydocs)  
> [https://twitter.com/scrutmydocs](https://twitter.com/scrutmydocs)
> 
> Le 7 octobre 2014 à 15:38:56, Rajit Garg ([rajit...@daffodilsw.com](mailto:rajit...@daffodilsw.com)  
> \<javascript:\>) a écrit:
> 
> Here I have a array type field in my document of Elasticsearch, like  
> below:-
> 
> "prog\_lists": [  
> {  
> "description": "engineer",  
> "age": 25,  
> "name": "ashu"  
> },  
> {  
> "description": "programmer",  
> "age": 26,  
> "name": "rajit"  
> },  
> {  
> "description": "designer",  
> "age": 27,  
> "name": "naveen"  
> }  
> ]
> 
> I want to update only those objects which satisfy any condition, like I want to query where name equals to ashu update his age to 30.
> 
> please suggest something....
> 
> Thanks in advance....
> 
> --  
> 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/1ed154d1-a441-46c9-bc37-82cc360ef7aa%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1ed154d1-a441-46c9-bc37-82cc360ef7aa%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/1ed154d1-a441-46c9-bc37-82cc360ef7aa%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/1ed154d1-a441-46c9-bc37-82cc360ef7aa%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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/894db422-23e9-44b2-9432-585bda23c73c%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/894db422-23e9-44b2-9432-585bda23c73c%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, 6:44am UTC](https://discuss.elastic.co/t/how-to-update-a-object-within-array-in-elastic-search/20115/4 "2014-10-08T06:44:48Z")

</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

127.0.0.1:5000/rest/update?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

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

please help....

On Tuesday, October 7, 2014 7:17:02 PM UTC+5:30, David Pilato wrote:

> Use nested documents.
> 
> See
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/nested-objects.html#nested-objects)
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-nested-type.html#mapping-nested-type)
> 
> [http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-nested-type.html#mapping-nested-type](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-nested-type.html#mapping-nested-type)
> 
> --  
> _David Pilato_ | Technical Advocate | _[elasticsearch.com](http://elasticsearch.com)  
> [http://elasticsearch.com](http://elasticsearch.com)_  
> [david....@elasticsearch.com](mailto:david....@elasticsearch.com) \<javascript:\>  
> @dadoonet [https://twitter.com/dadoonet](https://twitter.com/dadoonet) | @elasticsearchfr  
> [https://twitter.com/elasticsearchfr](https://twitter.com/elasticsearchfr) | @scrutmydocs  
> [http://twitter.com/scrutmydocs](http://twitter.com/scrutmydocs)  
> [https://twitter.com/scrutmydocs](https://twitter.com/scrutmydocs)
> 
> Le 7 octobre 2014 à 15:38:56, Rajit Garg ([rajit...@daffodilsw.com](mailto:rajit...@daffodilsw.com)  
> \<javascript:\>) a écrit:
> 
> Here I have a array type field in my document of Elasticsearch, like  
> below:-
> 
> "prog\_lists": [  
> {  
> "description": "engineer",  
> "age": 25,  
> "name": "ashu"  
> },  
> {  
> "description": "programmer",  
> "age": 26,  
> "name": "rajit"  
> },  
> {  
> "description": "designer",  
> "age": 27,  
> "name": "naveen"  
> }  
> ]
> 
> I want to update only those objects which satisfy any condition, like I want to query where name equals to ashu update his age to 30.
> 
> please suggest something....
> 
> Thanks in advance....
> 
> --  
> 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/1ed154d1-a441-46c9-bc37-82cc360ef7aa%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1ed154d1-a441-46c9-bc37-82cc360ef7aa%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/1ed154d1-a441-46c9-bc37-82cc360ef7aa%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/1ed154d1-a441-46c9-bc37-82cc360ef7aa%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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/35ee59f5-d769-4f54-ab0a-3d39e62f3820%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/35ee59f5-d769-4f54-ab0a-3d39e62f3820%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, 7:01am UTC](https://discuss.elastic.co/t/how-to-update-a-object-within-array-in-elastic-search/20115/5 "2014-10-08T07:01:51Z")

</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...._

On Tuesday, October 7, 2014 7:17:02 PM UTC+5:30, David Pilato wrote:

> Use nested documents.
> 
> See
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/nested-objects.html#nested-objects)
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-nested-type.html#mapping-nested-type)
> 
> [http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-nested-type.html#mapping-nested-type](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-nested-type.html#mapping-nested-type)
> 
> --  
> _David Pilato_ | Technical Advocate | _[elasticsearch.com](http://elasticsearch.com)  
> [http://elasticsearch.com](http://elasticsearch.com)_  
> [david....@elasticsearch.com](mailto:david....@elasticsearch.com) \<javascript:\>  
> @dadoonet [https://twitter.com/dadoonet](https://twitter.com/dadoonet) | @elasticsearchfr  
> [https://twitter.com/elasticsearchfr](https://twitter.com/elasticsearchfr) | @scrutmydocs  
> [http://twitter.com/scrutmydocs](http://twitter.com/scrutmydocs)  
> [https://twitter.com/scrutmydocs](https://twitter.com/scrutmydocs)
> 
> Le 7 octobre 2014 à 15:38:56, Rajit Garg ([rajit...@daffodilsw.com](mailto:rajit...@daffodilsw.com)  
> \<javascript:\>) a écrit:
> 
> Here I have a array type field in my document of Elasticsearch, like  
> below:-
> 
> "prog\_lists": [  
> {  
> "description": "engineer",  
> "age": 25,  
> "name": "ashu"  
> },  
> {  
> "description": "programmer",  
> "age": 26,  
> "name": "rajit"  
> },  
> {  
> "description": "designer",  
> "age": 27,  
> "name": "naveen"  
> }  
> ]
> 
> I want to update only those objects which satisfy any condition, like I want to query where name equals to ashu update his age to 30.
> 
> please suggest something....
> 
> Thanks in advance....
> 
> --  
> 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/1ed154d1-a441-46c9-bc37-82cc360ef7aa%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1ed154d1-a441-46c9-bc37-82cc360ef7aa%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/1ed154d1-a441-46c9-bc37-82cc360ef7aa%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/1ed154d1-a441-46c9-bc37-82cc360ef7aa%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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/a1f51260-3d2d-49ad-bcab-e3d481f62183%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a1f51260-3d2d-49ad-bcab-e3d481f62183%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, 7:10am UTC](https://discuss.elastic.co/t/how-to-update-a-object-within-array-in-elastic-search/20115/6 "2014-10-08T07:10:54Z")

</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....

On Tuesday, October 7, 2014 7:08:52 PM UTC+5:30, Rajit Garg wrote:

> Here I have a array type field in my document of Elasticsearch, like  
> below:-
> 
> "prog\_lists": [  
> {  
> "description": "engineer",  
> "age": 25,  
> "name": "ashu"  
> },  
> {  
> "description": "programmer",  
> "age": 26,  
> "name": "rajit"  
> },  
> {  
> "description": "designer",  
> "age": 27,  
> "name": "naveen"  
> }  
> ]
> 
> I want to update only those objects which satisfy any condition, like I want to query where name equals to ashu update his age to 30.
> 
> please suggest something....
> 
> Thanks in advance....

--  
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/cc5e8892-9083-492e-834f-ac3be92abb06%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/cc5e8892-9083-492e-834f-ac3be92abb06%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, 7:12am UTC](https://discuss.elastic.co/t/how-to-update-a-object-within-array-in-elastic-search/20115/7 "2014-10-08T07:12:10Z")

</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....

On Tuesday, October 7, 2014 7:08:52 PM UTC+5:30, Rajit Garg wrote:

> Here I have a array type field in my document of Elasticsearch, like  
> below:-
> 
> "prog\_lists": [  
> {  
> "description": "engineer",  
> "age": 25,  
> "name": "ashu"  
> },  
> {  
> "description": "programmer",  
> "age": 26,  
> "name": "rajit"  
> },  
> {  
> "description": "designer",  
> "age": 27,  
> "name": "naveen"  
> }  
> ]
> 
> I want to update only those objects which satisfy any condition, like I want to query where name equals to ashu update his age to 30.
> 
> please suggest something....
> 
> Thanks in advance....

--  
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/ef9566bb-eebc-43e4-9a97-b8f832cf7e7d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/ef9566bb-eebc-43e4-9a97-b8f832cf7e7d%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:14am UTC](https://discuss.elastic.co/t/how-to-update-a-object-within-array-in-elastic-search/20115/8 "2014-10-08T11:14:44Z")

</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:42:11 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....
> 
> On Tuesday, October 7, 2014 7:08:52 PM UTC+5:30, Rajit Garg wrote:
> 
> > Here I have a array type field in my document of Elasticsearch, like  
> > below:-
> > 
> > "prog\_lists": [  
> > {  
> > "description": "engineer",  
> > "age": 25,  
> > "name": "ashu"  
> > },  
> > {  
> > "description": "programmer",  
> > "age": 26,  
> > "name": "rajit"  
> > },  
> > {  
> > "description": "designer",  
> > "age": 27,  
> > "name": "naveen"  
> > }  
> > ]
> > 
> > I want to update only those objects which satisfy any condition, like I want to query where name equals to ashu update his age to 30.
> > 
> > please suggest something....
> > 
> > Thanks in advance....

--  
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/ec53ef95-6dbd-4c10-8354-ee52f4b68d7a%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/ec53ef95-6dbd-4c10-8354-ee52f4b68d7a%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 10, 2014, 1:26pm UTC](https://discuss.elastic.co/t/how-to-update-a-object-within-array-in-elastic-search/20115/9 "2014-10-10T13:26:51Z")

</div>

**I have got my answer, and its solution is**

```
POST /twitter/twit/1/_update

{
  "script": "item_to_remove = nil; foreach (item : ctx._source.list) { 

```

if (item['tweet\_id'] == tweet\_id) { item\_to\_remove=item; } } if  
(item\_to\_remove != nil) ctx.\_source.list.remove(item\_to\_remove);",  
"params": {"tweet\_id": "123"}  
}

**if you have more than one item that matches the criteria, use a list  
instead:**

```
POST /twitter/twit/1/_update
{
  "script": "items_to_remove = []; foreach (item : ctx._source.list) { 

```

if (item['tweet\_id'] == tweet\_id) { items\_to\_remove.add(item); } } foreach  
(item : items\_to\_remove) {ctx.\_source.list.remove(item);}",  
"params": {"tweet\_id": "123"}  
}

On Tuesday, June 24, 2014 12:48:17 PM UTC+5:30, Madhumita Sadhukhan wrote:

> I have a requirement where I need to update(not append) existing values  
> within a list or array in Elastic Search.  
> Is this feature supported in Elasticsearch?
> 
> For eg:
> 
> I have a field called jobs as part of my document
> 
> "jobs": [  
> {  
> "status": "InProgress",  
> "runId": 1,  
> "start\_date": 2101112,  
> "orderId": "undefined"  
> },  
> {  
> "status": "InProgress",  
> "runId": 2,  
> "start\_date": 2101112,  
> "orderId": "undefined"  
> },  
> ],  
> and I am required to update the orderId for each job run to different  
> values.  
> Currently I am only able to append a job but I cannot update the  
> attributes of each job later.  
> Is this usecase supported and possible in Elastic Search?

On Wednesday, October 8, 2014 4:44:45 PM UTC+5:30, Rajit Garg wrote:

> \*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:42:11 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....
> > 
> > On Tuesday, October 7, 2014 7:08:52 PM UTC+5:30, Rajit Garg wrote:
> > 
> > > Here I have a array type field in my document of Elasticsearch, like  
> > > below:-
> > > 
> > > "prog\_lists": [  
> > > {  
> > > "description": "engineer",  
> > > "age": 25,  
> > > "name": "ashu"  
> > > },  
> > > {  
> > > "description": "programmer",  
> > > "age": 26,  
> > > "name": "rajit"  
> > > },  
> > > {  
> > > "description": "designer",  
> > > "age": 27,  
> > > "name": "naveen"  
> > > }  
> > > ]
> > > 
> > > I want to update only those objects which satisfy any condition, like I want to query where name equals to ashu update his age to 30.
> > > 
> > > please suggest something....
> > > 
> > > Thanks in advance....

--  
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/50df0c6f-e570-4f5b-b3f3-a47e74fdc8c8%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/50df0c6f-e570-4f5b-b3f3-a47e74fdc8c8%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/how-to-update-a-object-within-array-in-elastic-search/20115/10 "2017-07-06T00:57:03Z")

</div>


