templates.asciidoc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. [[indices-templates]]
  2. === Index Templates
  3. Index templates allow you to define templates that will automatically be
  4. applied when new indices are created. The templates include both
  5. <<index-modules-settings,settings>> and <<mapping,mappings>>
  6. and a simple pattern template that controls whether the template should be
  7. applied to the new index.
  8. NOTE: Templates are only applied at index creation time. Changing a template
  9. will have no impact on existing indices. When using the create index API, the
  10. settings/mappings defined as part of the create index call will take precedence
  11. over any matching settings/mappings defined in the template.
  12. For example:
  13. [source,js]
  14. --------------------------------------------------
  15. PUT _template/template_1
  16. {
  17. "index_patterns": ["te*", "bar*"],
  18. "settings": {
  19. "number_of_shards": 1
  20. },
  21. "mappings": {
  22. "_source": {
  23. "enabled": false
  24. },
  25. "properties": {
  26. "host_name": {
  27. "type": "keyword"
  28. },
  29. "created_at": {
  30. "type": "date",
  31. "format": "EEE MMM dd HH:mm:ss Z yyyy"
  32. }
  33. }
  34. }
  35. }
  36. --------------------------------------------------
  37. // CONSOLE
  38. // TESTSETUP
  39. NOTE: Index templates provide C-style /* */ block comments. Comments are allowed
  40. everywhere in the JSON document except before the initial opening curly bracket.
  41. Defines a template named `template_1`, with a template pattern of `te*` or `bar*`.
  42. The settings and mappings will be applied to any index name that matches
  43. the `te*` or `bar*` pattern.
  44. It is also possible to include aliases in an index template as follows:
  45. [source,js]
  46. --------------------------------------------------
  47. PUT _template/template_1
  48. {
  49. "index_patterns" : ["te*"],
  50. "settings" : {
  51. "number_of_shards" : 1
  52. },
  53. "aliases" : {
  54. "alias1" : {},
  55. "alias2" : {
  56. "filter" : {
  57. "term" : {"user" : "kimchy" }
  58. },
  59. "routing" : "kimchy"
  60. },
  61. "{index}-alias" : {} <1>
  62. }
  63. }
  64. --------------------------------------------------
  65. // CONSOLE
  66. // TEST[s/^/DELETE _template\/template_1\n/]
  67. <1> the `{index}` placeholder in the alias name will be replaced with the
  68. actual index name that the template gets applied to, during index creation.
  69. [float]
  70. [[delete]]
  71. ==== Deleting a Template
  72. Index templates are identified by a name (in the above case
  73. `template_1`) and can be deleted as well:
  74. [source,js]
  75. --------------------------------------------------
  76. DELETE /_template/template_1
  77. --------------------------------------------------
  78. // CONSOLE
  79. [float]
  80. [[getting]]
  81. ==== Getting templates
  82. Index templates are identified by a name (in the above case
  83. `template_1`) and can be retrieved using the following:
  84. [source,js]
  85. --------------------------------------------------
  86. GET /_template/template_1
  87. --------------------------------------------------
  88. // CONSOLE
  89. You can also match several templates by using wildcards like:
  90. [source,js]
  91. --------------------------------------------------
  92. GET /_template/temp*
  93. GET /_template/template_1,template_2
  94. --------------------------------------------------
  95. // CONSOLE
  96. To get list of all index templates you can run:
  97. [source,js]
  98. --------------------------------------------------
  99. GET /_template
  100. --------------------------------------------------
  101. // CONSOLE
  102. [float]
  103. [[indices-templates-exists]]
  104. ==== Template exists
  105. Used to check if the template exists or not. For example:
  106. [source,js]
  107. -----------------------------------------------
  108. HEAD _template/template_1
  109. -----------------------------------------------
  110. // CONSOLE
  111. The HTTP status code indicates if the template with the given name
  112. exists or not. Status code `200` means it exists and `404` means
  113. it does not.
  114. NOTE: Before 7.0.0, the 'mappings' definition used to include a type name. Although mappings
  115. no longer contain a type name by default, you can still use the old format by setting
  116. the parameter include_type_name. For more details, please see <<removal-of-types>>.
  117. [float]
  118. [[multiple-templates]]
  119. ==== Multiple Templates Matching
  120. Multiple index templates can potentially match an index, in this case,
  121. both the settings and mappings are merged into the final configuration
  122. of the index. The order of the merging can be controlled using the
  123. `order` parameter, with lower order being applied first, and higher
  124. orders overriding them. For example:
  125. [source,js]
  126. --------------------------------------------------
  127. PUT /_template/template_1
  128. {
  129. "index_patterns" : ["*"],
  130. "order" : 0,
  131. "settings" : {
  132. "number_of_shards" : 1
  133. },
  134. "mappings" : {
  135. "_source" : { "enabled" : false }
  136. }
  137. }
  138. PUT /_template/template_2
  139. {
  140. "index_patterns" : ["te*"],
  141. "order" : 1,
  142. "settings" : {
  143. "number_of_shards" : 1
  144. },
  145. "mappings" : {
  146. "_source" : { "enabled" : true }
  147. }
  148. }
  149. --------------------------------------------------
  150. // CONSOLE
  151. // TEST[s/^/DELETE _template\/template_1\n/]
  152. The above will disable storing the `_source`, but
  153. for indices that start with `te*`, `_source` will still be enabled.
  154. Note, for mappings, the merging is "deep", meaning that specific
  155. object/property based mappings can easily be added/overridden on higher
  156. order templates, with lower order templates providing the basis.
  157. NOTE: Multiple matching templates with the same order value will
  158. result in a non-deterministic merging order.
  159. [float]
  160. [[versioning-templates]]
  161. ==== Template Versioning
  162. Templates can optionally add a `version` number, which can be any integer value,
  163. in order to simplify template management by external systems. The `version`
  164. field is completely optional and it is meant solely for external management of
  165. templates. To unset a `version`, simply replace the template without specifying
  166. one.
  167. [source,js]
  168. --------------------------------------------------
  169. PUT /_template/template_1
  170. {
  171. "index_patterns" : ["*"],
  172. "order" : 0,
  173. "settings" : {
  174. "number_of_shards" : 1
  175. },
  176. "version": 123
  177. }
  178. --------------------------------------------------
  179. // CONSOLE
  180. To check the `version`, you can
  181. <<common-options-response-filtering, filter responses>>
  182. using `filter_path` to limit the response to just the `version`:
  183. [source,js]
  184. --------------------------------------------------
  185. GET /_template/template_1?filter_path=*.version
  186. --------------------------------------------------
  187. // CONSOLE
  188. // TEST[continued]
  189. This should give a small response that makes it both easy and inexpensive to parse:
  190. [source,js]
  191. --------------------------------------------------
  192. {
  193. "template_1" : {
  194. "version" : 123
  195. }
  196. }
  197. --------------------------------------------------
  198. // TESTRESPONSE