Elasticsearch:Painless 编程调试「建议收藏」

Painless也就是无痛的意思。这是一个专为Elasticsearch而设计的。当初的设计人员取名为“Painless”,表达的意思的是在编程的时候没有疼痛感,很方便设计人员使用。由于这是一个脚本的语言,在实际的使用中,我们很难找到这些编程的方法及使用。在今天的教程中,我来讲述一下该如何来进行调试。Debug.ExplainPainless没有REPL,虽然很高兴有一天,但它不会告诉你调试Elasticsearch中嵌入的Painless脚本的全部过程,因为脚本可以访问或“上

大家好,又见面了,我是你们的朋友全栈君。

Painless 也就是无痛的意思。这是一个专为 Elasticsearch 而设计的。当初的设计人员取名为 “Painless”,表达的意思的是在编程的时候没有疼痛感,很方便设计人员使用。由于这是一个脚本的语言,在实际的使用中,我们很难找到这些编程的方法及使用。在今天的教程中,我来讲述一下该如何来进行调试。

Debug.Explain

Painless 没有 REPL,很希望将来有一天会有,但它不会告诉你调试 Elasticsearch 中嵌入的 Painless 脚本的全部过程,因为脚本可以访问或 “上下文”访问数据非常重要。 目前,调试嵌入式脚本的最佳方法是在选择的位置抛出异常。 尽管你可以抛出自己的异常(抛出新的Exception(’whatever’)),但 Painless 的沙箱可阻止你访问有用的信息,例如对象的类型。 因此,Painless 具有实用程序方法 Debug.explain,它可以为你引发异常。 例如,你可以使用 _explain 探索脚本查询可用的上下文。

例子一

我们在 Kibana 中打入如下的命令:

PUT /hockey/_doc/1?refresh
{
  "first": "johnny",
  "last": "gaudreau",
  "goals": [
    9,
    27,
    1
  ],
  "assists": [
    17,
    46,
    0
  ],
  "gp": [
    26,
    82,
    1
  ],
  "time": "2020-08-30"
}

上面的命令将生成如下的 mapping:

GET  hockey/_mapping
{
  "hockey" : {
    "mappings" : {
      "properties" : {
        "assists" : {
          "type" : "long"
        },
        "first" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "goals" : {
          "type" : "long"
        },
        "gp" : {
          "type" : "long"
        },
        "last" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "time" : {
          "type" : "date"
        }
      }
    }
  }
}

如上图所示,我们可以看到有几种类型的数据:long, text, keyword 以及 date。那么现在问题来了,在实际的 script 编程中,我们该如何对这些数据进行操作。它们的数据类型有什么可以供我们使用的方法呢?

我们使用如下的  _explain  终点:

POST /hockey/_explain/1
{
  "query": {
    "script": {
      "script": "Debug.explain(doc.goals)"
    }
  }
}

上面命令的相应结果为:

{
  "error" : {
    "root_cause" : [
      {
        "type" : "script_exception",
        "reason" : "runtime error",
        "painless_class" : "org.elasticsearch.index.fielddata.ScriptDocValues.Longs",
        "to_string" : "[1, 9, 27]",
        "java_class" : "org.elasticsearch.index.fielddata.ScriptDocValues$Longs",
        "script_stack" : [
          "Debug.explain(doc.goals)",
          "                 ^---- HERE"
        ],
        "script" : "Debug.explain(doc.goals)",
        "lang" : "painless",
        "position" : {
          "offset" : 17,
          "start" : 0,
          "end" : 24
        }
      }
    ],
    "type" : "script_exception",
    "reason" : "runtime error",
    "painless_class" : "org.elasticsearch.index.fielddata.ScriptDocValues.Longs",
    "to_string" : "[1, 9, 27]",
    "java_class" : "org.elasticsearch.index.fielddata.ScriptDocValues$Longs",
    "script_stack" : [
      "Debug.explain(doc.goals)",
      "                 ^---- HERE"
    ],
    "script" : "Debug.explain(doc.goals)",
    "lang" : "painless",
    "position" : {
      "offset" : 17,
      "start" : 0,
      "end" : 24
    },
    "caused_by" : {
      "type" : "painless_explain_error",
      "reason" : null
    }
  },
  "status" : 400
}

上面显示了一个 exception。同时它也显示了 doc.goals 是一个 这样类型的数据:

"painless_class" : "org.elasticsearch.index.fielddata.ScriptDocValues.Longs",
"java_class" : "org.elasticsearch.index.fielddata.ScriptDocValues$Longs",

接下来,我们来参考链接 Painless API Reference | Painless Scripting Language [7.0] | Elastic。我们寻找 org.elasticsearch.index.fielddata.ScriptDocValues.Longs:我们可以看到如下的一些说明:

这里展示了它的一些可供使用的 API 接口。从上面,我们可以看出来这个数据是一个 List 类型的数据,我们可以使用如下的方法来进行统计:

GET hockey/_search
{
  "query": {
    "function_score": {
      "script_score": {
        "script": {
          "lang": "painless",
          "source": """
             int total = 0; 
             for (int i = 0; i < doc['goals'].getLength(); ++i) { 
               total += doc['goals'].get(i); 
             } 
             return total;
          """
        }
      }
    }
  }
}

在这里,我们使用了 getLength() 以及 get(i)。这些方法都是 List 类型数据的方法。我们也可以使用精简的方法:

GET hockey/_search
{
  "query": {
    "function_score": {
      "script_score": {
        "script": {
          "lang": "painless",
          "source": """
             int total = 0; 
             for (int i = 0; i < doc['goals'].length; ++i) { 
               total += doc['goals'][i]; 
             } 
             return total;
          """
        }
      }
    }
  }
}

上面的显示的结果为:

{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 37.0,
    "hits" : [
      {
        "_index" : "hockey",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 37.0,
        "_source" : {
          "first" : "johnny",
          "last" : "gaudreau",
          "goals" : [
            9,
            27,
            1
          ],
          "assists" : [
            17,
            46,
            0
          ],
          "gp" : [
            26,
            82,
            1
          ],
          "time" : "2020-08-30"
        }
      }
    ]
  }
}

例子二

接下来例子,我们可以使用 date 数据类型为例。在 Kibana 中运行如下的命令:

POST /hockey/_explain/1
{
  "query": {
    "script": {
      "script": "Debug.explain(doc['time'])"
    }
  }
}

上面的命令将抛出如下的 exception:

{
  "error" : {
    "root_cause" : [
      {
        "type" : "script_exception",
        "reason" : "runtime error",
        "painless_class" : "org.elasticsearch.index.fielddata.ScriptDocValues.Dates",
        "to_string" : "[2020-08-30T00:00:00.000Z]",
        "java_class" : "org.elasticsearch.index.fielddata.ScriptDocValues$Dates",
        "script_stack" : [
          "Debug.explain(doc['time'])",
          "                 ^---- HERE"
        ],
        "script" : "Debug.explain(doc['time'])",
        "lang" : "painless",
        "position" : {
          "offset" : 17,
          "start" : 0,
          "end" : 26
        }
      }
    ],
    "type" : "script_exception",
    "reason" : "runtime error",
    "painless_class" : "org.elasticsearch.index.fielddata.ScriptDocValues.Dates",
    "to_string" : "[2020-08-30T00:00:00.000Z]",
    "java_class" : "org.elasticsearch.index.fielddata.ScriptDocValues$Dates",
    "script_stack" : [
      "Debug.explain(doc['time'])",
      "                 ^---- HERE"
    ],
    "script" : "Debug.explain(doc['time'])",
    "lang" : "painless",
    "position" : {
      "offset" : 17,
      "start" : 0,
      "end" : 26
    },
    "caused_by" : {
      "type" : "painless_explain_error",
      "reason" : null
    }
  },
  "status" : 400
}

从上面的 exception 中,我们可以看出来这个抛出的异常含有:

"painless_class" : "org.elasticsearch.index.fielddata.ScriptDocValues.Dates",
"java_class" : "org.elasticsearch.index.fielddata.ScriptDocValues$Dates",

它表明这个是一个 org.elasticsearch.index.fielddata.ScriptDocValues.Dates 类型的数据。我们查看 Painless API Reference | Painless Scripting Language [7.0] | Elastic, 并定位 org.elasticsearch.index.fielddata.ScriptDocValues.Dates,我们可以看到如下的方法描述:

org.elasticsearch.index.fielddata.ScriptDocValues.Dates

显然它是一个叫做 org.joda.time.ReadableDateTime 类型的数据。我们点击上面的链接,我们可以看到更多的关于这个类型的方法:

org.joda.time.ReadableDateTime

我们从上面的列表中,我们看到非常丰富的方法供我们来使用。基于上面的理解,我们可以使用如下的脚本来进行搜索:

POST /hockey/_search
{
  "query": {
    "script": {
      "script": """
       doc['time'].value.getYear() > 2000
     
      """
    }
  }
}

在上面,我们使用了 getYear() 来获取年份。上面搜索返回的结果是:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "hockey",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "first" : "johnny",
          "last" : "gaudreau",
          "goals" : [
            9,
            27,
            1
          ],
          "assists" : [
            17,
            46,
            0
          ],
          "gp" : [
            26,
            82,
            1
          ],
          "time" : "2020-08-30"
        }
      }
    ]
  }
}

当然,如果我们把搜索的年份修改为:

POST /hockey/_search
{
  "query": {
    "script": {
      "script": """
       doc['time'].value.getYear() < 2000
     
      """
    }
  }
}

我们将什么也搜索不到,这是因为文档的日期是 2020-08-30。

我们也可以直接使用最简洁的方法:

POST /hockey/_search
{
  "query": {
    "script": {
      "script": """
       return doc['time'].value.year > 1999
      """
    }
  }
}

这里,直接使用 year 作为一个属性来获取。

例子三

我们也可以直接对 _source 进行操作:

POST /hockey/_update/1
{
  "script": "Debug.explain(ctx._source)"
}

上面的显示结果为:

{
  "error" : {
    "root_cause" : [
      {
        "type" : "illegal_argument_exception",
        "reason" : "failed to execute script"
      }
    ],
    "type" : "illegal_argument_exception",
    "reason" : "failed to execute script",
    "caused_by" : {
      "type" : "script_exception",
      "reason" : "runtime error",
      "painless_class" : "java.util.LinkedHashMap",
      "to_string" : "{first=johnny, last=gaudreau, goals=[9, 27, 1], assists=[17, 46, 0], gp=[26, 82, 1], time=2020-08-30}",
      "java_class" : "java.util.LinkedHashMap",
      "script_stack" : [
        "Debug.explain(ctx._source)",
        "                 ^---- HERE"
      ],
      "script" : "Debug.explain(ctx._source)",
      "lang" : "painless",
      "position" : {
        "offset" : 17,
        "start" : 0,
        "end" : 26
      },
      "caused_by" : {
        "type" : "painless_explain_error",
        "reason" : null
      }
    }
  },
  "status" : 400
}

我们可以看到它是一个叫做 java.util.LinkedHashMap 类型的数据。我们可以直接查找 Java 的 API 文档来使用相应的方法。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/126513.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • UFT使用技巧

    UFT使用技巧1 UFT基本功能的使用         UFT的基本功能包括两大部分:一部分是提供给初级用户使用的关键字视图;另一部分是提供给熟悉VBScript脚本编写的自动化测试工程师使用的专家视图。但是,并没有严格的区分,在实际的自动化测试项目中完全可以两者结合着使用。 1.1 UFT自动化测试的基本过程          使用UFT进行自动化测试的基本过程与使用其他自动化测试工具进行自动化功能测试的过…

    2022年5月26日
    190
  • java11-泛型及其使用[通俗易懂]

    java11-泛型及其使用[通俗易懂]1.概述就本质而言“泛型”的意思就是参数化类型。参数化类型很重要,因为使用该特性创建的类、接口以及方法可以以参数的形式指定操作的数据类型。泛型通俗的说就是方法的返回值或参数是不确定的,可以随创建

    2022年8月4日
    8
  • git clone指定分支

    git clone指定分支技术背景Git是代码版本最常用的管理工具,此前也写过一篇介绍Git的基本使用的博客,而本文介绍一个可能在特定场景下能够用到的功能–直接拉取指定分支的内容。GitClone首先看一下如果我们按照常规的操作去拉取一个Gitee的代码仓,是什么样的效果:$gitclonehttps://gitee.com/mindspore/mindscience.git正克隆到’mindsci…

    2022年7月21日
    23
  • 0703-APP-Notification-statue-bar

    0703-APP-Notification-statue-bar

    2021年11月23日
    51
  • 爬虫PyQuery「建议收藏」

    爬虫PyQuery「建议收藏」–爬虫pyquery字符串初始化html=””” ……””””frompyqueryimportPyQueryaspqdoc=pq(html)print(doc(‘li’))–其实就是个css选择器,选出了所有的li标签url初始化frompyqueryimportPyQueryaspqdoc=pq(url=”http://www.baidu…

    2022年6月8日
    32
  • matlab 计算变异系数,变异系数法求权重matlab代码

    matlab 计算变异系数,变异系数法求权重matlab代码《变异系数法求权重matlab代码》由会员分享,可在线阅读,更多相关《变异系数法求权重matlab代码(1页珍藏版)》请在读根文库上搜索。1、变异系数法求权重matlab代码clear;clc;data1,header1=xlsread(statistic1.xlsx,ECO);%必须将statistic.xlsx至于默认文件下,或者给出完整路径data2,header2=xl…

    2022年4月28日
    114

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号