{"id":348,"date":"2022-08-04T22:25:57","date_gmt":"2022-08-04T22:25:57","guid":{"rendered":"http:\/\/192.168.1.102\/?p=348"},"modified":"2022-10-19T18:31:57","modified_gmt":"2022-10-19T18:31:57","slug":"index-templates","status":"publish","type":"post","link":"https:\/\/192.168.12.139\/blog\/index-templates","title":{"rendered":"Index Templates"},"content":{"rendered":"
Index templates are the configurations of indices to be created. However, the configuration of an index can be altered after it is created individually.<\/span><\/p>\n Whether an index is created manually or automatically while indexing a document, the new index will be created in the light of the template settings.<\/span><\/p>\n A template can be either a\u00a0<\/span>composable index template<\/span><\/strong>\u00a0(introduced in\u00a0<\/span>Elasticsearch 7.8<\/span><\/em>, index template) or a\u00a0<\/span>component template<\/span><\/strong>. Component templates are, as their name indicates,\u00a0<\/span>building blocks<\/span><\/em>\u00a0that can be used again and again to create index templates, and they define\u00a0<\/span>mappings<\/span><\/em>,\u00a0<\/span>settings<\/span><\/em>, and\u00a0<\/span>aliases<\/span><\/em>. Component templates make it easier and faster to create index templates.<\/span><\/p>\n The main purpose behind the component templates, making the templates more\u00a0<\/span>modular<\/span><\/em>\u00a0and more\u00a0<\/span>viable<\/span><\/em>\u00a0to apply with less effort.<\/span><\/p>\n Index templates may contain many component templates other than directly specified settings, mappings, and aliases. Just because an index template is composed of component templates, it doesn’t mean they are directly applied to a set of indices.\u00a0<\/span><\/p>\n In settings, specifications of an index are defined (<\/span>e.g.<\/span><\/strong>,\u00a0<\/span>number of shards<\/span><\/strong>,\u00a0<\/span>number of replicas<\/span><\/strong>,\u00a0<\/span>shard<\/span><\/strong>\u00a0<\/span>size<\/span><\/strong>, …)<\/span><\/p>\n In mappings, how the documents will be stored in the index is defined.<\/span><\/p>\n To remember an index’s name easily, aliases are used.<\/span><\/p>\n Basic template behavior:<\/strong><\/p>\n For example, if “number_of_replicas” is set to 2 for the component index template and it is defined to be 0 at the index creation, then it will be 0 replicas.<\/span><\/p>\n If overriding these templates needs to be prevented, then the new index templates need to have a priority of less than 100.<\/p>\n There are many ways to create index templates.\u00a0<\/span>Index template<\/span><\/strong>\u00a0or\u00a0<\/span>component template<\/span><\/strong>\u00a0APIs can be used. If the more interfacial way is preferred,\u00a0<\/span>Stack Management\u00a0<\/span><\/em>in\u00a0<\/span>Kibana<\/span><\/strong>\u00a0<\/span>UI<\/span><\/strong>\u00a0can also be used for this purpose.<\/span><\/p>\n We defined the wildcard pattern as *cars<\/strong>. In the future, at the creation time, any index matching this pattern (e.g., used_cars) will inherit the configurations of this template. <\/p>\n\n
logs-*-* and metrics-*-*<\/code><\/span>are the 2 of the index patterns and their built-in index templates got a priority of 100. <\/span><\/p>\n
2. Creating a Template<\/span><\/h2>\n
2.1 Create an Index Template<\/h3>\n
\nThe “manufacture_date” field is mapped in a type of date, and the format is dd-MM-yyyy<\/strong>, and it has a priority of 400, so it has precedence over the built-in index templates.<\/p>\nPUT<\/span> _index_template<\/span>\/<\/span>cars_template<\/span><\/span>\r\n{<\/span>\r\n \u00a0\"index_patterns\"<\/span>: [\"*cars\"<\/span>],<\/span>\r\n \u00a0\"priority\"<\/span>: 400<\/span>,<\/span>\r\n \u00a0\"template\"<\/span>: {<\/span>\r\n \u00a0 \u00a0\"mappings\"<\/span>: {<\/span>\r\n \u00a0 \u00a0 \u00a0\"properties\"<\/span>: {<\/span>\r\n \u00a0 \u00a0 \u00a0 \u00a0\"make\"<\/span>: {<\/span>\r\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\"type\"<\/span>: \"text\"<\/span><\/span>\r\n \u00a0 \u00a0 \u00a0 }, \u00a0<\/span>\r\n \u00a0 \u00a0 \u00a0 \u00a0\"manufacture_date\"<\/span>: {<\/span>\r\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\"type\"<\/span>: \"date\"<\/span>,<\/span>\r\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\"format\"<\/span>:\"dd-MM-yyyy\"<\/span><\/span>\r\n \u00a0 \u00a0 \u00a0 }<\/span>\r\n \u00a0 \u00a0 }<\/span>\r\n \u00a0 },<\/span>\r\n \u00a0 \u00a0\"settings\"<\/span>:{<\/span>\r\n \u00a0 \u00a0 \u00a0\"number_of_shards\"<\/span>:1<\/span>,<\/span>\r\n \u00a0 \u00a0 \u00a0\"number_of_replicas\"<\/span>:1<\/span><\/span>\r\n \u00a0 },<\/span>\r\n \u00a0 \u00a0\"aliases\"<\/span>:{<\/span>\r\n \u00a0 \u00a0 \u00a0\"all_cars\"<\/span>:{}<\/span>\r\n \u00a0 }<\/span>\r\n }<\/span>\r\n}\r\n\r\n<\/span><\/pre>\n