component-templates.asciidoc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. [[indices-component-template]]
  2. === Put component template API
  3. ++++
  4. <titleabbrev>Put component template</titleabbrev>
  5. ++++
  6. Component templates are building blocks that specify mappings, settings, or alias configuration, but
  7. don't apply to a set of indices themselves. To be used a component template must be specified in the
  8. `composed_of` of an <<indices-templates,index template>>.
  9. [source,console]
  10. --------------------------------------------------
  11. PUT _component_template/template_1
  12. {
  13. "template": {
  14. "settings": {
  15. "number_of_shards": 1
  16. },
  17. "mappings": {
  18. "_source": {
  19. "enabled": false
  20. },
  21. "properties": {
  22. "host_name": {
  23. "type": "keyword"
  24. },
  25. "created_at": {
  26. "type": "date",
  27. "format": "EEE MMM dd HH:mm:ss Z yyyy"
  28. }
  29. }
  30. }
  31. }
  32. }
  33. --------------------------------------------------
  34. // TESTSETUP
  35. //////////////////////////
  36. [source,console]
  37. --------------------------------------------------
  38. DELETE _component_template/template_*
  39. --------------------------------------------------
  40. // TEARDOWN
  41. //////////////////////////
  42. [[put-component-template-api-request]]
  43. ==== {api-request-title}
  44. `PUT /_component_template/<component-template>`
  45. [[put-component-template-api-desc]]
  46. ==== {api-description-title}
  47. Use the PUT component template API to create or update a component template.
  48. // tag::component-template-def[]
  49. Component templates define <<index-modules-settings,settings>>, <<mapping,mappings>>, and
  50. <<indices-aliases,aliases>> that you can automatically apply when creating new data streams and indices. On their own
  51. component templates are not matched or applied to a data stream or index, instead they are used in the
  52. `composed_of` definition of an <<indices-templates,index template>>.
  53. // end::component-template-def[]
  54. Component templates are only used during index creation. For data streams, this
  55. includes data stream creation and the creation of a stream's backing indices.
  56. Changes to component templates do not
  57. affect existing indices, including a stream's backing indices.
  58. Settings and mappings specified in <<indices-create-index, create index>>
  59. API requests and <<indices-templates,index templates>> override any settings or mappings specified
  60. in a component template.
  61. ===== Comments in component templates
  62. You can use C-style /* */ block comments in component templates.
  63. You can include comments anywhere in the request body,
  64. except before the opening curly bracket.
  65. [[put-component-template-api-path-params]]
  66. ==== {api-path-parms-title}
  67. `<component-template>`::
  68. (Required, string)
  69. Name of the component template to create.
  70. [[put-component-template-api-query-params]]
  71. ==== {api-query-parms-title}
  72. `create`::
  73. (Optional, boolean)
  74. If `true`, this request cannot replace or update existing component templates.
  75. Defaults to `false`.
  76. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=master-timeout]
  77. [[put-component-template-api-request-body]]
  78. ==== {api-request-body-title}
  79. `template`::
  80. (Required, object)
  81. This is the template to be applied, may optionally include a `mappings`,
  82. `settings`, or `aliases` configuration.
  83. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=aliases]
  84. +
  85. NOTE: You cannot add data streams to an index alias.
  86. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=mappings]
  87. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=settings]
  88. `version`::
  89. (Optional, integer)
  90. Version number used to manage component templates externally.
  91. This number is not automatically generated or incremented by {es}.
  92. `_meta`::
  93. (Optional, object)
  94. Optional user metadata about the component template. May have any contents.
  95. This map is not automatically generated by {es}.
  96. [[put-component-template-api-example]]
  97. ==== {api-examples-title}
  98. ===== Component template with index aliases
  99. You can include <<indices-aliases,index aliases>> in a component template.
  100. [source,console]
  101. --------------------------------------------------
  102. PUT _component_template/template_1
  103. {
  104. "template": {
  105. "settings" : {
  106. "number_of_shards" : 1
  107. },
  108. "aliases" : {
  109. "alias1" : {},
  110. "alias2" : {
  111. "filter" : {
  112. "term" : {"user.id" : "kimchy" }
  113. },
  114. "routing" : "shard-1"
  115. },
  116. "{index}-alias" : {} <1>
  117. }
  118. }
  119. }
  120. --------------------------------------------------
  121. <1> the `{index}` placeholder in the alias name will be replaced with the
  122. actual index name that the template gets applied to, during index creation.
  123. [[applying-component-templates]]
  124. ===== Applying component templates
  125. Component templates on their own don't apply or interact with data streams or indices at all.
  126. To take effect, they must be used as part of an index template's `composed_of` field. See
  127. the <<indices-templates,index templates>>.
  128. [[component-templates-version]]
  129. ===== Component template versioning
  130. You can use the `version` parameter to add an optional version number to a component template. External
  131. systems can use these version numbers to simplify template management.
  132. The `version` parameter is completely optional and not automatically generated by {es}.
  133. To unset a `version`, replace the template without specifying one.
  134. [source,console]
  135. --------------------------------------------------
  136. PUT /_component_template/template_1
  137. {
  138. "template": {
  139. "settings" : {
  140. "number_of_shards" : 1
  141. }
  142. },
  143. "version": 123
  144. }
  145. --------------------------------------------------
  146. To check the `version`, you can use the <<getting-component-templates,get component template>> API.
  147. [[component-templates-metadata]]
  148. ===== Component template metadata
  149. You can use the `_meta` parameter to add optional metadata to a component template. This is a
  150. user-defined map that can contain any data. This data will be stored in the cluster state however,
  151. so keeping it short is preferrable.
  152. The `_meta` parameter is completely optional and not automatically generated by {es}.
  153. To unset `_meta`, replace the template without specifying one.
  154. [source,console]
  155. --------------------------------------------------
  156. PUT /_component_template/template_1
  157. {
  158. "template": {
  159. "settings" : {
  160. "number_of_shards" : 1
  161. }
  162. },
  163. "_meta": {
  164. "description": "set number of shards to one",
  165. "serialization": {
  166. "class": "MyComponentTemplate",
  167. "id": 10
  168. }
  169. }
  170. }
  171. --------------------------------------------------
  172. To check the `_meta`, you can use the <<getting-component-templates,get component template>> API.
  173. [[getting-component-templates]]
  174. === Get component template API
  175. ++++
  176. <titleabbrev>Get component template</titleabbrev>
  177. ++++
  178. Returns information about one or more component templates.
  179. [source,console]
  180. --------------------------------------------------
  181. GET /_component_template/template_1
  182. --------------------------------------------------
  183. [[get-component-template-api-request]]
  184. ==== {api-request-title}
  185. `GET /_component-template/<component-template>`
  186. [[get-component-template-api-path-params]]
  187. ==== {api-path-parms-title}
  188. `<component-template>`
  189. (Required, string)
  190. Comma-separated list of component template names used to limit the request.
  191. Wildcard (`*`) expressions are supported.
  192. [[get-component-template-api-query-params]]
  193. ==== {api-query-parms-title}
  194. include::{docdir}/rest-api/common-parms.asciidoc[tag=flat-settings]
  195. include::{docdir}/rest-api/common-parms.asciidoc[tag=local]
  196. include::{docdir}/rest-api/common-parms.asciidoc[tag=master-timeout]
  197. [[get-component-template-api-example]]
  198. ==== {api-examples-title}
  199. [[get-component-template-api-wildcard-ex]]
  200. ===== Get component templates using a wildcard expression
  201. [source,console]
  202. --------------------------------------------------
  203. GET /_component_template/temp*
  204. --------------------------------------------------
  205. [[get-component-template-api-all-ex]]
  206. ===== Get all component templates
  207. [source,console]
  208. --------------------------------------------------
  209. GET /_component_template
  210. --------------------------------------------------