put-component-template.asciidoc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. [[indices-component-template]]
  2. === Put component template API
  3. ++++
  4. <titleabbrev>Put component template</titleabbrev>
  5. ++++
  6. Creates or updates a component template.
  7. Component templates are building blocks for constructing <<indices-templates,index templates>>.
  8. that specify index <<mapping,mappings>>, <<index-modules-settings,settings>>,
  9. and <<indices-aliases,aliases>>.
  10. [source,console]
  11. --------------------------------------------------
  12. PUT _component_template/template_1
  13. {
  14. "template": {
  15. "settings": {
  16. "number_of_shards": 1
  17. },
  18. "mappings": {
  19. "_source": {
  20. "enabled": false
  21. },
  22. "properties": {
  23. "host_name": {
  24. "type": "keyword"
  25. },
  26. "created_at": {
  27. "type": "date",
  28. "format": "EEE MMM dd HH:mm:ss Z yyyy"
  29. }
  30. }
  31. }
  32. }
  33. }
  34. --------------------------------------------------
  35. // TESTSETUP
  36. //////////////////////////
  37. [source,console]
  38. --------------------------------------------------
  39. DELETE _component_template/template_*
  40. --------------------------------------------------
  41. // TEARDOWN
  42. //////////////////////////
  43. [[put-component-template-api-request]]
  44. ==== {api-request-title}
  45. `PUT /_component_template/<component-template>`
  46. [[put-component-template-api-desc]]
  47. ==== {api-description-title}
  48. An index template can be composed of multiple component templates.
  49. To use a component template, specify it in an index template's `composed_of` list.
  50. Component templates are only applied to new data streams and indices
  51. as part of a matching index template.
  52. Settings and mappings specified directly in the index template or the <<indices-create-index, create index>>
  53. request override any settings or mappings specified in a component template.
  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. ===== Comments in component templates
  59. You can use C-style /* */ block comments in component templates.
  60. You can include comments anywhere in the request body,
  61. except before the opening curly bracket.
  62. [[put-component-template-api-path-params]]
  63. ==== {api-path-parms-title}
  64. `<component-template>`::
  65. (Required, string)
  66. Name of the component template to create.
  67. [[put-component-template-api-query-params]]
  68. ==== {api-query-parms-title}
  69. `create`::
  70. (Optional, boolean)
  71. If `true`, this request cannot replace or update existing component templates.
  72. Defaults to `false`.
  73. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=master-timeout]
  74. [[put-component-template-api-request-body]]
  75. ==== {api-request-body-title}
  76. `template`::
  77. (Required, object)
  78. This is the template to be applied, may optionally include a `mappings`,
  79. `settings`, or `aliases` configuration.
  80. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=aliases]
  81. +
  82. NOTE: You cannot add data streams to an index alias.
  83. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=mappings]
  84. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=settings]
  85. `version`::
  86. (Optional, integer)
  87. Version number used to manage component templates externally.
  88. This number is not automatically generated or incremented by {es}.
  89. `_meta`::
  90. (Optional, object)
  91. Optional user metadata about the component template. May have any contents.
  92. This map is not automatically generated by {es}.
  93. [[put-component-template-api-example]]
  94. ==== {api-examples-title}
  95. ===== Component template with index aliases
  96. You can include <<indices-aliases,index aliases>> in a component template.
  97. [source,console]
  98. --------------------------------------------------
  99. PUT _component_template/template_1
  100. {
  101. "template": {
  102. "settings" : {
  103. "number_of_shards" : 1
  104. },
  105. "aliases" : {
  106. "alias1" : {},
  107. "alias2" : {
  108. "filter" : {
  109. "term" : {"user.id" : "kimchy" }
  110. },
  111. "routing" : "shard-1"
  112. },
  113. "{index}-alias" : {} <1>
  114. }
  115. }
  116. }
  117. --------------------------------------------------
  118. <1> the `{index}` placeholder in the alias name will be replaced with the
  119. actual index name that the template gets applied to, during index creation.
  120. [[applying-component-templates]]
  121. ===== Applying component templates
  122. You cannot directly apply a component template to a data stream or index.
  123. To be applied, a component template must be included in an index template's `composed_of` list. See <<indices-templates>>.
  124. [[component-templates-version]]
  125. ===== Component template versioning
  126. You can use the `version` parameter to add a version number to a component template.
  127. External systems can use these version numbers to simplify template management.
  128. The `version` parameter is optional and not automatically generated or used by {es}.
  129. To unset a `version`, replace the template without specifying one.
  130. [source,console]
  131. --------------------------------------------------
  132. PUT /_component_template/template_1
  133. {
  134. "template": {
  135. "settings" : {
  136. "number_of_shards" : 1
  137. }
  138. },
  139. "version": 123
  140. }
  141. --------------------------------------------------
  142. To check the `version`, you can use the <<getting-component-templates,get component template API>>.
  143. [[component-templates-metadata]]
  144. ===== Component template metadata
  145. You can use the `_meta` parameter to add arbitrary metadata to a component template.
  146. This user-defined object is stored in the cluster state,
  147. so keeping it short is preferrable.
  148. The `_meta` parameter is optional and not automatically generated or used by {es}.
  149. To unset `_meta`, replace the template without specifying one.
  150. [source,console]
  151. --------------------------------------------------
  152. PUT /_component_template/template_1
  153. {
  154. "template": {
  155. "settings" : {
  156. "number_of_shards" : 1
  157. }
  158. },
  159. "_meta": {
  160. "description": "set number of shards to one",
  161. "serialization": {
  162. "class": "MyComponentTemplate",
  163. "id": 10
  164. }
  165. }
  166. }
  167. --------------------------------------------------
  168. To check the `_meta`, you can use the <<getting-component-templates,get component template>> API.