1. Painless 调试

原文链接 : https://www.elastic.co/guide/en/elasticsearch/reference/5.3/modules-scripting-painless-debugging.html

译文链接 : http://www.apache.wiki/pages/viewpage.action?pageId=10027222

贡献者 : @琴剑蓝天ApacheCNApache中文网

Painless 脚本语言是新的,仍然被标记为实验性语言。 如果需要,语法或API可能会以非向后兼容的方式在将来更改。

1.1. Debug.Explain

Painless 没有 REPL,虽然它有一天会很好,但它不会告诉你关于调试嵌入在弹性搜索中的 Painless 脚本的整个故事,因为脚本可以访问或“上下文 “是如此重要。 现在调试嵌入式脚本的最佳方法是在选择的地方抛出异常。 虽然你可以抛出你自己的异常(抛出新的异常

('whatever')),Painless 的沙箱阻止你访问有用的信息,如对象的类型。 所以 Painless 有一个实用的方法,Debug.explain 为你抛出异常。 例如,您可以使用 Explain API 来探索脚本查询可用的上下文。

PUT /hockey/player/1?refresh
{"first":"johnny","last":"gaudreau","goals":[9,27,1],"assists":[17,46,0],"gp":[26,82,1]}

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

这表明 doc.first 的类是 org.elasticsearch.index.fielddata.ScriptDocValues.Longs 通过响应:

{
   "error": {
      "type": "script_exception",
      "to_string": "[1, 9, 27]",
      "painless_class": "org.elasticsearch.index.fielddata.ScriptDocValues.Longs",
      "java_class": "org.elasticsearch.index.fielddata.ScriptDocValues$Longs",
      ...
   },
   "status": 500
}

您可以使用相同的技巧来查看 source_ 是 update API 中的 _LinkedHashMap

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

响应如下:

{
  "error" : {
    "root_cause": ...,
    "type": "illegal_argument_exception",
    "reason": "failed to execute script",
    "caused_by": {
      "type": "script_exception",
      "to_string": "{gp=[26, 82, 1], last=gaudreau, assists=[17, 46, 0], first=johnny, goals=[9, 27, 1]}",
      "painless_class": "LinkedHashMap",
      "java_class": "java.util.LinkedHashMap",
      ...
    }
  },
  "status": 400
}

一旦你有一个类,你可以去附录A,Painless API参考,看看可用的方法的列表。

Copyright © Kilvn 2021. all right reserved,powered by Gitbook最后更新时间: 2021-06-08 20:22:42

results matching ""

    No results matching ""