templates.asciidoc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. [[indices-templates]]
  2. == Index Templates
  3. Index templates allow to define templates that will automatically be
  4. applied to new indices created. The templates include both settings and
  5. mappings, and a simple pattern template that controls if the template
  6. will be applied to the index created. For example:
  7. [source,js]
  8. --------------------------------------------------
  9. curl -XPUT localhost:9200/_template/template_1 -d '
  10. {
  11. "template" : "te*",
  12. "settings" : {
  13. "number_of_shards" : 1
  14. },
  15. "mappings" : {
  16. "type1" : {
  17. "_source" : { "enabled" : false }
  18. }
  19. }
  20. }
  21. '
  22. --------------------------------------------------
  23. Defines a template named template_1, with a template pattern of `te*`.
  24. The settings and mappings will be applied to any index name that matches
  25. the `te*` template.
  26. coming[1.1.0]
  27. It is also possible to include aliases in an index template as follows:
  28. [source,js]
  29. --------------------------------------------------
  30. curl -XPUT localhost:9200/_template/template_1 -d '
  31. {
  32. "template" : "te*",
  33. "settings" : {
  34. "number_of_shards" : 1
  35. },
  36. "aliases" : {
  37. "alias1" : {},
  38. "alias2" : {
  39. "filter" : {
  40. "term" : {"user" : "kimchy" }
  41. },
  42. "routing" : "kimchy"
  43. },
  44. "{index}-alias" : {} <1>
  45. }
  46. }
  47. '
  48. --------------------------------------------------
  49. <1> the `{index}` placeholder within the alias name will be replaced with the
  50. actual index name that the template gets applied to during index creation.
  51. [float]
  52. [[delete]]
  53. === Deleting a Template
  54. Index templates are identified by a name (in the above case
  55. `template_1`) and can be deleted as well:
  56. [source,js]
  57. --------------------------------------------------
  58. curl -XDELETE localhost:9200/_template/template_1
  59. --------------------------------------------------
  60. [float]
  61. [[getting]]
  62. === GETting templates
  63. Index templates are identified by a name (in the above case
  64. `template_1`) and can be retrieved using the following:
  65. [source,js]
  66. --------------------------------------------------
  67. curl -XGET localhost:9200/_template/template_1
  68. --------------------------------------------------
  69. You can also match several templates by using wildcards like:
  70. [source,js]
  71. --------------------------------------------------
  72. curl -XGET localhost:9200/_template/temp*
  73. curl -XGET localhost:9200/_template/template_1,template_2
  74. --------------------------------------------------
  75. To get list of all index templates you can use
  76. <<cluster-state,Cluster State>> API
  77. and check for the metadata/templates section of the response.
  78. Or run:
  79. [source,js]
  80. --------------------------------------------------
  81. curl -XGET localhost:9200/_template/
  82. --------------------------------------------------
  83. [float]
  84. [[multiple-templates]]
  85. === Multiple Template Matching
  86. Multiple index templates can potentially match an index, in this case,
  87. both the settings and mappings are merged into the final configuration
  88. of the index. The order of the merging can be controlled using the
  89. `order` parameter, with lower order being applied first, and higher
  90. orders overriding them. For example:
  91. [source,js]
  92. --------------------------------------------------
  93. curl -XPUT localhost:9200/_template/template_1 -d '
  94. {
  95. "template" : "*",
  96. "order" : 0,
  97. "settings" : {
  98. "number_of_shards" : 1
  99. },
  100. "mappings" : {
  101. "type1" : {
  102. "_source" : { "enabled" : false }
  103. }
  104. }
  105. }
  106. '
  107. curl -XPUT localhost:9200/_template/template_2 -d '
  108. {
  109. "template" : "te*",
  110. "order" : 1,
  111. "settings" : {
  112. "number_of_shards" : 1
  113. },
  114. "mappings" : {
  115. "type1" : {
  116. "_source" : { "enabled" : true }
  117. }
  118. }
  119. }
  120. '
  121. --------------------------------------------------
  122. The above will disable storing the `_source` on all `type1` types, but
  123. for indices of that start with `te*`, source will still be enabled.
  124. Note, for mappings, the merging is "deep", meaning that specific
  125. object/property based mappings can easily be added/overridden on higher
  126. order templates, with lower order templates providing the basis.
  127. [float]
  128. [[config]]
  129. === Config
  130. Index templates can also be placed within the config location
  131. (`path.conf`) under the `templates` directory (note, make sure to place
  132. them on all master eligible nodes). For example, a file called
  133. `template_1.json` can be placed under `config/templates` and it will be
  134. added if it matches an index. Here is a sample of the mentioned file:
  135. [source,js]
  136. --------------------------------------------------
  137. {
  138. "template_1" : {
  139. "template" : "*",
  140. "settings" : {
  141. "index.number_of_shards" : 2
  142. },
  143. "mappings" : {
  144. "_default_" : {
  145. "_source" : {
  146. "enabled" : false
  147. }
  148. },
  149. "type1" : {
  150. "_all" : {
  151. "enabled" : false
  152. }
  153. }
  154. }
  155. }
  156. }
  157. --------------------------------------------------