带你读《Elastic Stack 实战手册》之31:——3.4.2.16.Painless scripting(中)

本文涉及的产品
Elasticsearch Serverless通用抵扣包,测试体验金 200元
简介: 带你读《Elastic Stack 实战手册》之31:——3.4.2.16.Painless scripting(中)


《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

相关实践学习
以电商场景为例搭建AI语义搜索应用
本实验旨在通过阿里云Elasticsearch结合阿里云搜索开发工作台AI模型服务,构建一个高效、精准的语义搜索系统,模拟电商场景,深入理解AI搜索技术原理并掌握其实现过程。
ElasticSearch 最新快速入门教程
本课程由千锋教育提供。全文搜索的需求非常大。而开源的解决办法Elasricsearch(Elastic)就是一个非常好的工具。目前是全文搜索引擎的首选。本系列教程由浅入深讲解了在CentOS7系统下如何搭建ElasticSearch,如何使用Kibana实现各种方式的搜索并详细分析了搜索的原理,最后讲解了在Java应用中如何集成ElasticSearch并实现搜索。  
相关文章
|
存储
uniApp监听左右滑动事件
uniApp监听左右滑动事件
944 0
|
存储 jenkins 开发工具
后端版本控制:原理、实践与挑战
后端版本控制:原理、实践与挑战
252 0
|
12月前
|
SQL 前端开发 JavaScript
Web前端开发工程师岗位要求
Web前端开发工程师岗位要求
|
人工智能 机器人 Serverless
【Python】Pandas的一系列经典操作(非常实用)
【Python】Pandas的一系列经典操作(非常实用)
|
存储 算法 测试技术
力扣经典150题第三十三题:最小覆盖子串
力扣经典150题第三十三题:最小覆盖子串
117 1
|
存储 JavaScript 前端开发
对象的属性方法和深浅拷贝
总结,理解对象的属性和方法对于编程是基础而重要的,而掌握深浅拷贝的差异和使用场合则是编程的高级技能,它能帮助你有效地管理数据的完整性和独立性。
64 0
|
API 开发工具
企业微信api接口调用-触发推送企业微信微信好友
企业微信api接口调用-触发推送企业微信微信好友
|
存储 Python 容器
python数据容器之字典相关的操作
python数据容器之字典相关的操作
55 2
python数据容器之字典相关的操作
|
Java Spring
探究Java spring中jdk代理和cglib代理!
探究Java spring中jdk代理和cglib代理!
254 0