Update API Conditional Delete

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.

--

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.

--

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.

--