put-index-template-v1.asciidoc 6.7 KB

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