# Order of CTX operators?

**URL:** <https://discuss.elastic.co/t/order-of-ctx-operators/281442>\
**Category:** Elasticsearch\
**Created:** [August 14, 2021, 2:36pm UTC](https://discuss.elastic.co/t/order-of-ctx-operators/281442 "2021-08-14T14:36:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jason\_Baumgartner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jason_baumgartner/32/92716_2.png) [@Jason\_Baumgartner](https://discuss.elastic.co/u/Jason_Baumgartner)\
**Post date:** [August 14, 2021, 2:36pm UTC](https://discuss.elastic.co/t/order-of-ctx-operators/281442/1 "2021-08-14T14:36:22Z")

</div>

I am trying to make an update script condition that specifies only updating a document if the retrieved\_utc is newer than the existing one and then after that, examine several other conditions. When I try to use parentheses, I get an error back for some reason (so I'm probably formatting it incorrectly) -- Without them, I don't get an error. This is what the conditions look like:

```auto
if (ctx._source.retrieved_utc < params.retrieved_utc && 
ctx._source.retweet_count < params.retweet_count ||
ctx._source.reply_count < params.reply_count ||
ctx._source.favorite_count < params.favorite_count)
{ctx._source.retweet_count = params.retweet_count;
ctx._source.reply_count = params.reply_count;
ctx._source.favorite_count = params.favorite_count;
ctx._source.updated_utc = params.updated_utc } else {ctx.op="none"}

```

Will it evaluate the AND and give it precedence over the ORs? Just to clarify, I want to first check if the document is newer (using the retrieved\_utc field) and then go through the OR conditions and if any of the OR conditions are true, it will update the document.

UPDATE: I finally got the parenthesis to work! I must have had some issue with the formatting previously. The new statement looks like this:

```auto
if (ctx._source.retrieved_utc < params.retrieved_utc &&
(ctx._source.retweet_count < params.retweet_count ||
ctx._source.reply_count < params.reply_count ||
ctx._source.favorite_count < params.favorite_count))
{ctx._source.retweet_count = params.retweet_count;
ctx._source.reply_count = params.reply_count;
ctx._source.favorite_count = params.favorite_count;
ctx._source.updated_utc = params.updated_utc } else {ctx.op="none"}

```

---

<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:** [September 11, 2021, 2:37pm UTC](https://discuss.elastic.co/t/order-of-ctx-operators/281442/2 "2021-09-11T14:37:17Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
