如何在AWS ElasticSearch的Painless内联脚本中替换没有正则表达式的字符串?

2023-12-26

文档中“级别”字段的类型已从“关键字”更改为“短”,我正在尝试重新索引现有数据以便能够在 Kibana 图表中使用它。 旧数据包含以下值:“100%”、“错误”或只是空字符串“”。

我只想获取新索引内的整数。我使用内部重新索引 API(添加新行以使代码片段更具可读性):

curl -s -X POST -H 'Content-Type: application/json' https://search-host.us-east-1.es.amazonaws.com/_reindex -d '{
  "source": {
    "index": "old-index"
  },  
  "dest": {
    "index": "new-index"
  },  
  "script": {
    "inline": "
        if (ctx._source.level == \"error\" || ctx._source.level == \"\")
        {
            ctx._source.level = -1
        } else {
            ctx._source.level = Integer.valueOf(ctx._source.level)    )
        }
    "
  }
}'

但我收到错误:“java.lang.String 无法转换为 java.lang.Number”,因为值末尾有“%”符号。

另外,我没有为 AWS ElasticSearch 启用正则表达式,并且不可能按照我的想法进行操作。所以带有replaceAll的变体对我来说不起作用。例如,如果我有自托管 ES,它可能是这样的(没有测试):/(%)?/.matcher(doc['level'].value).replaceAll('$1'):

但从 AWS ES 我看到了这一点:

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.

是否可以在没有正则表达式的情况下用 Painless 语言替换字符串?


"script": {
    "lang":"painless",
    "source": """

      //function declaration
      String replace(String word, String oldValue, String newValue) {
        String[] pieces = word.splitOnToken(oldValue);
        int lastElIndex = pieces.length-1;
        pieces[lastElIndex] = newValue;
        def list = Arrays.asList(pieces);
        return String.join('',list);
      }

      //usage sample
      ctx._source["date"] = replace(ctx._source["date"],"+0000","Z");

    """
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在AWS ElasticSearch的Painless内联脚本中替换没有正则表达式的字符串? 的相关文章

随机推荐