elasticsearch - 摄取处理器 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
但是,我对无痛脚本的理解还不够,无法解决这个问题。
解决方案
如果您有一个具体的数组字段(我们称之为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 新手,请试一试!
推荐阅读
- font-awesome - 字体真棒:双色调只出现一半的时间
- java - 在 JUnit 测试中给定 JWT 令牌时,MockMVC 不返回任何值
- node.js - 如何修复 SyntaxError: Unexpected token ... 在我的本地机器上使用 socket.io
- google-app-engine - Google App Engine 的文件缓存有哪些选项?
- wordpress - 覆盖 woocommerce 添加购物车链接
- python - 由于令牌问题,在 Heroku 上托管 Discord.py 机器人失败
- javascript - 如何访问嵌套的 JSON 数据
- javascript - 我需要使用 JSON 对象从数组创建图像库
- javascript - 弹出窗口打开和关闭时正确执行脚本
- python - 如何在 Python 中使用 OpenCV 检测银行收据中的列并将图像拆分为单独的文件?