templates.asciidoc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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,console]
  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. // TESTSETUP
  32. [[put-index-template-api-request]]
  33. ==== {api-request-title}
  34. `PUT /_template/<index-template>`
  35. [[put-index-template-api-desc]]
  36. ==== {api-description-title}
  37. Use the PUT index template API
  38. to create or update an index template.
  39. // tag::index-template-def[]
  40. Index templates define <<index-modules-settings,settings>> and <<mapping,mappings>>
  41. that you can automatically apply when creating new indices.
  42. {es} applies templates to new indices
  43. based on an index pattern that matches the index name.
  44. // end::index-template-def[]
  45. Index templates are only applied during index creation.
  46. Changes to index templates do not affect existing indices.
  47. Settings and mappings specified in <<indices-create-index, create index>> API requests
  48. override any settings or mappings specified in an index template.
  49. ===== Comments in index templates
  50. You can use C-style /* */ block comments in index templates.
  51. You can include comments anywhere in the request body,
  52. except before the opening curly bracket.
  53. [[getting]]
  54. ===== Getting templates
  55. See <<indices-get-template>>.
  56. [[put-index-template-api-path-params]]
  57. ==== {api-path-parms-title}
  58. `<index-template>`::
  59. (Required, string)
  60. Name of the index template to create.
  61. [[put-index-template-api-query-params]]
  62. ==== {api-query-parms-title}
  63. `create`::
  64. (Optional, boolean)
  65. If `true`, this request cannot replace or update existing index templates.
  66. Defaults to `false`.
  67. include::{docdir}/rest-api/common-parms.asciidoc[tag=flat-settings]
  68. `order`::
  69. (Optional,integer)
  70. Order in which {es} applies this template
  71. if index matches multiple templates.
  72. +
  73. Templates with lower `order` values are merged first.
  74. Templates with higher `order` values are merged later,
  75. overriding templates with lower values.
  76. include::{docdir}/rest-api/common-parms.asciidoc[tag=timeoutparms]
  77. [[put-index-template-api-request-body]]
  78. ==== {api-request-body-title}
  79. `index_patterns`::
  80. (Required, array of strings)
  81. Array of wildcard expressions
  82. used to match the names of indices during creation.
  83. include::{docdir}/rest-api/common-parms.asciidoc[tag=aliases]
  84. include::{docdir}/rest-api/common-parms.asciidoc[tag=mappings]
  85. include::{docdir}/rest-api/common-parms.asciidoc[tag=settings]
  86. `version`::
  87. (Optional, integer)
  88. Version number used to manage index templates externally.
  89. This number is not automatically generated by {es}.
  90. [[put-index-template-api-example]]
  91. ==== {api-examples-title}
  92. ===== Index template with index aliases
  93. You can include <<indices-aliases,index aliases>> in an index template.
  94. [source,console]
  95. --------------------------------------------------
  96. PUT _template/template_1
  97. {
  98. "index_patterns" : ["te*"],
  99. "settings" : {
  100. "number_of_shards" : 1
  101. },
  102. "aliases" : {
  103. "alias1" : {},
  104. "alias2" : {
  105. "filter" : {
  106. "term" : {"user" : "kimchy" }
  107. },
  108. "routing" : "kimchy"
  109. },
  110. "{index}-alias" : {} <1>
  111. }
  112. }
  113. --------------------------------------------------
  114. // TEST[s/^/DELETE _template\/template_1\n/]
  115. <1> the `{index}` placeholder in the alias name will be replaced with the
  116. actual index name that the template gets applied to, during index creation.
  117. [[multiple-templates]]
  118. ===== Indices matching multiple templates
  119. Multiple index templates can potentially match an index, in this case,
  120. both the settings and mappings are merged into the final configuration
  121. of the index. The order of the merging can be controlled using the
  122. `order` parameter, with lower order being applied first, and higher
  123. orders overriding them. For example:
  124. [source,console]
  125. --------------------------------------------------
  126. PUT /_template/template_1
  127. {
  128. "index_patterns" : ["*"],
  129. "order" : 0,
  130. "settings" : {
  131. "number_of_shards" : 1
  132. },
  133. "mappings" : {
  134. "_source" : { "enabled" : false }
  135. }
  136. }
  137. PUT /_template/template_2
  138. {
  139. "index_patterns" : ["te*"],
  140. "order" : 1,
  141. "settings" : {
  142. "number_of_shards" : 1
  143. },
  144. "mappings" : {
  145. "_source" : { "enabled" : true }
  146. }
  147. }
  148. --------------------------------------------------
  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.
  157. [[versioning-templates]]
  158. ===== Template versioning
  159. You can use the `version` parameter
  160. to add an optional version number to an index template.
  161. External systems can use these version numbers
  162. to simplify template management.
  163. The `version` parameter is completely optional
  164. and not automatically generated by {es}.
  165. To unset a `version`,
  166. replace the template without specifying one.
  167. [source,console]
  168. --------------------------------------------------
  169. PUT /_template/template_1
  170. {
  171. "index_patterns" : ["*"],
  172. "order" : 0,
  173. "settings" : {
  174. "number_of_shards" : 1
  175. },
  176. "version": 123
  177. }
  178. --------------------------------------------------
  179. To check the `version`,
  180. you can use the <<indices-get-template, get index template>> API
  181. with the <<common-options-response-filtering, `filter_path`>> query parameter
  182. to return only the version number:
  183. [source,console]
  184. --------------------------------------------------
  185. GET /_template/template_1?filter_path=*.version
  186. --------------------------------------------------
  187. // TEST[continued]
  188. The API returns the following response:
  189. [source,console-result]
  190. --------------------------------------------------
  191. {
  192. "template_1" : {
  193. "version" : 123
  194. }
  195. }
  196. --------------------------------------------------