put-component-template.asciidoc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. <<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. }
  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-prereqs]]
  46. ==== {api-prereq-title}
  47. * If the {es} {security-features} are enabled, you must have the
  48. `manage_index_templates` or `manage` <<privileges-list-cluster,cluster
  49. privilege>> to use this API.
  50. [[put-component-template-api-desc]]
  51. ==== {api-description-title}
  52. An index template can be composed of multiple component templates.
  53. To use a component template, specify it in an index template's `composed_of` list.
  54. Component templates are only applied to new data streams and indices
  55. as part of a matching index template.
  56. Settings and mappings specified directly in the index template or the <<indices-create-index, create index>>
  57. request override any settings or mappings specified in a component template.
  58. Component templates are only used during index creation. For data streams, this
  59. includes data stream creation and the creation of a stream's backing indices.
  60. Changes to component templates do not
  61. affect existing indices, including a stream's backing indices.
  62. ===== Comments in component templates
  63. You can use C-style /* */ block comments in component templates.
  64. You can include comments anywhere in the request body,
  65. except before the opening curly bracket.
  66. [[put-component-template-api-path-params]]
  67. ==== {api-path-parms-title}
  68. `<component-template>`::
  69. (Required, string)
  70. Name of the component template to create.
  71. +
  72. [IMPORTANT]
  73. ====
  74. {es} includes the following built-in component templates:
  75. // tag::built-in-component-templates[]
  76. - `logs@mappings`
  77. - `logs@settings`
  78. - `metrics@mappings`
  79. - `metrics@settings`
  80. - `metrics@tsdb-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.
  86. If you want to customize these templates, don't override them as they may be reset after an update.
  87. Instead, look for a `*@custom` component template in the `composed_of` section of the managed index template.
  88. These custom component templates allow you to customize the mappings of managed index templates,
  89. without having to override managed index templates or component templates.
  90. Note that the custom component templates may not exist yet.
  91. After you create them using the <<indices-component-template>>, they'll be picked up by the index template.
  92. See <<data-streams-change-mappings-and-settings>> on how to apply the changes to the corresponding data stream.
  93. To avoid naming collisions with built-in and Fleet-managed component templates,
  94. avoid using `@` as part of your own component template names.
  95. The exception of that rule are the `*@custom` component templates that let you safely customize managed index templates.
  96. ====
  97. [[put-component-template-api-query-params]]
  98. ==== {api-query-parms-title}
  99. `create`::
  100. (Optional, Boolean)
  101. If `true`, this request cannot replace or update existing component templates.
  102. Defaults to `false`.
  103. include::{es-ref-dir}/rest-api/common-parms.asciidoc[tag=master-timeout]
  104. [role="child_attributes"]
  105. [[put-component-template-api-request-body]]
  106. ==== {api-request-body-title}
  107. `template`::
  108. (Required, object)
  109. This is the template to be applied, may optionally include a `mappings`,
  110. `settings`, or `aliases` configuration.
  111. +
  112. .Properties of `template`
  113. [%collapsible%open]
  114. ====
  115. `aliases`::
  116. (Optional, object of objects) Aliases to add.
  117. +
  118. include::{es-ref-dir}/indices/put-index-template.asciidoc[tag=template-ds-alias]
  119. +
  120. include::{es-ref-dir}/indices/create-index.asciidoc[tag=aliases-props]
  121. include::{es-ref-dir}/rest-api/common-parms.asciidoc[tag=mappings]
  122. include::{es-ref-dir}/rest-api/common-parms.asciidoc[tag=settings]
  123. ====
  124. `version`::
  125. (Optional, integer)
  126. Version number used to manage component templates externally.
  127. This number is not automatically generated or incremented by {es}.
  128. `allow_auto_create`::
  129. (Optional, Boolean)
  130. This setting overrides the value of the
  131. <<index-creation,`action.auto_create_index`>> cluster setting. If set to
  132. `true` in a template, then indices can be automatically created using that
  133. template even if auto-creation of indices is disabled via
  134. `actions.auto_create_index`. If set to `false`, then indices or data streams matching the
  135. template must always be explicitly created, and may never be automatically
  136. created.
  137. `_meta`::
  138. (Optional, object)
  139. Optional user metadata about the component template. May have any contents.
  140. This map is not automatically generated by {es}.
  141. `deprecated`::
  142. (Optional, boolean)
  143. Marks this component template as deprecated.
  144. When a deprecated component template is referenced when creating or updating a non-deprecated index template,
  145. {es} will emit a deprecation warning.
  146. [[put-component-template-api-example]]
  147. ==== {api-examples-title}
  148. ===== Component template with index aliases
  149. You can include <<aliases,index aliases>> in a component template.
  150. [source,console]
  151. --------------------------------------------------
  152. PUT _component_template/template_1
  153. {
  154. "template": {
  155. "settings" : {
  156. "number_of_shards" : 1
  157. },
  158. "aliases" : {
  159. "alias1" : {},
  160. "alias2" : {
  161. "filter" : {
  162. "term" : {"user.id" : "kimchy" }
  163. },
  164. "routing" : "shard-1"
  165. },
  166. "{index}-alias" : {} <1>
  167. }
  168. }
  169. }
  170. --------------------------------------------------
  171. <1> the `{index}` placeholder in the alias name will be replaced with the
  172. actual index name that the template gets applied to, during index creation.
  173. [[applying-component-templates]]
  174. ===== Applying component templates
  175. You cannot directly apply a component template to a data stream or index.
  176. To be applied, a component template must be included in an index template's `composed_of` list. See <<index-templates>>.
  177. [[component-templates-version]]
  178. ===== Component template versioning
  179. You can use the `version` parameter to add a version number to a component template.
  180. External systems can use these version numbers to simplify template management.
  181. The `version` parameter is optional and not automatically generated or used by {es}.
  182. To unset a `version`, replace the template without specifying one.
  183. [source,console]
  184. --------------------------------------------------
  185. PUT /_component_template/template_1
  186. {
  187. "template": {
  188. "settings" : {
  189. "number_of_shards" : 1
  190. }
  191. },
  192. "version": 123
  193. }
  194. --------------------------------------------------
  195. To check the `version`, you can use the <<getting-component-templates,get component template API>>.
  196. [[component-templates-metadata]]
  197. ===== Component template metadata
  198. You can use the `_meta` parameter to add arbitrary metadata to a component template.
  199. This user-defined object is stored in the cluster state,
  200. so keeping it short is preferable.
  201. The `_meta` parameter is optional and not automatically generated or used by {es}.
  202. To unset `_meta`, replace the template without specifying one.
  203. [source,console]
  204. --------------------------------------------------
  205. PUT /_component_template/template_1
  206. {
  207. "template": {
  208. "settings" : {
  209. "number_of_shards" : 1
  210. }
  211. },
  212. "_meta": {
  213. "description": "set number of shards to one",
  214. "serialization": {
  215. "class": "MyComponentTemplate",
  216. "id": 10
  217. }
  218. }
  219. }
  220. --------------------------------------------------
  221. To check the `_meta`, you can use the <<getting-component-templates,get component template>> API.