"Number" elasticsearch 中的限制词是否无痛?

Is "Number" restricted word in elasticsearch painless?

我使用 elasticsearch 无痛工作了两年多。

这几天我发现了一个奇怪的问题,将painless变量命名为“Number”。 painless脚本中使用“Number”(首字母大写)时出现错误。

我成功重现了这个场景。 我正在使用 elasticsearch 版本 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; "}}}}' 

问题来自: def 数字 = 0;

抛出的错误是:

  "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
}

这对我来说发生在任何 elasticsearch 索引上,es 版本 6.8

更准确地说,这是此处显示的场景中使用的索引。

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"
  }
}
'

还有映射:

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
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

字符串“Number”作为“number”(全部小写)的任何变体都有效。 “Numb”、“Numbe”也适用..

有什么帮助吗?

谢谢。

解决方案:

这里提到的几乎所有区分大小写的关键字都会抛出与上面所示相同的错误。

我的好习惯是在创建与 elasticsearch 字段同名的无痛变量时使用下划线作为前缀..

例如:

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

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.

因此,您不能使用 Number 作为任意变量标识符。