put_script.asciidoc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. [[java-rest-high-put-stored-script]]
  2. === Create or update stored script API
  3. [[java-rest-high-put-stored-script-request]]
  4. ==== 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 create or update stored script request requires
  56. both the `PutStoredScriptRequest` instance and an `ActionListener` instance to
  57. be passed to the asynchronous method:
  58. ["source","java",subs="attributes,callouts,macros"]
  59. --------------------------------------------------
  60. include-tagged::{doc-tests}/StoredScriptsDocumentationIT.java[put-stored-script-execute-async]
  61. --------------------------------------------------
  62. <1> The `PutStoredScriptRequest` to execute and the `ActionListener` to use when
  63. the execution completes
  64. [[java-rest-high-put-stored-script-listener]]
  65. ===== Action Listener
  66. The asynchronous method does not block and returns immediately. Once it is
  67. completed the `ActionListener` is called back using the `onResponse` method
  68. if the execution successfully completed or using the `onFailure` method if
  69. it failed.
  70. A typical listener for `AcknowledgedResponse` looks like:
  71. ["source","java",subs="attributes,callouts,macros"]
  72. --------------------------------------------------
  73. include-tagged::{doc-tests}/StoredScriptsDocumentationIT.java[put-stored-script-execute-listener]
  74. --------------------------------------------------
  75. <1> Called when the execution is successfully completed. The response is
  76. provided as an argument
  77. <2> Called in case of failure. The raised exception is provided as an argument
  78. [[java-rest-high-put-stored-script-response]]
  79. ==== Response
  80. The returned `AcknowledgedResponse` allows to retrieve information about the
  81. executed operation as follows:
  82. ["source","java",subs="attributes,callouts,macros"]
  83. --------------------------------------------------
  84. include-tagged::{doc-tests}/StoredScriptsDocumentationIT.java[put-stored-script-response]
  85. --------------------------------------------------
  86. <1> Indicates whether all of the nodes have acknowledged the request