使用 Painless 在 Elastic 文档上设置硬编码值
我正在尝试学习 Painless,以便我可以在尝试丰富和操作传入文档的同时使用它。然而,我见过的每种访问文档的方法都会导致错误。 在 Kibana 的 Painless Lab 中输入此内容后,我收到以下错误:
def paths = new String[3];
paths[0]= '.com';
paths[1] = 'bar.com';
paths[2] = 'foo.bar.com';
doc['my_field'] = paths; // does not work: '[Ljava.lang.String; cannot be cast to org.elasticsearch.index.fielddata.ScriptDocValues'
ctx.my_field = paths; // does not compile: 'cannot resolve symbol [ctx.my_field]'
return doc['my_field'] == 'field_value'; // does not work: 'No field found for [my_field] in mapping'
doc['my_field'] == 'field_value'
尽管该字段存在于测试文档中,但仍抱怨 < code>doc.containsKey('my_field') 确实返回 false
。
我实际上应该如何访问和操作传入的文档?我正在使用 ElasticSearch 7.12。
I'm trying to learn Painless so that I could use it while trying to enrich and manipulate incoming documents. However, every way I've seen for accessing the document just results in errors.
Having input this in the Painless Lab in Kibana, these are the errors I'm getting:
def paths = new String[3];
paths[0]= '.com';
paths[1] = 'bar.com';
paths[2] = 'foo.bar.com';
doc['my_field'] = paths; // does not work: '[Ljava.lang.String; cannot be cast to org.elasticsearch.index.fielddata.ScriptDocValues'
ctx.my_field = paths; // does not compile: 'cannot resolve symbol [ctx.my_field]'
return doc['my_field'] == 'field_value'; // does not work: 'No field found for [my_field] in mapping'
doc['my_field'] == 'field_value'
complains despite the field being present in the test document, though doc.containsKey('my_field')
does return false
.
How should I actually be accessing and manipulating the incoming document? I'm using ElasticSearch 7.12.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 摄取管道 "https://www.elastic.co/guide/en/elasticsearch/reference/master/set-processor.html" rel="nofollow noreferrer">设置处理器,用于向传入文档添加硬编码值。
有非常具体的 上下文可用于无痛 API。您正在使用
String[]
,这可能会导致问题,因此您需要使用Arrays
或ArraysList
。您可以查看无痛实验室示例 这里。下面是我在无痛实验室中尝试过的脚本,它按预期工作:
在参数选项卡中添加以下内容,我错过了在答案中添加此内容。这是必需的,因为在实际实现中,您将从上下文中获取值并更新上下文。
You can create ingest pipeline with set processor for adding hardcode value to incoming document.
There are very specific context available for painless API. you are using
String[]
which may be causing issue so you need to use eitherArrays
orArraysList
. you can check example of painless lab here.Below is script i have tried in painless lab and it is working as expcted:
Add below in parameters tab, i missed to add this in answer. this required because in actual implmentation you will get value from context and update context.