# Partial update on a field, remove element from an array does not work

**URL:** <https://discuss.elastic.co/t/partial-update-on-a-field-remove-element-from-an-array-does-not-work/7346>\
**Category:** Elasticsearch\
**Created:** [April 16, 2012, 12:07pm UTC](https://discuss.elastic.co/t/partial-update-on-a-field-remove-element-from-an-array-does-not-work/7346 "2012-04-16T12:07:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sisu\_Alexandru](https://avatars.discourse-cdn.com/v4/letter/s/49beb7/32.png) [@Sisu\_Alexandru](https://discuss.elastic.co/u/Sisu_Alexandru)\
**Post date:** [April 16, 2012, 12:07pm UTC](https://discuss.elastic.co/t/partial-update-on-a-field-remove-element-from-an-array-does-not-work/7346/1 "2012-04-16T12:07:26Z")

</div>

Hi,

Here's the gist: [https://gist.github.com/2398248](https://gist.github.com/2398248)  
Im adding elements into a field. Im trying to remove them from there. It  
doesnt work.

curl -XPOST '[http://localhost:9200/testindex/posting/1/\_update](http://localhost:9200/testindex/posting/1/_update)' -d '{  
"script" : "ctx.\_source.labels += label",  
"params" : {  
"label" : 5  
}  
}'

curl -XGET '[http://localhost:9200/testindex/posting/1](http://localhost:9200/testindex/posting/1)'

This one does not work:  
curl -XPOST '[http://localhost:9200/testindex/posting/1/\_update](http://localhost:9200/testindex/posting/1/_update)' -d '{  
"script" : "ctx.\_source.labels -= label",  
"params" : {  
"label" : 5  
}  
}'

#result is {"error":"ElasticSearchIllegalArgumentException[failed to  
execute script]; nested: StringIndexOutOfBoundsException[String index out  
of range: -1]; ","status":400}

Am I using it incorrectly?

Tnx in advance,

Alex

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [April 19, 2012, 1:18pm UTC](https://discuss.elastic.co/t/partial-update-on-a-field-remove-element-from-an-array-does-not-work/7346/2 "2012-04-19T13:18:28Z")

</div>

Heya,

I chased it down, and it seems like its a problem with the list that has  
remove by an index (integer) and an object (equality). Forcing it to use  
the remove with object works:

curl -XPOST '[http://localhost:9200/testindex/posting/1/\_update](http://localhost:9200/testindex/posting/1/_update)' -d '{  
"script" : "ctx.\_source.labels.remove((Object) label);",  
"params" : {  
"label" : 5  
}  
}'

On Mon, Apr 16, 2012 at 3:07 PM, Sisu Alexandru [sisu.eugen@gmail.com](mailto:sisu.eugen@gmail.com)wrote:

> Hi,
> 
> Here's the gist: [ElasticSearch partial field update: remove element from array, does not work · GitHub](https://gist.github.com/2398248)  
> Im adding elements into a field. Im trying to remove them from there. It  
> doesnt work.
> 
> curl -XPOST '[http://localhost:9200/testindex/posting/1/\_update](http://localhost:9200/testindex/posting/1/_update)' -d '{  
> "script" : "ctx.\_source.labels += label",  
> "params" : {  
> "label" : 5  
> }  
> }'
> 
> curl -XGET '[http://localhost:9200/testindex/posting/1](http://localhost:9200/testindex/posting/1)'
> 
> This one does not work:  
> curl -XPOST '[http://localhost:9200/testindex/posting/1/\_update](http://localhost:9200/testindex/posting/1/_update)' -d '{  
> "script" : "ctx.\_source.labels -= label",  
> "params" : {  
> "label" : 5  
> }  
> }'
> 
> #result is {"error":"ElasticSearchIllegalArgumentException[failed to  
> execute script]; nested: StringIndexOutOfBoundsException[String index out  
> of range: -1]; ","status":400}
> 
> Am I using it incorrectly?
> 
> Tnx in advance,
> 
> Alex

---

<div class="post-metadata">

**Author:** ![Sisu\_Alexandru](https://avatars.discourse-cdn.com/v4/letter/s/49beb7/32.png) [@Sisu\_Alexandru](https://discuss.elastic.co/u/Sisu_Alexandru)\
**Post date:** [April 19, 2012, 1:55pm UTC](https://discuss.elastic.co/t/partial-update-on-a-field-remove-element-from-an-array-does-not-work/7346/3 "2012-04-19T13:55:18Z")

</div>

Tnx !🙂

On Thu, Apr 19, 2012 at 3:18 PM, Shay Banon [kimchy@gmail.com](mailto:kimchy@gmail.com) wrote:

> Heya,
> 
> I chased it down, and it seems like its a problem with the list that has  
> remove by an index (integer) and an object (equality). Forcing it to use  
> the remove with object works:
> 
> curl -XPOST '[http://localhost:9200/testindex/posting/1/\_update](http://localhost:9200/testindex/posting/1/_update)' -d '{  
> "script" : "ctx.\_source.labels.remove((Object) label);",  
> "params" : {  
> "label" : 5  
> }  
> }'
> 
> On Mon, Apr 16, 2012 at 3:07 PM, Sisu Alexandru [sisu.eugen@gmail.com](mailto:sisu.eugen@gmail.com)wrote:
> 
> > Hi,
> > 
> > Here's the gist: [ElasticSearch partial field update: remove element from array, does not work · GitHub](https://gist.github.com/2398248)  
> > Im adding elements into a field. Im trying to remove them from there. It  
> > doesnt work.
> > 
> > curl -XPOST '[http://localhost:9200/testindex/posting/1/\_update](http://localhost:9200/testindex/posting/1/_update)' -d '{  
> > "script" : "ctx.\_source.labels += label",  
> > "params" : {  
> > "label" : 5  
> > }  
> > }'
> > 
> > curl -XGET '[http://localhost:9200/testindex/posting/1](http://localhost:9200/testindex/posting/1)'
> > 
> > This one does not work:  
> > curl -XPOST '[http://localhost:9200/testindex/posting/1/\_update](http://localhost:9200/testindex/posting/1/_update)' -d '{  
> > "script" : "ctx.\_source.labels -= label",  
> > "params" : {  
> > "label" : 5  
> > }  
> > }'
> > 
> > #result is {"error":"ElasticSearchIllegalArgumentException[failed to  
> > execute script]; nested: StringIndexOutOfBoundsException[String index out  
> > of range: -1]; ","status":400}
> > 
> > Am I using it incorrectly?
> > 
> > Tnx in advance,
> > 
> > Alex

---

<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, 3:31am UTC](https://discuss.elastic.co/t/partial-update-on-a-field-remove-element-from-an-array-does-not-work/7346/4 "2017-07-06T03:31:57Z")

</div>


