put_template.asciidoc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. [[java-rest-high-put-template]]
  2. === Put Template API
  3. [[java-rest-high-put-template-request]]
  4. ==== Put Index Template Request
  5. A `PutIndexTemplateRequest` specifies the `name` of a template and `patterns`
  6. which controls whether the template should be applied to the new index.
  7. ["source","java",subs="attributes,callouts,macros"]
  8. --------------------------------------------------
  9. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-template-request]
  10. --------------------------------------------------
  11. <1> The name of the template
  12. <2> The patterns of the template
  13. ==== Settings
  14. The settings of the template will be applied to the new index whose name matches the
  15. template's patterns.
  16. ["source","java",subs="attributes,callouts,macros"]
  17. --------------------------------------------------
  18. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-template-request-settings]
  19. --------------------------------------------------
  20. <1> Settings for this template
  21. [[java-rest-high-put-template-request-mappings]]
  22. ==== Mappings
  23. The mapping of the template will be applied to the new index whose name matches the
  24. template's patterns.
  25. ["source","java",subs="attributes,callouts,macros"]
  26. --------------------------------------------------
  27. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-template-request-mappings-json]
  28. --------------------------------------------------
  29. <1> The type to define
  30. <2> The mapping for this type, provided as a JSON string
  31. The mapping source can be provided in different ways in addition to the
  32. `String` example shown above:
  33. ["source","java",subs="attributes,callouts,macros"]
  34. --------------------------------------------------
  35. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-template-request-mappings-map]
  36. --------------------------------------------------
  37. <1> Mapping source provided as a `Map` which gets automatically converted
  38. to JSON format
  39. ["source","java",subs="attributes,callouts,macros"]
  40. --------------------------------------------------
  41. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-template-request-mappings-xcontent]
  42. --------------------------------------------------
  43. <1> Mapping source provided as an `XContentBuilder` object, the Elasticsearch
  44. built-in helpers to generate JSON content
  45. ["source","java",subs="attributes,callouts,macros"]
  46. --------------------------------------------------
  47. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-template-request-mappings-shortcut]
  48. --------------------------------------------------
  49. <1> Mapping source provided as `Object` key-pairs, which gets converted to
  50. JSON format
  51. ==== Aliases
  52. The aliases of the template will define aliasing to the index whose name matches the
  53. template's patterns. A placeholder `{index}` can be used in an alias of a template.
  54. ["source","java",subs="attributes,callouts,macros"]
  55. --------------------------------------------------
  56. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-template-request-aliases]
  57. --------------------------------------------------
  58. <1> The alias to define
  59. <2> The alias to define with placeholder
  60. ==== Order
  61. In case multiple templates match an index, the orders of matching templates determine
  62. the sequence that settings, mappings, and alias of each matching template is applied.
  63. Templates with lower orders are applied first, and higher orders override them.
  64. ["source","java",subs="attributes,callouts,macros"]
  65. --------------------------------------------------
  66. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-template-request-order]
  67. --------------------------------------------------
  68. <1> The order of the template
  69. ==== Version
  70. A template can optionally specify a version number which can be used to simplify template
  71. management by external systems.
  72. ["source","java",subs="attributes,callouts,macros"]
  73. --------------------------------------------------
  74. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-template-request-version]
  75. --------------------------------------------------
  76. <1> The version number of the template
  77. ==== Providing the whole source
  78. The whole source including all of its sections (mappings, settings and aliases)
  79. can also be provided:
  80. ["source","java",subs="attributes,callouts,macros"]
  81. --------------------------------------------------
  82. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-template-whole-source]
  83. --------------------------------------------------
  84. <1> The source provided as a JSON string. It can also be provided as a `Map`
  85. or an `XContentBuilder`.
  86. ==== Optional arguments
  87. The following arguments can optionally be provided:
  88. ["source","java",subs="attributes,callouts,macros"]
  89. --------------------------------------------------
  90. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-template-request-create]
  91. --------------------------------------------------
  92. <1> To force to only create a new template; do not overwrite the existing template
  93. ["source","java",subs="attributes,callouts,macros"]
  94. --------------------------------------------------
  95. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-template-request-masterTimeout]
  96. --------------------------------------------------
  97. <1> Timeout to connect to the master node as a `TimeValue`
  98. <2> Timeout to connect to the master node as a `String`
  99. [[java-rest-high-put-template-sync]]
  100. ==== Synchronous Execution
  101. ["source","java",subs="attributes,callouts,macros"]
  102. --------------------------------------------------
  103. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-template-execute]
  104. --------------------------------------------------
  105. [[java-rest-high-put-template-async]]
  106. ==== Asynchronous Execution
  107. The asynchronous execution of a put template request requires both the `PutIndexTemplateRequest`
  108. instance and an `ActionListener` instance to be passed to the asynchronous method:
  109. ["source","java",subs="attributes,callouts,macros"]
  110. --------------------------------------------------
  111. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-template-execute-async]
  112. --------------------------------------------------
  113. <1> The `PutIndexTemplateRequest` to execute and the `ActionListener` to use when
  114. the execution completes
  115. The asynchronous method does not block and returns immediately. Once it is
  116. completed the `ActionListener` is called back using the `onResponse` method
  117. if the execution successfully completed or using the `onFailure` method if
  118. it failed.
  119. A typical listener for `PutIndexTemplateResponse` looks like:
  120. ["source","java",subs="attributes,callouts,macros"]
  121. --------------------------------------------------
  122. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-template-execute-listener]
  123. --------------------------------------------------
  124. <1> Called when the execution is successfully completed. The response is
  125. provided as an argument
  126. <2> Called in case of failure. The raised exception is provided as an argument
  127. [[java-rest-high-put-template-response]]
  128. ==== Put Index Template Response
  129. The returned `PutIndexTemplateResponse` allows to retrieve information about the
  130. executed operation as follows:
  131. ["source","java",subs="attributes,callouts,macros"]
  132. --------------------------------------------------
  133. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[put-template-response]
  134. --------------------------------------------------
  135. <1> Indicates whether all of the nodes have acknowledged the request