Home > front end >  Is "Number" restricted word in elasticsearch painless?
Is "Number" restricted word in elasticsearch painless?

Time:11-26

I'm working with elasticsearch painless more than two years.

These days I found out a weird problem with naming painless variable as "Number". When "Number" (first letter is uppercase) is used in painless script, error occured.

I sucessfully reproduce this scenario. I'm using elasticsearch version 6.8.

curl -X POST "http://localhost:9201/logs-my_app-default/_search?size=2&pretty" -H 'Content-Type: application/json' -d '{"_source": ["-1"],  "script_fields": {"SCRIPT_FLAG": {"script": {"lang": "painless", "source": "  def Number = 0; "}}}}' 

The problem comes from: def Number = 0;

Thrown error is:

  "error" : {
    "root_cause" : [
      {
        "type" : "script_exception",
        "reason" : "compile error",
        "script_stack" : [
          "  def Number = 0; ",
          "      ^---- HERE"
        ],
        "script" : "  def Number = 0; ",
        "lang" : "painless"
      }
    ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query",
    "grouped" : true,
    "failed_shards" : [
      {
        "shard" : 0,
        "index" : "logs-my_app-default",
        "node" : "9jBNNKjdSIO6I8UH_HLVRw",
        "reason" : {
          "type" : "script_exception",
          "reason" : "compile error",
          "script_stack" : [
            "  def Number = 0; ",
            "      ^---- HERE"
          ],
          "script" : "  def Number = 0; ",
          "lang" : "painless",
          "caused_by" : {
            "type" : "illegal_argument_exception",
            "reason" : "invalid sequence of tokens near ['Number'].",
            "caused_by" : {
              "type" : "no_viable_alt_exception",
              "reason" : null
            }
          }
        }
      }
    ]
  },
  "status" : 500
}

This is happening on any elasticsearch index for me, es version 6.8

To be more precise, this is the index used in scenario shown here.

curl -X POST "localhost:9201/logs-my_app-default/_doc?pretty" -H 'Content-Type: application/json' -d'
{
  "@timestamp": "2099-05-06T16:21:15.000Z",
  "event": {
    "original": "192.0.2.42 - - [06/May/2099:16:21:15  0000] \"GET /images/bg.jpg HTTP/1.0\" 200 24736"
  }
}
'

Mapping as well:

curl -X GET "localhost:9201/logs-my_app-default/_mapping?pretty"
{
  "logs-my_app-default" : {
    "mappings" : {
      "_doc" : {
        "properties" : {
          "@timestamp" : {
            "type" : "date"
          },
          "event" : {
            "properties" : {
              "original" : {
                "type" : "text",
                "fields" : {
                  "keyword" : {
                    "type" : "keyword",
                    "ignore_above" : 256
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Any variant of string "Number" as "number" (all lowercase) works. "Numb", "Numbe" works as well..

Any help?

Thank you.

SOLUTION:

Almost all of the casesensitive keywords mentioned here are throwing the same error as shown above.

Good practice for me is to use underscore as prefix when creating painless variable with same name as elasticsearch field ..

For example:

def _Number = 0; // instead of def Number = 0, 
def _Void = 0;   // instead of def Void = 0; , 
def _System = 0; // instead of def System = 0;   ..

CodePudding user response:

The Painless scripting language is very similar to the Java programming language, compiles directly into JVM bytecode, and its API borrows a lot from the Java API, namely the Number class.

For this reason, you cannot use Number as an arbitrary variable identifier.

  • Related