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

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: 带你读《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

相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
数据安全/隐私保护
带你读《Elastic Stack 实战手册》之62:—— 3.5.18.1.Workplace Search(4)
带你读《Elastic Stack 实战手册》之62:—— 3.5.18.1.Workplace Search(4)
141 0
带你读《Elastic Stack 实战手册》之62:—— 3.5.18.1.Workplace Search(4)
|
存储 安全 Java
带你读《Elastic Stack 实战手册》之62:—— 3.5.18.1.Workplace Search(3)
带你读《Elastic Stack 实战手册》之62:—— 3.5.18.1.Workplace Search(3)
109 0
带你读《Elastic Stack 实战手册》之62:—— 3.5.18.1.Workplace Search(3)
|
存储 缓存 安全
带你读《Elastic Stack 实战手册》之31:——3.4.2.16.Painless scripting(上)
带你读《Elastic Stack 实战手册》之31:——3.4.2.16.Painless scripting(上)
110 0
|
存储 API 项目管理
带你读《Elastic Stack 实战手册》之31:——3.4.2.16.Painless scripting(下)
带你读《Elastic Stack 实战手册》之31:——3.4.2.16.Painless scripting(下)
|
机器学习/深度学习 运维 安全
带你读《Elastic Stack 实战手册》之57:——3.5.16.1.Setup and security(上)
带你读《Elastic Stack 实战手册》之57:——3.5.16.1.Setup and security(上)
104 0
|
机器学习/深度学习 SQL 运维
带你读《Elastic Stack 实战手册》之57:——3.5.16.1.Setup and security(下)
带你读《Elastic Stack 实战手册》之57:——3.5.16.1.Setup and security(下)
111 0
|
SQL 自然语言处理 监控
带你读《Elastic Stack 实战手册》之2:——二、导读(上)
带你读《Elastic Stack 实战手册》之2:——二、导读(上)
333 0
|
JSON Java 数据格式
带你读《Elastic Stack 实战手册》之33:——3.4.2.17.2.Schemaless(下)
带你读《Elastic Stack 实战手册》之33:——3.4.2.17.2.Schemaless(下)
100 0
|
自然语言处理 索引
带你读《Elastic Stack 实战手册》之33:——3.4.2.17.2.Schemaless(上)
带你读《Elastic Stack 实战手册》之33:——3.4.2.17.2.Schemaless(上)
107 0
|
存储 运维 监控
带你读《Elastic Stack 实战手册》之2:——二、导读(下)
带你读《Elastic Stack 实战手册》之2:——二、导读(下)
260 0

热门文章

最新文章