使用 Painless 在 Elastic 文档上设置硬编码值

发布于 2025-01-11 11:33:37 字数 741 浏览 0 评论 0原文

我正在尝试学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥抱影子 2025-01-18 11:33:37

您可以使用 摄取管道 "https://www.elastic.co/guide/en/elasticsearch/reference/master/set-processor.html" rel="nofollow noreferrer">设置处理器,用于向传入文档添加硬编码值。

{
  "description" : "sets the value of count to 1",
  "set": {
    "field": "count",
    "value": 1
  }
}

有非常具体的 上下文可用于无痛 API。您正在使用String[],这可能会导致问题,因此您需要使用ArraysArraysList。您可以查看无痛实验室示例 这里

下面是我在无痛实验室中尝试过的脚本,它按预期工作:

def ctx = params.ctx;
ArrayList paths = new ArrayList();
paths.add('.com');
paths.add('bar.com');
paths.add('foo.bar.com');
ctx['my_field'] = paths;
return ctx

在此处输入图像描述

在参数选项卡中添加以下内容,我错过了在答案中添加此内容。这是必需的,因为在实际实现中,您将从上下文中获取值并更新上下文。

{
  "ctx":{
    "my_field":["test"]
  }
}

You can create ingest pipeline with set processor for adding hardcode value to incoming document.

{
  "description" : "sets the value of count to 1",
  "set": {
    "field": "count",
    "value": 1
  }
}

There are very specific context available for painless API. you are using String[] which may be causing issue so you need to use either Arrays or ArraysList. you can check example of painless lab here.

Below is script i have tried in painless lab and it is working as expcted:

def ctx = params.ctx;
ArrayList paths = new ArrayList();
paths.add('.com');
paths.add('bar.com');
paths.add('foo.bar.com');
ctx['my_field'] = paths;
return ctx

enter image description here

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.

{
  "ctx":{
    "my_field":["test"]
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文