《Elastic Stack 实战手册》——三、产品能力——3.4.入门篇——3.4.2.Elasticsearch基础应用——3.4.2.16.Painless scripting(上) https://developer.aliyun.com/article/1230167
ingest node
在ingest pipeline中更新字段值:
#定义 pipeline PUT _ingest/pipeline/add_my_goods_newField { "processors": [ { "script": { "lang": "painless", "source": "ctx.skuCode_brandName = ctx.skuCode + ctx.brandName" } } ] } #执行 pipeline POST my_goods/_update_by_query?pipeline=add_my_goods_newField #查询结果 GET my_goods/_search #返回 "hits" : [ { "_index" : "my_goods", "_type" : "_doc", "_id" : "2", "_score" : 1.0, "_source" : { "shopCode" : "sc00002", "brandName" : "苹果", "closeUserCode" : [ "0" ], "skuCode_brandName" : "skuCode2苹果", "channelType" : "cloudPlatform", "publicPrice" : 12377.76, "goodsName_length" : 13, "groupPrice" : null, "boxPrice" : null, "boostValue" : 1.0, "goodsName" : "苹果 55英寸 3K超高清", "skuCode" : "skuCode2" } }, { "_index" : "my_goods", "_type" : "_doc", "_id" : "3", "_score" : 1.0, "_source" : { "shopCode" : "sc00001", "brandName" : "美国苹果", "closeUserCode" : [ "0" ], "skuCode_brandName" : "skuCode3美国苹果", "channelType" : "cloudPlatform", "publicPrice" : 16777.76, "goodsName_length" : 26, "groupPrice" : null, "boxPrice" : [ { "boxType" : "box1", "boxUserCode" : [ "htd003", "uc004" ], "boxPriceDetail" : 4388.88 }, { "boxType" : "box2", "boxUserCode" : [ "uc005", "uc0010" ], "boxPriceDetail" : 5388.88 } ], "boostValue" : 1.2, "goodsName" : "苹果UA55RU7520JXXZ 53英寸 4K高清", "skuCode" : "skuCode3" } }, .... ]
可以看到,skuCode_brandName 是通过 skuCode+brandName 拼接成功的,通过 ctx.field 访问字段成功。
Painless Debug
Elasticsearch 中为我们提供了脚本调试方法,使我们在使用时可以方便的进行脚本调试。
#定义用户信息,shop_id为用户开的店铺ID信息 PUT /user_info/_doc/1?refresh { "first": "Michael", "last": "Jordan", "shop_id": [ 100, 102, 103 ], "time": "2021-05-09" } PUT /user_info/_doc/2?refresh { "first": "Michael2", "last": "Jordan2", "shop_id": [ 110, 112, 113, 114, 115 ], "time": "2021-05-08" } #查看mapping GET user_info/_mapping #返回 { "user_info" : { "mappings" : { "properties" : { "first" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "last" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "shop_id" : { "type" : "long" }, "time" : { "type" : "date" } } } } }
可以看到返回了很多字段类型,包括:long、date、keyword、text,每种类型有哪些方法可以操作呢?一种是查看官网文档,另外一种获取使用的方法就是通过调试来获取信息了,使用
_explain 来看看效果:
POST /user_info/_explain/1 { "query": { "script": { "script": "Debug.explain(doc.shop_id)" } } } #返回: { "error": { "root_cause": [ { "type": "script_exception", "reason": "runtime error", "painless_class": "org.elasticsearch.index.fielddata.ScriptDocValues.Longs", "to_string": "[100, 102, 103]", "java_class": "org.elasticsearch.index.fielddata.ScriptDocValues$Longs", "script_stack": [ "Debug.explain(doc.shop_id)", " ^---- HERE" ], "script": "Debug.explain(doc.shop_id)", "lang": "painless", "position": { "offset": 17, "start": 0, "end": 26 } } ], "type": "script_exception", "reason": "runtime error", "painless_class": "org.elasticsearch.index.fielddata.ScriptDocValues.Longs", "to_string": "[100, 102, 103]", "java_class": "org.elasticsearch.index.fielddata.ScriptDocValues$Longs", "script_stack": [ "Debug.explain(doc.shop_id)", " ^---- HERE" ], "script": "Debug.explain(doc.shop_id)", "lang": "painless", "position": { "offset": 17, "start": 0, "end": 26 }, "caused_by": { "type": "painless_explain_error", "reason": null } }, "status": 400 }
可以看到是一个 runtime error 异常,那我们应该如何解决呢?
仔细观察,doc.shop_id 是这样的类提供支撑:
"painless_class": "org.elasticsearch.index.fielddata.ScriptDocValues.Longs" "java_class": "org.elasticsearch.index.fielddata.ScriptDocValues$Longs"
《Elastic Stack 实战手册》——三、产品能力——3.4.入门篇——3.4.2.Elasticsearch基础应用——3.4.2.16.Painless scripting(下) https://developer.aliyun.com/article/1230165