ES[7.9]script脚本操作

ES[7.9]script脚本操作

2023年7月18日发(作者:)

ES[7.9]script脚本操作ES中的script脚本操作GET product2/_searchPOST product2/_update/4{ "script": { "source": "ctx._ -=10" }}#简写POST product2/_update/4{ "script": "ctx._ -=10;ctx._ += '程飞'"}GET product2/_doc/4利⽤传参的⽅式,这样的效率更⾼,因为当你批量修改的时候,script代码只会编译⼀次POST product2/_update/4{ "script": { "lang": "painless", "source": "ctx._(_name)", "params": {"tag_name":"⽆线充电"} }}delete删除操作GET product2/_doc/5POST product2/_update/5{ "script": { "lang": "painless", "source": "='delete'" }

}upsert如果对应的id有数据,就直接update,如果没有,就⽤upsert的数据进⾏insert的操作GET product2/_doc/4POST product2/_update/4{ "script": { "lang": "painless",

"source": "ctx._ += " , "params": { "par":100 } }, "upsert": { "name":"⼩⽶10", "price":4999 }}正则表达式如果未启⽤ 会报错,我们需要在配置⽂件进⾏如下的配置 d:true正则我们⼀般很少⽤,他的性能不好,⽽且会绕过 painless针对长时间运⾏和占⽤内存的脚本的保护措施,⽽且有深度堆栈⾏为GET product2/_doc/1POST product2/_update/1{ "script": { "lang": "painless", "source": """ if(ctx_ =~ /[sS]*phone[sS]*/){ ctx._ += "***"; }else{ = "noop"; } """ }}[0-9]{4}-[0-9]{2}-[0-9]{2}匹配出来的不⼀定是⽇期 ⽐如 9999-99-[0-9]{4}-[0-9]{2}-[0-9]{2}匹配出来的不⼀定是⽇期 ⽐如 9999-99-99 但是⽇期⼀定能匹配上99 但是⽇期⼀定能匹配上=是模糊匹配,==是完全匹配POST product2/_update/1{ "script": { "lang": "painless", "source": """ if (ctx._time ==~ /[0-9]{4}-[0-9]{2}-[0-9]{2}/) { ctx._ += "|***"; } else { = "noop"; } """ }}改成_bulk批量操作呢,_bulk是不能换⾏符的,不然会报错GET product2/_doc/4POST product2/_bulk{ "update":{ "_id":4, "retry_on_conflict":3 }}{ "script":{ "source":"ctx._ += ", "lang":"painless", "params":{"par":10000} }, "upsert":{ "name":"红⽶旗舰版", "price":15986 }}POST product2/_bulk{"update":{"_id":4,"retry_on_conflict":3}}{"script":{"source":"ctx._ += ","lang":"painless","params":{"par":10000}},"upsert":{"name":"红⽶旗舰版","price":15986}}GET查询 除了painless(默认) ES还⽀持 (了解)expression(快速的⾃定义排名和排序)mustache(范本)java(专家API)这些语⾔应⽤场景更窄,但是可能性能更好GET product2/_search{ "script_fields": { "test_field": { "script": { "lang": "expression", "source": "doc['price']" } } }}_search的script脚本操作,如果source中的字段没有,就会报错GET product2/_searchGET product2/_search{ "script_fields": { "test_bala": { "script": { "lang": "painless",

"source": "doc['price'].value*9" } } }}更换num的值 对⽐took消耗doc['price'] * num只编译⼀次⽽doc['price'] * 9 会随着数字改变⽽⼀直编译,ES默认每分钟⽀持15次编译GET product2/_search{ "script_fields": { "test_field": { "script": { "lang": "expression", "source": "doc['price'] * num", "params": { "num": 6 } } } }}例如 打8折价格GET product2/_search{ "script_fields": { "chengfei": { "script": { "source": "*nt", "params": { "discount":0.8 } } } }}原始价格 和 多个打折价格GET product2/_search{ "script_fields": { "discount": { "script": { "source": "[doc['price'].value * nt_8,doc['price'].value * nt_7]" , "params": { "discount_8":0.8, "discount_7":0.7 } } } }}POST /product/_refreshStored scripts :可以理解为script模板 缓存在集群的cache中Stored scripts :可以理解为script模板 缓存在集群的cache中/_scripts/{id} 类似存储过程 /_scripts/{id} 类似存储过程 计算折扣 作⽤域为整个集群计算折扣 作⽤域为整个集群默认缓存⼤⼩是100MB 没有过期时间 可以⼿⼯设置过期时间默认缓存⼤⼩是100MB 没有过期时间 可以⼿⼯设置过期时间 通过_size设置缓存⼤⼩ 脚本最⼤64MB 通过_size_in_bytes配置 只有发⽣变更时重新编译这⾥设置的discount变量是让使⽤它的程序传⼊的,相当于是存储过程,或是函数POST _scripts/bala1{ "script":{ "lang": "painless",

"source": "doc['price'].value * nt" }}GET _scripts/balaGET product2/_search{ "script_fields": { "bala": { "script": { "id": "bala", "params": { "discount":0.5 } } } }}Dates官⽹Bug: ⽇期字段公开为 ZonedDateTime,因此它们⽀持诸如之类的⽅法getYear,getDayOfWeek 或例如从历元开始到毫秒getMillis。要在脚本中使⽤它们,请省略get前缀并继续使⽤⼩写的⽅法名其余部分。例如,以下代码返回每个冰球运动员的出⽣年份getYear()getMonth()getDayOfMonth()getDayOfWeek()getDayOfYear()getHour()getMinute()getSecond()getNano()当然了前提是你的字段是时间类型是date的⽽且写法⼀定要是GET product2/_searchPUT my_index2{ "mappings": { "properties": { "name":{"type": "keyword"}, "create_time":{"type": "date"} } }}POST /my_index3/_bulk{"index":{"_id":1}}{"name":"chengfei","create_time":"2015-01-01T12:10:30Z"}{"index":{"_id":2}}{"name":"xuxu","create_time":"2015-01-01T18:10:30Z"}GET /my_index3/_mappingGET my_index/_searchGET /my_index3/_search{ "script_fields": { "date_test": { "script": { "source": "_" } } }}下⾯的这种⽇期⽅法也⾏,只要是index插⼊的默认数据类型是date的都⾏,根据官⽹,这种⽇期写法确实也是date类型的,官⽹对于date类型的描述POST /my_index4/_bulk{"index":{"_id":1}}{"name":"chengfei","create_time":"2015/01/01 23:50:30"}{"index":{"_id":2}}{"name":"xuxu","create_time":"2015/01/01 12:10:30"}GET /my_index4/_mappingGET my_index/_searchGET /my_index4/_search{ "script_fields": { "date_test": { "script": { "source": "[_,_eek]" } } }}那么遇到复杂脚本,写很多⾏怎么办呢GET product2/_searchPOST product2/_update/3{ "script": { "lang": "painless", "source": """ ctx._ += ; ctx._ -= """, "params": { "name":"⽆线充电宝", "price":100 } }}聚合查询的使⽤统计所有价格⼩于1000商品tag的 数量 不考虑去重GET product2/_search{ "query": { "bool": { "filter": [ {"range": { "price": { "lt": 1000 } }} ] } }, "aggs": { "script_aggs_test": { "terms": { "field": "d", "size": 10 } } }}GET product/_search{ "query": { "bool": { "filter": [ {"range": { "price": { "lt": 1000 } }} ] } }, "aggs": { "script_aggs_test": { "sum": { "script": { "lang": "painless", "source": """ int total = 0; for (int i = 0 ;i<;i++){ total ++ } return total; """ } } } }}以下为案件的嫌疑⼈信息统计男性嫌疑⼈的数量如果字段是复杂类型,如数组或是object的话,就不能直接⽤doc的⽅式,要⽤params._source的⽅式获取GET test_index/_searchDELETE test_indexPUT test_index/_bulk?refres{"index":{"_id":1}}{"ajbh": "12345","ajmc": "⽴案案件","lasj": "2020/05/21 13:25:23","jsbax_sjjh2_xz_ryjbxx_cleaning": [{"XM": "张三","NL": "30","SF": "男"},{"XM": "李四","NL": "31","SF": "男"},{"XM":{"index":{"_id":2}}{"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": "男"},{"X{"index":{"_id":3}}{"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": "男"},{"XMGET test_index/_search{ "aggs": { "how_much_man": { "sum": { "script": { "lang": "painless", "source": """ int total = 0; for(int i = 0;i

if(params.__sjjh2_xz_ryjbxx_cleaning[i].SF=="男"){ total++; } } return total; """ } } } }}GET test_index/_search{ "aggs": { "how_much_man": { "sum": { "script": { "lang": "painless", "source": """ int total = 0; for(int i = 0;i

发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1689630069a272247.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信