我正在尝试获取字符串的编号。我正在使用正则表达式模式执行此操作。当我得到结果时,我得到类似“1”、“10”、“2”的东西。我想对它进行排序,所以我需要在左边添加零才能自然地对它们进行排序。
我的脚本在这里:
{
"from": 0,
"size": 40,
"sort": [
{
"fields.myfield.keyword": { "order": "asc" },
"_script": {
"type": "string",
"script": {
"inline":
"def m = /(\\d+$)/.matcher(doc['fields.myfield.keyword'].value);
if ( m.find() )
{
String s = m.group(1);
String.format(\"%05d\", s.toString());
}
else { return 0 }"
},
"order" : "asc"
}
}
],
"_source": { "include": ["fields.myfield"] }}
当我尝试执行命令时,出现以下错误:
"failed_shards": [
{
"shard": 0,
"index": "myindex",
"node": "kctN6d5ITrqtIMj4cbChKQ",
"reason": {
"type": "script_exception",
"reason": "compile error",
"script_stack": [
"... ; String.format(\"%05d\", s.toString()); } else { re ...",
" ^---- HERE"
],
"script": "def m = /(\\d+$)/.matcher(doc['fields.myfield.keyword'].value); if ( m.find() ) { String s = m.group(1); String.format(\"%05d\", s.toString()); } else { return 0 }",
"lang": "painless",
"caused_by": {
"type": "class_cast_exception",
"reason": "Cannot cast from [String] to [def[]]."
}
}
}
如何在不使用 String.Formatter 的情况下添加零?
我在哪里尝试从 STRING 转换为 DEF?