Elasticsearch
Ingest Pipeline 与 Painless Script
需求:修复与增强写⼊的数据

Ingest Node
- Elasticsearch 5.0 后,引⼊的⼀种新的节点类型。默认配置下,每个节点都是 Ingest Node
- 具有预处理数据的能⼒,可拦截 Index 或 Bulk API 的请求
- 对数据进⾏转换,并重新返回给 Index 或 Bulk API
- ⽆需 Logstash,就可以进⾏数据的预处理,例如
- 为某个字段设置默认值;重命名某个字段的字段名;对字段值进⾏ Split 操作
- ⽀持设置 Painless 脚本,对数据进⾏更加复杂的加⼯
Pipeline & Processor
- Pipeline - 管道会对通过的数据(⽂档),按照顺序进⾏加⼯
- Processor - Elasticsearch 对⼀些加⼯的⾏为进⾏了抽象包装
- Elasticsearch 有很多内置的 Processors。也⽀持通过插件的⽅式,实现⾃⼰的 Processor

使⽤ Pipeline 切分字符串

为⽂档增加字段

Pipeline API

添加 Pipeline 并测试

Index & Update By Query

⼀些内置 Processors
- https://www.elastic.co/guide/en/elasticsearch/reference/7.1/ingest-processors.html
- Split Processor (例:将给定字段值分成⼀个数组)
- Remove / Rename Processor (例:移除⼀个重命名字段)
- Append (例:为商品增加⼀个新的标签)
- Convert(例:将商品价格,从字符串转换成 float 类型)
- Date / JSON(例:⽇期格式转换,字符串转 JSON 对象)
- Date Index Name Processor (例:将通过该处理器的⽂档,分配到指定时间格式的索引中)
- Fail Processor (⼀旦出现异常,该 Pipeline 指定的错误信息能返回给⽤户)
- Foreach Process(数组字段,数组的每个元素都会使⽤到⼀个相同的处理器)
- Grok Processor(⽇志的⽇期格式切割)
- Gsub / Join / Split(字符串替换 / 数组转字符串/ 字符串转数组)
- Lowercase / Upcase(⼤⼩写转换)
Ingest Node v.s Logstash
| Logstash | Ingest Node |
---|
数据输⼊与输出 | ⽀持从不同的数据源读取,并写⼊不同的数据源 | ⽀持从 ES REST API 获取数据,并且写⼊ Elasticsearch |
数据缓冲 | 实现了简单的数据队列,⽀持重写 | 不⽀持缓冲 |
数据处理 | ⽀持⼤量的插件,也⽀持定制开发 | 内置的插件,可以开发 Plugin 进⾏扩展(Plugin 更新需要重启) |
配置和使⽤ | 增加了⼀定的架构复杂度 | ⽆需额外部署 |
https://www.elastic.co/cn/blog/should-i-use-logstash-or-elasticsearch-ingest-nodes
Painless 简介
- ⾃ Elasticsearch 5.x 后引⼊,专⻔为 Elasticsearch 设计,扩展了 Java 的语法。
- 6.0 开始,ES 只⽀持 Painless。Groovy, JavaScript 和 Python 都不再⽀持
- Painless ⽀持所有 Java 的数据类型及 Java API ⼦集
- Painless Script 具备以下特性
Painless 的⽤途
- 可以对⽂档字段进⾏加⼯处理
- 更新或删除字段,处理数据聚合操作
- Script Field:对返回的字段提前进⾏计算
- Function Score:对⽂档的算分进⾏处理
- 在 Ingest Pipeline 中执⾏脚本
- 在 Reindex API,Update By Query 时,对数据进⾏处理
通过 Painless 脚本访问字段
上下⽂ | 语法 |
---|
Ingestion | ctx.field_name |
Update | ctx._source.field_name |
Search & Aggregation | doc[“field_name”] |
案例 1:Script Processor

案例 2:⽂档更新计数

案例 3:搜索时的 Script 字段

Script: Inline v.s Stored

脚本缓存

本节知识点
- 概念讲解:Ingest Node,Pipeline 与 Processor
- Ingest Node 与 Logstash 的⽐较
- Pipeline 的 相关操作 / 内置 Processor 讲解与演示
- Painless 脚本与
- Ingestion (Pipeline)
- Update
- Search & Aggregation
demoAPI
DELETE tech_blogs
PUT tech_blogs/_doc/1
{
"title":"Introducing big data......",
"tags":"hadoop,elasticsearch,spark",
"content":"You konw, for big data"
}
POST _ingest/pipeline/_simulate
{
"pipeline": {
"description": "to split blog tags",
"processors": [
{
"split": {
"field": "tags",
"separator": ","
}
}
]
},
"docs": [
{
"_index": "index",
"_id": "id",
"_source": {
"title": "Introducing big data......",
"tags": "hadoop,elasticsearch,spark",
"content": "You konw, for big data"
}
},
{
"_index": "index",
"_id": "idxx",
"_source": {
"title": "Introducing cloud computering",
"tags": "openstack,k8s",
"content": "You konw, for cloud"
}
}
]
}
POST _ingest/pipeline/_simulate
{
"pipeline": {
"description": "to split blog tags",
"processors": [
{
"split": {
"field": "tags",
"separator": ","
}
},
{
"set":{
"field": "views",
"value": 0
}
}
]
},
"docs": [
{
"_index":"index",
"_id":"id",
"_source":{
"title":"Introducing big data......",
"tags":"hadoop,elasticsearch,spark",
"content":"You konw, for big data"
}
},
{
"_index":"index",
"_id":"idxx",
"_source":{
"title":"Introducing cloud computering",
"tags":"openstack,k8s",
"content":"You konw, for cloud"
}
}
]
}
PUT _ingest/pipeline/blog_pipeline
{
"description": "a blog pipeline",
"processors": [
{
"split": {
"field": "tags",
"separator": ","
}
},
{
"set":{
"field": "views",
"value": 0
}
}
]
}
GET _ingest/pipeline/blog_pipeline
POST _ingest/pipeline/blog_pipeline/_simulate
{
"docs": [
{
"_source": {
"title": "Introducing cloud computering",
"tags": "openstack,k8s",
"content": "You konw, for cloud"
}
}
]
}
PUT tech_blogs/_doc/1
{
"title":"Introducing big data......",
"tags":"hadoop,elasticsearch,spark",
"content":"You konw, for big data"
}
PUT tech_blogs/_doc/2?pipeline=blog_pipeline
{
"title": "Introducing cloud computering",
"tags": "openstack,k8s",
"content": "You konw, for cloud"
}
POST tech_blogs/_search
{}
POST tech_blogs/_update_by_query?pipeline=blog_pipeline
{
}
POST tech_blogs/_update_by_query?pipeline=blog_pipeline
{
"query": {
"bool": {
"must_not": {
"exists": {
"field": "views"
}
}
}
}
}
POST _ingest/pipeline/_simulate
{
"pipeline": {
"description": "to split blog tags",
"processors": [
{
"split": {
"field": "tags",
"separator": ","
}
},
{
"script": {
"source": """
if(ctx.containsKey("content")){
ctx.content_length = ctx.content.length();
}else{
ctx.content_length=0;
}
"""
}
},
{
"set":{
"field": "views",
"value": 0
}
}
]
},
"docs": [
{
"_index":"index",
"_id":"id",
"_source":{
"title":"Introducing big data......",
"tags":"hadoop,elasticsearch,spark",
"content":"You konw, for big data"
}
},
{
"_index":"index",
"_id":"idxx",
"_source":{
"title":"Introducing cloud computering",
"tags":"openstack,k8s",
"content":"You konw, for cloud"
}
}
]
}
DELETE tech_blogs
PUT tech_blogs/_doc/1
{
"title":"Introducing big data......",
"tags":"hadoop,elasticsearch,spark",
"content":"You konw, for big data",
"views":0
}
POST tech_blogs/_update/1
{
"script": {
"source": "ctx._source.views += params.new_views",
"params": {
"new_views":100
}
}
}
POST tech_blogs/_search
{
}
POST _scripts/update_views
{
"script":{
"lang": "painless",
"source": "ctx._source.views += params.new_views"
}
}
POST tech_blogs/_update/1
{
"script": {
"id": "update_views",
"params": {
"new_views":1000
}
}
}
GET tech_blogs/_search
{
"script_fields": {
"rnd_views": {
"script": {
"lang": "painless",
"source": """
java.util.Random rnd = new Random();
doc['views'].value+rnd.nextInt(1000);
"""
}
}
},
"query": {
"match_all": {}
}
}