put-index-template-v1.asciidoc 6.9 KB

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