templates.asciidoc 6.2 KB

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