#########Demo for Painless###############
POST _ingest/pipeline/_simulate
{
"pipeline": {
"description": "to s[lit 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/_doc/1
GET tech_blogs/_search
{
"script_fields": {
"random_fields": {
"script": {
"lang": "painless",
"source": """
java.util.Random rnd = new Random();
doc['views'].value + rnd.nextInt(1000);
"""
}
}
},
"query": {
"match_all": {}
}
}
POST tech_blogs/_update/1
{
"script": {
"source": "ctx._source.views += params.new_views",
"params": {
"new_views": 100
}
}
}
### 将脚本保存在cluster state中
POST _scripts/update_views
{
"script": {
"lang": "painless",
"source": "ctx._source.views += params.new_views"
}
}
### 通过id使用 使用params 减少编译的条数
POST tech_blogs/_update/1
{
"script": {
"id": "update_views",
"params": {
"new_views": 1000
}
}
}
###● 编译的开销相较⼤
###● Elasticsearch 会将脚本编译后缓存在Cache 中
###● Inline scripts 和 Stored Scripts 都会被缓存
###● 默认缓存 100 个脚本