put-component-template.asciidoc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. Component templates are building blocks
  7. for constructing <<index-templates,index templates>> that specify index
  8. <<mapping,mappings>>, <<index-modules-settings,settings>>, and
  9. <<alias,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. [role="child_attributes"]
  100. [[put-component-template-api-request-body]]
  101. ==== {api-request-body-title}
  102. `template`::
  103. (Required, object)
  104. This is the template to be applied, may optionally include a `mappings`,
  105. `settings`, or `aliases` configuration.
  106. +
  107. .Properties of `template`
  108. [%collapsible%open]
  109. ====
  110. `aliases`::
  111. (Optional, object of objects) Aliases for the index. If an index template
  112. includes `data_stream`, this parameter is not supported.
  113. +
  114. include::{es-repo-dir}/indices/create-index.asciidoc[tag=aliases-props]
  115. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=mappings]
  116. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=settings]
  117. ====
  118. `version`::
  119. (Optional, integer)
  120. Version number used to manage component templates externally.
  121. This number is not automatically generated or incremented by {es}.
  122. `allow_auto_create`::
  123. (Optional, Boolean)
  124. This setting overrides the value of the
  125. <<index-creation,`action.auto_create_index`>> cluster setting. If set to
  126. `true` in a template, then indices can be automatically created using that
  127. template even if auto-creation of indices is disabled via
  128. `actions.auto_create_index`. If set to `false`, then indices or data streams matching the
  129. template must always be explicitly created, and may never be automatically
  130. created.
  131. `_meta`::
  132. (Optional, object)
  133. Optional user metadata about the component template. May have any contents.
  134. This map is not automatically generated by {es}.
  135. [[put-component-template-api-example]]
  136. ==== {api-examples-title}
  137. ===== Component template with index aliases
  138. You can include <<alias,index aliases>> in a component template.
  139. [source,console]
  140. --------------------------------------------------
  141. PUT _component_template/template_1
  142. {
  143. "template": {
  144. "settings" : {
  145. "number_of_shards" : 1
  146. },
  147. "aliases" : {
  148. "alias1" : {},
  149. "alias2" : {
  150. "filter" : {
  151. "term" : {"user.id" : "kimchy" }
  152. },
  153. "routing" : "shard-1"
  154. },
  155. "{index}-alias" : {} <1>
  156. }
  157. }
  158. }
  159. --------------------------------------------------
  160. <1> the `{index}` placeholder in the alias name will be replaced with the
  161. actual index name that the template gets applied to, during index creation.
  162. [[applying-component-templates]]
  163. ===== Applying component templates
  164. You cannot directly apply a component template to a data stream or index.
  165. To be applied, a component template must be included in an index template's `composed_of` list. See <<index-templates>>.
  166. [[component-templates-version]]
  167. ===== Component template versioning
  168. You can use the `version` parameter to add a version number to a component template.
  169. External systems can use these version numbers to simplify template management.
  170. The `version` parameter is optional and not automatically generated or used by {es}.
  171. To unset a `version`, replace the template without specifying one.
  172. [source,console]
  173. --------------------------------------------------
  174. PUT /_component_template/template_1
  175. {
  176. "template": {
  177. "settings" : {
  178. "number_of_shards" : 1
  179. }
  180. },
  181. "version": 123
  182. }
  183. --------------------------------------------------
  184. To check the `version`, you can use the <<getting-component-templates,get component template API>>.
  185. [[component-templates-metadata]]
  186. ===== Component template metadata
  187. You can use the `_meta` parameter to add arbitrary metadata to a component template.
  188. This user-defined object is stored in the cluster state,
  189. so keeping it short is preferable.
  190. The `_meta` parameter is optional and not automatically generated or used by {es}.
  191. To unset `_meta`, replace the template without specifying one.
  192. [source,console]
  193. --------------------------------------------------
  194. PUT /_component_template/template_1
  195. {
  196. "template": {
  197. "settings" : {
  198. "number_of_shards" : 1
  199. }
  200. },
  201. "_meta": {
  202. "description": "set number of shards to one",
  203. "serialization": {
  204. "class": "MyComponentTemplate",
  205. "id": 10
  206. }
  207. }
  208. }
  209. --------------------------------------------------
  210. To check the `_meta`, you can use the <<getting-component-templates,get component template>> API.