Control flow script not working


(deejay) #1

Hi,

I have a java method that creates a new document if it does not already exist otherwise sets a field in the doc to a value given a condition. This is done in a bulk request.

bulkRequest.add(client.prepareUpdate("tests", "test", "myId")
.setFields("_source")
.setScript("ctx._source.counter > 0 ? 20")
.setUpsert(contentBuilder)

The above does not work.

However if I remove the conditional check and simply increment the counter value it works (without changing anything else in the code).

bulkRequest.add(client.prepareUpdate("tests", "test", "myId")
.setFields("_source")
.setScript("ctx._source.counter += 1")
.setUpsert(contentBuilder)

Can someone please assist me as I do not know what I am doing wrong. I am using ES 1.0.

Thank you.

Dirk


(deejay) #2

I am not sure if anyone can read this post as it says it has not been accepted by the mailing list.

But just an update - I managed to fix the issue by adding parentheses to the code:

bulkRequest.add(client.prepareUpdate("tests", "test", "myId")
.setFields("_source")
.setScript("if (ctx._source.counter > 0) ctx._source.counter = 20")
.setUpsert(contentBuilder)

Does not work.

bulkRequest.add(client.prepareUpdate("tests", "test", "myId")
.setFields("_source")
.setScript("if (ctx._source.counter > 0) { ctx._source.counter = 20};")
.setUpsert(contentBuilder)

Working.


(system) #3