index-templates.asciidoc 16 KB

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