templates.asciidoc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. [float]
  27. === Deleting a Template
  28. Index templates are identified by a name (in the above case
  29. `template_1`) and can be delete as well:
  30. [source,js]
  31. --------------------------------------------------
  32. curl -XDELETE localhost:9200/_template/template_1
  33. --------------------------------------------------
  34. [float]
  35. === GETting a Template
  36. Index templates are identified by a name (in the above case
  37. `template_1`) and can be retrieved using the following:
  38. [source,js]
  39. --------------------------------------------------
  40. curl -XGET localhost:9200/_template/template_1
  41. --------------------------------------------------
  42. To get list of all index templates you can use
  43. <<cluster-state,Cluster State>> API
  44. and check for the metadata/templates section of the response.
  45. [float]
  46. === Multiple Template Matching
  47. Multiple index templates can potentially match an index, in this case,
  48. both the settings and mappings are merged into the final configuration
  49. of the index. The order of the merging can be controlled using the
  50. `order` parameter, with lower order being applied first, and higher
  51. orders overriding them. For example:
  52. [source,js]
  53. --------------------------------------------------
  54. curl -XPUT localhost:9200/_template/template_1 -d '
  55. {
  56. "template" : "*",
  57. "order" : 0,
  58. "settings" : {
  59. "number_of_shards" : 1
  60. },
  61. "mappings" : {
  62. "type1" : {
  63. "_source" : { "enabled" : false }
  64. }
  65. }
  66. }
  67. '
  68. curl -XPUT localhost:9200/_template/template_2 -d '
  69. {
  70. "template" : "te*",
  71. "order" : 1,
  72. "settings" : {
  73. "number_of_shards" : 1
  74. },
  75. "mappings" : {
  76. "type1" : {
  77. "_source" : { "enabled" : true }
  78. }
  79. }
  80. }
  81. '
  82. --------------------------------------------------
  83. The above will disable storing the `_source` on all `type1` types, but
  84. for indices of that start with `te*`, source will still be enabled.
  85. Note, for mappings, the merging is "deep", meaning that specific
  86. object/property based mappings can easily be added/overridden on higher
  87. order templates, with lower order templates providing the basis.
  88. [float]
  89. === Config
  90. Index templates can also be placed within the config location
  91. (`path.conf`) under the `templates` directory (note, make sure to place
  92. them on all master eligible nodes). For example, a file called
  93. `template_1.json` can be placed under `config/templates` and it will be
  94. added if it matches an index. Here is a sample of the mentioned file:
  95. [source,js]
  96. --------------------------------------------------
  97. {
  98. "template_1" : {
  99. "template" : "*",
  100. "settings" : {
  101. "index.number_of_shards" : 2
  102. },
  103. "mappings" : {
  104. "_default_" : {
  105. "_source" : {
  106. "enabled" : false
  107. }
  108. },
  109. "type1" : {
  110. "_all" : {
  111. "enabled" : false
  112. }
  113. }
  114. }
  115. }
  116. }
  117. --------------------------------------------------