put-index-template-v1.asciidoc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. [role="child_attributes"]
  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. `aliases`::
  98. (Optional, object of objects) Aliases for the index.
  99. +
  100. include::{es-repo-dir}/indices/create-index.asciidoc[tag=aliases-props]
  101. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=mappings]
  102. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=settings]
  103. `version`::
  104. (Optional, integer)
  105. Version number used to manage index templates externally.
  106. This number is not automatically generated by {es}.
  107. [[put-index-template-v1-api-example]]
  108. ==== {api-examples-title}
  109. ===== Index template with index aliases
  110. You can include <<aliases,index aliases>> in an index template.
  111. [source,console]
  112. --------------------------------------------------
  113. PUT _template/template_1
  114. {
  115. "index_patterns" : ["te*"],
  116. "settings" : {
  117. "number_of_shards" : 1
  118. },
  119. "aliases" : {
  120. "alias1" : {},
  121. "alias2" : {
  122. "filter" : {
  123. "term" : {"user.id" : "kimchy" }
  124. },
  125. "routing" : "shard-1"
  126. },
  127. "{index}-alias" : {} <1>
  128. }
  129. }
  130. --------------------------------------------------
  131. <1> the `{index}` placeholder in the alias name will be replaced with the
  132. actual index name that the template gets applied to, during index creation.
  133. [[multiple-templates-v1]]
  134. ===== Indices matching multiple templates
  135. Multiple index templates can potentially match an index, in this case,
  136. both the settings and mappings are merged into the final configuration
  137. of the index. The order of the merging can be controlled using the
  138. `order` parameter, with lower order being applied first, and higher
  139. orders overriding them. For example:
  140. [source,console]
  141. --------------------------------------------------
  142. PUT /_template/template_1
  143. {
  144. "index_patterns" : ["te*"],
  145. "order" : 0,
  146. "settings" : {
  147. "number_of_shards" : 1
  148. },
  149. "mappings" : {
  150. "_source" : { "enabled" : false }
  151. }
  152. }
  153. PUT /_template/template_2
  154. {
  155. "index_patterns" : ["tes*"],
  156. "order" : 1,
  157. "settings" : {
  158. "number_of_shards" : 1
  159. },
  160. "mappings" : {
  161. "_source" : { "enabled" : true }
  162. }
  163. }
  164. --------------------------------------------------
  165. The above will disable storing the `_source`, but
  166. for indices that start with `tes*`, `_source` will still be enabled.
  167. Note, for mappings, the merging is "deep", meaning that specific
  168. object/property based mappings can easily be added/overridden on higher
  169. order templates, with lower order templates providing the basis.
  170. NOTE: Multiple matching templates with the same order value will
  171. result in a non-deterministic merging order.
  172. [[versioning-templates-v1]]
  173. ===== Template versioning
  174. You can use the `version` parameter
  175. to add an optional version number to an index template.
  176. External systems can use these version numbers
  177. to simplify template management.
  178. The `version` parameter is completely optional
  179. and not automatically generated by {es}.
  180. To unset a `version`,
  181. replace the template without specifying one.
  182. [source,console]
  183. --------------------------------------------------
  184. PUT /_template/template_1
  185. {
  186. "index_patterns" : ["my-index-*"],
  187. "order" : 0,
  188. "settings" : {
  189. "number_of_shards" : 1
  190. },
  191. "version": 123
  192. }
  193. --------------------------------------------------
  194. To check the `version`,
  195. you can use the <<indices-get-template, get index template>> API
  196. with the <<common-options-response-filtering, `filter_path`>> query parameter
  197. to return only the version number:
  198. [source,console]
  199. --------------------------------------------------
  200. GET /_template/template_1?filter_path=*.version
  201. --------------------------------------------------
  202. // TEST[continued]
  203. The API returns the following response:
  204. [source,console-result]
  205. --------------------------------------------------
  206. {
  207. "template_1" : {
  208. "version" : 123
  209. }
  210. }
  211. --------------------------------------------------