企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# Painless 调试 原文链接 : [https://www.elastic.co/guide/en/elasticsearch/reference/5.3/modules-scripting-painless-debugging.html](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/modules-scripting-painless-debugging.html) 译文链接 : [http://www.apache.wiki/pages/viewpage.action?pageId=10027222](http://www.apache.wiki/pages/viewpage.action?pageId=10027222) 贡献者 : [@琴剑蓝天](http://www.apache.wiki/users/viewuserprofile.action?username=xujie),[ApacheCN](/display/~apachecn),[Apache中文网](/display/~apachechina)  Painless 脚本语言是新的,仍然被标记为实验性语言。 如果需要,语法或API可能会以非向后兼容的方式在将来更改。 ## Debug.Explain **Painless** 没有 [**REPL**](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop),虽然它有一天会很好,但它不会告诉你关于调试嵌入在弹性搜索中的 **Painless **脚本的整个故事,因为脚本可以访问或“上下文 “是如此重要。 现在调试嵌入式脚本的最佳方法是在选择的地方抛出异常。 虽然你可以抛出你自己的异常(抛出新的异常 ('whatever')),**Painless** 的沙箱阻止你访问有用的信息,如对象的类型。 所以 **Painless** 有一个实用的方法,_Debug.explain_ 为你抛出异常。 例如,您可以使用 [**Explain API**](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/search-explain.html) 来探索[脚本查询](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/query-dsl-script-query.html)可用的上下文。 ``` 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_](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/painless-api-reference.html)参考,看看可用的方法的列表。