1 概述
1.1 背景
5.x后引入,专门为elasticsearch设计,扩展了Java的语法。
6.0开始,ES只支持Painless。Groovy,JavaScript和python都不再支持
Painless支持所有Java的数据类型及Java API子集
Painless Script具备以下特性
高性能/安全
支持显示类型或者动态定义类型
1.2 用途
可以对文档字段进行加工处理
更新或删除字段,处理数据聚合操作
Script Field:对返回的字段提前进行计算
Function Score:对文档的算分进行处理
在Ingest Pipeline中执行脚本
在Reindex API,Update By Query时,对数据进行处理
2 语法及配置
2.1 语法
上下文 | 语法 |
---|---|
Ingestion | ctx.field_name |
Update | ctx._source.field_name |
Search & Aggregation | doc["field_name"] |
2.2 脚本缓存相关配置参数
参数 | 说明 |
---|---|
script.cache.max_size | 设置最大缓存数 |
script.cache.expire | 设置缓存超时 |
script.max_compilations_rate | 默认5分钟最多75次编译(75/5m) |
3 案例
3.1 增加painless用于统计content字段长度
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"
}
}
]
}
#使用simulate api模拟pipeline
#输出结果中添加两个字段contyent_length(统计字段长度)和views(赋默认值0)复制
3.2 文档更新时操作
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
}
}
}
#更新文档,没刷新一次views的值增加100
#通过ctx._source访问数据
GET tech_blogs/_doc/1
#获取结果验证复制
3.3 搜索时操作
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": {
}
}
}
#查询时输出rnd_views,该字段是[views,views+1000)范围内的随机数
#通过doc['views'].value访问数据复制
3.4 创建脚本和调用脚本分开
# 查看views计数
POST tech_blogs/_search
{
}
#保存脚本在 Cluster State
#创建脚本
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
}
}
}复制
文章转载自lin在路上,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。