Link Search Menu Expand Document Documentation Menu

Split 处理器

split 处理器用于根据指定的分隔符将字符串字段拆分为子字符串数组。

以下是 split 处理器的语法

{
  "split": {
    "field": "field_to_split",
    "separator": "<delimiter>",
    "target_field": "split_field"
  }
}

配置参数

下表列出了 split 处理器的必填和可选参数。

参数 必需/可选 描述
field 必需 包含要拆分字符串的字段。
separator 必需 用于拆分字符串的分隔符。这可以是一个正则表达式模式。
preserve_trailing 可选 如果设置为 true,则保留结果数组中的空尾随字段(例如 '')。如果设置为 false,则从结果数组中删除空尾随字段。默认为 false
target_field 可选 存储子字符串数组的字段。如果未指定,则字段会原地更新。
ignore_missing 可选 指定处理器是否应忽略不包含指定字段的文档。如果设置为 true,则处理器会忽略字段中缺失的值,并保持 target_field 不变。默认为 false
description 可选 处理器的简要描述。
if 可选 运行处理器的条件。
ignore_failure 可选 指定处理器即使遇到错误是否仍继续执行。如果设置为 true,则忽略失败。默认为 false
on_failure 可选 处理器失败时要运行的处理器列表。
tag 可选 处理器的标识符标签。有助于调试以区分相同类型的处理器。

使用处理器

按照以下步骤在管道中使用处理器。

步骤 1:创建管道

以下查询创建一个名为 split_pipeline 的管道,该管道使用 split 处理器根据逗号字符拆分 log_message 字段,并将结果数组存储在 log_parts 字段中

PUT _ingest/pipeline/split_pipeline
{
  "description": "Split log messages by comma",
  "processors": [
    {
      "split": {
        "field": "log_message",
        "separator": ",",
        "target_field": "log_parts"
      }
    }
  ]
}

步骤 2(可选):测试管道

建议在摄取文档之前测试您的管道。

要测试管道,请运行以下查询

POST _ingest/pipeline/split_pipeline/_simulate
{
  "docs": [
    {
      "_source": {
        "log_message": "error,warning,info"
      }
    }
  ]
}

响应

以下示例响应确认管道按预期工作

{
  "docs": [
    {
      "doc": {
        "_index": "_index",
        "_id": "_id",
        "_source": {
          "log_message": "error,warning,info",
          "log_parts": [
            "error",
            "warning",
            "info"
          ]
        },
        "_ingest": {
          "timestamp": "2024-04-26T22:29:23.207849376Z"
        }
      }
    }
  ]
}

步骤 3:摄取文档

以下查询将文档摄取到名为 testindex1 的索引中

PUT testindex1/_doc/1?pipeline=split_pipeline
{
  "log_message": "error,warning,info"
}

响应

该请求将文档索引到 testindex1 索引中,并在索引前根据逗号分隔符拆分 log_message 字段,如下面的响应所示

{
  "_index": "testindex1",
  "_id": "1",
  "_version": 70,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 72,
  "_primary_term": 47
}

步骤 4(可选):检索文档

要检索文档,请运行以下查询

GET testindex1/_doc/1

响应

响应显示 log_message 字段是一个根据逗号分隔符拆分的值数组

{
  "_index": "testindex1",
  "_id": "1",
  "_version": 70,
  "_seq_no": 72,
  "_primary_term": 47,
  "found": true,
  "_source": {
    "log_message": "error,warning,info",
    "log_parts": [
      "error",
      "warning",
      "info"
    ]
  }
}
剩余 350 字符

有问题?

想要贡献?