painless-debugging.asciidoc 3.8 KB

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