put-index-template.asciidoc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. [[indices-put-template]]
  2. === Create or update index template API
  3. ++++
  4. <titleabbrev>Create or update index template</titleabbrev>
  5. ++++
  6. Creates or updates an index template. Index templates define
  7. <<index-modules-settings,settings>>, <<mapping,mappings>>, and <<aliases,aliases>>
  8. that can be applied automatically to new indices.
  9. [source,console]
  10. --------------------------------------------------
  11. PUT /_index_template/template_1
  12. {
  13. "index_patterns" : ["te*"],
  14. "priority" : 1,
  15. "template": {
  16. "settings" : {
  17. "number_of_shards" : 2
  18. }
  19. }
  20. }
  21. --------------------------------------------------
  22. // TESTSETUP
  23. //////////////////////////
  24. [source,console]
  25. --------------------------------------------------
  26. DELETE _index_template/template_*
  27. --------------------------------------------------
  28. // TEARDOWN
  29. //////////////////////////
  30. [[put-index-template-api-request]]
  31. ==== {api-request-title}
  32. `PUT /_index_template/<index-template>`
  33. [[put-index-template-api-prereqs]]
  34. ==== {api-prereq-title}
  35. * If the {es} {security-features} are enabled, you must have the
  36. `manage_index_templates` or `manage` <<privileges-list-cluster,cluster
  37. privilege>> to use this API.
  38. [[put-index-template-api-desc]]
  39. ==== {api-description-title}
  40. {es} applies templates to new indices based on an
  41. wildcard pattern that matches the index name.
  42. Index templates are applied during data stream or index creation.
  43. For data streams, these settings and mappings are applied when the stream's backing indices are created.
  44. Settings and mappings specified in a <<indices-create-index, create index>>
  45. request override any settings or mappings specified in an index template.
  46. Changes to index templates do not affect
  47. existing indices, including the existing backing indices of a data stream.
  48. ===== Comments in index templates
  49. You can use C-style /* */ block comments in index templates. You can include comments anywhere in
  50. the request body, except before the opening curly bracket.
  51. [[put-index-template-api-path-params]]
  52. ==== {api-path-parms-title}
  53. `<index-template>`::
  54. (Required, string)
  55. Name of the index template to create.
  56. [[put-index-template-api-query-params]]
  57. ==== {api-query-parms-title}
  58. `create`::
  59. (Optional, Boolean)
  60. If `true`, this request cannot replace or update existing index templates. Defaults to `false`.
  61. include::{docdir}/rest-api/common-parms.asciidoc[tag=master-timeout]
  62. [role="child_attributes"]
  63. [[put-index-template-api-request-body]]
  64. ==== {api-request-body-title}
  65. `composed_of`::
  66. (Optional, array of strings)
  67. An ordered list of component template names. Component templates are merged in the order
  68. specified, meaning that the last component template specified has the highest precedence. See
  69. <<multiple-component-templates,Composing multiple component templates>> for an example.
  70. // tag::index-template-api-body[]
  71. `data_stream`::
  72. (Optional, object)
  73. If this object is included, the template is used to create data streams and
  74. their backing indices. Supports an empty object.
  75. +
  76. Data streams require a matching index template with a `data_stream` object.
  77. See <<create-index-template,create an index template>>.
  78. +
  79. .Properties of `data_stream`
  80. [%collapsible%open]
  81. ====
  82. `hidden`::
  83. (Optional, Boolean) If `true`, the data stream is <<hidden,hidden>>. Defaults to
  84. `false`.
  85. `allow_custom_routing`::
  86. (Optional, Boolean) If `true`, the data stream supports
  87. <<mapping-routing-field,custom routing>>. Defaults to `false`.
  88. ====
  89. `index_patterns`::
  90. (Required, array of strings)
  91. Array of wildcard (`*`) expressions
  92. used to match the names of data streams and indices during creation.
  93. +
  94. {es} includes several built-in index templates. To avoid naming collisions with
  95. these templates, see <<avoid-index-pattern-collisions>>.
  96. `_meta`::
  97. (Optional, object)
  98. Optional user metadata about the index template. May have any contents.
  99. This map is not automatically generated by {es}.
  100. `priority`::
  101. (Optional, integer)
  102. Priority to determine index template precedence when a new data stream or index is created. The index template with
  103. the highest priority is chosen. If no priority is specified the template is treated as though it is
  104. of priority 0 (lowest priority).
  105. This number is not automatically generated by {es}.
  106. `template`::
  107. (Optional, object)
  108. Template to be applied. It may optionally include an `aliases`, `mappings`, or
  109. `settings` configuration.
  110. +
  111. .Properties of `template`
  112. [%collapsible%open]
  113. ====
  114. `aliases`::
  115. (Optional, object of objects) Aliases to add.
  116. +
  117. // tag::template-ds-alias[]
  118. If the index template includes a `data_stream` object, these are data stream
  119. aliases. Otherwise, these are index aliases. Data stream aliases ignore the
  120. `index_routing`, `routing`, and `search_routing` options.
  121. // end::template-ds-alias[]
  122. +
  123. include::{es-repo-dir}/indices/create-index.asciidoc[tag=aliases-props]
  124. include::{docdir}/rest-api/common-parms.asciidoc[tag=mappings]
  125. include::{docdir}/rest-api/common-parms.asciidoc[tag=settings]
  126. ====
  127. `version`::
  128. (Optional, integer)
  129. Version number used to manage index templates externally.
  130. This number is not automatically generated by {es}.
  131. // end::index-template-api-body[]
  132. [[put-index-template-api-example]]
  133. ==== {api-examples-title}
  134. ===== Index template with index aliases
  135. You can include <<aliases,index aliases>> in an index template.
  136. [source,console]
  137. --------------------------------------------------
  138. PUT _index_template/template_1
  139. {
  140. "index_patterns" : ["te*"],
  141. "template": {
  142. "settings" : {
  143. "number_of_shards" : 1
  144. },
  145. "aliases" : {
  146. "alias1" : {},
  147. "alias2" : {
  148. "filter" : {
  149. "term" : {"user.id" : "kimchy" }
  150. },
  151. "routing" : "shard-1"
  152. },
  153. "{index}-alias" : {} <1>
  154. }
  155. }
  156. }
  157. --------------------------------------------------
  158. <1> the `{index}` placeholder in the alias name will be replaced with the
  159. actual index name that the template gets applied to, during index creation.
  160. [[multiple-templates]]
  161. ===== Multiple matching templates
  162. If multiple index templates match the name of a new index or data stream,
  163. the template with the highest priority is used. For example:
  164. [source,console]
  165. --------------------------------------------------
  166. PUT /_index_template/template_1
  167. {
  168. "index_patterns" : ["t*"],
  169. "priority" : 0,
  170. "template": {
  171. "settings" : {
  172. "number_of_shards" : 1,
  173. "number_of_replicas": 0
  174. },
  175. "mappings" : {
  176. "_source" : { "enabled" : false }
  177. }
  178. }
  179. }
  180. PUT /_index_template/template_2
  181. {
  182. "index_patterns" : ["te*"],
  183. "priority" : 1,
  184. "template": {
  185. "settings" : {
  186. "number_of_shards" : 2
  187. },
  188. "mappings" : {
  189. "_source" : { "enabled" : true }
  190. }
  191. }
  192. }
  193. --------------------------------------------------
  194. For indices that start with `te*`, `_source` will enabled, and the index will have two primary
  195. shards and one replica, because only `template_2` will be applied.
  196. NOTE: Multiple templates with overlapping index patterns at the same priority are not allowed, and
  197. an error will be thrown when attempting to create a template matching an existing index template at
  198. identical priorities.
  199. [[versioning-templates]]
  200. ===== Template versioning
  201. You can use the `version` parameter to add a version number to an index template.
  202. External systems can use these version numbers to simplify template management.
  203. The `version` parameter is optional and not automatically generated or used by {es}.
  204. To unset a `version`, replace the template without specifying one.
  205. [source,console]
  206. --------------------------------------------------
  207. PUT /_index_template/template_1
  208. {
  209. "index_patterns" : ["foo", "bar"],
  210. "priority" : 0,
  211. "template": {
  212. "settings" : {
  213. "number_of_shards" : 1
  214. }
  215. },
  216. "version": 123
  217. }
  218. --------------------------------------------------
  219. To check the `version`, you can use the <<indices-get-template, get index template>> API.
  220. [[template-metadata]]
  221. ===== Template metadata
  222. You can use the `_meta` parameter to add arbitrary metadata to an index template.
  223. This user-defined object is stored in the cluster state,
  224. so keeping it short is preferable.
  225. The `_meta` parameter is optional and not automatically generated or used by {es}.
  226. To unset `_meta`, replace the template without specifying one.
  227. [source,console]
  228. --------------------------------------------------
  229. PUT /_index_template/template_1
  230. {
  231. "index_patterns": ["foo", "bar"],
  232. "template": {
  233. "settings" : {
  234. "number_of_shards" : 3
  235. }
  236. },
  237. "_meta": {
  238. "description": "set number of shards to three",
  239. "serialization": {
  240. "class": "MyIndexTemplate",
  241. "id": 17
  242. }
  243. }
  244. }
  245. --------------------------------------------------
  246. To check the `_meta`, you can use the <<indices-get-template, get index template>> API.
  247. [[data-stream-definition]]
  248. ===== Data stream definition
  249. To use an index template for data streams, the template must include a
  250. `data_stream` object. See <<create-index-template,create an index template>>.
  251. [source,console]
  252. --------------------------------------------------
  253. PUT /_index_template/template_1
  254. {
  255. "index_patterns": ["logs-*"],
  256. "data_stream": { }
  257. }
  258. --------------------------------------------------
  259. [[multiple-component-templates]]
  260. ===== Composing aliases, mappings, and settings
  261. When multiple component templates are specified in the `composed_of` field for an index template,
  262. they are merged in the order specified, meaning that later component templates override earlier
  263. component templates. Any mappings, settings, or aliases from the parent index template are merged
  264. in next. Finally, any configuration on the index request itself is merged.
  265. In this example, the order of the two component templates changes the number of shards for an
  266. index:
  267. [source,console]
  268. --------------------------------------------------
  269. PUT /_component_template/template_with_2_shards
  270. {
  271. "template": {
  272. "settings": {
  273. "index.number_of_shards": 2
  274. }
  275. }
  276. }
  277. PUT /_component_template/template_with_3_shards
  278. {
  279. "template": {
  280. "settings": {
  281. "index.number_of_shards": 3
  282. }
  283. }
  284. }
  285. PUT /_index_template/template_1
  286. {
  287. "index_patterns": ["t*"],
  288. "composed_of": ["template_with_2_shards", "template_with_3_shards"]
  289. }
  290. --------------------------------------------------
  291. In this case, an index matching `t*` will have three primary shards. If the order of composed
  292. templates were reversed, the index would have two primary shards.
  293. Mapping definitions are merged recursively, which means that later mapping components can
  294. introduce new field mappings and update the mapping configuration. If a field mapping is
  295. already contained in an earlier component, its definition will be completely overwritten
  296. by the later one.
  297. This recursive merging strategy applies not only to field mappings, but also root options like
  298. `dynamic_templates` and `meta`. If an earlier component contains a `dynamic_templates` block,
  299. then by default new `dynamic_templates` entries are appended onto the end. If an entry already
  300. exists with the same key, then it is overwritten by the new definition.