失败处理器
fail
处理器对于在索引过程中执行数据转换和丰富非常有用。fail
处理器的主要用例是在满足特定条件时使索引操作失败。
以下是 fail
处理器的语法
"fail": {
"if": "ctx.foo == 'bar'",
"message": "Custom error message"
}
配置参数
下表列出了 fail
处理器所需和可选的参数。
参数 | 必需/可选 | 描述 |
---|---|---|
message | 必需 | 要在失败响应中包含的自定义错误消息。 |
description | 可选 | 处理器的简要描述。 |
if | 可选 | 运行处理器的条件。 |
ignore_failure | 可选 | 指定处理器在遇到错误时是否继续执行。如果设置为 true ,则忽略失败。默认为 false 。 |
on_failure | 可选 | 处理器失败时要运行的处理器列表。 |
tag | 可选 | 处理器的标识符标签。有助于调试以区分相同类型的处理器。 |
使用处理器
按照以下步骤在管道中使用处理器。
步骤 1:创建管道
以下查询创建了一个名为 fail-log-pipeline
的管道,该管道使用 fail
处理器有意使日志事件的管道执行失败
PUT _ingest/pipeline/fail-log-pipeline
{
"description": "A pipeline to test the fail processor for log events",
"processors": [
{
"fail": {
"if": "ctx.user_info.contains('password') || ctx.user_info.contains('credit card')",
"message": "Document containing personally identifiable information (PII) cannot be indexed!"
}
}
]
}
步骤 2(可选):测试管道
建议在摄取文档之前测试您的管道。
要测试管道,请运行以下查询
POST _ingest/pipeline/fail-log-pipeline/_simulate
{
"docs": [
{
"_source": {
"user_info": "Sensitive information including credit card"
}
}
]
}
响应
以下示例响应确认管道按预期工作
{
"docs": [
{
"error": {
"root_cause": [
{
"type": "fail_processor_exception",
"reason": "Document containing personally identifiable information (PII) cannot be indexed!"
}
],
"type": "fail_processor_exception",
"reason": "Document containing personally identifiable information (PII) cannot be indexed!"
}
}
]
}
步骤 3:摄取文档
以下查询将文档摄取到名为 testindex1
的索引中
PUT testindex1/_doc/1?pipeline=fail-log-pipeline
{
"user_info": "Sensitive information including credit card"
}
响应
由于字符串 credit card
存在于 user_info
中,请求无法将日志事件索引到索引 testindex1
中。以下响应包含在 fail 处理器中指定的自定义错误消息
"error": {
"root_cause": [
{
"type": "fail_processor_exception",
"reason": "Document containing personally identifiable information (PII) cannot be indexed!"
}
],
"type": "fail_processor_exception",
"reason": "Document containing personally identifiable information (PII) cannot be indexed!"
},
"status": 500
}
步骤 4(可选):检索文档
由于管道失败导致日志事件未被索引,尝试检索它会导致未找到文档错误 "found": false
GET testindex1/_doc/1
文档错误示例
{
"_index": "testindex1",
"_id": "1",
"found": false
}