put-index-template.asciidoc 11 KB

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