Painless empty block?

I find it odd that Painless appears to not allow brackets-only empty blocks, e.g. for if statements (ES 5.3).

The following works OK:

          "script" : {
             "script" : {
                "inline": "if(doc != null){ /* comment only */ true}",
                "lang": "painless"

while removing the "true" causes the snippet to fail with "Extraneous if statement."

    "script_stack": [
      "if(doc != null){ /* comme ...",
      "^---- HERE"
    "script": "if(doc != null){ /* comment only */ }",
    "lang": "painless",
    "caused_by": {
      "type": "illegal_argument_exception",
      "reason": "Extraneous if statement."

have you tried just return doc != null to simplify this piece of code?

We're generating painless scripts from higher-level condition representations. One obvious workaround there is to prevent the generator from outputting the condition in the first place, when the block would be empty. Or, add a no-op like "true", as above. Or, like you say, re-write to remove the whole "if".

Certainly, this is not a bug. I think it's just odd. Don't have practically all bracket-style languages a definition for "block-statement" like "block-statement := { statement-list }" with "statement-list := empty, or a statement-list followed by a statement".

Well, Painless apparently does not. Fair enough, NP. Of course, the purpose of this note is to help anyone wondering about the error message "Extraneous if statement" (like Painless is a pain in my head and

1 Like

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