templates.asciidoc 4.6 KB

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