首页 > 解决方案 > 脚本字段正则表达式不适用于多个节点

问题描述

我想创建一个基于 cs_uri_query 列的脚本字段。当我创建这个正则表达式公式时,我一直在使用 elasticsearch 7.5 直到 7.6。它在索引 A 的脚本字段中运行良好。现在我已经将我的 elasticsearch 升级到 7.7,并且我已经将索引 B 摄取到我的 elasticsearch。索引 B 和索引 A 具有相同的格式(列中的所有内容都相同)所以我想使用相同的正则表达式公式。但不幸的是,当我将索引 A 脚本字段复制到索引 B 中时。

它在索引 B 上返回此错误

{
 "root_cause": [
  {
   "type": "script_exception",
   "reason": "compile error",
   "script_stack": [
    "... = 0) return '';\r\ndef m = /(...(?=&custid))/.matche ...",
    "                             ^---- HERE"
   ],
   "script": "if (doc['cs_uri_query.keyword'].size() == 0) return '';\r\ndef m = /(...(?=&custid))/.matcher(doc['cs_uri_query.keyword'].value);\r\nif(m.find () )\r\n{return m.group(1) } \r\nelse { return \"No ID\" }",
   "lang": "painless",
   "position": {
    "offset": 65,
    "start": 40,
    "end": 90
   }
  }
 ],
 "type": "search_phase_execution_exception",
 "reason": "all shards failed",
 "phase": "query",
 "grouped": true,
 "failed_shards": [
  {
   "shard": 0,
   "index": "janmei",
   "node": "KVuYPKPBRIO-mIQu0lS3LQ",
   "reason": {
    "type": "script_exception",
    "reason": "compile error",
    "script_stack": [
     "... = 0) return '';\r\ndef m = /(...(?=&custid))/.matche ...",
     "                             ^---- HERE"
    ],
    "script": "if (doc['cs_uri_query.keyword'].size() == 0) return '';\r\ndef m = /(...(?=&custid))/.matcher(doc['cs_uri_query.keyword'].value);\r\nif(m.find () )\r\n{return m.group(1) } \r\nelse { return \"No ID\" }",
    "lang": "painless",
    "position": {
     "offset": 65,
     "start": 40,
     "end": 90
    },
    "caused_by": {
     "type": "illegal_state_exception",
     "reason": "Regexes are disabled. Set [script.painless.regex.enabled] to [true] in elasticsearch.yaml to allow them. Be careful though, regexes break out of Painless's protection against deep recursion and long loops."
    }
   }
  }
 ]
}

样本

id=000&custid=77777777&email=email@gmail.com

脚本字段

if (doc['cs_uri_query.keyword'].size() == 0) return '';
def m = /(...(?=&custid))/.matcher(doc['cs_uri_query.keyword'].value);
if(m.find () )
{return m.group(1) } 
else { return "No ID" }

我将完全相同的脚本从索引 A 复制到索引 B。有人可以向我解释一下吗?非常感谢

标签: regexelasticsearchkibanaelastic-stack

解决方案


正如错误所述,

正则表达式被禁用。在 elasticsearch.yaml 中将 [script.painless.regex.enabled] 设置为 [true] 以允许它们。不过要小心,正则表达式会突破 Painless 对深度递归和长循环的保护。

你需要设置

script.painless.regex.enabled: true

在您的elasticsearch.yml字段中并重新启动 ES。


推荐阅读