# Update API Conditional Delete

**URL:** <https://discuss.elastic.co/t/update-api-conditional-delete/9726>\
**Category:** Elasticsearch\
**Created:** [November 15, 2012, 12:20pm UTC](https://discuss.elastic.co/t/update-api-conditional-delete/9726 "2012-11-15T12:20:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris\_harrington](https://avatars.discourse-cdn.com/v4/letter/c/77aa72/32.png) [@chris\_harrington](https://discuss.elastic.co/u/chris_harrington)\
**Post date:** [November 15, 2012, 12:20pm UTC](https://discuss.elastic.co/t/update-api-conditional-delete/9726/1 "2012-11-15T12:20:57Z")

</div>

I've only just started learning ES and I'm going through the API guide and  
playing around with it to see what I can and can't do. I'm currently  
looking at the Update API and have a question on script update.

From the API

curl -XPOST 'localhost:9200/test/type1/1/\_update' -d '{

```
"script" : "ctx._source.tags += tag",

"params" : {

    "tag" : "blue"

}

```

}'

So I tried this and it worked fine curl -XPOST  
'localhost:9200/twitter/tweet/1/\_update' -d  
'{"script":"ctx.\_source.alignment=='''evil'''? ctx.\_source.arrested =  
"true"","params":{"alignment":"evil"}}'

and further down the page it has what looks like an inline if statement

curl -XPOST 'localhost:9200/test/type1/1/\_update' -d '{

```
"script" : "ctx._source.tags.contains(tag) ? ctx.op = \"delete\" : 

```

ctx.op = "none"",

```
"params" : {

    "tag" : "blue"

}

```

}'

And since I'm only playing around and seeing what I can and can't do with  
ES, I thought maybe this would work, it executes but doesn't add  
"arrested":"false" to the document.

curl -XPOST 'localhost:9200/twitter/tweet/2/\_update' -d  
'{"script":"ctx.\_source.alignment=='''evil'''? ctx.\_source.arrested =  
"true":ctx.\_source.arrested="false"","params":{"alignment":"evil"}}'

is the inline if given in the guide purely for operations and I'm trying to  
do something not allowed or is there support for conditional updates like  
this and I've just done it wrong.

Of course I could just update the docs with the field alignment = evil then  
update the docs where alignment = good, but like I said just trying to  
figure what I can and can't do.

--

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [November 15, 2012, 9:24pm UTC](https://discuss.elastic.co/t/update-api-conditional-delete/9726/2 "2012-11-15T21:24:15Z")

</div>

Welcome to the mailing list.

I am not sure if this is a bug or a feature that I just don't quite  
understand, but for some reason MVEL interprets this expression:  
ctx.\_source.alignment=="evil"? ctx.\_source.arrested =  
"true":ctx.\_source.arrested="false"  
as this:  
ctx.\_source.alignment=="evil"? ( ctx.\_source.arrested = "true" :  
ctx.\_source.arrested="false")  
and then it just drop the part after ":". So, if expression is "true" the  
arrested field is set, otherwise nothing happen. It only behaves this way  
when you have an assignment as the first expression after "?".

Possible workarounds are:  
ctx.\_source.alignment=="evil"? (ctx.\_source.arrested =  
"true"):(ctx.\_source.arrested="false")  
ctx.\_source.arrested = (ctx.\_source.alignment=="evil")  
if (ctx.\_source.alignment=="evil") {ctx.\_source.arrested = "true"} else  
{ctx.\_source.arrested="false"}

On Thursday, November 15, 2012 7:20:58 AM UTC-5, chris harrington wrote:

> I've only just started learning ES and I'm going through the API guide and  
> playing around with it to see what I can and can't do. I'm currently  
> looking at the Update API and have a question on script update.
> 
> From the API
> 
> curl -XPOST 'localhost:9200/test/type1/1/\_update' -d '{
> 
> ```
> "script" : "ctx._source.tags += tag",
> 
> "params" : {
> 
> "tag" : "blue"
> 
> }
> 
> ```
> 
> }'
> 
> So I tried this and it worked fine curl -XPOST  
> 'localhost:9200/twitter/tweet/1/\_update' -d  
> '{"script":"ctx.\_source.alignment=='''evil'''? ctx.\_source.arrested =  
> "true"","params":{"alignment":"evil"}}'
> 
> and further down the page it has what looks like an inline if statement
> 
> curl -XPOST 'localhost:9200/test/type1/1/\_update' -d '{
> 
> ```
> "script" : "ctx._source.tags.contains(tag) ? ctx.op = \"delete\" : 
> 
> ```
> 
> ctx.op = "none"",
> 
> ```
> "params" : {
> 
> "tag" : "blue"
> 
> }
> 
> ```
> 
> }'
> 
> And since I'm only playing around and seeing what I can and can't do with  
> ES, I thought maybe this would work, it executes but doesn't add  
> "arrested":"false" to the document.
> 
> curl -XPOST 'localhost:9200/twitter/tweet/2/\_update' -d  
> '{"script":"ctx.\_source.alignment=='''evil'''? ctx.\_source.arrested =  
> "true":ctx.\_source.arrested="false"","params":{"alignment":"evil"}}'
> 
> is the inline if given in the guide purely for operations and I'm trying  
> to do something not allowed or is there support for conditional updates  
> like this and I've just done it wrong.
> 
> Of course I could just update the docs with the field alignment = evil  
> then update the docs where alignment = good, but like I said just trying to  
> figure what I can and can't do.

--

---

<div class="post-metadata">

**Author:** ![chris\_harrington](https://avatars.discourse-cdn.com/v4/letter/c/77aa72/32.png) [@chris\_harrington](https://discuss.elastic.co/u/chris_harrington)\
**Post date:** [November 16, 2012, 11:29am UTC](https://discuss.elastic.co/t/update-api-conditional-delete/9726/3 "2012-11-16T11:29:53Z")

</div>

Thanks Igor,

I tried the workarounds you suggested. Each of them worked.  
Seems ES likes brackets around the clauses for the inline if.

On Thursday, 15 November 2012 21:24:15 UTC, Igor Motov wrote:

> Welcome to the mailing list.
> 
> I am not sure if this is a bug or a feature that I just don't quite  
> understand, but for some reason MVEL interprets this expression:  
> ctx.\_source.alignment=="evil"? ctx.\_source.arrested =  
> "true":ctx.\_source.arrested="false"  
> as this:  
> ctx.\_source.alignment=="evil"? ( ctx.\_source.arrested = "true" :  
> ctx.\_source.arrested="false")  
> and then it just drop the part after ":". So, if expression is "true" the  
> arrested field is set, otherwise nothing happen. It only behaves this way  
> when you have an assignment as the first expression after "?".
> 
> Possible workarounds are:  
> ctx.\_source.alignment=="evil"? (ctx.\_source.arrested =  
> "true"):(ctx.\_source.arrested="false")  
> ctx.\_source.arrested = (ctx.\_source.alignment=="evil")  
> if (ctx.\_source.alignment=="evil") {ctx.\_source.arrested = "true"}  
> else {ctx.\_source.arrested="false"}
> 
> On Thursday, November 15, 2012 7:20:58 AM UTC-5, chris harrington wrote:
> 
> > I've only just started learning ES and I'm going through the API guide  
> > and playing around with it to see what I can and can't do. I'm currently  
> > looking at the Update API and have a question on script update.
> > 
> > From the API
> > 
> > curl -XPOST 'localhost:9200/test/type1/1/\_update' -d '{
> > 
> > ```
> > "script" : "ctx._source.tags += tag",
> > 
> > "params" : {
> > 
> > "tag" : "blue"
> > 
> > }
> > 
> > ```
> > 
> > }'
> > 
> > So I tried this and it worked fine curl -XPOST  
> > 'localhost:9200/twitter/tweet/1/\_update' -d  
> > '{"script":"ctx.\_source.alignment=='''evil'''? ctx.\_source.arrested =  
> > "true"","params":{"alignment":"evil"}}'
> > 
> > and further down the page it has what looks like an inline if statement
> > 
> > curl -XPOST 'localhost:9200/test/type1/1/\_update' -d '{
> > 
> > ```
> > "script" : "ctx._source.tags.contains(tag) ? ctx.op = \"delete\" : 
> > 
> > ```
> > 
> > ctx.op = "none"",
> > 
> > ```
> > "params" : {
> > 
> > "tag" : "blue"
> > 
> > }
> > 
> > ```
> > 
> > }'
> > 
> > And since I'm only playing around and seeing what I can and can't do with  
> > ES, I thought maybe this would work, it executes but doesn't add  
> > "arrested":"false" to the document.
> > 
> > curl -XPOST 'localhost:9200/twitter/tweet/2/\_update' -d  
> > '{"script":"ctx.\_source.alignment=='''evil'''? ctx.\_source.arrested =  
> > "true":ctx.\_source.arrested="false"","params":{"alignment":"evil"}}'
> > 
> > is the inline if given in the guide purely for operations and I'm trying  
> > to do something not allowed or is there support for conditional updates  
> > like this and I've just done it wrong.
> > 
> > Of course I could just update the docs with the field alignment = evil  
> > then update the docs where alignment = good, but like I said just trying to  
> > figure what I can and can't do.

--

---

<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:03am UTC](https://discuss.elastic.co/t/update-api-conditional-delete/9726/4 "2017-07-06T03:03:59Z")

</div>


