3

I want to update 2 fields in a document in a single update request, using an inline painless script:

{
    "script" : {
        "inline": "ctx._source.counter1 ++ ; ctx._source.counter2 == 0 ? ctx.op = 'noop' : ctx._source.counter2 ++"}
}

Problem is - if the condition is met and ctx.op = 'noop' then the first part of the script (ctx._source.counter1 ++ ;) is also not being executed.

How would u recommend I should do this?
I can split the operation into 2 update requests which will double my db calls (but maybe a 'noop' call is extremely fast).
I also tried to swap the 2 parts of script (the conditional first , the increment second) - but then I'm getting a compilation error:

  "script_stack": [
    " ctx._source. ...",
    " ^---- HERE"
  ],
  "script": " ctx._source.counter2 > 0 ? ctx.op = 'noop' : ctx._source.counter2++ ; ctx._source.counter1++  ",
  "lang": "painless",
  "caused_by": {
    "type": "illegal_argument_exception",
    "reason": "Not a statement."
  }

Any ideas? Thanks

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
belostoky
  • 934
  • 2
  • 11
  • 22

0 Answers0