# Delete/update nested documents with elasticsearch Java API

**URL:** <https://discuss.elastic.co/t/delete-update-nested-documents-with-elasticsearch-java-api/11523>\
**Category:** Elasticsearch\
**Created:** [April 10, 2013, 6:52am UTC](https://discuss.elastic.co/t/delete-update-nested-documents-with-elasticsearch-java-api/11523 "2013-04-10T06:52:14Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrei\_Tolnai](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrei_tolnai/32/2314_2.png) [@Andrei\_Tolnai](https://discuss.elastic.co/u/Andrei_Tolnai)\
**Post date:** [April 10, 2013, 6:52am UTC](https://discuss.elastic.co/t/delete-update-nested-documents-with-elasticsearch-java-api/11523/1 "2013-04-10T06:52:14Z")

</div>

Hello.

I use \*Elastic Search Java API \*for basic \*CRUD \*operations on ES  
documents; with _root type_ documents it is working fine.

However when I want to delete a nested document, I don't know how to use  
the _Java API_ and scripting that \*Elastic Search \*provides.

I have the following _root type_ document which has in it a nested \*users _property  
(nested document).  
{  
"customer":  
{  
"\_all" : {"enabled" : false},  
"\_source" : {"enabled" : true},  
"store" : "true",  
"properties":  
{  
"location" : { "type": "string", "store": "yes",  
"index":"not\_analyzed" },  
"comments" : { "type": "string", "store": "yes",  
"index":"not\_analyzed" },  
\* "users":_

- 

```
             {*

```

- 

```
                "type":"nested",*

```

- 

```
                "store":"yes", *

```

- 

```
                "properties":*

```

- 

```
                   {*

```

- 

```
                      "_id" : { "type":"string", "store":"yes", 

```

"index":"not\_analyzed" },\*

- 

```
                      "name" : { "type":"string", "store":"yes", 

```

"index":"not\_analyzed" },\*

- 

```
                      "birthDate" : { "type":"date", "store":"yes", 

```

"index":"not\_analyzed" }\*

- 

```
                   }*

```

- 

```
             }*
      }
}

```

}

If I want to add a user I do the following in \*Java \*/ \*Scala \*code:

val json = "  
{"\_id":"1","name":"Anthony","birthDate":"2013-04-10T06:45:26.186Z"}"  
val updateRequest = new UpdateRequest("indexName", "customer", customerId)  
updateRequest.script(" if (ctx.\_source.users == null) { ctx.\_source.users =  
"+json+" } else { ctx.\_source.users += "+json+" } ")

This works OK for adding nested \*users \*AKA updating the \*customer root type

- document.

How is it to be done for _deleting_ or \*updating \*a specific user ID from  
the nested _users_ nested document?

Thanks!

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [April 10, 2013, 10:51am UTC](https://discuss.elastic.co/t/delete-update-nested-documents-with-elasticsearch-java-api/11523/2 "2013-04-10T10:51:49Z")

</div>

It is a bit tricky, but you can remove elements from your users nested  
field by using a for loop as script with the update api.  
Something like this should work:  
curl -s -XPOST 'localhost:9200/test/customer/1/\_update' -d '{  
"script" : "for (int i = 0; i \< ctx.\_source.users.size();  
i++){if(ctx.\_source.users[i].\_id == id){ctx.\_source.users.remove(i);}}",  
"params" : {  
"id" : "1"  
}  
}'

In the above case the user with `_id` value 1 should be removed.

On 10 April 2013 08:52, Andrei Tolnai [dreytc@gmail.com](mailto:dreytc@gmail.com) wrote:

> Hello.
> 
> I use \*Elastic Search Java API \*for basic \*CRUD \*operations on ES  
> documents; with _root type_ documents it is working fine.
> 
> However when I want to delete a nested document, I don't know how to use  
> the _Java API_ and scripting that \*Elastic Search \*provides.
> 
> I have the following _root type_ document which has in it a nested \*users  
> _property (nested document).  
> {  
> "customer":  
> {  
> "\_all" : {"enabled" : false},  
> "\_source" : {"enabled" : true},  
> "store" : "true",  
> "properties":  
> {  
> "location" : { "type": "string", "store": "yes",  
> "index":"not\_analyzed" },  
> "comments" : { "type": "string", "store": "yes",  
> "index":"not\_analyzed" },  
> \* "users":_
> 
> - 
> 
> ```
> {*
> 
> ```
> 
> - 
> 
> ```
> "type":"nested",*
> 
> ```
> 
> - 
> 
> ```
> "store":"yes", *
> 
> ```
> 
> - 
> 
> ```
> "properties":*
> 
> ```
> 
> - 
> 
> ```
> {*
> 
> ```
> 
> - 
> 
> ```
> "_id" : { "type":"string", "store":"yes",
> 
> ```
> 
> "index":"not\_analyzed" },\*
> 
> - 
> 
> ```
> "name" : { "type":"string", "store":"yes",
> 
> ```
> 
> "index":"not\_analyzed" },\*
> 
> - 
> 
> ```
> "birthDate" : { "type":"date", "store":"yes",
> 
> ```
> 
> "index":"not\_analyzed" }\*
> 
> - 
> 
> ```
> }*
> 
> ```
> 
> - 
> 
> ```
> }*
> }
> }
> 
> ```
> 
> }
> 
> If I want to add a user I do the following in \*Java \*/ \*Scala \*code:
> 
> val json = "  
> {"\_id":"1","name":"Anthony","birthDate":"2013-04-10T06:45:26.186Z"}"  
> val updateRequest = new UpdateRequest("indexName", "customer", customerId)  
> updateRequest.script(" if (ctx.\_source.users == null) { ctx.\_source.users  
> = "+json+" } else { ctx.\_source.users += "+json+" } ")
> 
> This works OK for adding nested \*users \*AKA updating the _customer root  
> type_ document.
> 
> How is it to be done for _deleting_ or \*updating \*a specific user ID from  
> the nested _users_ nested document?
> 
> Thanks!
> 
> --  
> 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).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
Met vriendelijke groet,

Martijn van Groningen

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Andrei\_Tolnai](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrei_tolnai/32/2314_2.png) [@Andrei\_Tolnai](https://discuss.elastic.co/u/Andrei_Tolnai)\
**Post date:** [April 11, 2013, 3:35pm UTC](https://discuss.elastic.co/t/delete-update-nested-documents-with-elasticsearch-java-api/11523/3 "2013-04-11T15:35:28Z")

</div>

Thanks for the reply Martijn!

The DELETE/REMOVE of a user does not work for me; I don't get any error  
either its just that the specific user ID I put in its just not getting  
removed.

However I was able to do the UPDATE for a specific user ID by adapting a  
bit your DELETE script into:

for (int i = 0; i \< ctx.\_source.users.size();  
i++){if(ctx.\_source.users[i].\_id ==  
"1"){ctx.\_source.users[i]= "\_id":"1","name":"Updated  
John","birthDate":"2013-04-11T15:27:31.218Z" ;}}

Any other clues on how I should do the remove an entry from a nested array  
using ES Java API?

Thanks.

On Wednesday, April 10, 2013 1:51:49 PM UTC+3, Martijn v Groningen wrote:

> It is a bit tricky, but you can remove elements from your users nested  
> field by using a for loop as script with the update api.  
> Something like this should work:  
> curl -s -XPOST 'localhost:9200/test/customer/1/\_update' -d '{  
> "script" : "for (int i = 0; i \< ctx.\_source.users.size();  
> i++){if(ctx.\_source.users[i].\_id == id){ctx.\_source.users.remove(i);}}",  
> "params" : {  
> "id" : "1"  
> }  
> }'
> 
> In the above case the user with `_id` value 1 should be removed.
> 
> On 10 April 2013 08:52, Andrei Tolnai \<[dre...@gmail.com](mailto:dre...@gmail.com) \<javascript:\>\>wrote:
> 
> > Hello.
> > 
> > I use \*Elastic Search Java API \*for basic \*CRUD \*operations on ES  
> > documents; with _root type_ documents it is working fine.
> > 
> > However when I want to delete a nested document, I don't know how to use  
> > the _Java API_ and scripting that \*Elastic Search \*provides.
> > 
> > I have the following _root type_ document which has in it a nested \*users  
> > _property (nested document).  
> > {  
> > "customer":  
> > {  
> > "\_all" : {"enabled" : false},  
> > "\_source" : {"enabled" : true},  
> > "store" : "true",  
> > "properties":  
> > {  
> > "location" : { "type": "string", "store": "yes",  
> > "index":"not\_analyzed" },  
> > "comments" : { "type": "string", "store": "yes",  
> > "index":"not\_analyzed" },  
> > \* "users":_
> > 
> > - 
> > 
> > ```
> > {*
> > 
> > ```
> > 
> > - 
> > 
> > ```
> > "type":"nested",*
> > 
> > ```
> > 
> > - 
> > 
> > ```
> > "store":"yes", *
> > 
> > ```
> > 
> > - 
> > 
> > ```
> > "properties":*
> > 
> > ```
> > 
> > - 
> > 
> > ```
> > {*
> > 
> > ```
> > 
> > - 
> > 
> > ```
> > "_id" : { "type":"string", "store":"yes", 
> > 
> > ```
> > 
> > "index":"not\_analyzed" },\*
> > 
> > - 
> > 
> > ```
> > "name" : { "type":"string", "store":"yes", 
> > 
> > ```
> > 
> > "index":"not\_analyzed" },\*
> > 
> > - 
> > 
> > ```
> > "birthDate" : { "type":"date", 
> > 
> > ```
> > 
> > "store":"yes", "index":"not\_analyzed" }\*
> > 
> > - 
> > 
> > ```
> > }*
> > 
> > ```
> > 
> > - 
> > 
> > ```
> > }*
> > }
> > }
> > 
> > ```
> > 
> > }
> > 
> > If I want to add a user I do the following in \*Java \*/ \*Scala \*code:
> > 
> > val json = "  
> > {"\_id":"1","name":"Anthony","birthDate":"2013-04-10T06:45:26.186Z"}"  
> > val updateRequest = new UpdateRequest("indexName", "customer", customerId)  
> > updateRequest.script(" if (ctx.\_source.users == null) { ctx.\_source.users  
> > = "+json+" } else { ctx.\_source.users += "+json+" } ")
> > 
> > This works OK for adding nested \*users \*AKA updating the _customer root  
> > type_ document.
> > 
> > How is it to be done for _deleting_ or \*updating \*a specific user ID  
> > from the nested _users_ nested document?
> > 
> > Thanks!
> > 
> > --  
> > 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:\>.  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> Met vriendelijke groet,
> 
> Martijn van Groningen

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Andrei\_Tolnai](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrei_tolnai/32/2314_2.png) [@Andrei\_Tolnai](https://discuss.elastic.co/u/Andrei_Tolnai)\
**Post date:** [April 11, 2013, 3:43pm UTC](https://discuss.elastic.co/t/delete-update-nested-documents-with-elasticsearch-java-api/11523/4 "2013-04-11T15:43:25Z")

</div>

"for (int i = 0; i \< ctx.\_source.users.size();  
i++){if(ctx.\_source.users[i].\_id == id){ctx.\_source.users.remove(i);}}",

I am not 100% sure but I think the issue about DELETE script is around the  
last part: ctx.\_source.users.remove(i);

Could we do that remove by specifying there the the _\_id_ of the user we  
want to delete from the nested array?

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Andrei\_Tolnai](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrei_tolnai/32/2314_2.png) [@Andrei\_Tolnai](https://discuss.elastic.co/u/Andrei_Tolnai)\
**Post date:** [April 11, 2013, 4:04pm UTC](https://discuss.elastic.co/t/delete-update-nested-documents-with-elasticsearch-java-api/11523/5 "2013-04-11T16:04:08Z")

</div>

Oddly enough the DELETE script seems to be working if in the array there is  
only one user with a particular given _id_. 🙂

If I have multiple users with the same _\_id_ value the script is not  
removing all of them. How should the script be changed to remove all the  
users that have a specific _\_id_ value?

Regards.

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [April 11, 2013, 7:46pm UTC](https://discuss.elastic.co/t/delete-update-nested-documents-with-elasticsearch-java-api/11523/6 "2013-04-11T19:46:33Z")

</div>

Hi Andrei,

I think the following should work:  
"script" : "for (int i = 0; i \< ctx.\_source.users.size();  
i++){if(ctx.\_source.users[i].\_id == id){ctx.\_source.users.remove(i);i--;}}"

I added: 'i--'. Once an entry is removed, the list has been shorter and we  
shouldn't skip over the next entry.

Martijn

On 11 April 2013 18:04, Andrei Tolnai [dreytc@gmail.com](mailto:dreytc@gmail.com) wrote:

> Oddly enough the DELETE script seems to be working if in the array there  
> is only one user with a particular given _id_. 🙂
> 
> If I have multiple users with the same _\_id_ value the script is not  
> removing all of them. How should the script be changed to remove all the  
> users that have a specific _\_id_ value?
> 
> Regards.

--  
Met vriendelijke groet,

Martijn van Groningen

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Andrei\_Tolnai](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrei_tolnai/32/2314_2.png) [@Andrei\_Tolnai](https://discuss.elastic.co/u/Andrei_Tolnai)\
**Post date:** [April 11, 2013, 10:18pm UTC](https://discuss.elastic.co/t/delete-update-nested-documents-with-elasticsearch-java-api/11523/7 "2013-04-11T22:18:41Z")

</div>

Great, that worked! Thanks Martijn!

> 

Now I have to figure out how to do a retrieve for a specific user id using  
the _Java API_ and _org.elasticsearch.action.get.GetRequest_

Regards

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Deepesh\_naini](https://avatars.discourse-cdn.com/v4/letter/d/bbce88/32.png) [@Deepesh\_naini](https://discuss.elastic.co/u/Deepesh_naini)\
**Post date:** [August 6, 2013, 11:36am UTC](https://discuss.elastic.co/t/delete-update-nested-documents-with-elasticsearch-java-api/11523/8 "2013-08-06T11:36:18Z")

</div>

Hello,

I am using Spring data elasticsearch[https://github.com/BioMedCentralLtd/spring-data-elasticsearch](https://github.com/BioMedCentralLtd/spring-data-elasticsearch) in  
my project and I need to update an Indexed field. Is it possible to do this  
using this framework??

On Wednesday, April 10, 2013 12:22:14 PM UTC+5:30, Andrei Tolnai wrote:

> Hello.
> 
> I use \*Elastic Search Java API \*for basic \*CRUD \*operations on ES  
> documents; with _root type_ documents it is working fine.
> 
> However when I want to delete a nested document, I don't know how to use  
> the _Java API_ and scripting that \*Elastic Search \*provides.
> 
> I have the following _root type_ document which has in it a nested \*users  
> _property (nested document).  
> {  
> "customer":  
> {  
> "\_all" : {"enabled" : false},  
> "\_source" : {"enabled" : true},  
> "store" : "true",  
> "properties":  
> {  
> "location" : { "type": "string", "store": "yes",  
> "index":"not\_analyzed" },  
> "comments" : { "type": "string", "store": "yes",  
> "index":"not\_analyzed" },  
> \* "users":_
> 
> - 
> 
> ```
> {*
> 
> ```
> 
> - 
> 
> ```
> "type":"nested",*
> 
> ```
> 
> - 
> 
> ```
> "store":"yes", *
> 
> ```
> 
> - 
> 
> ```
> "properties":*
> 
> ```
> 
> - 
> 
> ```
> {*
> 
> ```
> 
> - 
> 
> ```
> "_id" : { "type":"string", "store":"yes", 
> 
> ```
> 
> "index":"not\_analyzed" },\*
> 
> - 
> 
> ```
> "name" : { "type":"string", "store":"yes", 
> 
> ```
> 
> "index":"not\_analyzed" },\*
> 
> - 
> 
> ```
> "birthDate" : { "type":"date", "store":"yes", 
> 
> ```
> 
> "index":"not\_analyzed" }\*
> 
> - 
> 
> ```
> }*
> 
> ```
> 
> - 
> 
> ```
> }*
> }
> }
> 
> ```
> 
> }
> 
> If I want to add a user I do the following in \*Java \*/ \*Scala \*code:
> 
> val json = "  
> {"\_id":"1","name":"Anthony","birthDate":"2013-04-10T06:45:26.186Z"}"  
> val updateRequest = new UpdateRequest("indexName", "customer", customerId)  
> updateRequest.script(" if (ctx.\_source.users == null) { ctx.\_source.users  
> = "+json+" } else { ctx.\_source.users += "+json+" } ")
> 
> This works OK for adding nested \*users \*AKA updating the _customer root  
> type_ document.
> 
> How is it to be done for _deleting_ or \*updating \*a specific user ID from  
> the nested _users_ nested document?
> 
> Thanks!

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 5:27am UTC](https://discuss.elastic.co/t/delete-update-nested-documents-with-elasticsearch-java-api/11523/9 "2014-10-08T05:27:44Z")

</div>

Hi,

I have tried to update or remove the nested object in the same way as  
suggested , but this is not working

I am doing this

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

my users field contain the following

"users": [  
{  
"code": "abcd",  
"\_id": "1",  
"name": "naveen"  
},  
{  
"code": "efgh",  
"\_id": "2",  
"name": "rajit"  
},  
{  
"code": "ijkl",  
"\_id": "3",  
"name": "ashu"  
}  
]

and I am getting this error

VerifyError[Bad type on operand stack  
Exception Details:  
Location:  
ASMAccessorImpl\_11232799721412745799160.setValue(Ljava/lang/Object;Ljava/lang/Object;Lorg/elasticsearch/common/mvel2/integration/VariableResolverFactory;Ljava/lang/Object;)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\_11232799721412745799160', 'java/lang/Object', 'java/lang/Object', 'org/elasticsearch/common/mvel2/integration/VariableResolverFactory', 'java/lang/Object' }  
stack: { 'java/util/List', 'java/lang/Object' }  
Bytecode:  
0000000: 2d12 0eb9 0014 0200 b900 1a01 00c0 001c  
0000010: 121e b900 2202 00c0 001c 1224 b900 2202  
0000020: 00c0 0026 2d12 28b9 0014 0200 b900 1a01  
0000030: 00b9 002b 0200 c000 2d12 2f19 04b6 0035  
0000040: 1904 b0  
]  
Error: VerifyError[Bad type on operand stack  
Exception Details:  
Location:----------------------------

* * *

* * *

On Friday, April 12, 2013 1:16:33 AM UTC+5:30, Martijn v Groningen wrote:

> Hi Andrei,
> 
> I think the following should work:  
> "script" : "for (int i = 0; i \< ctx.\_source.users.size();  
> i++){if(ctx.\_source.users[i].\_id == id){ctx.\_source.users.remove(i);i--;}}"
> 
> I added: 'i--'. Once an entry is removed, the list has been shorter and we  
> shouldn't skip over the next entry.
> 
> Martijn
> 
> On 11 April 2013 18:04, Andrei Tolnai \<[dre...@gmail.com](mailto:dre...@gmail.com) \<javascript:\>\>  
> wrote:
> 
> > Oddly enough the DELETE script seems to be working if in the array there  
> > is only one user with a particular given _id_. 🙂
> > 
> > If I have multiple users with the same _\_id_ value the script is not  
> > removing all of them. How should the script be changed to remove all the  
> > users that have a specific _\_id_ value?
> > 
> > Regards.
> 
> --  
> Met vriendelijke groet,
> 
> Martijn van Groningen

--  
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/5671672d-cc4e-451b-93a6-e40b574ea467%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5671672d-cc4e-451b-93a6-e40b574ea467%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:12am UTC](https://discuss.elastic.co/t/delete-update-nested-documents-with-elasticsearch-java-api/11523/10 "2014-10-08T11:12:55Z")

</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, April 10, 2013 12:22:14 PM UTC+5:30, Andrei Tolnai wrote:

> Hello.
> 
> I use \*Elastic Search Java API \*for basic \*CRUD \*operations on ES  
> documents; with _root type_ documents it is working fine.
> 
> However when I want to delete a nested document, I don't know how to use  
> the _Java API_ and scripting that \*Elastic Search \*provides.
> 
> I have the following _root type_ document which has in it a nested \*users  
> _property (nested document).  
> {  
> "customer":  
> {  
> "\_all" : {"enabled" : false},  
> "\_source" : {"enabled" : true},  
> "store" : "true",  
> "properties":  
> {  
> "location" : { "type": "string", "store": "yes",  
> "index":"not\_analyzed" },  
> "comments" : { "type": "string", "store": "yes",  
> "index":"not\_analyzed" },  
> \* "users":_
> 
> - 
> 
> ```
> {*
> 
> ```
> 
> - 
> 
> ```
> "type":"nested",*
> 
> ```
> 
> - 
> 
> ```
> "store":"yes", *
> 
> ```
> 
> - 
> 
> ```
> "properties":*
> 
> ```
> 
> - 
> 
> ```
> {*
> 
> ```
> 
> - 
> 
> ```
> "_id" : { "type":"string", "store":"yes", 
> 
> ```
> 
> "index":"not\_analyzed" },\*
> 
> - 
> 
> ```
> "name" : { "type":"string", "store":"yes", 
> 
> ```
> 
> "index":"not\_analyzed" },\*
> 
> - 
> 
> ```
> "birthDate" : { "type":"date", "store":"yes", 
> 
> ```
> 
> "index":"not\_analyzed" }\*
> 
> - 
> 
> ```
> }*
> 
> ```
> 
> - 
> 
> ```
> }*
> }
> }
> 
> ```
> 
> }
> 
> If I want to add a user I do the following in \*Java \*/ \*Scala \*code:
> 
> val json = "  
> {"\_id":"1","name":"Anthony","birthDate":"2013-04-10T06:45:26.186Z"}"  
> val updateRequest = new UpdateRequest("indexName", "customer", customerId)  
> updateRequest.script(" if (ctx.\_source.users == null) { ctx.\_source.users  
> = "+json+" } else { ctx.\_source.users += "+json+" } ")
> 
> This works OK for adding nested \*users \*AKA updating the _customer root  
> type_ document.
> 
> How is it to be done for _deleting_ or \*updating \*a specific user ID from  
> the nested _users_ nested document?
> 
> Thanks!

--  
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/171950f9-5843-4327-be49-b6858e1b3832%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/171950f9-5843-4327-be49-b6858e1b3832%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![sharp00k](https://avatars.discourse-cdn.com/v4/letter/s/7ea924/32.png) [@sharp00k](https://discuss.elastic.co/u/sharp00k)\
**Post date:** [October 28, 2014, 3:10am UTC](https://discuss.elastic.co/t/delete-update-nested-documents-with-elasticsearch-java-api/11523/11 "2014-10-28T03:10:12Z")

</div>

"script": "removeObjects = new java.util.ArrayList(); foreach (item :  
ctx.\_source.list) {if (item['tweet\_id'] == tweet\_id)  
{removeObjects.add(item) } }  
for(removeObject:removeObjects){ctx.\_source.list.remove(removeObject);}",

2014년 10월 8일 수요일 오후 8시 12분 55초 UTC+9, Rajit Garg 님의 말:

> 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, April 10, 2013 12:22:14 PM UTC+5:30, Andrei Tolnai wrote:
> 
> > Hello.
> > 
> > I use \*Elastic Search Java API \*for basic \*CRUD \*operations on ES  
> > documents; with _root type_ documents it is working fine.
> > 
> > However when I want to delete a nested document, I don't know how to use  
> > the _Java API_ and scripting that \*Elastic Search \*provides.
> > 
> > I have the following _root type_ document which has in it a nested \*users  
> > _property (nested document).  
> > {  
> > "customer":  
> > {  
> > "\_all" : {"enabled" : false},  
> > "\_source" : {"enabled" : true},  
> > "store" : "true",  
> > "properties":  
> > {  
> > "location" : { "type": "string", "store": "yes",  
> > "index":"not\_analyzed" },  
> > "comments" : { "type": "string", "store": "yes",  
> > "index":"not\_analyzed" },  
> > \* "users":_
> > 
> > - 
> > 
> > ```
> > {*
> > 
> > ```
> > 
> > - 
> > 
> > ```
> > "type":"nested",*
> > 
> > ```
> > 
> > - 
> > 
> > ```
> > "store":"yes", *
> > 
> > ```
> > 
> > - 
> > 
> > ```
> > "properties":*
> > 
> > ```
> > 
> > - 
> > 
> > ```
> > {*
> > 
> > ```
> > 
> > - 
> > 
> > ```
> > "_id" : { "type":"string", "store":"yes", 
> > 
> > ```
> > 
> > "index":"not\_analyzed" },\*
> > 
> > - 
> > 
> > ```
> > "name" : { "type":"string", "store":"yes", 
> > 
> > ```
> > 
> > "index":"not\_analyzed" },\*
> > 
> > - 
> > 
> > ```
> > "birthDate" : { "type":"date", "store":"yes", 
> > 
> > ```
> > 
> > "index":"not\_analyzed" }\*
> > 
> > - 
> > 
> > ```
> > }*
> > 
> > ```
> > 
> > - 
> > 
> > ```
> > }*
> > }
> > }
> > 
> > ```
> > 
> > }
> > 
> > If I want to add a user I do the following in \*Java \*/ \*Scala \*code:
> > 
> > val json = "  
> > {"\_id":"1","name":"Anthony","birthDate":"2013-04-10T06:45:26.186Z"}"  
> > val updateRequest = new UpdateRequest("indexName", "customer", customerId)  
> > updateRequest.script(" if (ctx.\_source.users == null) { ctx.\_source.users  
> > = "+json+" } else { ctx.\_source.users += "+json+" } ")
> > 
> > This works OK for adding nested \*users \*AKA updating the _customer root  
> > type_ document.
> > 
> > How is it to be done for _deleting_ or \*updating \*a specific user ID  
> > from the nested _users_ nested document?
> > 
> > Thanks!

--  
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/8ef09dca-306d-4fa7-8936-a20e86125dac%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8ef09dca-306d-4fa7-8936-a20e86125dac%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 28, 2014, 6:34am UTC](https://discuss.elastic.co/t/delete-update-nested-documents-with-elasticsearch-java-api/11523/12 "2014-10-28T06:34:25Z")

</div>

Thanks this is working

On Tuesday, October 28, 2014 8:40:12 AM UTC+5:30, [shar...@gmail.com](mailto:shar...@gmail.com) wrote:

> "script": "removeObjects = new java.util.ArrayList(); foreach (item :  
> ctx.\_source.list) {if (item['tweet\_id'] == tweet\_id)  
> {removeObjects.add(item) } }  
> for(removeObject:removeObjects){ctx.\_source.list.remove(removeObject);}",
> 
> 2014년 10월 8일 수요일 오후 8시 12분 55초 UTC+9, Rajit Garg 님의 말:
> 
> > 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, April 10, 2013 12:22:14 PM UTC+5:30, Andrei Tolnai wrote:
> > 
> > > Hello.
> > > 
> > > I use \*Elastic Search Java API \*for basic \*CRUD \*operations on ES  
> > > documents; with _root type_ documents it is working fine.
> > > 
> > > However when I want to delete a nested document, I don't know how to use  
> > > the _Java API_ and scripting that \*Elastic Search \*provides.
> > > 
> > > I have the following _root type_ document which has in it a nested \*users  
> > > _property (nested document).  
> > > {  
> > > "customer":  
> > > {  
> > > "\_all" : {"enabled" : false},  
> > > "\_source" : {"enabled" : true},  
> > > "store" : "true",  
> > > "properties":  
> > > {  
> > > "location" : { "type": "string", "store": "yes",  
> > > "index":"not\_analyzed" },  
> > > "comments" : { "type": "string", "store": "yes",  
> > > "index":"not\_analyzed" },  
> > > \* "users":_
> > > 
> > > - 
> > > 
> > > ```
> > > {*
> > > 
> > > ```
> > > 
> > > - 
> > > 
> > > ```
> > > "type":"nested",*
> > > 
> > > ```
> > > 
> > > - 
> > > 
> > > ```
> > > "store":"yes", *
> > > 
> > > ```
> > > 
> > > - 
> > > 
> > > ```
> > > "properties":*
> > > 
> > > ```
> > > 
> > > - 
> > > 
> > > ```
> > > {*
> > > 
> > > ```
> > > 
> > > - 
> > > 
> > > ```
> > > "_id" : { "type":"string", "store":"yes", 
> > > 
> > > ```
> > > 
> > > "index":"not\_analyzed" },\*
> > > 
> > > - 
> > > 
> > > ```
> > > "name" : { "type":"string", "store":"yes", 
> > > 
> > > ```
> > > 
> > > "index":"not\_analyzed" },\*
> > > 
> > > - 
> > > 
> > > ```
> > > "birthDate" : { "type":"date", 
> > > 
> > > ```
> > > 
> > > "store":"yes", "index":"not\_analyzed" }\*
> > > 
> > > - 
> > > 
> > > ```
> > > }*
> > > 
> > > ```
> > > 
> > > - 
> > > 
> > > ```
> > > }*
> > > }
> > > }
> > > 
> > > ```
> > > 
> > > }
> > > 
> > > If I want to add a user I do the following in \*Java \*/ \*Scala \*code:
> > > 
> > > val json = "  
> > > {"\_id":"1","name":"Anthony","birthDate":"2013-04-10T06:45:26.186Z"}"  
> > > val updateRequest = new UpdateRequest("indexName", "customer",  
> > > customerId)  
> > > updateRequest.script(" if (ctx.\_source.users == null) {  
> > > ctx.\_source.users = "+json+" } else { ctx.\_source.users += "+json+" } ")
> > > 
> > > This works OK for adding nested \*users \*AKA updating the _customer root  
> > > type_ document.
> > > 
> > > How is it to be done for _deleting_ or \*updating \*a specific user ID  
> > > from the nested _users_ nested document?
> > > 
> > > Thanks!

--  
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/a4ccc10c-a06e-4c47-b486-8fdd1da4bfa3%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a4ccc10c-a06e-4c47-b486-8fdd1da4bfa3%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:53am UTC](https://discuss.elastic.co/t/delete-update-nested-documents-with-elasticsearch-java-api/11523/13 "2017-07-06T00:53:34Z")

</div>


