templates.asciidoc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. [[indices-templates]]
  2. === Put index template API
  3. ++++
  4. <titleabbrev>Put index template</titleabbrev>
  5. ++++
  6. Creates or updates an index template.
  7. [source,js]
  8. --------------------------------------------------
  9. PUT _template/template_1
  10. {
  11. "index_patterns": ["te*", "bar*"],
  12. "settings": {
  13. "number_of_shards": 1
  14. },
  15. "mappings": {
  16. "_source": {
  17. "enabled": false
  18. },
  19. "properties": {
  20. "host_name": {
  21. "type": "keyword"
  22. },
  23. "created_at": {
  24. "type": "date",
  25. "format": "EEE MMM dd HH:mm:ss Z yyyy"
  26. }
  27. }
  28. }
  29. }
  30. --------------------------------------------------
  31. // CONSOLE
  32. // TESTSETUP
  33. [[put-index-template-api-request]]
  34. ==== {api-request-title}
  35. `PUT /_template/<index-template>`
  36. [[put-index-template-api-desc]]
  37. ==== {api-description-title}
  38. Use the PUT index template API
  39. to create or update an index template.
  40. // tag::index-template-def[]
  41. Index templates define <<index-modules-settings,settings>> and <<mapping,mappings>>
  42. that you can automatically apply when creating new indices.
  43. {es} applies templates to new indices
  44. based on an index pattern that matches the index name.
  45. // end::index-template-def[]
  46. Index templates are only applied during index creation.
  47. Changes to index templates do not affect existing indices.
  48. Settings and mappings specified in <<indices-create-index, create index>> API requests
  49. override any settings or mappings specified in an index template.
  50. ===== Comments in index templates
  51. You can use C-style /* */ block comments in index templates.
  52. You can includes comments anywhere in the request body,
  53. except before the opening curly bracket.
  54. [[getting]]
  55. ===== Getting templates
  56. See <<indices-get-template>>.
  57. [[put-index-template-api-path-params]]
  58. ==== {api-path-parms-title}
  59. `<index-template>`::
  60. (Required, string)
  61. Name of the index template to create.
  62. [[put-index-template-api-query-params]]
  63. ==== {api-query-parms-title}
  64. `create`::
  65. (Optional, boolean)
  66. If `true`, this request cannot replace or update existing index templates.
  67. Defaults to `false`.
  68. include::{docdir}/rest-api/common-parms.asciidoc[tag=flat-settings]
  69. include::{docdir}/rest-api/common-parms.asciidoc[tag=include-type-name]
  70. `order`::
  71. (Optional,integer)
  72. Order in which {es} applies this template
  73. if index matches multiple templates.
  74. +
  75. Templates with lower `order` values are merged first.
  76. Templates with higher `order` values are merged later,
  77. overriding templates with lower values.
  78. include::{docdir}/rest-api/common-parms.asciidoc[tag=timeoutparms]
  79. [[put-index-template-api-request-body]]
  80. ==== {api-request-body-title}
  81. `index_patterns`::
  82. (Required, array of strings)
  83. Array of wildcard expressions
  84. used to match the names of indices during creation.
  85. include::{docdir}/rest-api/common-parms.asciidoc[tag=aliases]
  86. include::{docdir}/rest-api/common-parms.asciidoc[tag=mappings]
  87. include::{docdir}/rest-api/common-parms.asciidoc[tag=settings]
  88. [[put-index-template-api-example]]
  89. ==== {api-examples-title}
  90. ===== Index template with index aliases
  91. You can include <<indices-aliases,index aliases>> in an index template.
  92. [source,js]
  93. --------------------------------------------------
  94. PUT _template/template_1
  95. {
  96. "index_patterns" : ["te*"],
  97. "settings" : {
  98. "number_of_shards" : 1
  99. },
  100. "aliases" : {
  101. "alias1" : {},
  102. "alias2" : {
  103. "filter" : {
  104. "term" : {"user" : "kimchy" }
  105. },
  106. "routing" : "kimchy"
  107. },
  108. "{index}-alias" : {} <1>
  109. }
  110. }
  111. --------------------------------------------------
  112. // CONSOLE
  113. // TEST[s/^/DELETE _template\/template_1\n/]
  114. <1> the `{index}` placeholder in the alias name will be replaced with the
  115. actual index name that the template gets applied to, during index creation.
  116. [[multiple-templates]]
  117. ===== Indices matching multiple templates
  118. Multiple index templates can potentially match an index, in this case,
  119. both the settings and mappings are merged into the final configuration
  120. of the index. The order of the merging can be controlled using the
  121. `order` parameter, with lower order being applied first, and higher
  122. orders overriding them. For example:
  123. [source,js]
  124. --------------------------------------------------
  125. PUT /_template/template_1
  126. {
  127. "index_patterns" : ["*"],
  128. "order" : 0,
  129. "settings" : {
  130. "number_of_shards" : 1
  131. },
  132. "mappings" : {
  133. "_source" : { "enabled" : false }
  134. }
  135. }
  136. PUT /_template/template_2
  137. {
  138. "index_patterns" : ["te*"],
  139. "order" : 1,
  140. "settings" : {
  141. "number_of_shards" : 1
  142. },
  143. "mappings" : {
  144. "_source" : { "enabled" : true }
  145. }
  146. }
  147. --------------------------------------------------
  148. // CONSOLE
  149. // TEST[s/^/DELETE _template\/template_1\n/]
  150. The above will disable storing the `_source`, but
  151. for indices that start with `te*`, `_source` will still be enabled.
  152. Note, for mappings, the merging is "deep", meaning that specific
  153. object/property based mappings can easily be added/overridden on higher
  154. order templates, with lower order templates providing the basis.
  155. NOTE: Multiple matching templates with the same order value will
  156. result in a non-deterministic merging order.