# Control flow (if statement) script not working

**URL:** <https://discuss.elastic.co/t/control-flow-if-statement-script-not-working/15824>\
**Category:** Elasticsearch\
**Created:** [February 15, 2014, 3:25pm UTC](https://discuss.elastic.co/t/control-flow-if-statement-script-not-working/15824 "2014-02-15T15:25:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![deejay](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@deejay](https://discuss.elastic.co/u/deejay)\
**Post date:** [February 15, 2014, 3:25pm UTC](https://discuss.elastic.co/t/control-flow-if-statement-script-not-working/15824/1 "2014-02-15T15:25:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![deejay](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@deejay](https://discuss.elastic.co/u/deejay)\
**Post date:** [February 15, 2014, 5:23pm UTC](https://discuss.elastic.co/t/control-flow-if-statement-script-not-working/15824/2 "2014-02-15T17:23:36Z")

</div>

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.

---

<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, 1:49am UTC](https://discuss.elastic.co/t/control-flow-if-statement-script-not-working/15824/3 "2017-07-06T01:49:51Z")

</div>


