templates.asciidoc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. [[indices-templates-v1]]
  2. === Put index template API
  3. ++++
  4. <titleabbrev>Put index template</titleabbrev>
  5. ++++
  6. This documentation is about legacy index templates, which are deprecated and will be replaced by
  7. composable templates. For information about composable templates, see <<indices-templates>>.
  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. Creates or updates an index template.
  15. [source,console]
  16. --------------------------------------------------
  17. PUT _template/template_1
  18. {
  19. "index_patterns": ["te*", "bar*"],
  20. "settings": {
  21. "number_of_shards": 1
  22. },
  23. "mappings": {
  24. "_source": {
  25. "enabled": false
  26. },
  27. "properties": {
  28. "host_name": {
  29. "type": "keyword"
  30. },
  31. "created_at": {
  32. "type": "date",
  33. "format": "EEE MMM dd HH:mm:ss Z yyyy"
  34. }
  35. }
  36. }
  37. }
  38. --------------------------------------------------
  39. // TESTSETUP
  40. //////////////////////////
  41. [source,console]
  42. --------------------------------------------------
  43. DELETE _template/template_*
  44. --------------------------------------------------
  45. // TEARDOWN
  46. //////////////////////////
  47. [[put-index-template-v1-api-request]]
  48. ==== {api-request-title}
  49. `PUT /_template/<index-template>`
  50. [[put-index-template-v1-api-desc]]
  51. ==== {api-description-title}
  52. Use the PUT index template API
  53. to create or update an index template.
  54. // tag::index-template-def[]
  55. Index templates define <<index-modules-settings,settings>> and <<mapping,mappings>>
  56. that you can automatically apply when creating new indices.
  57. {es} applies templates to new indices
  58. based on an index pattern that matches the index name.
  59. // end::index-template-def[]
  60. Index templates are only applied during index creation.
  61. Changes to index templates do not affect existing indices.
  62. Settings and mappings specified in <<indices-create-index, create index>> API requests
  63. override any settings or mappings specified in an index template.
  64. ===== Comments in index templates
  65. You can use C-style /* */ block comments in index templates.
  66. You can include comments anywhere in the request body,
  67. except before the opening curly bracket.
  68. [[getting-v1]]
  69. ===== Getting templates
  70. See <<indices-get-template-v1>>.
  71. [[put-index-template-v1-api-path-params]]
  72. ==== {api-path-parms-title}
  73. `<index-template>`::
  74. (Required, string)
  75. Name of the index template to create.
  76. [[put-index-template-v1-api-query-params]]
  77. ==== {api-query-parms-title}
  78. `create`::
  79. (Optional, boolean)
  80. If `true`, this request cannot replace or update existing index templates.
  81. Defaults to `false`.
  82. `order`::
  83. (Optional,integer)
  84. Order in which {es} applies this template
  85. if index matches multiple templates.
  86. +
  87. Templates with lower `order` values are merged first.
  88. Templates with higher `order` values are merged later,
  89. overriding templates with lower values.
  90. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=master-timeout]
  91. [[put-index-template-v1-api-request-body]]
  92. ==== {api-request-body-title}
  93. `index_patterns`::
  94. (Required, array of strings)
  95. Array of wildcard expressions
  96. used to match the names of indices during creation.
  97. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=aliases]
  98. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=mappings]
  99. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=settings]
  100. `version`::
  101. (Optional, integer)
  102. Version number used to manage index templates externally.
  103. This number is not automatically generated by {es}.
  104. [[put-index-template-v1-api-example]]
  105. ==== {api-examples-title}
  106. ===== Index template with index aliases
  107. You can include <<indices-aliases,index aliases>> in an index template.
  108. [source,console]
  109. --------------------------------------------------
  110. PUT _template/template_1
  111. {
  112. "index_patterns" : ["te*"],
  113. "settings" : {
  114. "number_of_shards" : 1
  115. },
  116. "aliases" : {
  117. "alias1" : {},
  118. "alias2" : {
  119. "filter" : {
  120. "term" : {"user" : "kimchy" }
  121. },
  122. "routing" : "kimchy"
  123. },
  124. "{index}-alias" : {} <1>
  125. }
  126. }
  127. --------------------------------------------------
  128. <1> the `{index}` placeholder in the alias name will be replaced with the
  129. actual index name that the template gets applied to, during index creation.
  130. [[multiple-templates-v1]]
  131. ===== Indices matching multiple templates
  132. Multiple index templates can potentially match an index, in this case,
  133. both the settings and mappings are merged into the final configuration
  134. of the index. The order of the merging can be controlled using the
  135. `order` parameter, with lower order being applied first, and higher
  136. orders overriding them. For example:
  137. [source,console]
  138. --------------------------------------------------
  139. PUT /_template/template_1
  140. {
  141. "index_patterns" : ["*"],
  142. "order" : 0,
  143. "settings" : {
  144. "number_of_shards" : 1
  145. },
  146. "mappings" : {
  147. "_source" : { "enabled" : false }
  148. }
  149. }
  150. PUT /_template/template_2
  151. {
  152. "index_patterns" : ["te*"],
  153. "order" : 1,
  154. "settings" : {
  155. "number_of_shards" : 1
  156. },
  157. "mappings" : {
  158. "_source" : { "enabled" : true }
  159. }
  160. }
  161. --------------------------------------------------
  162. The above will disable storing the `_source`, but
  163. for indices that start with `te*`, `_source` will still be enabled.
  164. Note, for mappings, the merging is "deep", meaning that specific
  165. object/property based mappings can easily be added/overridden on higher
  166. order templates, with lower order templates providing the basis.
  167. NOTE: Multiple matching templates with the same order value will
  168. result in a non-deterministic merging order.
  169. [[versioning-templates-v1]]
  170. ===== Template versioning
  171. You can use the `version` parameter
  172. to add an optional version number to an index template.
  173. External systems can use these version numbers
  174. to simplify template management.
  175. The `version` parameter is completely optional
  176. and not automatically generated by {es}.
  177. To unset a `version`,
  178. replace the template without specifying one.
  179. [source,console]
  180. --------------------------------------------------
  181. PUT /_template/template_1
  182. {
  183. "index_patterns" : ["*"],
  184. "order" : 0,
  185. "settings" : {
  186. "number_of_shards" : 1
  187. },
  188. "version": 123
  189. }
  190. --------------------------------------------------
  191. To check the `version`,
  192. you can use the <<indices-get-template, get index template>> API
  193. with the <<common-options-response-filtering, `filter_path`>> query parameter
  194. to return only the version number:
  195. [source,console]
  196. --------------------------------------------------
  197. GET /_template/template_1?filter_path=*.version
  198. --------------------------------------------------
  199. // TEST[continued]
  200. The API returns the following response:
  201. [source,console-result]
  202. --------------------------------------------------
  203. {
  204. "template_1" : {
  205. "version" : 123
  206. }
  207. }
  208. --------------------------------------------------