本文档介绍了如何在 OpenSearch 摄取管道中使用 convert
处理器。如果您的用例涉及大型或复杂数据集,请考虑使用在 OpenSearch 集群上运行的 Data Prepper convert_entry_type
处理器。
Convert 处理器
convert
处理器将文档中的字段转换为不同的类型,例如,将字符串转换为整数,或将整数转换为字符串。对于数组字段,数组中的所有值都将被转换。
语法
convert
处理器的语法如下:
{
"convert": {
"field": "field_name",
"type": "type-value"
}
}
配置参数
下表列出了 convert
处理器所需和可选的参数。
参数 | 必需/可选 | 描述 |
---|---|---|
field(字段) | 必需 | 包含要转换数据的字段名称。支持模板代码段。 |
type(类型) | 必需 | 将字段值转换为的类型。支持的类型有 integer 、long 、float 、double 、string 、boolean 、ip 和 auto 。如果 type 是 boolean ,则如果字段值为字符串 true (不区分大小写),则值设置为 true ;如果字段值为字符串 false (不区分大小写),则值设置为 false 。如果类型设置为 ip ,则此处理器会验证字段值是否符合 IPv4 或 IPv6 地址的正确格式;如果值无效,则会引发错误。如果值不是允许值之一,则会发生错误。 |
description(描述) | 可选 | 处理器的简要描述。 |
if(如果) | 可选 | 运行处理器的条件。 |
ignore_failure(忽略失败) | 可选 | 指定即使处理器遇到错误是否继续执行。如果设置为 true ,则忽略失败。默认为 false 。 |
ignore_missing(忽略缺失) | 可选 | 指定处理器是否应忽略不包含指定字段的文档。如果设置为 true ,则如果字段不存在或为 null ,处理器将不会修改文档。默认值为 false 。 |
on_failure(失败时) | 可选 | 处理器失败时要运行的处理器列表。 |
tag(标签) | 可选 | 处理器的标识符标签。有助于调试以区分相同类型的处理器。 |
target_field(目标字段) | 可选 | 存储解析数据的字段名称。如果未指定,则值将存储在 field 字段中。默认值为 field 。 |
使用处理器
按照以下步骤在管道中使用处理器。
步骤 1:创建管道
以下查询创建了一个名为 convert-price
的管道,该管道将 price
转换为浮点数,将转换后的值存储在 price_float
字段中,如果值小于 0
,则将其设置为 0
。
PUT _ingest/pipeline/convert-price
{
"description": "Pipeline that converts price to floating-point number and sets value to zero if price less than zero",
"processors": [
{
"convert": {
"field": "price",
"type": "float",
"target_field": "price_float"
}
},
{
"set": {
"field": "price",
"value": "0",
"if": "ctx.price_float < 0"
}
}
]
}
步骤 2(可选):测试管道
建议在摄取文档之前测试您的管道。
要测试管道,请运行以下查询
POST _ingest/pipeline/convert-price/_simulate
{
"docs": [
{
"_index": "testindex1",
"_id": "1",
"_source": {
"price": "-10.5"
}
}
]
}
响应
以下示例响应确认管道按预期工作
{
"docs": [
{
"doc": {
"_index": "testindex1",
"_id": "1",
"_source": {
"price_float": -10.5,
"price": "0"
},
"_ingest": {
"timestamp": "2023-08-22T15:38:21.180688799Z"
}
}
}
]
}
步骤 3:摄取文档
以下查询将文档摄取到名为 testindex1
的索引中
PUT testindex1/_doc/1?pipeline=convert-price
{
"price": "10.5"
}
步骤 4(可选):检索文档
要检索文档,请运行以下查询
GET testindex1/_doc/1