首页 > 解决方案 > 摄取处理器 foreach 或脚本以替换数组中的所有项目

问题描述

我正在尝试运行摄取管道以将数组中的“开”和“关”实例替换为真和假。

这适用于普通字符串,例如像这样的数据

[{onoffboolean: "on"}]

我可以用以下方法处理这个:

processors: [
  {
    set: {
      field: 'onoffboolean',
      description: 'String represented trues to native true',
      if: "ctx?.onoffboolean == 'on'",
      value: true
    }
  },
  {
    set: {
      field: 'onoffboolean',
      description: 'String represented falses to native true',
      if: "ctx?.onoffboolean == 'off'",
      value: false
    }
  },
],

但是,当它是一个值数组时,例如:

["on", "on", "off"] to process into [true, true, false]

我无法找到合适的处理器来处理这个问题。我曾尝试使用 foreach,但似乎在使用“if”条件时“_ingest._value”不可用。

这个弹性论坛主题建议使用无痛脚本

https://discuss.elastic.co/t/foreach-ingest-processor-conditional-append-processor/216884/2

但是,我对无痛脚本的理解还不够,无法解决这个问题。

标签: elasticsearchelastic-stackelasticsearch-painlessdata-ingestion

解决方案


如果您有一个具体的数组字段(我们称之为list_of_attributes),您可以使用以下脚本处理器:

PUT _ingest/pipeline/bool_converter
{
  "description": "Trims and lowercases all string values",
  "processors": [
    {
      "script": {
        "source": """
          ctx.list_of_attributes = ctx.list_of_attributes.stream()
                                                         .map(str -> str == 'on' ? true : false)
                                                         .collect(Collectors.toList()) 
        """
      }
    }
  ]
}

然后在您摄取文档时应用它:

POST your-index/_doc?pipeline=bool_converter
{
  "list_of_attributes": ["on", "on", "off"]
}

如果您有多个这样的数组字段,您可以通过调整我对Run Elasticsearch processor on all fields of a document问题的回答来迭代文档的字段。

无耻的插件:我在最近发布的Elasticsearch Handbook中专门用了一章来介绍摄取和管道。如果您是 ES 新手,请试一试!


推荐阅读