正则表达式条件
摄入管道使用正则表达式(regex)和 Painless 脚本语言支持条件逻辑。这允许根据文本字段的结构和内容对要处理的文档进行精细控制。正则表达式可以在 if
参数中使用,以评估字符串模式。这对于匹配 IP 格式、验证电子邮件地址、识别 UUID 或处理带有特定关键字的日志特别有用。
示例:电子邮件域筛选
以下管道使用正则表达式识别来自 @example.com
电子邮件域的用户并相应地标记这些文档。
PUT _ingest/pipeline/tag_example_com_users
{
"processors": [
{
"set": {
"field": "user_domain",
"value": "example.com",
"if": "ctx.email != null && ctx.email =~ /@example.com$/"
}
}
]
}
使用以下请求模拟管道:
POST _ingest/pipeline/tag_example_com_users/_simulate
{
"docs": [
{ "_source": { "email": "alice@example.com" } },
{ "_source": { "email": "bob@another.com" } }
]
}
只有第一个文档添加了 user_domain
。
{
"docs": [
{
"doc": {
"_source": {
"email": "alice@example.com",
"user_domain": "example.com"
}
}
},
{
"doc": {
"_source": {
"email": "bob@another.com"
}
}
}
]
}
示例:检测 IPv6 地址
以下管道使用正则表达式识别并标记 IPv6 格式的地址:
PUT _ingest/pipeline/ipv6_flagger
{
"processors": [
{
"set": {
"field": "ip_type",
"value": "IPv6",
"if": "ctx.ip != null && ctx.ip =~ /^[a-fA-F0-9:]+$/ && ctx.ip.contains(':')"
}
}
]
}
使用以下请求模拟管道:
POST _ingest/pipeline/ipv6_flagger/_simulate
{
"docs": [
{ "_source": { "ip": "2001:0db8:85a3:0000:0000:8a2e:0370:7334" } },
{ "_source": { "ip": "192.168.0.1" } }
]
}
第一个文档包含一个附加的 ip_type
字段,设置为 IPv6
。
{
"docs": [
{
"doc": {
"_source": {
"ip": "2001:0db8:85a3:0000:0000:8a2e:0370:7334",
"ip_type": "IPv6"
}
}
},
{
"doc": {
"_source": {
"ip": "192.168.0.1"
}
}
}
]
}
示例:验证 UUID 字符串
以下管道使用正则表达式验证 session_id
字段是否包含有效的 UUID:
PUT _ingest/pipeline/uuid_checker
{
"processors": [
{
"set": {
"field": "valid_uuid",
"value": true,
"if": "ctx.session_id != null && ctx.session_id =~ /^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/"
}
}
]
}
使用以下请求模拟管道:
POST _ingest/pipeline/uuid_checker/_simulate
{
"docs": [
{ "_source": { "session_id": "550e8400-e29b-41d4-a716-446655440000" } },
{ "_source": { "session_id": "invalid-uuid-1234" } }
]
}
第一个文档被标记了一个新的 valid_uuid
字段。
{
"docs": [
{
"doc": {
"_source": {
"session_id": "550e8400-e29b-41d4-a716-446655440000",
"valid_uuid": true
}
}
},
{
"doc": {
"_source": {
"session_id": "invalid-uuid-1234"
}
}
}
]
}