Scripting脚本语言
一、脚本查询
1、概念:Scripting是es支持的一种专门用于复杂场景下支持自定义编程的强大的脚本功能
2、es5.0之后默认语言是painless
3、
1 #语法:ctx._source.<field-name> ctx可以理解为上下文 2 #将doc为2数据的price属性-1 3 POST product/_update/2 修改属性要使用post请求 4 { 5 "script": { 6 "source": "ctx._source.price-=1" 7 } 8 }
GET product/_doc/2
二、Scripting的CRUD
新增:
1 #将id为6数据的tags属性新增无线充电标签 2 POST product/_update/6 3 { 4 "script": { 5 "lang": "painless", 6 "source": "ctx._source.tags.add('无线充电')" 7 } 8 }
删除:
#将id为11的数据删除 POST product/_update/11 { "script": { "lang": "painless", "source": "ctx.op='delete'" } }
upsert:
1 #upsert 当需要修的数据存在时执行update操作,当数据不存在时执行insert操作 2 POST product/_update/15 3 { 4 "script": { 5 "lang": "painless", 6 "source": "ctx._source.price+=100" 7 }, 8 "upsert": { 9 "name" : "小米手机10", 10 "desc" : "充电贼快掉电更快,超级无敌望远镜,高刷电竞屏", 11 "price" : "1599" 12 } 13 }
select:
1 GET product/_search 2 { 3 "script_fields": { 4 "my_price": { 5 "script": { 6 "lang": "painless", 7 "source": "doc['price']*0.9" 8 } 9 } 10 } 11 }
三、函数式编程
1 POST product/_update/1 2 { 3 "script": { 4 "lang": "painless", 5 "source": """ """xxx"""相当于java中的{} 6 ctx._source.tags.add(params.tag_name); 7 ctx._source.price-=100 8 """, 9 "params": { 10 "tag_name": "无线充电" 11 } 12 } 13 }
#统计所有价格小于1000的商品的tag的数量 不考虑重复的情况
1 GET product/_search 2 { 3 "query": { 4 "constant_score": { 5 "filter": { 6 "range": { 7 "price": { 8 "lt": 1000 9 } 10 } 11 } 12 } 13 }, 14 "aggs": { 15 "tag_agg": { 16 "sum": { 17 "script": { 18 "source": """ 19 int total = 0; 20 for(int i=0;i<doc['tags.keyword'].length;i++){ 21 total++; 22 } 23 return total; 24 """ 25 } 26 } 27 } 28 } 29 }
复杂案例:
1 数据 2 PUT test_index/_bulk?refresh 3 {"index":{"_id":1}} 4 {"ajbh": "12345","ajmc": "立案案件","lasj": "2020/05/21 13:25:23","jsbax_sjjh2_xz_ryjbxx_cleaning": [{"XM": "张三","NL": "30","SF": "男"},{"XM": "李四","NL": "31","SF": "男"},{"XM": "王五","NL": "30","SF": "女"},{"XM": "赵六","NL": 23,"SF": "男"}]} 5 {"index":{"_id":2}} 6 {"ajbh": "563245","ajmc": "结案案件","lasj": "2020/05/21 13:25:23","jsbax_sjjh2_xz_ryjbxx_cleaning": [{"XM": "张三2","NL": "30","SF": "男"},{"XM": "李四2","NL": "31","SF": "男"},{"XM": "王五2","NL": "30","SF": "女"},{"XM": "赵六2","NL": "23","SF": "女"}]} 7 {"index":{"_id":3}} 8 {"ajbh": "12345","ajmc": "立案案件","lasj": "2020/05/21 13:25:23","jsbax_sjjh2_xz_ryjbxx_cleaning": [{"XM": "张三3","NL": "30","SF": "男"},{"XM": "李四3","NL": "31","SF": "男"},{"XM": "王五3","NL": "30","SF": "女"},{"XM": "赵六3","NL": "23","SF": "男"}]}
统计男性嫌疑人的数量
1 GET test_index/_search 2 { 3 "size": 0, 4 "aggs": { 5 "sum_persion": { 6 "sum": { 7 "script": { 8 "source": """ 9 int total = 0; 10 for(int i=0;i<params['_source']['jsbax_sjjh2_xz_ryjbxx_cleaning'].length;i++){ 11 if(params['_source']['jsbax_sjjh2_xz_ryjbxx_cleaning'][i]['SF']=="男"){ 12 total+=1; 13 } 14 } 15 return total; 16 """ 17 } 18 } 19 } 20 } 21 }
注意:doc['field'].value和params['_source']['field']的区别
doc['field'].value只能用于简单类型不适用于复杂类型,如nested
而params['_source']['field']适用于复杂类型
作者:http://cnblogs.com/lyc-code/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权力。