component-templates.asciidoc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. [[indices-component-template]]
  2. === Put component template API
  3. ++++
  4. <titleabbrev>Put component template</titleabbrev>
  5. ++++
  6. Component templates are building blocks that specify mappings, settings, or alias configuration, but
  7. don't apply to a set of indices themselves. To be used a component template must be specified in the
  8. `composed_of` of an <<indices-templates,index template>>.
  9. [source,console]
  10. --------------------------------------------------
  11. PUT _component_template/template_1
  12. {
  13. "template": {
  14. "settings": {
  15. "number_of_shards": 1
  16. },
  17. "mappings": {
  18. "_source": {
  19. "enabled": false
  20. },
  21. "properties": {
  22. "host_name": {
  23. "type": "keyword"
  24. },
  25. "created_at": {
  26. "type": "date",
  27. "format": "EEE MMM dd HH:mm:ss Z yyyy"
  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-desc]]
  46. ==== {api-description-title}
  47. Use the PUT component template API to create or update a component template.
  48. // tag::component-template-def[]
  49. Component templates define <<index-modules-settings,settings>>, <<mapping,mappings>>, and
  50. <<indices-aliases,aliases>> that you can automatically apply when creating new indices. On their own
  51. component templates are not matched or applied to an index, instead they are used in the
  52. `composed_of` definition of an <<indices-templates,index template>> to make up the final index
  53. configuration.
  54. // end::component-template-def[]
  55. Component templates are only used during index creation. Changes to component templates do not
  56. affect existing indices. Settings and mappings specified in <<indices-create-index, create index>>
  57. API requests and <<indices-templates,index templates>> override any settings or mappings specified
  58. in a component template.
  59. ===== Comments in component templates
  60. You can use C-style /* */ block comments in component templates.
  61. You can include comments anywhere in the request body,
  62. except before the opening curly bracket.
  63. [[put-component-template-api-path-params]]
  64. ==== {api-path-parms-title}
  65. `<component-template>`::
  66. (Required, string)
  67. Name of the component template to create.
  68. [[put-component-template-api-query-params]]
  69. ==== {api-query-parms-title}
  70. `create`::
  71. (Optional, boolean)
  72. If `true`, this request cannot replace or update existing component templates.
  73. Defaults to `false`.
  74. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=master-timeout]
  75. [[put-component-template-api-request-body]]
  76. ==== {api-request-body-title}
  77. `template`::
  78. (Required, object)
  79. This is the template to be applied, may optionally include a `mappings`,
  80. `settings`, or `aliases` configuration.
  81. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=aliases]
  82. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=mappings]
  83. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=settings]
  84. `version`::
  85. (Optional, integer)
  86. Version number used to manage component templates externally.
  87. This number is not automatically generated or incremented by {es}.
  88. `_meta`::
  89. (Optional, object)
  90. Optional user metadata about the component template. May have any contents.
  91. This map is not automatically generated by {es}.
  92. [[put-component-template-api-example]]
  93. ==== {api-examples-title}
  94. ===== Component template with index aliases
  95. You can include <<indices-aliases,index aliases>> in a component template.
  96. [source,console]
  97. --------------------------------------------------
  98. PUT _component_template/template_1
  99. {
  100. "template": {
  101. "settings" : {
  102. "number_of_shards" : 1
  103. },
  104. "aliases" : {
  105. "alias1" : {},
  106. "alias2" : {
  107. "filter" : {
  108. "term" : {"user" : "kimchy" }
  109. },
  110. "routing" : "kimchy"
  111. },
  112. "{index}-alias" : {} <1>
  113. }
  114. }
  115. }
  116. --------------------------------------------------
  117. <1> the `{index}` placeholder in the alias name will be replaced with the
  118. actual index name that the template gets applied to, during index creation.
  119. [[applying-component-templates]]
  120. ===== Applying component templates
  121. Component templates on their own don't apply or interact with indices at all. In order for them to
  122. be effective, they must be used as part of index template's `composed_of` field to take effect. See
  123. the <<indices-templates,index templates>> documentation for more information.
  124. [[component-templates-version]]
  125. ===== Component template versioning
  126. You can use the `version` parameter to add an optional version number to a component template. External
  127. systems can use these version numbers to simplify template management.
  128. The `version` parameter is completely optional and not automatically generated 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 optional metadata to a component template. This is a
  146. user-defined map that can contain any data. This data will be stored in the cluster state however,
  147. so keeping it short is preferrable.
  148. The `_meta` parameter is completely optional and not automatically generated 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.
  169. [[getting-component-templates]]
  170. === Get component template API
  171. ++++
  172. <titleabbrev>Get component template</titleabbrev>
  173. ++++
  174. Returns information about one or more component templates.
  175. [source,console]
  176. --------------------------------------------------
  177. GET /_component_template/template_1
  178. --------------------------------------------------
  179. [[get-component-template-api-request]]
  180. ==== {api-request-title}
  181. `GET /_component-template/<component-template>`
  182. [[get-component-template-api-path-params]]
  183. ==== {api-path-parms-title}
  184. include::{docdir}/rest-api/common-parms.asciidoc[tag=index-template]
  185. +
  186. To return all component templates, omit this parameter or use a value of `*`.
  187. [[get-component-template-api-query-params]]
  188. ==== {api-query-parms-title}
  189. include::{docdir}/rest-api/common-parms.asciidoc[tag=flat-settings]
  190. include::{docdir}/rest-api/common-parms.asciidoc[tag=local]
  191. include::{docdir}/rest-api/common-parms.asciidoc[tag=master-timeout]
  192. [[get-component-template-api-example]]
  193. ==== {api-examples-title}
  194. [[get-component-template-api-wildcard-ex]]
  195. ===== Get component templates using a wildcard expression
  196. [source,console]
  197. --------------------------------------------------
  198. GET /_component_template/temp*
  199. --------------------------------------------------
  200. [[get-component-template-api-all-ex]]
  201. ===== Get all component templates
  202. [source,console]
  203. --------------------------------------------------
  204. GET /_component_template
  205. --------------------------------------------------