painless-debugging.asciidoc 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. [[painless-debugging]]
  2. === Painless Debugging
  3. ==== Debug.Explain
  4. Painless doesn't have a
  5. {wikipedia}/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,console]
  17. ---------------------------------------------------------
  18. PUT /hockey/_doc/1?refresh
  19. {"first":"johnny","last":"gaudreau","goals":[9,27,1],"assists":[17,46,0],"gp":[26,82,1]}
  20. POST /hockey/_explain/1
  21. {
  22. "query": {
  23. "script": {
  24. "script": "Debug.explain(doc.goals)"
  25. }
  26. }
  27. }
  28. ---------------------------------------------------------
  29. // TEST[s/_explain\/1/_explain\/1?error_trace=false/ catch:/painless_explain_error/]
  30. // The test system sends error_trace=true by default for easier debugging so
  31. // we have to override it to get a normal shaped response
  32. Which shows that the class of `doc.first` is
  33. `org.elasticsearch.index.fielddata.ScriptDocValues.Longs` by responding with:
  34. [source,console-result]
  35. ---------------------------------------------------------
  36. {
  37. "error": {
  38. "type": "script_exception",
  39. "to_string": "[1, 9, 27]",
  40. "painless_class": "org.elasticsearch.index.fielddata.ScriptDocValues.Longs",
  41. "java_class": "org.elasticsearch.index.fielddata.ScriptDocValues$Longs",
  42. ...
  43. },
  44. "status": 400
  45. }
  46. ---------------------------------------------------------
  47. // 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/]
  48. You can use the same trick to see that `_source` is a `LinkedHashMap`
  49. in the `_update` API:
  50. [source,console]
  51. ---------------------------------------------------------
  52. POST /hockey/_update/1
  53. {
  54. "script": "Debug.explain(ctx._source)"
  55. }
  56. ---------------------------------------------------------
  57. // TEST[continued s/_update\/1/_update\/1?error_trace=false/ catch:/painless_explain_error/]
  58. The response looks like:
  59. [source,console-result]
  60. ---------------------------------------------------------
  61. {
  62. "error" : {
  63. "root_cause": ...,
  64. "type": "illegal_argument_exception",
  65. "reason": "failed to execute script",
  66. "caused_by": {
  67. "type": "script_exception",
  68. "to_string": "{gp=[26, 82, 1], last=gaudreau, assists=[17, 46, 0], first=johnny, goals=[9, 27, 1]}",
  69. "painless_class": "java.util.LinkedHashMap",
  70. "java_class": "java.util.LinkedHashMap",
  71. ...
  72. }
  73. },
  74. "status": 400
  75. }
  76. ---------------------------------------------------------
  77. // TESTRESPONSE[s/"root_cause": \.\.\./"root_cause": $body.error.root_cause/]
  78. // 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/]
  79. // TESTRESPONSE[s/"to_string": ".+"/"to_string": $body.error.caused_by.to_string/]
  80. Once you have a class you can go to <<painless-api-reference>> to see a list of
  81. available methods.