put-index-template.asciidoc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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.
  7. Index templates define <<index-modules-settings,settings>>, <<mapping,mappings>>,
  8. and <<indices-aliases,aliases>> 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: `data_stream: { }`
  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)
  84. If `true`, the data stream is <<hidden-indices,hidden>>. Defaults to `false`.
  85. ====
  86. `index_patterns`::
  87. (Required, array of strings)
  88. Array of wildcard (`*`) expressions
  89. used to match the names of data streams and indices during creation.
  90. +
  91. {es} includes several built-in index templates. To avoid naming collisions with
  92. these templates, see <<avoid-index-pattern-collisions>>.
  93. `_meta`::
  94. (Optional, object)
  95. Optional user metadata about the index template. May have any contents.
  96. This map is not automatically generated by {es}.
  97. `priority`::
  98. (Optional, integer)
  99. Priority to determine index template precedence when a new data stream or index is created. The index template with
  100. the highest priority is chosen. If no priority is specified the template is treated as though it is
  101. of priority 0 (lowest priority).
  102. This number is not automatically generated by {es}.
  103. `template`::
  104. (Optional, object)
  105. Template to be applied. It may optionally include an `aliases`, `mappings`, or
  106. `settings` configuration.
  107. +
  108. .Properties of `template`
  109. [%collapsible%open]
  110. ====
  111. include::{docdir}/rest-api/common-parms.asciidoc[tag=aliases]
  112. +
  113. NOTE: You cannot add data streams to an index alias.
  114. include::{docdir}/rest-api/common-parms.asciidoc[tag=mappings]
  115. include::{docdir}/rest-api/common-parms.asciidoc[tag=settings]
  116. ====
  117. `version`::
  118. (Optional, integer)
  119. Version number used to manage index templates externally.
  120. This number is not automatically generated by {es}.
  121. // end::index-template-api-body[]
  122. [[put-index-template-api-example]]
  123. ==== {api-examples-title}
  124. ===== Index template with index aliases
  125. You can include <<indices-aliases,index aliases>> in an index template.
  126. [source,console]
  127. --------------------------------------------------
  128. PUT _index_template/template_1
  129. {
  130. "index_patterns" : ["te*"],
  131. "template": {
  132. "settings" : {
  133. "number_of_shards" : 1
  134. },
  135. "aliases" : {
  136. "alias1" : {},
  137. "alias2" : {
  138. "filter" : {
  139. "term" : {"user.id" : "kimchy" }
  140. },
  141. "routing" : "shard-1"
  142. },
  143. "{index}-alias" : {} <1>
  144. }
  145. }
  146. }
  147. --------------------------------------------------
  148. <1> the `{index}` placeholder in the alias name will be replaced with the
  149. actual index name that the template gets applied to, during index creation.
  150. [[multiple-templates]]
  151. ===== Multiple matching templates
  152. If multiple index templates match the name of a new index or data stream,
  153. the template with the highest priority is used. For example:
  154. [source,console]
  155. --------------------------------------------------
  156. PUT /_index_template/template_1
  157. {
  158. "index_patterns" : ["t*"],
  159. "priority" : 0,
  160. "template": {
  161. "settings" : {
  162. "number_of_shards" : 1,
  163. "number_of_replicas": 0
  164. },
  165. "mappings" : {
  166. "_source" : { "enabled" : false }
  167. }
  168. }
  169. }
  170. PUT /_index_template/template_2
  171. {
  172. "index_patterns" : ["te*"],
  173. "priority" : 1,
  174. "template": {
  175. "settings" : {
  176. "number_of_shards" : 2
  177. },
  178. "mappings" : {
  179. "_source" : { "enabled" : true }
  180. }
  181. }
  182. }
  183. --------------------------------------------------
  184. For indices that start with `te*`, `_source` will enabled, and the index will have two primary
  185. shards and one replica, because only `template_2` will be applied.
  186. NOTE: Multiple templates with overlapping index patterns at the same priority are not allowed, and
  187. an error will be thrown when attempting to create a template matching an existing index template at
  188. identical priorities.
  189. [[versioning-templates]]
  190. ===== Template versioning
  191. You can use the `version` parameter to add a version number to an index template.
  192. External systems can use these version numbers to simplify template management.
  193. The `version` parameter is optional and not automatically generated or used by {es}.
  194. To unset a `version`, replace the template without specifying one.
  195. [source,console]
  196. --------------------------------------------------
  197. PUT /_index_template/template_1
  198. {
  199. "index_patterns" : ["foo", "bar"],
  200. "priority" : 0,
  201. "template": {
  202. "settings" : {
  203. "number_of_shards" : 1
  204. }
  205. },
  206. "version": 123
  207. }
  208. --------------------------------------------------
  209. To check the `version`, you can use the <<indices-get-template, get index template>> API.
  210. [[template-metadata]]
  211. ===== Template metadata
  212. You can use the `_meta` parameter to add arbitrary metadata to an index template.
  213. This user-defined object is stored in the cluster state,
  214. so keeping it short is preferable.
  215. The `_meta` parameter is optional and not automatically generated or used by {es}.
  216. To unset `_meta`, replace the template without specifying one.
  217. [source,console]
  218. --------------------------------------------------
  219. PUT /_index_template/template_1
  220. {
  221. "index_patterns": ["foo", "bar"],
  222. "template": {
  223. "settings" : {
  224. "number_of_shards" : 3
  225. }
  226. },
  227. "_meta": {
  228. "description": "set number of shards to three",
  229. "serialization": {
  230. "class": "MyIndexTemplate",
  231. "id": 17
  232. }
  233. }
  234. }
  235. --------------------------------------------------
  236. To check the `_meta`, you can use the <<indices-get-template, get index template>> API.
  237. [[data-stream-definition]]
  238. ===== Data stream definition
  239. To use an index template for a data stream, the template must include an empty `data_stream` object.
  240. Data stream templates are only used for a stream's backing indices,
  241. they are not applied to regular indices.
  242. See <<create-index-template,create an index template>>.
  243. [source,console]
  244. --------------------------------------------------
  245. PUT /_index_template/template_1
  246. {
  247. "index_patterns": ["logs-*"],
  248. "data_stream": { }
  249. }
  250. --------------------------------------------------
  251. [[multiple-component-templates]]
  252. ===== Composing aliases, mappings, and settings
  253. When multiple component templates are specified in the `composed_of` field for an index template,
  254. they are merged in the order specified, meaning that later component templates override earlier
  255. component templates. Any mappings, settings, or aliases from the parent index template are merged
  256. in next. Finally, any configuration on the index request itself is merged.
  257. In this example, the order of the two component templates changes the number of shards for an
  258. index:
  259. [source,console]
  260. --------------------------------------------------
  261. PUT /_component_template/template_with_2_shards
  262. {
  263. "template": {
  264. "settings": {
  265. "index.number_of_shards": 2
  266. }
  267. }
  268. }
  269. PUT /_component_template/template_with_3_shards
  270. {
  271. "template": {
  272. "settings": {
  273. "index.number_of_shards": 3
  274. }
  275. }
  276. }
  277. PUT /_index_template/template_1
  278. {
  279. "index_patterns": ["t*"],
  280. "composed_of": ["template_with_2_shards", "template_with_3_shards"]
  281. }
  282. --------------------------------------------------
  283. In this case, an index matching `t*` will have three primary shards. If the order of composed
  284. templates were reversed, the index would have two primary shards.
  285. Mapping definitions are merged recursively, which means that later mapping components can
  286. introduce new field mappings and update the mapping configuration. If a field mapping is
  287. already contained in an earlier component, its definition will be completely overwritten
  288. by the later one.
  289. This recursive merging strategy applies not only to field mappings, but also root options like
  290. `dynamic_templates` and `meta`. If an earlier component contains a `dynamic_templates` block,
  291. then by default new `dynamic_templates` entries are appended onto the end. If an entry already
  292. exists with the same key, then it is overwritten by the new definition.