Simon
Simon

Reputation: 1761

How does elasticsearch handle returns inside a scripted update query?

I can't find the relevant documentation describing the return keyword. Where is this documented?

I am running the following query

POST /myindex/mytype/FwOaGmQBdhLB1nuQhK1Q/_update
{
  "script": {
    "source": """
      if (ctx._source.owner._id.equals(params.signedInUserId)){
        for (int i = 0; i < ctx._source.managers.length; i++) {
          if (ctx._source.managers[i].email.equals(params.managerEmail)) {
            ctx._source.managers.remove(i);
            return;
          }
        }
      }
      ctx.op = 'noop';
    """,
    "lang": "painless",
    "params": {
      "signedInUserId": "auth0|5a78c1ccebf64a46ecdd0d9c",
      "managerEmail": "[email protected]"
    }
  },
  "_source": true
}

but I'm getting the error

"type": "illegal_argument_exception",
"reason": "failed to execute script",
"caused_by": {
  "type": "script_exception",
  "reason": "compile error",
  "script_stack": [
    "... ve(i);\n            return;\n          }\n        }\n  ...",
    "                             ^---- HERE"
  ],
  "script": <the script here>,
  "lang": "painless",
  "caused_by": {
    "type": "illegal_argument_exception",
    "reason": "invalid sequence of tokens near [';'].",
    "caused_by": {
      "type": "no_viable_alt_exception",
      "reason": null
    }
  }

If I remove return keyword, then the script runs but I get the wrong behavior as expected. I can correct the behavior by using a Boolean to keep track of email removal, but why can't I return early?

Upvotes: 1

Views: 1382

Answers (1)

dcd018
dcd018

Reputation: 781

It's hard to say, you could avoid null/void returns by passing a lambda comparator to either retainAll or removeIf

ctx._source.managers.removeIf(m -> m.email.equals(params.managerEmail))

Lambda expressions and method references work the same as Java’s.

Upvotes: 1

Related Questions