put-component-template.asciidoc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 <<index-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. `allow_auto_create`::
  90. (Optional, Boolean)
  91. This setting overrides the value of the
  92. <<index-creation,`action.auto_create_index`>> cluster setting. If set to
  93. `true` in a template, then indices can be automatically created using that
  94. template even if auto-creation of indices is disabled via
  95. `actions.auto_create_index`. If set to `false`, then indices or data streams matching the
  96. template must always be explicitly created, and may never be automatically
  97. created.
  98. `_meta`::
  99. (Optional, object)
  100. Optional user metadata about the component template. May have any contents.
  101. This map is not automatically generated by {es}.
  102. [[put-component-template-api-example]]
  103. ==== {api-examples-title}
  104. ===== Component template with index aliases
  105. You can include <<indices-aliases,index aliases>> in a component template.
  106. [source,console]
  107. --------------------------------------------------
  108. PUT _component_template/template_1
  109. {
  110. "template": {
  111. "settings" : {
  112. "number_of_shards" : 1
  113. },
  114. "aliases" : {
  115. "alias1" : {},
  116. "alias2" : {
  117. "filter" : {
  118. "term" : {"user.id" : "kimchy" }
  119. },
  120. "routing" : "shard-1"
  121. },
  122. "{index}-alias" : {} <1>
  123. }
  124. }
  125. }
  126. --------------------------------------------------
  127. <1> the `{index}` placeholder in the alias name will be replaced with the
  128. actual index name that the template gets applied to, during index creation.
  129. [[applying-component-templates]]
  130. ===== Applying component templates
  131. You cannot directly apply a component template to a data stream or index.
  132. To be applied, a component template must be included in an index template's `composed_of` list. See <<index-templates>>.
  133. [[component-templates-version]]
  134. ===== Component template versioning
  135. You can use the `version` parameter to add a version number to a component template.
  136. External systems can use these version numbers to simplify template management.
  137. The `version` parameter is optional and not automatically generated or used by {es}.
  138. To unset a `version`, replace the template without specifying one.
  139. [source,console]
  140. --------------------------------------------------
  141. PUT /_component_template/template_1
  142. {
  143. "template": {
  144. "settings" : {
  145. "number_of_shards" : 1
  146. }
  147. },
  148. "version": 123
  149. }
  150. --------------------------------------------------
  151. To check the `version`, you can use the <<getting-component-templates,get component template API>>.
  152. [[component-templates-metadata]]
  153. ===== Component template metadata
  154. You can use the `_meta` parameter to add arbitrary metadata to a component template.
  155. This user-defined object is stored in the cluster state,
  156. so keeping it short is preferrable.
  157. The `_meta` parameter is optional and not automatically generated or used by {es}.
  158. To unset `_meta`, replace the template without specifying one.
  159. [source,console]
  160. --------------------------------------------------
  161. PUT /_component_template/template_1
  162. {
  163. "template": {
  164. "settings" : {
  165. "number_of_shards" : 1
  166. }
  167. },
  168. "_meta": {
  169. "description": "set number of shards to one",
  170. "serialization": {
  171. "class": "MyComponentTemplate",
  172. "id": 10
  173. }
  174. }
  175. }
  176. --------------------------------------------------
  177. To check the `_meta`, you can use the <<getting-component-templates,get component template>> API.