templates.asciidoc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 includes 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. include::{docdir}/rest-api/common-parms.asciidoc[tag=include-type-name]
  69. `order`::
  70. (Optional,integer)
  71. Order in which {es} applies this template
  72. if index matches multiple templates.
  73. +
  74. Templates with lower `order` values are merged first.
  75. Templates with higher `order` values are merged later,
  76. overriding templates with lower values.
  77. include::{docdir}/rest-api/common-parms.asciidoc[tag=timeoutparms]
  78. [[put-index-template-api-request-body]]
  79. ==== {api-request-body-title}
  80. `index_patterns`::
  81. (Required, array of strings)
  82. Array of wildcard expressions
  83. used to match the names of indices during creation.
  84. include::{docdir}/rest-api/common-parms.asciidoc[tag=aliases]
  85. include::{docdir}/rest-api/common-parms.asciidoc[tag=mappings]
  86. include::{docdir}/rest-api/common-parms.asciidoc[tag=settings]
  87. `version`::
  88. (Optional, integer)
  89. Version number used to manage index templates externally.
  90. This number is not automatically generated by {es}.
  91. [[put-index-template-api-example]]
  92. ==== {api-examples-title}
  93. ===== Index template with index aliases
  94. You can include <<indices-aliases,index aliases>> in an index template.
  95. [source,console]
  96. --------------------------------------------------
  97. PUT _template/template_1
  98. {
  99. "index_patterns" : ["te*"],
  100. "settings" : {
  101. "number_of_shards" : 1
  102. },
  103. "aliases" : {
  104. "alias1" : {},
  105. "alias2" : {
  106. "filter" : {
  107. "term" : {"user" : "kimchy" }
  108. },
  109. "routing" : "kimchy"
  110. },
  111. "{index}-alias" : {} <1>
  112. }
  113. }
  114. --------------------------------------------------
  115. // TEST[s/^/DELETE _template\/template_1\n/]
  116. <1> the `{index}` placeholder in the alias name will be replaced with the
  117. actual index name that the template gets applied to, during index creation.
  118. [[multiple-templates]]
  119. ===== Indices matching multiple templates
  120. Multiple index templates can potentially match an index, in this case,
  121. both the settings and mappings are merged into the final configuration
  122. of the index. The order of the merging can be controlled using the
  123. `order` parameter, with lower order being applied first, and higher
  124. orders overriding them. For example:
  125. [source,console]
  126. --------------------------------------------------
  127. PUT /_template/template_1
  128. {
  129. "index_patterns" : ["*"],
  130. "order" : 0,
  131. "settings" : {
  132. "number_of_shards" : 1
  133. },
  134. "mappings" : {
  135. "_source" : { "enabled" : false }
  136. }
  137. }
  138. PUT /_template/template_2
  139. {
  140. "index_patterns" : ["te*"],
  141. "order" : 1,
  142. "settings" : {
  143. "number_of_shards" : 1
  144. },
  145. "mappings" : {
  146. "_source" : { "enabled" : true }
  147. }
  148. }
  149. --------------------------------------------------
  150. // TEST[s/^/DELETE _template\/template_1\n/]
  151. The above will disable storing the `_source`, but
  152. for indices that start with `te*`, `_source` will still be enabled.
  153. Note, for mappings, the merging is "deep", meaning that specific
  154. object/property based mappings can easily be added/overridden on higher
  155. order templates, with lower order templates providing the basis.
  156. NOTE: Multiple matching templates with the same order value will
  157. result in a non-deterministic merging order.
  158. [[versioning-templates]]
  159. ===== Template versioning
  160. You can use the `version` parameter
  161. to add an optional version number to an index template.
  162. External systems can use these version numbers
  163. to simplify template management.
  164. The `version` parameter is completely optional
  165. and not automatically generated by {es}.
  166. To unset a `version`,
  167. replace the template without specifying one.
  168. [source,console]
  169. --------------------------------------------------
  170. PUT /_template/template_1
  171. {
  172. "index_patterns" : ["*"],
  173. "order" : 0,
  174. "settings" : {
  175. "number_of_shards" : 1
  176. },
  177. "version": 123
  178. }
  179. --------------------------------------------------
  180. To check the `version`,
  181. you can use the <<indices-get-template, get index template>> API
  182. with the <<common-options-response-filtering, `filter_path`>> query parameter
  183. to return only the version number:
  184. [source,console]
  185. --------------------------------------------------
  186. GET /_template/template_1?filter_path=*.version
  187. --------------------------------------------------
  188. // TEST[continued]
  189. The API returns the following response:
  190. [source,console-result]
  191. --------------------------------------------------
  192. {
  193. "template_1" : {
  194. "version" : 123
  195. }
  196. }
  197. --------------------------------------------------