Ingest Pipeline 数据预处理
本章定位:数据写入 ES 之前往往需要清理和转换。Ingest Pipeline 让你在写入前完成这些操作,避免客户端或中间层的额外处理。
定义与作用
Ingest Pipeline 是 Elasticsearch 的写入预处理管道,在文档索引之前执行一系列 Processor(处理器),如 set(设置字段)、remove(删除字段)、grok(正则提取)、script(脚本处理)等。
完整示例:飞翔科技的数据清洗
场景一:添加时间戳和清理字段
PUT /_ingest/pipeline/student_pipeline
{
"processors": [
{ "set": { "field": "indexed_at", "value": "{{_ingest.timestamp}}" } },
{ "remove": { "field": "temp_field" } },
{ "script": { "source": "ctx.score = Math.min(ctx.score, 100)" } }
]
}
PUT /students/_doc/1?pipeline=student_pipeline
{ "name": "黄小俪", "province": "广东", "score": 150 }
# → indexed_at 自动添加,score 被截断为 100
小结
Pipeline 的核心优势:低耦合——数据清洗逻辑集中在 ES 侧,客户端无需关心。常用 Processor:set、remove、grok、script、date。