Link Search Menu Expand Document Documentation Menu

条件执行

在摄入管道中,您可以使用可选的 if 参数控制处理器是否运行。这允许根据传入文档的内容对处理器进行条件执行。条件以 Painless 脚本的形式编写,并针对文档上下文 (ctx) 进行评估。

基本条件执行

每个处理器都可以包含一个 if 子句。如果条件评估为 true,则处理器运行;否则,它将被跳过。

示例:丢弃调试级别日志

以下管道会丢弃任何 log_level 字段等于 debug 的文档

PUT _ingest/pipeline/drop_debug_logs
{
  "processors": [
    {
      "drop": {
        "if": "ctx.log_level == 'debug'"
      }
    }
  ]
}

示例索引请求

POST logs/_doc/1?pipeline=drop_debug_logs
{
  "message": "User logged in",
  "log_level": "debug"
}

此文档被丢弃,因为条件评估为 true

{
  "_index": "logs",
  "_id": "1",
  "_version": -3,
  "result": "noop",
  "_shards": {
    "total": 0,
    "successful": 0,
    "failed": 0
  }
}

使用嵌套字段时的空值安全字段检查

处理嵌套字段时,避免空指针异常非常重要。在 Painless 脚本中使用空值安全运算符 ?.

示例:根据嵌套字段丢弃文档

以下丢弃处理器仅在嵌套的 app.env 字段存在且等于 debug 时执行

PUT _ingest/pipeline/drop_debug_env
{
  "processors": [
    {
      "drop": {
        "if": "ctx.app?.env == 'debug'"
      }
    }
  ]
}

如果未配置空值安全运算符 ?.,则索引任何不包含 app.env 字段的文档将触发以下空指针异常

{
  "error": "IngestProcessorException[ScriptException[runtime error]; nested: NullPointerException[Cannot invoke \"Object.getClass()\" because \"callArgs[0]\" is null];]",
  "status": 400
}

处理扁平化字段

如果您的文档包含扁平化字段,例如 "app.env": "debug",请使用 dot_expander 处理器将其转换为嵌套结构

PUT _ingest/pipeline/drop_debug_env
{
  "processors": [
    {
      "dot_expander": {
        "field": "app.env"
      }
    },
    {
      "drop": {
        "if": "ctx.app?.env == 'debug'"
      }
    }
  ]
}

条件中的安全方法调用

避免对可能为空的值调用方法。请改用常量或空值检查。

{
  "drop": {
    "if": "ctx.app?.env != null && ctx.app.env.contains('debug')"
  }
}

完整示例:多步条件管道

以下摄入管道使用三个处理器

  1. set: 如果 user 字段中未提供值,则将 user 字段设置为 guest
  2. set: 如果提供了 status_code 并且高于 400,则将 error 字段设置为 true
  3. drop: 如果 app.env 字段等于 debug,则丢弃整个文档。
PUT _ingest/pipeline/logs_processing
{
  "processors": [
    {
      "set": {
        "field": "user",
        "value": "guest",
        "if": "ctx.user == null"
      }
    },
    {
      "set": {
        "field": "error",
        "value": true,
        "if": "ctx.status_code != null && ctx.status_code >= 400"
      }
    },
    {
      "drop": {
        "if": "ctx.app?.env == 'debug'"
      }
    }
  ]
}

模拟管道

以下模拟请求将条件逻辑应用于三个文档

POST _ingest/pipeline/logs_processing/_simulate
{
  "docs": [
    {
      "_source": {
        "message": "Successful login",
        "status_code": 200
      }
    },
    {
      "_source": {
        "message": "Database error",
        "status_code": 500,
        "user": "alice"
      }
    },
    {
      "_source": {
        "message": "Debug mode trace",
        "app": { "env": "debug" }
      }
    }
  ]
}

响应演示了处理器如何根据每个条件进行响应

{
  "docs": [
    {
      "doc": {
        "_index": "_index",
        "_id": "_id",
        "_source": {
          "status_code": 200,
          "message": "Successful login",
          "user": "guest"
        },
        "_ingest": {
          "timestamp": "2025-04-16T14:04:35.923159885Z"
        }
      }
    },
    {
      "doc": {
        "_index": "_index",
        "_id": "_id",
        "_source": {
          "status_code": 500,
          "message": "Database error",
          "error": true,
          "user": "alice"
        },
        "_ingest": {
          "timestamp": "2025-04-16T14:04:35.923198551Z"
        }
      }
    },
    null
  ]
}

相关文章