Elasticsearch 摄取管道是否有一种方法可以根据字段名称后缀在多个字段上使用转换处理器?
Is there a way on Elasticsearch ingest-pipelines to use convert processor on multiple fields based on field name suffix?
我们有一个带有默认管道的索引,可将字段转换为布尔值,如此处所述https://www.elastic.co/guide/en/elasticsearch/reference/7.10/convert-processor.html
我们正在使用 Elasticsearch v7.10。
有没有一种方法可以创建管道以根据字段名称后缀(如“_b”)转换多个字段?
类似于:
PUT _ingest/pipeline/string-to-bool
{
"description": "converts fields ending in '_b' to boolean",
"processors" : [
{
"convert" : {
"field": "*_b",
"type": "boolean"
}
}
]
}
Tldr;
据我所知convert processor不允许模糊匹配。
解决方法
正如您所建议的那样,您可以编写一个脚本来完成这项工作。
PUT _ingest/pipeline/string-to-bool-script
{
"processors" : [
{
"script": {
"lang": "painless",
"source": """
for (key in ctx.keySet()) {
if (key.endsWith("_b")) {
ctx[key] = Boolean.parseBoolean(ctx[key]);
}
}
"""
}
}
]
}
POST /_ingest/pipeline/string-to-bool-script/_simulate
{
"docs": [
{
"_index": "index",
"_id": "id",
"_source": {
"plop_b": "true",
"_b": "false"
}
}
]
}
请记住,这非常简单,仅适用于值为 true
/ false
的字符串(不区分大小写)。
我们有一个带有默认管道的索引,可将字段转换为布尔值,如此处所述https://www.elastic.co/guide/en/elasticsearch/reference/7.10/convert-processor.html 我们正在使用 Elasticsearch v7.10。
有没有一种方法可以创建管道以根据字段名称后缀(如“_b”)转换多个字段? 类似于:
PUT _ingest/pipeline/string-to-bool
{
"description": "converts fields ending in '_b' to boolean",
"processors" : [
{
"convert" : {
"field": "*_b",
"type": "boolean"
}
}
]
}
Tldr;
据我所知convert processor不允许模糊匹配。
解决方法
正如您所建议的那样,您可以编写一个脚本来完成这项工作。
PUT _ingest/pipeline/string-to-bool-script
{
"processors" : [
{
"script": {
"lang": "painless",
"source": """
for (key in ctx.keySet()) {
if (key.endsWith("_b")) {
ctx[key] = Boolean.parseBoolean(ctx[key]);
}
}
"""
}
}
]
}
POST /_ingest/pipeline/string-to-bool-script/_simulate
{
"docs": [
{
"_index": "index",
"_id": "id",
"_source": {
"plop_b": "true",
"_b": "false"
}
}
]
}
请记住,这非常简单,仅适用于值为 true
/ false
的字符串(不区分大小写)。