Elasticsearch 中的无痛脚本:使用 String.format 时出现“无法从 [int] 转换为 [def[]]”错误

问题描述 投票:0回答:1

我正在尝试在 Elasticsearch 中使用 Painless 脚本来格式化带有前导零的整数。这是我正在使用的脚本:

{
  "query": {
    "match_all": {}
  },
  "sort": {
    "_script": {
      "type": "string",
      "script": {
        "source": """
          int paddingWidth = 10;
          def value = doc['employee_code.keyword'].value;
          if (value == null) return "";
          boolean isNumeric = true;
          if (isNumeric) {
            int temp = 2;
            return String.format('%010d', temp);
          } else {
            return value;
          }
        """,
        "lang": "painless"
      },
      "order": "asc"
    }
  }
}

但是,我收到以下错误:

{
  "error" : {
    "root_cause" : [
      {
        "type" : "script_exception",
        "reason" : "compile error",
        "script_stack" : [
          "... n String.format('%010d', temp);\n          } else { ...",
          "                             ^---- HERE"
        ],
        "script" : "\n          int paddingWidth = 10;\n          def value = doc['employee_code.keyword'].value;\n          if (value == null) return \"\";\n          boolean isNumeric = true;\n          if (isNumeric) {\n            int temp = 2;\n            return String.format('%010d', temp);\n          } else {\n            return value;\n          }\n        ",
        "lang" : "painless",
        "position" : {
          "offset" : 263,
          "start" : 238,
          "end" : 288
        }
      }
    ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query",
    "grouped" : true,
    "failed_shards" : [
      {
        "shard" : 0,
        "index" : "search_psr_core_index",
        "node" : "govZA-NHQyStPH2nWOujMQ",
        "reason" : {
          "type" : "script_exception",
          "reason" : "compile error",
          "script_stack" : [
            "... n String.format('%010d', temp);\n          } else { ...",
            "                             ^---- HERE"
          ],
          "script" : "\n          int paddingWidth = 10;\n          def value = doc['employee_code.keyword'].value;\n          if (value == null) return \"\";\n          boolean isNumeric = true;\n          if (isNumeric) {\n            int temp = 2;\n            return String.format('%010d', temp);\n          } else {\n            return value;\n          }\n        ",
          "lang" : "painless",
          "position" : {
            "offset" : 263,
            "start" : 238,
            "end" : 288
          },
          "caused_by" : {
            "type" : "class_cast_exception",
            "reason" : "Cannot cast from [int] to [def[]]."
          }
        }
      }
    ]
  },
  "status" : 400
}

附加信息:

  1. Elasticsearch 版本:7.x

我尝试解决此错误,但它似乎与 Painless 处理字符串格式的方式有关。任何帮助或指示将不胜感激。

elasticsearch
1个回答
0
投票

我认为你应该使用这个

String.format('%010d', new def[] {temp})

而不是

String.format('%010d', temp);

我认为问题就在这里,painless 不像 java 和 format 方法需要一个对象数组来插入。由于您直接将整数插入格式,因此会出现错误。

我们可以插入单个元素数组。

© www.soinside.com 2019 - 2024. All rights reserved.