templates.asciidoc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 simple pattern template that controls whether the template
  6. should be 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. "template": "te*",
  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. Defines a template named template_1, with a template pattern of `te*`.
  39. The settings and mappings will be applied to any index name that matches
  40. the `te*` template.
  41. It is also possible to include aliases in an index template as follows:
  42. [source,js]
  43. --------------------------------------------------
  44. PUT _template/template_1
  45. {
  46. "template" : "te*",
  47. "settings" : {
  48. "number_of_shards" : 1
  49. },
  50. "aliases" : {
  51. "alias1" : {},
  52. "alias2" : {
  53. "filter" : {
  54. "term" : {"user" : "kimchy" }
  55. },
  56. "routing" : "kimchy"
  57. },
  58. "{index}-alias" : {} <1>
  59. }
  60. }
  61. --------------------------------------------------
  62. // CONSOLE
  63. // TEST[s/^/DELETE _template\/template_1\n/]
  64. <1> the `{index}` placeholder within the alias name will be replaced with the
  65. actual index name that the template gets applied to during index creation.
  66. [float]
  67. [[delete]]
  68. === Deleting a Template
  69. Index templates are identified by a name (in the above case
  70. `template_1`) and can be deleted as well:
  71. [source,js]
  72. --------------------------------------------------
  73. DELETE /_template/template_1
  74. --------------------------------------------------
  75. // CONSOLE
  76. [float]
  77. [[getting]]
  78. === Getting templates
  79. Index templates are identified by a name (in the above case
  80. `template_1`) and can be retrieved using the following:
  81. [source,js]
  82. --------------------------------------------------
  83. GET /_template/template_1
  84. --------------------------------------------------
  85. // CONSOLE
  86. You can also match several templates by using wildcards like:
  87. [source,js]
  88. --------------------------------------------------
  89. GET /_template/temp*
  90. GET /_template/template_1,template_2
  91. --------------------------------------------------
  92. // CONSOLE
  93. To get list of all index templates you can run:
  94. [source,js]
  95. --------------------------------------------------
  96. GET /_template
  97. --------------------------------------------------
  98. // CONSOLE
  99. [float]
  100. [[indices-templates-exists]]
  101. === Templates exists
  102. Used to check if the template exists or not. For example:
  103. [source,js]
  104. -----------------------------------------------
  105. HEAD _template/template_1
  106. -----------------------------------------------
  107. // CONSOLE
  108. The HTTP status code indicates if the template with the given name
  109. exists or not. A status code `200` means it exists, a `404` it does not.
  110. [float]
  111. [[multiple-templates]]
  112. === Multiple Template Matching
  113. Multiple index templates can potentially match an index, in this case,
  114. both the settings and mappings are merged into the final configuration
  115. of the index. The order of the merging can be controlled using the
  116. `order` parameter, with lower order being applied first, and higher
  117. orders overriding them. For example:
  118. [source,js]
  119. --------------------------------------------------
  120. PUT /_template/template_1
  121. {
  122. "template" : "*",
  123. "order" : 0,
  124. "settings" : {
  125. "number_of_shards" : 1
  126. },
  127. "mappings" : {
  128. "type1" : {
  129. "_source" : { "enabled" : false }
  130. }
  131. }
  132. }
  133. PUT /_template/template_2
  134. {
  135. "template" : "te*",
  136. "order" : 1,
  137. "settings" : {
  138. "number_of_shards" : 1
  139. },
  140. "mappings" : {
  141. "type1" : {
  142. "_source" : { "enabled" : true }
  143. }
  144. }
  145. }
  146. --------------------------------------------------
  147. // CONSOLE
  148. // TEST[s/^/DELETE _template\/template_1\n/]
  149. The above will disable storing the `_source` on all `type1` types, but
  150. for indices of that start with `te*`, source will still be enabled.
  151. Note, for mappings, the merging is "deep", meaning that specific
  152. object/property based mappings can easily be added/overridden on higher
  153. order templates, with lower order templates providing the basis.