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

简介: 带你读《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并实现搜索。  
相关文章
|
存储 jenkins 开发工具
后端版本控制:原理、实践与挑战
后端版本控制:原理、实践与挑战
433 0
|
SQL 前端开发 JavaScript
Web前端开发工程师岗位要求
Web前端开发工程师岗位要求
|
前端开发 Java 数据库
基于Springboot的漫画网站22(程序+数据库+论文)可帮忙远程调试
基于Springboot的漫画网站22(程序+数据库+论文)可帮忙远程调试
|
存储 缓存 负载均衡
kudu参数优化设置,让集群飞起来~
kudu参数优化设置,让集群飞起来~
|
机器学习/深度学习 算法
机器学习之从极大似然估计到最大熵原理以及EM算法详解
一、极大似然估计 极大似然估计是建立在极大似然原理的基础上的一个统计方法,极大似然原理的直观想法是,一个随机试验如有若干个可能的结果A,B,C,… ,若在一次试验中,结果A出现了,那么可以认为实验条件对A的出现有利,也即出现的概率P(A)较大。
3547 0
|
关系型数据库 Apache 数据库
|
14天前
|
人工智能 自然语言处理 文字识别
阿里云百炼Qwen3.7-Max简介:能力、优势、支持订阅计划参考
Qwen3.7-Max是阿里云百炼面向智能体时代推出的新一代旗舰模型,对标GPT-5.5、Claude Opus 4.7等闭源旗舰。该模型支持百万级token上下文窗口,具备顶级推理能力、多模态搜索与视觉理解增强、流式输出低延迟响应等核心优势,覆盖编程、办公、长周期自主执行等复杂场景。同时支持OpenAI接口兼容,便于系统快速迁移。用户可通过Token Plan团队或节省计划等订阅方式灵活调用,适合企业级高要求场景使用。
5498 28
阿里云百炼Qwen3.7-Max简介:能力、优势、支持订阅计划参考
|
9天前
|
存储 定位技术 数据库
CodeGraph 如何让 Claude Code减少 7 成工具调用?
CodeGraph 为 Coding Agent 提供本地代码知识图谱,把函数、类、调用链和框架路由提前整理成“项目地图”,减少盲目搜索和文件读取。它不是新 Agent,而是上下文基础设施,让 Agent 更快找到正确代码路径,平均减少 7 成工具调用。
1119 1

热门文章

最新文章