put-component-template.asciidoc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. [[indices-component-template]]
  2. === Create or update component template API
  3. ++++
  4. <titleabbrev>Create or update 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-prereqs]]
  47. ==== {api-prereq-title}
  48. * If the {es} {security-features} are enabled, you must have the
  49. `manage_index_templates` or `manage` <<privileges-list-cluster,cluster
  50. privilege>> to use this API.
  51. [[put-component-template-api-desc]]
  52. ==== {api-description-title}
  53. An index template can be composed of multiple component templates.
  54. To use a component template, specify it in an index template's `composed_of` list.
  55. Component templates are only applied to new data streams and indices
  56. as part of a matching index template.
  57. Settings and mappings specified directly in the index template or the <<indices-create-index, create index>>
  58. request override any settings or mappings specified in a component template.
  59. Component templates are only used during index creation. For data streams, this
  60. includes data stream creation and the creation of a stream's backing indices.
  61. Changes to component templates do not
  62. affect existing indices, including a stream's backing indices.
  63. ===== Comments in component templates
  64. You can use C-style /* */ block comments in component templates.
  65. You can include comments anywhere in the request body,
  66. except before the opening curly bracket.
  67. [[put-component-template-api-path-params]]
  68. ==== {api-path-parms-title}
  69. `<component-template>`::
  70. (Required, string)
  71. Name of the component template to create.
  72. +
  73. [IMPORTANT]
  74. ====
  75. {es} includes the following built-in component templates:
  76. // tag::built-in-component-templates[]
  77. - `logs-mappings`
  78. - `logs-settings`
  79. - `metrics-mappings`
  80. - `metrics-settings`
  81. - `synthetics-mapping`
  82. - `synthetics-settings`
  83. // end::built-in-component-templates[]
  84. {fleet-guide}/fleet-overview.html[{agent}] uses these templates to configure
  85. backing indices for its data streams. If you use {agent} and want to overwrite
  86. one of these templates, set the `version` for your replacement template higher
  87. than the current version.
  88. If you don't use {agent} and want to disable all built-in component and index
  89. templates, set <<stack-templates-enabled,`stack.templates.enabled`>> to `false`
  90. using the <<cluster-update-settings,cluster update settings API>>.
  91. ====
  92. [[put-component-template-api-query-params]]
  93. ==== {api-query-parms-title}
  94. `create`::
  95. (Optional, Boolean)
  96. If `true`, this request cannot replace or update existing component templates.
  97. Defaults to `false`.
  98. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=master-timeout]
  99. [[put-component-template-api-request-body]]
  100. ==== {api-request-body-title}
  101. `template`::
  102. (Required, object)
  103. This is the template to be applied, may optionally include a `mappings`,
  104. `settings`, or `aliases` configuration.
  105. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=aliases]
  106. +
  107. NOTE: You cannot add data streams to an index alias.
  108. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=mappings]
  109. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=settings]
  110. `version`::
  111. (Optional, integer)
  112. Version number used to manage component templates externally.
  113. This number is not automatically generated or incremented by {es}.
  114. `allow_auto_create`::
  115. (Optional, Boolean)
  116. This setting overrides the value of the
  117. <<index-creation,`action.auto_create_index`>> cluster setting. If set to
  118. `true` in a template, then indices can be automatically created using that
  119. template even if auto-creation of indices is disabled via
  120. `actions.auto_create_index`. If set to `false`, then indices or data streams matching the
  121. template must always be explicitly created, and may never be automatically
  122. created.
  123. `_meta`::
  124. (Optional, object)
  125. Optional user metadata about the component template. May have any contents.
  126. This map is not automatically generated by {es}.
  127. [[put-component-template-api-example]]
  128. ==== {api-examples-title}
  129. ===== Component template with index aliases
  130. You can include <<indices-aliases,index aliases>> in a component template.
  131. [source,console]
  132. --------------------------------------------------
  133. PUT _component_template/template_1
  134. {
  135. "template": {
  136. "settings" : {
  137. "number_of_shards" : 1
  138. },
  139. "aliases" : {
  140. "alias1" : {},
  141. "alias2" : {
  142. "filter" : {
  143. "term" : {"user.id" : "kimchy" }
  144. },
  145. "routing" : "shard-1"
  146. },
  147. "{index}-alias" : {} <1>
  148. }
  149. }
  150. }
  151. --------------------------------------------------
  152. <1> the `{index}` placeholder in the alias name will be replaced with the
  153. actual index name that the template gets applied to, during index creation.
  154. [[applying-component-templates]]
  155. ===== Applying component templates
  156. You cannot directly apply a component template to a data stream or index.
  157. To be applied, a component template must be included in an index template's `composed_of` list. See <<index-templates>>.
  158. [[component-templates-version]]
  159. ===== Component template versioning
  160. You can use the `version` parameter to add a version number to a component template.
  161. External systems can use these version numbers to simplify template management.
  162. The `version` parameter is optional and not automatically generated or used by {es}.
  163. To unset a `version`, replace the template without specifying one.
  164. [source,console]
  165. --------------------------------------------------
  166. PUT /_component_template/template_1
  167. {
  168. "template": {
  169. "settings" : {
  170. "number_of_shards" : 1
  171. }
  172. },
  173. "version": 123
  174. }
  175. --------------------------------------------------
  176. To check the `version`, you can use the <<getting-component-templates,get component template API>>.
  177. [[component-templates-metadata]]
  178. ===== Component template metadata
  179. You can use the `_meta` parameter to add arbitrary metadata to a component template.
  180. This user-defined object is stored in the cluster state,
  181. so keeping it short is preferable.
  182. The `_meta` parameter is optional and not automatically generated or used by {es}.
  183. To unset `_meta`, replace the template without specifying one.
  184. [source,console]
  185. --------------------------------------------------
  186. PUT /_component_template/template_1
  187. {
  188. "template": {
  189. "settings" : {
  190. "number_of_shards" : 1
  191. }
  192. },
  193. "_meta": {
  194. "description": "set number of shards to one",
  195. "serialization": {
  196. "class": "MyComponentTemplate",
  197. "id": 10
  198. }
  199. }
  200. }
  201. --------------------------------------------------
  202. To check the `_meta`, you can use the <<getting-component-templates,get component template>> API.