前言
索引模板的功能可以让你在创建索引时依据自定义模板的规则来生成,这样可以有效的帮助你管理索引,模板内容可以包含setting和mapping,以及控制哪些索引需要按照模板生成,哪些不需要。
- 模板仅仅在索引被创建时生效,修改模板不会影响已经被创建的索引
- 当使用正常创建索引API时,作为创建索引调用一部分定义的setting/mapping将优先于模板中定义的任何匹配setting/mapping。
创建索引模板
定义一个名为template_1的模板,setting和mapping将应用于匹配te或bar模式的任何索引名。
模板定义主分片和副本分片都为1,mapping设置了source为不可见,并且分别约定了host_name和created_at两个属性的type和format。
PUT _template/template_1
{
"index_patterns": ["te*", "bar*"],
"settings": {
"number_of_shards": 1,
"number_of_replicas":1
},
"mappings": {
"_doc": {
"_source": {
"enabled": false
},
"properties": {
"host_name": {
"type": "keyword"
},
"created_at": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
}
}
}
}
}
创建一个名为test的索引,符合te*的规则
PUT /test/
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "test"
}
查看索引信息
GET /test/
{
"test": {
"aliases": {},
"mappings": {
"_doc": {
"_source": {
"enabled": false
},
"properties": {
"created_at": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"host_name": {
"type": "keyword"
}
}
}
},
"settings": {
"index": {
"creation_date": "1622597378559",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "7l-6e-rxSOC2RCeTNciWmg",
"version": {
"created": "6040099"
},
"provided_name": "test"
}
}
}
}
插入一条文档
PUT /test/_doc/1
{
"created_at":"2020-01-01 00:00:00"
}
{
"_index": "test",
"_type": "_doc",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
查看索引信息
GET /test/_doc/_search
{
"query": {
"match_all": {}
}
}
source信息不可见
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "test",
"_type": "_doc",
"_id": "1",
"_score": 1
}
]
}
}
查看模板信息
GET /_template/template_1
{
"template_1": {
"order": 0,
"index_patterns": [
"te*",
"bar*"
],
"settings": {
"index": {
"number_of_shards": "1",
"number_of_replicas": "1"
}
},
"mappings": {
"_doc": {
"_source": {
"enabled": false
},
"properties": {
"host_name": {
"type": "keyword"
},
"created_at": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
}
}
}
},
"aliases": {}
}
}
判断模板是否存在
HEAD _template/template_1
200 - OK
删除模板
DELETE /_template/template_1
{
"acknowledged": true
}
再判断是否存在,返回404
404 - Not Found
多索引文档匹配
当有多个索引都匹配到了索引模板时,可以利用order来控制具体应用的模板,order大的会覆盖order小的。
举个例子,template_1指定source不可见,template_2指定可见
PUT /_template/template_1
{
"index_patterns" : ["*"],
"order" : 0,
"settings" : {
"number_of_shards" : 1
},
"mappings" : {
"_doc" : {
"_source" : { "enabled" : false }
}
}
}
PUT /_template/template_2
{
"index_patterns" : ["te*"],
"order" : 1,
"settings" : {
"number_of_shards" : 1
},
"mappings" : {
"_doc" : {
"_source" : { "enabled" : true }
}
}
}
创建并查询mytest索引,source不可见
PUT /mytest/
GET /mytest/
{
"mytest": {
"aliases": {},
"mappings": {
"_doc": {
"_source": {
"enabled": false
}
}
},
"settings": {
"index": {
"creation_date": "1622599058715",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "CLZtSzAVQbWq7j9ILNoizg",
"version": {
"created": "6040099"
},
"provided_name": "mytest"
}
}
}
}
创建并查看test索引,source可见
PUT /test/
GET /test/
{
"test": {
"aliases": {},
"mappings": {
"_doc": {
"properties": {
"host_name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
},
"settings": {
"index": {
"creation_date": "1622599082908",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "Pc3oZ3cAQuWjDSeV-nyPVg",
"version": {
"created": "6040099"
},
"provided_name": "test"
}
}
}
}