index-templates.asciidoc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. [[indices-templates]]
  2. === Index templates
  3. ++++
  4. <titleabbrev>Index template</titleabbrev>
  5. ++++
  6. This documentation is about composable templates. For legacy templates please see the
  7. <<indices-templates-v1,legacy template documentation>>.
  8. [NOTE]
  9. ====
  10. In {es} 7.8 composable templates were introduced. When a composable template matches a given index
  11. it always takes precedence over a legacy template. If no composable template matches, a legacy
  12. template may still match and be applied.
  13. ====
  14. [[getting]]
  15. An index template is a way to tell {es} how to configure an index when it is created.
  16. For data streams, the index template configures the stream's backing indices as they
  17. are created. Templates are configured prior to index creation and then when an
  18. index is created either manually or through indexing a document, the template
  19. settings are used as a basis for creating the index.
  20. There are two types of templates, index templates and <<indices-component-template,component
  21. templates>>. Component templates are reusable building blocks that configure mappings, settings, and
  22. aliases. You use component templates to construct index templates, they aren't directly applied to a
  23. set of indices. Index templates can contain a collection of component templates, as well as directly
  24. specify settings, mappings, and aliases.
  25. If a new data stream or index matches more than one index template, the index template with the highest priority is used.
  26. If an index is created with explicit settings and also matches an index template,
  27. the settings from the create index request take precedence over settings specified in the index template and its component templates.
  28. [source,console]
  29. --------------------------------------------------
  30. PUT _component_template/component_template1
  31. {
  32. "template": {
  33. "mappings": {
  34. "properties": {
  35. "@timestamp": {
  36. "type": "date"
  37. }
  38. }
  39. }
  40. }
  41. }
  42. PUT _component_template/other_component_template
  43. {
  44. "template": {
  45. "mappings": {
  46. "properties": {
  47. "ip_address": {
  48. "type": "ip"
  49. }
  50. }
  51. }
  52. }
  53. }
  54. PUT _index_template/template_1
  55. {
  56. "index_patterns": ["te*", "bar*"],
  57. "template": {
  58. "settings": {
  59. "number_of_shards": 1
  60. },
  61. "mappings": {
  62. "_source": {
  63. "enabled": false
  64. },
  65. "properties": {
  66. "host_name": {
  67. "type": "keyword"
  68. },
  69. "created_at": {
  70. "type": "date",
  71. "format": "EEE MMM dd HH:mm:ss Z yyyy"
  72. }
  73. }
  74. },
  75. "aliases": {
  76. "mydata": { }
  77. }
  78. },
  79. "priority": 10,
  80. "composed_of": ["component_template1", "other_component_template"],
  81. "version": 3,
  82. "_meta": {
  83. "description": "my custom"
  84. }
  85. }
  86. --------------------------------------------------
  87. // TESTSETUP
  88. //////////////////////////
  89. [source,console]
  90. --------------------------------------------------
  91. DELETE _index_template/*
  92. DELETE _component_template/*
  93. --------------------------------------------------
  94. // TEARDOWN
  95. //////////////////////////
  96. [[simulating-templates]]
  97. ==== Simulating template composition
  98. Since templates can be composed not only of multiple component templates, but also the index
  99. template itself, there are two simulation APIs to determine what the resulting index settings will
  100. be.
  101. To simulate the settings that would be applied to a matching index name:
  102. [source,console]
  103. --------------------------------------------------
  104. POST /_index_template/_simulate_index/myindex
  105. --------------------------------------------------
  106. To simulate the settings that would be applied from a particular template:
  107. [source,console]
  108. --------------------------------------------------
  109. POST /_index_template/_simulate/template_1
  110. POST /_index_template/_simulate
  111. {
  112. "index_patterns": ["foo"],
  113. "template": {
  114. "settings": {
  115. "number_of_replicas": 0
  116. }
  117. }
  118. }
  119. --------------------------------------------------
  120. Here's an example demonstrating simulating both an index name and template name:
  121. [source,console]
  122. --------------------------------------------------
  123. PUT /_component_template/ct1 <1>
  124. {
  125. "template": {
  126. "settings": {
  127. "index.number_of_shards": 2
  128. }
  129. }
  130. }
  131. PUT /_component_template/ct2 <2>
  132. {
  133. "template": {
  134. "settings": {
  135. "index.number_of_replicas": 0
  136. },
  137. "mappings": {
  138. "properties": {
  139. "@timestamp": {
  140. "type": "date"
  141. }
  142. }
  143. }
  144. }
  145. }
  146. PUT /_index_template/final-template <3>
  147. {
  148. "index_patterns": ["logdata-*"],
  149. "composed_of": ["ct1", "ct2"],
  150. "priority": 5
  151. }
  152. POST /_index_template/_simulate_index/logdata-2019-02-01 <4>
  153. POST /_index_template/_simulate/final-template <5>
  154. POST /_index_template/_simulate <6>
  155. {
  156. "index_patterns": ["logdata-*"],
  157. "composed_of": ["ct2"],
  158. "priority": 10,
  159. "template": {
  160. "settings": {
  161. "index.number_of_replicas": 1
  162. }
  163. }
  164. }
  165. --------------------------------------------------
  166. <1> Creating a component template (ct1) setting the number of shards to two
  167. <2> Creating another component template (ct2) setting the number of replicas to zero with mappings
  168. <3> Creating an index template called "final" template using ct1 and ct2
  169. <4> Simulate the settings that would be applied for a new index "logdata-2019-02-01"
  170. <5> Simulate the settings composed using the "final-template" index template
  171. <6> Simulate the settings composed using a custom specified template
  172. The output of the simulate API from the last example call looks like:
  173. [source,console-result]
  174. ---------------------------------------------------------
  175. {
  176. "template" : {
  177. "settings" : {
  178. "index" : {
  179. "number_of_replicas" : "1" <1>
  180. }
  181. },
  182. "mappings" : {
  183. "properties" : {
  184. "@timestamp" : { <2>
  185. "type" : "date"
  186. }
  187. }
  188. },
  189. "aliases" : { }
  190. },
  191. "overlapping" : [ <3>
  192. {
  193. "name" : "final-template",
  194. "index_patterns" : [
  195. "logdata-*"
  196. ]
  197. }
  198. ]
  199. }
  200. ---------------------------------------------------------
  201. <1> The number of replicas from the simulated template body
  202. <2> The `@timestamp` field inherited from the "ct2" component template
  203. <3> Any overlapping templates that would have matched, but have lower priority
  204. When simulating a template and specifying a template in the body of the request, the simulated
  205. template is not added to the existing templates, it is only used for the simulation.
  206. [[indices-put-template]]
  207. === Put index template API
  208. ++++
  209. <titleabbrev>Put index template</titleabbrev>
  210. ++++
  211. Creates or updates an index template.
  212. [source,console]
  213. --------------------------------------------------
  214. PUT /_index_template/template_1
  215. {
  216. "index_patterns" : ["te*"],
  217. "priority" : 1,
  218. "template": {
  219. "settings" : {
  220. "number_of_shards" : 2
  221. }
  222. }
  223. }
  224. --------------------------------------------------
  225. [[put-index-template-api-request]]
  226. ==== {api-request-title}
  227. `PUT /_index_template/<index-template>`
  228. [[put-index-template-api-desc]]
  229. ==== {api-description-title}
  230. // tag::index-template-def[]
  231. Index templates define <<index-modules-settings,settings>> and <<mapping,mappings>> that you can
  232. automatically apply when creating new indices.
  233. For data streams, these settings and mappings are applied to the stream's backing indices when created.
  234. {es} applies templates to new indices based on an
  235. index pattern that matches the index name.
  236. // end::index-template-def[]
  237. Index templates are only applied during data stream or index creation. Changes to index templates do not affect
  238. existing indices, including the existing backing indices of a data stream.
  239. Settings and mappings specified in <<indices-create-index, create index>> API
  240. requests override any settings or mappings specified in an index template.
  241. ===== Comments in index templates
  242. You can use C-style /* */ block comments in index templates. You can include comments anywhere in
  243. the request body, except before the opening curly bracket.
  244. [[put-index-template-api-path-params]]
  245. ==== {api-path-parms-title}
  246. `<index-template>`::
  247. (Required, string)
  248. Name of the index template to create.
  249. [[put-index-template-api-query-params]]
  250. ==== {api-query-parms-title}
  251. `create`::
  252. (Optional, boolean)
  253. If `true`, this request cannot replace or update existing index templates. Defaults to `false`.
  254. include::{docdir}/rest-api/common-parms.asciidoc[tag=master-timeout]
  255. [[put-index-template-api-request-body]]
  256. ==== {api-request-body-title}
  257. `index_patterns`::
  258. (Required, array of strings)
  259. Array of wildcard (`*`) expressions
  260. used to match the names of data streams and indices during creation.
  261. include::{docdir}/rest-api/common-parms.asciidoc[tag=aliases]
  262. +
  263. NOTE: You cannot add data streams to an index alias.
  264. [xpack]#`data_stream`#::
  265. (Optional, object)
  266. Indicates whether the template is used to create data streams and their backing
  267. indices. If so, use an empty object as the argument: +
  268. `data_stream: { }`.
  269. +
  270. Data streams require a matching index template with a `data_stream` object.
  271. See <<create-a-data-stream-template>>.
  272. include::{docdir}/rest-api/common-parms.asciidoc[tag=mappings]
  273. include::{docdir}/rest-api/common-parms.asciidoc[tag=settings]
  274. `template`::
  275. (Optional, object)
  276. This is the template to be applied, may optionally include a `mappings`,
  277. `settings`, or `aliases` configuration.
  278. `composed_of`::
  279. (Optional, array of strings)
  280. An ordered list of component template names. Component templates are merged in the order
  281. specified, meaning that the last component template specified has the highest precedence. See
  282. <<multiple-component-templates,Composing multiple component templates>> for an example.
  283. `priority`::
  284. (Optional, integer)
  285. Priority to determine index template precedence when a new data stream or index is created. The index template with
  286. the highest priority is chosen. If no priority is specified the template is treated as though it is
  287. of priority 0 (lowest priority).
  288. This number is not automatically generated by {es}.
  289. `version`::
  290. (Optional, integer)
  291. Version number used to manage index templates externally.
  292. This number is not automatically generated by {es}.
  293. `_meta`::
  294. (Optional, object)
  295. Optional user metadata about the index template. May have any contents.
  296. This map is not automatically generated by {es}.
  297. [[put-index-template-api-example]]
  298. ==== {api-examples-title}
  299. ===== Index template with index aliases
  300. You can include <<indices-aliases,index aliases>> in an index template.
  301. [source,console]
  302. --------------------------------------------------
  303. PUT _index_template/template_1
  304. {
  305. "index_patterns" : ["te*"],
  306. "template": {
  307. "settings" : {
  308. "number_of_shards" : 1
  309. },
  310. "aliases" : {
  311. "alias1" : {},
  312. "alias2" : {
  313. "filter" : {
  314. "term" : {"user" : "kimchy" }
  315. },
  316. "routing" : "kimchy"
  317. },
  318. "{index}-alias" : {} <1>
  319. }
  320. }
  321. }
  322. --------------------------------------------------
  323. <1> the `{index}` placeholder in the alias name will be replaced with the
  324. actual index name that the template gets applied to, during index creation.
  325. [[multiple-templates]]
  326. ===== Indices matching multiple templates
  327. When an index is created that matches multiple index templates, only the index template with the
  328. highest priority is applied. For example:
  329. [source,console]
  330. --------------------------------------------------
  331. PUT /_index_template/template_1
  332. {
  333. "index_patterns" : ["t*"],
  334. "priority" : 0,
  335. "template": {
  336. "settings" : {
  337. "number_of_shards" : 1,
  338. "number_of_replicas": 0
  339. },
  340. "mappings" : {
  341. "_source" : { "enabled" : false }
  342. }
  343. }
  344. }
  345. PUT /_index_template/template_2
  346. {
  347. "index_patterns" : ["te*"],
  348. "priority" : 1,
  349. "template": {
  350. "settings" : {
  351. "number_of_shards" : 2
  352. },
  353. "mappings" : {
  354. "_source" : { "enabled" : true }
  355. }
  356. }
  357. }
  358. --------------------------------------------------
  359. For indices that start with `te*`, `_source` will enabled, and the index will have two primary
  360. shards and one replica, because only `template_2` will be applied.
  361. NOTE: Multiple templates with overlapping index patterns at the same priority are not allowed, and
  362. an error will be thrown when attempting to create a template matching an existing index template at
  363. identical priorities.
  364. [[versioning-templates]]
  365. ===== Template versioning
  366. You can use the `version` parameter to add an optional version number to an index template. External
  367. systems can use these version numbers to simplify template management.
  368. The `version` parameter is completely optional and not automatically generated by {es}.
  369. To unset a `version`, replace the template without specifying one.
  370. [source,console]
  371. --------------------------------------------------
  372. PUT /_index_template/template_1
  373. {
  374. "index_patterns" : ["foo", "bar"],
  375. "priority" : 0,
  376. "template": {
  377. "settings" : {
  378. "number_of_shards" : 1
  379. }
  380. },
  381. "version": 123
  382. }
  383. --------------------------------------------------
  384. To check the `version`, you can use the <<indices-get-template, get index template>> API.
  385. [[template-metadata]]
  386. ===== Template metadata
  387. You can use the `_meta` parameter to add optional metadata to an index template. This is a
  388. user-defined map that can contain any data. This data will be stored in the cluster state however,
  389. so keeping it short is preferrable.
  390. The `_meta` parameter is completely optional and not automatically generated by {es}.
  391. To unset `_meta`, replace the template without specifying one.
  392. [source,console]
  393. --------------------------------------------------
  394. PUT /_index_template/template_1
  395. {
  396. "index_patterns": ["foo", "bar"],
  397. "template": {
  398. "settings" : {
  399. "number_of_shards" : 3
  400. }
  401. },
  402. "_meta": {
  403. "description": "set number of shards to three",
  404. "serialization": {
  405. "class": "MyIndexTemplate",
  406. "id": 17
  407. }
  408. }
  409. }
  410. --------------------------------------------------
  411. To check the `_meta`, you can use the <<indices-get-template, get index template>> API.
  412. [[data-stream-definition]]
  413. ===== Data stream definition
  414. If an index template should create data streams, the template must include an
  415. empty `data_stream` object. See <<create-a-data-stream-template>>.
  416. [source,console]
  417. --------------------------------------------------
  418. PUT /_index_template/template_1
  419. {
  420. "index_patterns": ["logs-*"],
  421. "data_stream": { }
  422. }
  423. --------------------------------------------------
  424. [[multiple-component-templates]]
  425. ===== Composing multiple component templates
  426. When multiple component templates are specified in the `composed_of` field for an index template,
  427. they are merged in the order specified, meaning that later component templates override earlier
  428. component templates.
  429. For two component templates, the order they are specified changes the number of shards for an index:
  430. [source,console]
  431. --------------------------------------------------
  432. PUT /_component_template/template_with_2_shards
  433. {
  434. "template": {
  435. "settings": {
  436. "index.number_of_shards": 2
  437. }
  438. }
  439. }
  440. PUT /_component_template/template_with_3_shards
  441. {
  442. "template": {
  443. "settings": {
  444. "index.number_of_shards": 3
  445. }
  446. }
  447. }
  448. PUT /_index_template/template_1
  449. {
  450. "index_patterns": ["t*"],
  451. "composed_of": ["template_with_2_shards", "template_with_3_shards"]
  452. }
  453. --------------------------------------------------
  454. In this case, an index matching `t*` will have three primary shards. If the order of composed
  455. templates were reversed, the index would have two primary shards.
  456. [[indices-get-template]]
  457. === Get index template API [[getting-templates]]
  458. ++++
  459. <titleabbrev>Get index template</titleabbrev>
  460. ++++
  461. Returns information about one or more index templates.
  462. [source,console]
  463. --------------------------------------------------
  464. GET /_index_template/template_1
  465. --------------------------------------------------
  466. [[get-template-api-request]]
  467. ==== {api-request-title}
  468. `GET /_index_template/<index-template>`
  469. [[get-template-api-path-params]]
  470. ==== {api-path-parms-title}
  471. include::{docdir}/rest-api/common-parms.asciidoc[tag=index-template]
  472. +
  473. To return all index templates, omit this parameter or use a value of `*`.
  474. [[get-template-api-query-params]]
  475. ==== {api-query-parms-title}
  476. include::{docdir}/rest-api/common-parms.asciidoc[tag=flat-settings]
  477. include::{docdir}/rest-api/common-parms.asciidoc[tag=local]
  478. include::{docdir}/rest-api/common-parms.asciidoc[tag=master-timeout]
  479. [[get-template-api-example]]
  480. ==== {api-examples-title}
  481. [[get-template-api-wildcard-ex]]
  482. ===== Get index templates using a wildcard expression
  483. [source,console]
  484. --------------------------------------------------
  485. GET /_index_template/temp*
  486. --------------------------------------------------
  487. [[get-template-api-all-ex]]
  488. ===== Get all index templates
  489. [source,console]
  490. --------------------------------------------------
  491. GET /_index_template
  492. --------------------------------------------------