painless-debugging.asciidoc 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. [[painless-debugging]]
  2. === Painless Debugging
  3. ==== Debug.Explain
  4. Painless doesn't have a
  5. https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop[REPL]
  6. and while it'd be nice for it to have one day, it wouldn't tell you the
  7. whole story around debugging painless scripts embedded in Elasticsearch because
  8. the data that the scripts have access to or "context" is so important. For now
  9. the best way to debug embedded scripts is by throwing exceptions at choice
  10. places. While you can throw your own exceptions
  11. (`throw new Exception('whatever')`), Painless's sandbox prevents you from
  12. accessing useful information like the type of an object. So Painless has a
  13. utility method, `Debug.explain` which throws the exception for you. For
  14. example, you can use {ref}/search-explain.html[`_explain`] to explore the
  15. context available to a {ref}/query-dsl-script-query.html[script query].
  16. [source,js]
  17. ---------------------------------------------------------
  18. PUT /hockey/player/1?refresh
  19. {"first":"johnny","last":"gaudreau","goals":[9,27,1],"assists":[17,46,0],"gp":[26,82,1]}
  20. POST /hockey/player/1/_explain
  21. {
  22. "query": {
  23. "script": {
  24. "script": "Debug.explain(doc.goals)"
  25. }
  26. }
  27. }
  28. ---------------------------------------------------------
  29. // CONSOLE
  30. // TEST[s/_explain/_explain?error_trace=false/ catch:/painless_explain_error/]
  31. // The test system sends error_trace=true by default for easier debugging so
  32. // we have to override it to get a normal shaped response
  33. Which shows that the class of `doc.first` is
  34. `org.elasticsearch.index.fielddata.ScriptDocValues.Longs` by responding with:
  35. [source,js]
  36. ---------------------------------------------------------
  37. {
  38. "error": {
  39. "type": "script_exception",
  40. "to_string": "[1, 9, 27]",
  41. "painless_class": "org.elasticsearch.index.fielddata.ScriptDocValues.Longs",
  42. "java_class": "org.elasticsearch.index.fielddata.ScriptDocValues$Longs",
  43. ...
  44. },
  45. "status": 400
  46. }
  47. ---------------------------------------------------------
  48. // TESTRESPONSE[s/\.\.\./"script_stack": $body.error.script_stack, "script": $body.error.script, "lang": $body.error.lang, "caused_by": $body.error.caused_by, "root_cause": $body.error.root_cause, "reason": $body.error.reason/]
  49. You can use the same trick to see that `_source` is a `LinkedHashMap`
  50. in the `_update` API:
  51. [source,js]
  52. ---------------------------------------------------------
  53. POST /hockey/player/1/_update
  54. {
  55. "script": "Debug.explain(ctx._source)"
  56. }
  57. ---------------------------------------------------------
  58. // CONSOLE
  59. // TEST[continued s/_update/_update?error_trace=false/ catch:/painless_explain_error/]
  60. The response looks like:
  61. [source,js]
  62. ---------------------------------------------------------
  63. {
  64. "error" : {
  65. "root_cause": ...,
  66. "type": "illegal_argument_exception",
  67. "reason": "failed to execute script",
  68. "caused_by": {
  69. "type": "script_exception",
  70. "to_string": "{gp=[26, 82, 1], last=gaudreau, assists=[17, 46, 0], first=johnny, goals=[9, 27, 1]}",
  71. "painless_class": "java.util.LinkedHashMap",
  72. "java_class": "java.util.LinkedHashMap",
  73. ...
  74. }
  75. },
  76. "status": 400
  77. }
  78. ---------------------------------------------------------
  79. // TESTRESPONSE[s/"root_cause": \.\.\./"root_cause": $body.error.root_cause/]
  80. // TESTRESPONSE[s/\.\.\./"script_stack": $body.error.caused_by.script_stack, "script": $body.error.caused_by.script, "lang": $body.error.caused_by.lang, "caused_by": $body.error.caused_by.caused_by, "reason": $body.error.caused_by.reason/]
  81. // TESTRESPONSE[s/"to_string": ".+"/"to_string": $body.error.caused_by.to_string/]
  82. Once you have a class you can go to <<painless-api-reference>> to see a list of
  83. available methods.