templates.asciidoc 6.0 KB

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