介绍
在新建索引/自动新建索引时,若索引名符合索引模板的条件,则会按照模板创建模板。
相关官方文档:
- https://www.elastic.co/guide/en/elasticsearch/reference/current/index-templates.html
- https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates-v1.html
命令示例
查询所有模板信息:
GET _index_template
查询指定模板信息:
GET _index_template/template_1
GET _index_template/template*
删除模板:
DELETE /_index_template/template_1
操作实战1:基础示例
创建模板,匹配 stu
开头的索引:
PUT /_index_template/template_1
{
"index_patterns" : ["stu*"],
"template": {
"mappings" : {
"properties" : {
"name" : { "type" : "keyword" },
"age" : { "type" : "integer" }
}
},
"settings" : {
"number_of_shards" : 2
}
}
}
查询所有模板信息:
GET _index_template
创建索引时会自动匹配模板:
# 创建索引
PUT student
# 查询索引结构
GET student
# 查询结果
{
"student" : {
"aliases" : { },
"mappings" : {
"properties" : {
"age" : {
"type" : "integer"
},
"name" : {
"type" : "keyword"
}
}
},
"settings" : {
"index" : {
"routing" : {
"allocation" : {
"include" : {
"_tier_preference" : "data_content"
}
}
},
"number_of_shards" : "2",
"provided_name" : "student",
"creation_date" : "1634356238830",
"number_of_replicas" : "1",
"uuid" : "DiR_5wuNQEqoMjVzRXN_RQ",
"version" : {
"created" : "7150099"
}
}
}
}
}
如果创建索引时,指定的部分数据属性和配置,则优先使用。命中的模板会作为补充:
PUT student_2
{
"mappings" : {
"properties" : {
"name" : {
"type" : "text"
},
"height" : {
"type" : "long"
}
}
},
"settings" : {
"index" : {
"number_of_shards" : 1,
"number_of_replicas" : 2
}
}
}
# 查询索引结构
GET student_2
# 查询结果
{
"student_2" : {
"aliases" : { },
"mappings" : {
"properties" : {
"age" : {
"type" : "integer" # 通过模板增加该字段
},
"height" : {
"type" : "long"
},
"name" : {
"type" : "text"
}
}
},
"settings" : {
"index" : {
"routing" : {
"allocation" : {
"include" : {
"_tier_preference" : "data_content"
}
}
},
"number_of_shards" : "1", // 没有使用模板指定的2
"provided_name" : "student_2",
"creation_date" : "1634518830185",
"number_of_replicas" : "2",
"uuid" : "QD9n3uPcSiud4d3fqayZOg",
"version" : {
"created" : "7150099"
}
}
}
}
}
删除模板:
# 请求
DELETE /_index_template/template_1
# 响应
{
"acknowledged" : true
}
操作实战2:模板优先级
如果索引名,匹配了两个模板,会如何?
我们创建两个模板:
PUT /_index_template/template_1
{
"index_patterns" : ["stu*"],
"template": {
"mappings" : {
"properties" : {
"name" : { "type" : "keyword" },
"age" : { "type" : "integer" }
}
},
"settings" : {
"number_of_shards" : 2
}
}
}
PUT /_index_template/template_2
{
"index_patterns" : ["stud*"],
"template": {
"mappings" : {
"properties" : {
"age" : { "type" : "long" },
"height" : { "type" : "long" }
}
},
"settings" : {
"number_of_shards" : 3
}
}
}
// 报错如下:
{
"error" : {
"root_cause" : [
{
"type" : "illegal_argument_exception",
"reason" : "index template [template_2] has index patterns [stud*] matching patterns from existing templates [template_1] with patterns (template_1 => [stu*]) that have the same priority [0], multiple index templates may not match during index creation, please use a different priority"
}
],
"type" : "illegal_argument_exception",
"reason" : "index template [template_2] has index patterns [stud*] matching patterns from existing templates [template_1] with patterns (template_1 => [stu*]) that have the same priority [0], multiple index templates may not match during index creation, please use a different priority"
},
"status" : 400
}
如果 index_patterns 有相同的前缀,要指定 **priority**
。**priority**
** 默认值为0,代表最低优先级。创建索引时,使用最高优先级的模板。**
下面的方式创建,则没有问题:
PUT /_index_template/template_2
{
"index_patterns" : ["stud*"],
"priority": 1,
"template": {
"mappings" : {
"properties" : {
"age" : { "type" : "long" },
"height" : { "type" : "long" }
}
},
"settings" : {
"number_of_shards" : 3
}
}
}
创建索引:
PUT student
查询索引结构:
{
"student" : {
"aliases" : { },
"mappings" : {
"properties" : {
"age" : {
"type" : "long"
},
"height" : {
"type" : "long"
}
}
},
"settings" : {
"index" : {
"routing" : {
"allocation" : {
"include" : {
"_tier_preference" : "data_content"
}
}
},
"number_of_shards" : "3",
"provided_name" : "student",
"creation_date" : "1634606600477",
"number_of_replicas" : "1",
"uuid" : "6tjI9CKAQGWGKpA3a1NFKQ",
"version" : {
"created" : "7150099"
}
}
}
}
}
操作实例3:更新索引/仅创建索引
使用 PUT /_index_template/<template_name>
时,若之前模板已存在则会覆盖。 使用 PUT /_index_template/<template_name>?create
时,若之前模板已存在则会报错。
示例:
# 创建
PUT /_index_template/template_1
{
"index_patterns" : ["stu*"], # 匹配 stu 开头的索引
"template": {
"mappings" : {
"properties" : {
"name" : { "type" : "keyword" },
"age" : { "type" : "integer" } # 类型是 integer
}
},
"settings" : {
"number_of_shards" : 2
}
}
}
GET /_index_template/template_1
# 查询结果:
{
"index_templates" : [
{
"name" : "template_1",
"index_template" : {
"index_patterns" : [
"stu*"
],
"template" : {
"settings" : {
"index" : {
"number_of_shards" : "2"
}
},
"mappings" : {
"properties" : {
"name" : {
"type" : "keyword"
},
"age" : {
"type" : "integer"
}
}
}
},
"composed_of" : [ ]
}
}
]
}
# 再次创建
PUT /_index_template/template_1
{
"index_patterns" : ["stud*"], # 匹配 stud 开头的索引
"template": {
"mappings" : {
"properties" : {
"name" : { "type" : "keyword" },
"age" : { "type" : "long" } # 类型是 long ,和之前不一样
}
},
"settings" : {
"number_of_shards" : 2
}
}
}
// 再次查看
GET /_index_template/template_1
// 查询结果
{
"index_templates" : [
{
"name" : "template_1",
"index_template" : {
"index_patterns" : [
"stud*"
],
"template" : {
"settings" : {
"index" : {
"number_of_shards" : "2"
}
},
"mappings" : {
"properties" : {
"name" : {
"type" : "keyword"
},
"age" : {
"type" : "long"
}
}
}
},
"composed_of" : [ ]
}
}
]
}
// 追加 ?create 参数创建索引
PUT /_index_template/template_1?create
{
"index_patterns" : ["stud*"],
"template": {
"mappings" : {
"properties" : {
"name" : { "type" : "keyword" },
"age" : { "type" : "long" }
}
},
"settings" : {
"number_of_shards" : 2
}
}
}
# 会报错如下:
{
"error" : {
"root_cause" : [
{
"type" : "illegal_argument_exception",
"reason" : "index template [template_1] already exists"
}
],
"type" : "illegal_argument_exception",
"reason" : "index template [template_1] already exists"
},
"status" : 400
}
组合模板
从 ES 7.8 引入了组合模板。 具体可以参考:https://www.elastic.co/guide/en/elasticsearch/reference/current/index-templates.html