学网站建设的专业叫什么互联网广告销售
目录
Painless简介
Painless的用途
通过Painless脚本访问字段
案例1:script processor
案例2:查看views计数
案例3:保存脚本在Cluster State
案例4:在search中应用
脚本缓存
Inline scripts VS Stored Scripts
Painless简介
-
自ES5.x之后,专门为ES设计,扩展了Java语法
-
6.0开始,ES只支持PainLess
-
PainLess支持所有Java的数据类型及Java API子集
-
PainLess Script具备一下特性
-
高性能 /安全
-
支持显示类型或者动态定义类型
-
Painless的用途
- 可以对文档字段进行加工处理
- 更新或者删除字段,处理数据聚合操作
- Script Field:对返回的字段提前计算
- Function Score:对文档的算分进行处理
- 在Ingest Pipeline中执行脚本
- 在ReindexAPI,UpdateByQuery时,对数据进行处理
通过Painless脚本访问字段
上下文 | 语法 |
ingestion | ctx.field_name |
Update | ctx.source.field_name |
Search & Aggregation | doc["field_name"] |
案例1:script processor
- 通过simulate指定我们进行pipeline模拟测试,将painless 脚本当成processors中的一个环节,对我们的文档数据进行处理 在source:""" … “”"中定义我们的脚本
- 具体的脚本逻辑:我们的数据的key中包含content 则计算他的长度,否则,他的长度为0
POST _ingest/pipeline/_simulate
{
"pipeline": {
"description": "to spilt 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"
}
}
]
}
案例2:查看views计数
#添加数据
PUT tech_blogs/_doc/1
{
"title":"Introducing big data......",
"tags":"hadoop,elasticsearch,spark",
"content":"You konw, for big data",
"views":0
}#通过script更新文档
POST tech_blogs/_update/1
{
"script": {
"source":"ctx._source.views += params.new_views",
"params": {
"new_views":100
}
}
}#通过script更新文档:根据文档id的_source中是否有views字段更新文档
POST tech_blogs/_update/1
{
"script": {
"source": """
if(ctx._source.containsKey("view")){
ctx._source.views += params.new_views;
}else{
ctx._source.views = 0;
}
""",
"params": {
"new_views":100
}
}
}
案例3:保存脚本在Cluster State
#保存脚本在 Cluster State
POST _scripts/update_views
{
"script": {
"lang": "painless",
"source": "ctx._source.views += params.new_views"
}
}#通过脚本id应用脚本
POST tech_blogs/_update/1
{
"script": {
"id": "update_views",
"params": {
"new_views": 1000
}
}
}
案例4:在search中应用
#在search中更新views的值
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": {}
}
}
脚本缓存
- 编译的开销相较⼤
- Elasticsearch 会将脚本编译后缓存在Cache 中
- Inline scripts 和 Stored Scripts 都会被缓存
- 默认缓存 100 个脚本
Inline scripts VS Stored Scripts
- 在线模式Inline脚本
POST tech_blogs/_update/1
{
"script": {
"source": "ctx._source.views += params.new_views",
"params": {
"new_views": 100
}
}
}
- 将脚本保存在cluster state中:通过id使用 使用params 减少编译的条数
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
}
}
}