put_script.asciidoc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. [[java-rest-high-put-stored-script]]
  2. === Put Stored Script API
  3. [[java-rest-high-put-stored-script-request]]
  4. ==== Put Stored Script Request
  5. A `PutStoredScriptRequest` requires an `id` and `content`:
  6. ["source","java",subs="attributes,callouts,macros"]
  7. --------------------------------------------------
  8. include-tagged::{doc-tests}/StoredScriptsDocumentationIT.java[put-stored-script-request]
  9. --------------------------------------------------
  10. <1> The id of the script
  11. <2> The content of the script
  12. [[java-rest-high-put-stored-script-content]]
  13. ==== Content
  14. The content of a script can be written in different languages and provided in
  15. different ways:
  16. ["source","java",subs="attributes,callouts,macros"]
  17. --------------------------------------------------
  18. include-tagged::{doc-tests}/StoredScriptsDocumentationIT.java[put-stored-script-content-painless]
  19. --------------------------------------------------
  20. <1> Specify a painless script and provided as `XContentBuilder` object.
  21. Note that the builder needs to be passed as a `BytesReference` object
  22. ["source","java",subs="attributes,callouts,macros"]
  23. --------------------------------------------------
  24. include-tagged::{doc-tests}/StoredScriptsDocumentationIT.java[put-stored-script-content-mustache]
  25. --------------------------------------------------
  26. <1> Specify a mustache script and provided as `XContentBuilder` object.
  27. Note that value of source can be directly provided as a JSON string
  28. ==== Optional arguments
  29. The following arguments can optionally be provided:
  30. ["source","java",subs="attributes,callouts,macros"]
  31. --------------------------------------------------
  32. include-tagged::{doc-tests}/StoredScriptsDocumentationIT.java[put-stored-script-context]
  33. --------------------------------------------------
  34. <1> The context the script should be executed in.
  35. ["source","java",subs="attributes,callouts,macros"]
  36. --------------------------------------------------
  37. include-tagged::{doc-tests}/StoredScriptsDocumentationIT.java[put-stored-script-timeout]
  38. --------------------------------------------------
  39. <1> Timeout to wait for the all the nodes to acknowledge the script creation as a `TimeValue`
  40. <2> Timeout to wait for the all the nodes to acknowledge the script creation as a `String`
  41. ["source","java",subs="attributes,callouts,macros"]
  42. --------------------------------------------------
  43. include-tagged::{doc-tests}/StoredScriptsDocumentationIT.java[put-stored-script-masterTimeout]
  44. --------------------------------------------------
  45. <1> Timeout to connect to the master node as a `TimeValue`
  46. <2> Timeout to connect to the master node as a `String`
  47. [[java-rest-high-put-stored-script-sync]]
  48. ==== Synchronous Execution
  49. ["source","java",subs="attributes,callouts,macros"]
  50. --------------------------------------------------
  51. include-tagged::{doc-tests}/StoredScriptsDocumentationIT.java[put-stored-script-execute]
  52. --------------------------------------------------
  53. [[java-rest-high-put-stored-script-async]]
  54. ==== Asynchronous Execution
  55. The asynchronous execution of a put stored script request requires both the `PutStoredScriptRequest`
  56. instance and an `ActionListener` instance to be passed to the asynchronous method:
  57. ["source","java",subs="attributes,callouts,macros"]
  58. --------------------------------------------------
  59. include-tagged::{doc-tests}/StoredScriptsDocumentationIT.java[put-stored-script-execute-async]
  60. --------------------------------------------------
  61. <1> The `PutStoredScriptRequest` to execute and the `ActionListener` to use when
  62. the execution completes
  63. [[java-rest-high-put-stored-script-listener]]
  64. ===== Action Listener
  65. The asynchronous method does not block and returns immediately. Once it is
  66. completed the `ActionListener` is called back using the `onResponse` method
  67. if the execution successfully completed or using the `onFailure` method if
  68. it failed.
  69. A typical listener for `AcknowledgedResponse` looks like:
  70. ["source","java",subs="attributes,callouts,macros"]
  71. --------------------------------------------------
  72. include-tagged::{doc-tests}/StoredScriptsDocumentationIT.java[put-stored-script-execute-listener]
  73. --------------------------------------------------
  74. <1> Called when the execution is successfully completed. The response is
  75. provided as an argument
  76. <2> Called in case of failure. The raised exception is provided as an argument
  77. [[java-rest-high-put-stored-script-response]]
  78. ==== Put Stored Script Response
  79. The returned `AcknowledgedResponse` allows to retrieve information about the
  80. executed operation as follows:
  81. ["source","java",subs="attributes,callouts,macros"]
  82. --------------------------------------------------
  83. include-tagged::{doc-tests}/StoredScriptsDocumentationIT.java[put-stored-script-response]
  84. --------------------------------------------------
  85. <1> Indicates whether all of the nodes have acknowledged the request