templates.asciidoc 4.3 KB

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