原文网址:ES之API系列--index template(索引模板)的用法(有实例)_IT利刃出鞘的博客-CSDN博客
说明
本文介绍ElasticSearch的index template(索引模板)的用法(有实例)。
官网网址
https://www.elastic.co/guide/en/elasticsearch/reference/8.0/index-templates.html
作用概述
在新建索引时,如果索引名字与索引模板的通配符匹配,那么就使用索引模板的设置(_setting、_mapping等)。
应用场景
版本的区别
7.8之后的命令是:_index_template
7.8之前的命令是:_template
用法完全一样。本文介绍7.8及之后的命令。
创建和更新的命令是一样的。如果不存在则创建,如果存在则更新。
PUT _index_template/template1 { "index_patterns": ["te*", "bar*"], "template": { "settings": { "number_of_shards": 1 }, "mappings": { "_source": { "enabled": false }, "properties": { "host_name": { "type": "keyword" }, "created_at": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss.SSS" } } }, "aliases": { "mydata": { } } }, "priority": 10, "version": 3, "_meta": { "description": "my custom" } } 上边这个模板设置了index_patterns 为 te* 和bar*意思就是:te或bar开头的索引在创建时都会使用这个模板。
PUT _component_template/component_template1 { "template": { "mappings": { "properties": { "@timestamp": { "type": "date" } } } } } PUT _component_template/other_component_template { "template": { "mappings": { "properties": { "ip_address": { "type": "ip" } } } } } PUT _index_template/template_1 { "index_patterns": ["te*", "bar*"], "template": { "settings": { "number_of_shards": 1 }, "mappings": { "_source": { "enabled": false }, "properties": { "host_name": { "type": "keyword" }, "created_at": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss.SSS" } } }, "aliases": { "mydata": { } } }, "priority": 10, "composed_of": ["component_template1", "other_component_template"], "version": 3, "_meta": { "description": "my custom" } } index_template 创建时,如果它包含的 component_template与component_template 或 component_template与index_template 中properties存在重复的属性,则index_template创建会报错。
查看所有索引模板
GET _index_template 示例

查看单个索引模板
GET _index_template/template_name 示例

查看多个索引模板(通配符)
GET _index_template/template_pattern 示例

查看组件模板
GET _component_template/component_template_name 删除索引模板
DELETE _index_template/template_name 删除组件模板
DELETE _component_template/component_template_name 概述
如果某个字段没有设置映射,那么在新建文档时,会自动根据字段来生成映射,比如:日期和数字。
示例
配置方法
默认这种类型检测是开启的,配置方法如下:
PUT _index_template/template_name { "index_patterns": ["test*"], "priority": 1, "settings": { "number_of_shards": 1, "number_of_replicas": 2 }, "mappings": { "date_detection": false, "numeric_detection": true } }
上一篇:vite 创建vue3项目,使用 Prettier 统一格式化代码,集成 ESLint、Stylelint 代码校验规范
下一篇:解决“Module build failed (from ./node_modules/sass-loader/dist/cjs.js)“错误