put-index-template-v1.asciidoc 6.9 KB

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