painless-debugging.asciidoc 4.4 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[catch:/painless_explain_error/]
  32. Which shows that the class of `doc.first` is
  33. `org.elasticsearch.index.fielddata.ScriptDocValues$Longs` by responding with:
  34. [source,js]
  35. ---------------------------------------------------------
  36. {
  37. "error": {
  38. "type": "script_exception",
  39. "class": "org.elasticsearch.index.fielddata.ScriptDocValues$Longs",
  40. "to_string": "[1, 9, 27]",
  41. ...
  42. },
  43. "status": 500
  44. }
  45. ---------------------------------------------------------
  46. // 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/]
  47. You can use the same trick to see that `_source` is a `java.util.LinkedHashMap`
  48. in the `_update` API:
  49. [source,js]
  50. ---------------------------------------------------------
  51. POST /hockey/player/1/_update
  52. {
  53. "script": "Debug.explain(ctx._source)"
  54. }
  55. ---------------------------------------------------------
  56. // CONSOLE
  57. // TEST[continued catch:/painless_explain_error/]
  58. The response looks like:
  59. [source,js]
  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. "class": "java.util.LinkedHashMap",
  69. "to_string": "{gp=[26, 82, 1], last=gaudreau, assists=[17, 46, 0], first=johnny, goals=[9, 27, 1]}",
  70. ...
  71. }
  72. },
  73. "status": 400
  74. }
  75. ---------------------------------------------------------
  76. // TESTRESPONSE[s/"root_cause": \.\.\./"root_cause": $body.error.root_cause/]
  77. // 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/]
  78. // TESTRESPONSE[s/"to_string": ".+"/"to_string": $body.error.caused_by.to_string/]
  79. <1> This is the explanation.
  80. // TODO we should build some javadoc like mashup so people don't have to jump through these hoops.
  81. Once you have the class of an object you can go
  82. https://github.com/elastic/elasticsearch/tree/{branch}/modules/lang-painless/src/main/resources/org/elasticsearch/painless[here]
  83. and check the available methods. Painless uses a strict whitelist to prevent
  84. scripts that don't work well with Elasticsearch and all whitelisted methods
  85. are listed in a file named after the package of the object (everything before
  86. the last `.`). So `java.util.Map` is listed in a file named `java.util.txt`
  87. starting on the line that looks like `class Map -> java.util.Map {`.
  88. With the list of whitelisted methods in hand you can turn to either
  89. https://docs.oracle.com/javase/8/docs/api/[Javadoc],
  90. https://github.com/elastic/elasticsearch/tree/{branch}[Elasticsearch's source tree]
  91. or, for whitelisted methods ending in `*`, the
  92. https://github.com/elastic/elasticsearch/blob/{branch}/modules/lang-painless/src/main/java/org/elasticsearch/painless/Augmentation.java[Augmentation]
  93. class.