当前位置: 首页 > 后端技术 > Java

使用painless的elasticsearch的一些简单例子

时间:2023-04-01 21:13:42 Java

1。背景本文档只是简单记录一些painless的简单例子,以防日后遗忘,但涉及到更多painless的语法。2、准备数据2.1mappingPUT/index_person{"mappings":{"properties":{"name":{"type":"keyword"},"age":{"type":"integer"},"province":{"type":"keyword"}}}}2.2插入数据PUT/index_person/_bulk{"index":{"_id":1}}{"name":"张三","age":20,"province":"湖北"}{"index":{"_id":2}}{"name":"李四","age":25,"province":"北京"}{"index":{"_id":3}}{"name":"WangWu","age":30,"province":"Hunan"}3、例3.1(更新)更新文档id=1的文档,添加age2岁POSTindex_person/_update/1{"script":{"lang":"painless","source":"""ctx['_source']['age']+=params['incrAge']""","params":{"incrAge":2}}}3.2(update_by_query)如果省份是北京,年龄减1POSTindex_person/_update_by_query{"query":{"term":{"province":{"value":"北京"}}},"script":{"lang":"painless","source":"""ctx['_source']['age']-=params['decrAge']""","params":{"decrAge":1}}}3.3(ctx.op)如果张三年龄小于20岁,则不处理,否则删除该文件POSTindex_person/_update/1{"script":{"lang":"painless","source":"""//这是默认值,表示更新值,重新索引记录ctx.op='index';if(ctx._source.age<20){//表示不处理ctx.op='none';}else{//表示删除这个文档ctx.op='delete';}"""}}3.4(存储脚本)如果是湖南省,增加城市字段,值为长沙3.4.1创建存储脚本PUT_scripts/add_city{"script":{"lang":"painless","source":"ctx._source.city=params.city"}}add_city是脚本的id3.4.2使用存储脚本POSTindex_person/_update_by_query{"query":{"term":{"province":{"value":"Hunan"}}},"script":{"id":"add_city","params":{"city":"Changsha"}}}3.5(pipeline)如果通过pipeline插入的文档age<10,则放入放入index_person_small索引3.5.1创建pipelinePUT_ingest/pipeline/pipeline_index_person_small{"description":"如果插入的文档age<10,放入index_person_small索引","processors":[{"script":{"source":"""if(ctx.age<10){ctx._index='index_person_small';}"""}}]}3.5.2使用pipelinePUTindex_person/_doc/4?pipeline=pipeline_index_person_small{"name":"ZhaoLiu","age":8,"province":"Sichuan"}3.5.33.6function_score中的运行结果使用script_score计算分值3.6.1要求如果用户是湖南人,使用年龄作为分值3.6.2dslGETindex_person/_search{"query":{"function_score":{"query":{"match_all":{}},"functions":[{"script_score":{"script":"""if(doc.province.value==0){return0;}if(doc.province.value=='湖南'){returndoc.age.value;}return0;"""}}],"boost_mode":"sum","score_mode":"sum"}}}3.6.3运算结果3.7script_fields中添加字段GETdex_person/_search{"query":{"match_all":{}},"fields":["double_age"],"script_fields":{"double_age":{"script":{"lang":"painless","source":"doc.age.value*2"}}}}3.8runtimefield添加字段3.8.1要求对于age<25的文档,返回double_age字段,否则不处理3.8.2dslGETindex_person/_search{"query":{"match_all":{}},"fields":["double_age"],"runtime_mappings":{"double_age":{"type":"keyword","script":"""if(doc.age.size()==0){return;}if(doc.age.value<25){emit(doc.age.value*2+'');}"""}}}runtime字段需要使用emit返回数据,不能使用emit(null)3.9.1dslPOST_reindexin3.9_reindex{"source":{"index":"index_person"},"dest":{"index":"index_person_new"},"script":{"lang":"painless","source":"""if(ctx._source.age<25){ctx._source.tag='youngPeople';}else{ctx._source.tag='middle-aged';}"""}}3.9.2运行结果3.10脚本查询queryage<25GETindex_person/_search{"query":{"script":{"script":{"lang":"painless","source":"""if(doc.age.size()==0){returnfalse;}returndoc.age.value<25;"""}}}}3.11脚本聚合GETindex_person/_search{"size":0,"aggs":{"agg_province":{"terms":{"script":{"lang":"painless","source":"""returndoc.province"""},"size":10}},"agg_age":{"avg":{"script":"params._source.age"}}}}4.无痛脚本调试可以通过Debug.explain做一些简单的调试5.脚本中的doc[..]和params._source[..]doc[..]:使用doc关键字将导致该字段的项被加载到内存中(缓存),这将导致更快的执行,但更多的内存消耗。此外,doc[…]符号只允许简单的值字段(你不能从中返回JSON对象),并且只对非分析或基于单项的字段有意义。但是,如果可能,使用doc仍然是访问记录值的推荐方法。params_source:每次使用_source都要加载解析,所以使用_source会比较慢。![doc[..]andparams._source[..]]inthescript(https://img-blog.csdnimg.cn/1...)6.无痛脚本中context详情请请参阅此文档https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-contexts.html