index-templates.asciidoc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. [[index-templates]]
  2. = Index templates
  3. NOTE: This topic describes the composable index templates introduced in {es} 7.8.
  4. For information about how index templates worked previously,
  5. see the <<indices-templates-v1,legacy template documentation>>.
  6. [[getting]]
  7. An index template is a way to tell {es} how to configure an index when it is
  8. created. For data streams, the index template configures the stream's backing
  9. indices as they are created. Templates are configured
  10. *prior to index creation*. When an index is created - either manually or
  11. through indexing a document - the template settings are used as a basis for
  12. creating the index.
  13. There are two types of templates: index templates and <<indices-component-template,component templates>>. Component templates are reusable building
  14. blocks that configure mappings, settings, and aliases. While you can use
  15. component templates to construct index templates, they aren't directly applied
  16. to a set of indices. Index templates can contain a collection of component
  17. templates, as well as directly specify settings, mappings, and aliases.
  18. The following conditions apply to index templates:
  19. * Composable templates take precedence over legacy templates. If no composable
  20. template matches a given index, a legacy template may still match and be
  21. applied.
  22. * If an index is created with explicit settings and also matches an index
  23. template, the settings from the <<indices-create-index,create index>> request
  24. take precedence over settings specified in the index template and its component
  25. templates.
  26. * Settings specified in the index template itself take precedence over the settings
  27. in its component templates.
  28. * If a new data stream or index matches more than one index template, the index
  29. template with the highest priority is used.
  30. [[avoid-index-pattern-collisions]]
  31. .Avoid index pattern collisions
  32. ****
  33. {es} has built-in index templates, each with a priority of `100`, for the
  34. following index patterns:
  35. // tag::built-in-index-template-patterns[]
  36. - `logs-*-*`
  37. - `metrics-*-*`
  38. - `synthetics-*-*`
  39. - `profiling-*`
  40. // end::built-in-index-template-patterns[]
  41. {fleet-guide}/fleet-overview.html[{agent}] uses these templates to create
  42. data streams. Index templates created by {fleet} integrations use similar
  43. overlapping index patterns and have a priority up to `200`.
  44. If you use {fleet} or {agent}, assign your index templates a priority lower than
  45. `100` to avoid overriding these templates. Otherwise, to avoid accidentally
  46. applying the templates, do one or more of the following:
  47. - To disable all built-in index and component templates, set
  48. <<stack-templates-enabled,`stack.templates.enabled`>> to `false` using the
  49. <<cluster-update-settings,cluster update settings API>>.
  50. - Use a non-overlapping index pattern.
  51. - Assign templates with an overlapping pattern a `priority` higher than `200`.
  52. For example, if you don't use {fleet} or {agent} and want to create a template
  53. for the `logs-*` index pattern, assign your template a priority of `500`. This
  54. ensures your template is applied instead of the built-in template for
  55. `logs-*-*`.
  56. - To avoid naming collisions with built-in and Fleet-managed index templates, avoid using `@` as part of the name of your own index templates.
  57. ****
  58. [discrete]
  59. [[create-index-templates]]
  60. == Create index template
  61. Use the <<indices-put-template,index template>> and <<indices-component-template,put component template>> APIs to create and update index templates.
  62. You can also <<index-mgmt,manage index templates>> from Stack
  63. Management in {kib}.
  64. The following requests create two component templates.
  65. [source,console]
  66. ----
  67. PUT _component_template/component_template1
  68. {
  69. "template": {
  70. "mappings": {
  71. "properties": {
  72. "@timestamp": {
  73. "type": "date"
  74. }
  75. }
  76. }
  77. }
  78. }
  79. PUT _component_template/runtime_component_template
  80. {
  81. "template": {
  82. "mappings": {
  83. "runtime": { <1>
  84. "day_of_week": {
  85. "type": "keyword",
  86. "script": {
  87. "source": "emit(doc['@timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT))"
  88. }
  89. }
  90. }
  91. }
  92. }
  93. }
  94. ----
  95. <1> This component template adds a <<runtime-mapping-fields,runtime field>>
  96. named `day_of_week` to the mappings when a new index matches the template.
  97. The following request creates an index template that is _composed of_ these
  98. component templates.
  99. [source,console]
  100. ----
  101. PUT _index_template/template_1
  102. {
  103. "index_patterns": ["te*", "bar*"],
  104. "template": {
  105. "settings": {
  106. "number_of_shards": 1
  107. },
  108. "mappings": {
  109. "_source": {
  110. "enabled": true
  111. },
  112. "properties": {
  113. "host_name": {
  114. "type": "keyword"
  115. },
  116. "created_at": {
  117. "type": "date",
  118. "format": "EEE MMM dd HH:mm:ss Z yyyy"
  119. }
  120. }
  121. },
  122. "aliases": {
  123. "mydata": { }
  124. }
  125. },
  126. "priority": 500,
  127. "composed_of": ["component_template1", "runtime_component_template"], <1>
  128. "version": 3,
  129. "_meta": {
  130. "description": "my custom"
  131. }
  132. }
  133. ----
  134. // TEST[continued]
  135. ////
  136. [source,console]
  137. ----
  138. DELETE _index_template/template_1
  139. DELETE _component_template/runtime_component_template
  140. DELETE _component_template/component_template1
  141. ----
  142. // TEST[continued]
  143. ////
  144. include::simulate-multi-component-templates.asciidoc[]
  145. include::ignore-missing-component-templates.asciidoc[]