# Update api

**URL:** https://discuss.elastic.co/t/update-api/11582
**Category:** Elasticsearch
**Created:** [April 15, 2013, 9:30am UTC](https://discuss.elastic.co/t/update-api/11582 "2013-04-15T09:30:08Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![kuwar\_sahani\_2](https://avatars.discourse-cdn.com/v4/letter/k/fbc32d/32.png) [@kuwar\_sahani\_2](https://discuss.elastic.co/u/kuwar_sahani_2)
#### Post date: [April 15, 2013, 9:30am UTC](https://discuss.elastic.co/t/update-api/11582/1 "2013-04-15T09:30:08Z")

</div>

Hi

for using the update query i referred to  
[http://www.elasticsearch.org/guide/reference/api/update/](http://www.elasticsearch.org/guide/reference/api/update/)

eg: And, we can delete the doc if the tags contain blue, or ignore (noop):

curl -XPOST 'localhost:9200/test/type1/1/\_update' -d '{  
"script" : "ctx.\_source.tags.contains(tag) ? ctx.op = "delete" : ctx.op  
= "none"",  
"params" : {  
"tag" : "blue"  
}  
}'

I have modified the above query as follows:

curl -XPOST "[http://10.118.192.145:9200/test/type1/1/\_update](http://10.118.192.145:9200/test/type1/1/_update)" -d '{  
"script" : "ctx.\_source.tags.contains(tag) ? ctx.op = "none" : ctx.\_source.tags += tag",  
"params" : {  
"tag" : "Orange"  
}  
}'

I want to check for a tag before adding to it. If the tag exists, do nothing else perform the update to add.

the above query creates a new version for the record but does not add the new tag.

How can i acheive this?

Thanks

Kuwar Sahani

--  
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: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [April 15, 2013, 10:03am UTC](https://discuss.elastic.co/t/update-api/11582/2 "2013-04-15T10:03:47Z")

</div>

Hey,

this works

curl -XPUT localhost:9200/test/type1/1 -d '{  
"counter" : 1,  
"tags" : ["red"]  
}'  
curl -XPOST 'localhost:9200/test/type1/1/\_update' -d '{  
"script" : "!ctx.\_source.tags.contains(tag) ? ctx.\_source.tags += tag :  
ctx.op = "none"",  
"params" : {  
"tag" : "blue"  
}  
}'  
curl localhost:9200/test/type1/1

this does not

curl -XPUT localhost:9200/test/type1/1 -d '{  
"counter" : 1,  
"tags" : ["red"]  
}'  
curl -XPOST 'localhost:9200/test/type1/1/\_update' -d '{  
"script" : "ctx.\_source.tags.contains(tag) ? ctx.op = "none" :  
ctx.\_source.tags += tag ",  
"params" : {  
"tag" : "blue"  
}  
}'  
curl localhost:9200/test/type1/1

looks like a bug, I need to check that further, in the meantime try the  
first solution...

--Alex

On Mon, Apr 15, 2013 at 11:30 AM, kuwar sahani [kuwarsahani@gmail.com](mailto:kuwarsahani@gmail.com)wrote:

> Hi
> 
> for using the update query i referred to  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/update/)
> 
> eg: And, we can delete the doc if the tags contain blue, or ignore (noop):
> 
> curl -XPOST 'localhost:9200/test/type1/1/\_update' -d '{  
> "script" : "ctx.\_source.tags.contains(tag) ? ctx.op = "delete" :  
> ctx.op = "none"",  
> "params" : {  
> "tag" : "blue"  
> }  
> }'
> 
> I have modified the above query as follows:
> 
> curl -XPOST "[http://10.118.192.145:9200/test/type1/1/\_update](http://10.118.192.145:9200/test/type1/1/_update)" -d '{  
> "script" : "ctx.\_source.tags.contains(tag) ? ctx.op = "none" : ctx.\_source.tags += tag",  
> "params" : {  
> "tag" : "Orange"  
> }  
> }'
> 
> I want to check for a tag before adding to it. If the tag exists, do nothing else perform the update to add.
> 
> the above query creates a new version for the record but does not add the new tag.
> 
> How can i acheive this?
> 
> Thanks
> 
> Kuwar Sahani
> 
> --  
> 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).

--  
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: ![kuwar\_sahani\_2](https://avatars.discourse-cdn.com/v4/letter/k/fbc32d/32.png) [@kuwar\_sahani\_2](https://discuss.elastic.co/u/kuwar_sahani_2)
#### Post date: [April 15, 2013, 12:18pm UTC](https://discuss.elastic.co/t/update-api/11582/3 "2013-04-15T12:18:03Z")

</div>

thanks!!  
On Monday, 15 April 2013 15:33:47 UTC+5:30, Alexander Reelsen wrote:

> Hey,
> 
> this works
> 
> curl -XPUT localhost:9200/test/type1/1 -d '{  
> "counter" : 1,  
> "tags" : ["red"]  
> }'  
> curl -XPOST 'localhost:9200/test/type1/1/\_update' -d '{  
> "script" : "!ctx.\_source.tags.contains(tag) ? ctx.\_source.tags += tag  
> : ctx.op = "none"",  
> "params" : {  
> "tag" : "blue"  
> }  
> }'  
> curl localhost:9200/test/type1/1
> 
> this does not
> 
> curl -XPUT localhost:9200/test/type1/1 -d '{  
> "counter" : 1,  
> "tags" : ["red"]  
> }'  
> curl -XPOST 'localhost:9200/test/type1/1/\_update' -d '{  
> "script" : "ctx.\_source.tags.contains(tag) ? ctx.op = "none" :  
> ctx.\_source.tags += tag ",  
> "params" : {  
> "tag" : "blue"  
> }  
> }'  
> curl localhost:9200/test/type1/1
> 
> looks like a bug, I need to check that further, in the meantime try the  
> first solution...
> 
> --Alex
> 
> On Mon, Apr 15, 2013 at 11:30 AM, kuwar sahani \<[kuwar...@gmail.com](mailto:kuwar...@gmail.com)\<javascript:\>
> 
> > wrote:
> 
> > Hi
> > 
> > for using the update query i referred to  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/update/)
> > 
> > eg: And, we can delete the doc if the tags contain blue, or ignore (noop):
> > 
> > curl -XPOST 'localhost:9200/test/type1/1/\_update' -d '{  
> > "script" : "ctx.\_source.tags.contains(tag) ? ctx.op = "delete" :  
> > ctx.op = "none"",  
> > "params" : {  
> > "tag" : "blue"  
> > }  
> > }'
> > 
> > I have modified the above query as follows:
> > 
> > curl -XPOST "[http://10.118.192.145:9200/test/type1/1/\_update](http://10.118.192.145:9200/test/type1/1/_update)" -d '{
> > 
> > "script" : "ctx.\_source.tags.contains(tag) ? ctx.op = "none" : ctx.\_source.tags += tag",  
> > "params" : {  
> > "tag" : "Orange"  
> > }  
> > }'
> > 
> > I want to check for a tag before adding to it. If the tag exists, do nothing else perform the update to add.
> > 
> > the above query creates a new version for the record but does not add the new tag.
> > 
> > How can i acheive this?
> > 
> > Thanks
> > 
> > Kuwar Sahani
> > 
> > --  
> > 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).

--  
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: ![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, 2:41am UTC](https://discuss.elastic.co/t/update-api/11582/4 "2017-07-06T02:41:02Z")

</div>


