painless-debugging.asciidoc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. "class": "org.elasticsearch.index.fielddata.ScriptDocValues$Longs",
  42. "to_string": "[1, 9, 27]",
  43. ...
  44. },
  45. "status": 500
  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 `java.util.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. "class": "java.util.LinkedHashMap",
  71. "to_string": "{gp=[26, 82, 1], last=gaudreau, assists=[17, 46, 0], first=johnny, goals=[9, 27, 1]}",
  72. ...
  73. }
  74. },
  75. "status": 400
  76. }
  77. ---------------------------------------------------------
  78. // TESTRESPONSE[s/"root_cause": \.\.\./"root_cause": $body.error.root_cause/]
  79. // 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/]
  80. // TESTRESPONSE[s/"to_string": ".+"/"to_string": $body.error.caused_by.to_string/]
  81. // TODO we should build some javadoc like mashup so people don't have to jump through these hoops.
  82. Once you have the class of an object you can go
  83. https://github.com/elastic/elasticsearch/tree/{branch}/modules/lang-painless/src/main/resources/org/elasticsearch/painless[here]
  84. and check the available methods. Painless uses a strict whitelist to prevent
  85. scripts that don't work well with Elasticsearch and all whitelisted methods
  86. are listed in a file named after the package of the object (everything before
  87. the last `.`). So `java.util.Map` is listed in a file named `java.util.txt`
  88. starting on the line that looks like `class Map -> java.util.Map {`.
  89. With the list of whitelisted methods in hand you can turn to either
  90. https://docs.oracle.com/javase/8/docs/api/[Javadoc],
  91. https://github.com/elastic/elasticsearch/tree/{branch}[Elasticsearch's source tree]
  92. or, for whitelisted methods ending in `*`, the
  93. https://github.com/elastic/elasticsearch/blob/{branch}/modules/lang-painless/src/main/java/org/elasticsearch/painless/Augmentation.java[Augmentation]
  94. class.