2017-05-12 19:17:06 -04:00
|
|
|
[[painless-debugging]]
|
|
|
|
=== Painless Debugging
|
|
|
|
|
|
|
|
==== Debug.Explain
|
|
|
|
|
|
|
|
Painless doesn't have a
|
|
|
|
https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop[REPL]
|
2018-08-28 07:16:43 -04:00
|
|
|
and while it'd be nice for it to have one day, it wouldn't tell you the
|
2017-05-12 19:17:06 -04:00
|
|
|
whole story around debugging painless scripts embedded in Elasticsearch because
|
|
|
|
the data that the scripts have access to or "context" is so important. For now
|
|
|
|
the best way to debug embedded scripts is by throwing exceptions at choice
|
|
|
|
places. While you can throw your own exceptions
|
|
|
|
(`throw new Exception('whatever')`), Painless's sandbox prevents you from
|
|
|
|
accessing useful information like the type of an object. So Painless has a
|
|
|
|
utility method, `Debug.explain` which throws the exception for you. For
|
|
|
|
example, you can use {ref}/search-explain.html[`_explain`] to explore the
|
|
|
|
context available to a {ref}/query-dsl-script-query.html[script query].
|
|
|
|
|
2019-09-09 13:38:14 -04:00
|
|
|
[source,console]
|
2017-05-12 19:17:06 -04:00
|
|
|
---------------------------------------------------------
|
2019-01-29 13:51:07 -05:00
|
|
|
PUT /hockey/_doc/1?refresh
|
2017-05-12 19:17:06 -04:00
|
|
|
{"first":"johnny","last":"gaudreau","goals":[9,27,1],"assists":[17,46,0],"gp":[26,82,1]}
|
|
|
|
|
2019-01-29 13:51:07 -05:00
|
|
|
POST /hockey/_explain/1
|
2017-05-12 19:17:06 -04:00
|
|
|
{
|
|
|
|
"query": {
|
|
|
|
"script": {
|
|
|
|
"script": "Debug.explain(doc.goals)"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
---------------------------------------------------------
|
2019-01-29 13:51:07 -05:00
|
|
|
// TEST[s/_explain\/1/_explain\/1?error_trace=false/ catch:/painless_explain_error/]
|
2017-05-12 19:17:06 -04:00
|
|
|
// The test system sends error_trace=true by default for easier debugging so
|
|
|
|
// we have to override it to get a normal shaped response
|
|
|
|
|
|
|
|
Which shows that the class of `doc.first` is
|
|
|
|
`org.elasticsearch.index.fielddata.ScriptDocValues.Longs` by responding with:
|
|
|
|
|
2019-09-06 16:09:09 -04:00
|
|
|
[source,console-result]
|
2017-05-12 19:17:06 -04:00
|
|
|
---------------------------------------------------------
|
|
|
|
{
|
|
|
|
"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",
|
|
|
|
...
|
|
|
|
},
|
2018-05-30 08:00:07 -04:00
|
|
|
"status": 400
|
2017-05-12 19:17:06 -04:00
|
|
|
}
|
|
|
|
---------------------------------------------------------
|
Scripting: Add char position of script errors (#51069) (#51266)
Add the character position of a scripting error to error responses.
The contents of the `position` field are experimental and subject to
change. Currently, `offset` refers to the character location where the
error was encountered, `start` and `end` define a range of characters
that contain the error.
eg.
```
{
"error": {
"root_cause": [
{
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"y = x;",
" ^---- HERE"
],
"script": "def x = new ArrayList(); Map y = x;",
"lang": "painless",
"position": {
"offset": 33,
"start": 29,
"end": 35
}
}
```
Refs: #50993
2020-01-21 15:45:59 -05:00
|
|
|
// TESTRESPONSE[s/\.\.\./"script_stack": $body.error.script_stack, "script": $body.error.script, "lang": $body.error.lang, "position": $body.error.position, "caused_by": $body.error.caused_by, "root_cause": $body.error.root_cause, "reason": $body.error.reason/]
|
2017-05-12 19:17:06 -04:00
|
|
|
|
|
|
|
You can use the same trick to see that `_source` is a `LinkedHashMap`
|
|
|
|
in the `_update` API:
|
|
|
|
|
2019-09-09 13:38:14 -04:00
|
|
|
[source,console]
|
2017-05-12 19:17:06 -04:00
|
|
|
---------------------------------------------------------
|
2019-01-29 13:51:07 -05:00
|
|
|
POST /hockey/_update/1
|
2017-05-12 19:17:06 -04:00
|
|
|
{
|
|
|
|
"script": "Debug.explain(ctx._source)"
|
|
|
|
}
|
|
|
|
---------------------------------------------------------
|
2019-01-29 13:51:07 -05:00
|
|
|
// TEST[continued s/_update\/1/_update\/1?error_trace=false/ catch:/painless_explain_error/]
|
2017-05-12 19:17:06 -04:00
|
|
|
|
|
|
|
The response looks like:
|
|
|
|
|
2019-09-06 16:09:09 -04:00
|
|
|
[source,console-result]
|
2017-05-12 19:17:06 -04:00
|
|
|
---------------------------------------------------------
|
|
|
|
{
|
|
|
|
"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]}",
|
2017-12-11 19:37:35 -05:00
|
|
|
"painless_class": "java.util.LinkedHashMap",
|
2017-05-12 19:17:06 -04:00
|
|
|
"java_class": "java.util.LinkedHashMap",
|
|
|
|
...
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"status": 400
|
|
|
|
}
|
|
|
|
---------------------------------------------------------
|
|
|
|
// TESTRESPONSE[s/"root_cause": \.\.\./"root_cause": $body.error.root_cause/]
|
Scripting: Add char position of script errors (#51069) (#51266)
Add the character position of a scripting error to error responses.
The contents of the `position` field are experimental and subject to
change. Currently, `offset` refers to the character location where the
error was encountered, `start` and `end` define a range of characters
that contain the error.
eg.
```
{
"error": {
"root_cause": [
{
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"y = x;",
" ^---- HERE"
],
"script": "def x = new ArrayList(); Map y = x;",
"lang": "painless",
"position": {
"offset": 33,
"start": 29,
"end": 35
}
}
```
Refs: #50993
2020-01-21 15:45:59 -05:00
|
|
|
// TESTRESPONSE[s/\.\.\./"script_stack": $body.error.caused_by.script_stack, "script": $body.error.caused_by.script, "lang": $body.error.caused_by.lang, "position": $body.error.caused_by.position, "caused_by": $body.error.caused_by.caused_by, "reason": $body.error.caused_by.reason/]
|
2017-05-12 19:17:06 -04:00
|
|
|
// TESTRESPONSE[s/"to_string": ".+"/"to_string": $body.error.caused_by.to_string/]
|
|
|
|
|
|
|
|
Once you have a class you can go to <<painless-api-reference>> to see a list of
|
|
|
|
available methods.
|