Painless scripting
Painless scripting 是一种简单的、安全的针对ES设计的脚本语言,Painless 可以使用在任何可以使用scripting的场景,脚本提供了以下优点:
- 更高的性能,scripting 脚本比其他的可选脚本快数倍。
- 安全性高,更小颗粒度的字段授权机制,避免可能不必要的安全隐患安全。
- 可选类型,变量和参数可以使用显示或者动态类型编程方式
- 语法,扩展java的语法并兼容了其他脚本
- 优化,专为ES设计的脚本语言
常用关键字:
if、else、while、do、for、in,continue,break,return,
new、try、catch、throw、this、instanceof
常用举例
#添加测试数据
POST my_goods/_bulk
{"index":{"_id":1}}
{"goodsName":"苹果 51英寸 4K超高清","skuCode":"skuCode1","brandName":"苹果","closeUserCode":["0"],"channelType":"cloudPlatform","shopCode":"sc00001","publicPrice":8188.88,"groupPrice":null,"boxPrice":null,"boostValue":1.8}
{"index":{"_id":2}}
{"goodsName":"苹果 55英寸 3K超高清","skuCode":"skuCode2","brandName":"苹果","closeUserCode":["0"],"channelType":"cloudPlatform","shopCode":"sc00002","publicPrice":6188.88,"groupPrice":null,"boxPrice":null,"boostValue":1.0}
{"index":{"_id":3}}
{"goodsName":"苹果UA55RU7520JXXZ 53英寸 4K高清","skuCode":"skuCode3","brandName":"美国苹果","closeUserCode":["0"],"channelType":"cloudPlatform","shopCode":"sc00001","publicPrice":8388.88,"groupPrice":null,"boxPrice":[{"boxType":"box1","boxUserCode":["htd003","uc004"],"boxPriceDetail":4388.88},{"boxType":"box2","boxUserCode":["uc005","uc0010"],"boxPriceDetail":5388.88}],"boostValue":1.2}
{"index":{"_id":4}}
{"goodsName":"山东苹果UA55RU7520JXXZ 苹果54英寸 5K超高清","skuCode":"skuCode4","brandName":"山东苹果","closeUserCode":["uc001","uc002","uc003"],"channelType":"cloudPlatform","shopCode":"sc00001","publicPrice":8488.88,"groupPrice":[{"level":"level1","boxLevelPrice":"2488.88"},{"level":"level2","boxLevelPrice":"3488.88"}],"boxPrice":[{"boxType":"box1","boxUserCode":["uc004","uc005","uc006","uc001"],"boxPriceDetail":4488.88},{"boxType":"box2","boxUserCode":["htd007","htd008","htd009","uc0010"],"boxPriceDetail":5488.88}],"boostValue":1.2}
添加字段
POST my_goods/_update_by_query
{
"script" : "ctx._source.new_brandName = ctx._source.brandName + '新品'"
}
#查询结果
GET my_goods/_search
#返回(省略部分无关字段)
"hits" : [
{
"_index" : "my_goods",
"_source" : {
"shopCode" : "sc00001",
"new_brandName" : "苹果新品",
"brandName" : "苹果",
"closeUserCode" : [
"0"
]
}
},
{
"_index" : "my_goods",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"shopCode" : "sc00002",
"new_brandName" : "苹果新品",
"brandName" : "苹果",
"closeUserCode" : [
"0"
],
"groupPrice" : null,
"boxPrice" : null,
"channelType" : "cloudPlatform",
"boostValue" : 1.0,
"publicPrice" : "6188.88",
"goodsName" : "苹果 55英寸 3K超高清",
"skuCode" : "skuCode2"
}
},
....
]
#可以看到使用脚本新增的字段new_brandName已经生效
删除字段
POST my_goods/_update_by_query
{
"script":"ctx._source.remove('new_brandName')"
}
更改字段值
#将ID为1的商品的价格提高2倍
POST my_goods/_update/1
{
"script": {
"source": "ctx._source.publicPrice = ctx._source.publicPrice * params.promote_percent",
"lang": "painless",
"params": {
"promote_percent": 2
}
}
}
#查询
GET my_goods/_doc/1
#返回
{
"_index" : "my_goods",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"_seq_no" : 4,
"_primary_term" : 1,
"found" : true,
"_source" : {
"goodsName" : "苹果 51英寸 4K超高清",
"skuCode" : "skuCode1",
"brandName" : "苹果",
"closeUserCode" : [
"0"
],
"channelType" : "cloudPlatform",
"shopCode" : "sc00001",
"publicPrice" : 16377.76,
"groupPrice" : null,
"boxPrice" : null,
"boostValue" : 1.8
}
}
#可以看到,在更新前价格为“8188.88”,通过脚本更新后价格变为16377.76
排序
#修改goodsName可以被doc访问
PUT my_goods/_mapping
{
"properties": {
"goodsName":{
"type":"text",
"fielddata": "true"
}
}
}
#查询并排序,根据商品名称长度并添加干扰因子1.1倍为最终排序结果
POST my_goods/_search
{
"query": {
"match": {
"brandName": "苹果"
}
},
"sort": {
"_script": {
"type": "number",
"script": {
"lang": "painless",
"source": "doc['goodsName'].value.length() * params.factor",
"params": {
"factor": 1.1
}
},
"order": "asc"
}
}
}
网友评论