gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 22:13:54 24 4
gpt4 key购买 nike

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

我只想在新索引中获取整数。我使用内部重新索引 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 cannot be cast to 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");

"""
}

关于elasticsearch - 如何在 AWS ElasticSearch 的无痛内联脚本中替换没有正则表达式的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47698187/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com