注: 部分概念介绍来源于网络

1、什么是索引模板
索引模板: 就是把已经创建好的某个索引的参数设置(settings)和索引映射(mapping)保存下来作为模板, 在创建新索引时, 指定要使用的模板名, 就可以直接重用已经定义好的模板中的设置和映射。
1.1、索引模板的用途
索引模板一般用在时间序列相关的索引中,也就是说, 如果你需要每间隔一定的时间就建立一次索引, 你只需要配置好索引模板, 以后就可以直接使用这个模板中的设置, 不用每次都设置settings和mappings.
索引模板一般与索引别名一起使用。

索引模板有多个级别"order": 0,相同设置(setting/mapping/...)优先采用高级别索引模板,采用完高级别索引模板后,也会采用下面的低级别其它索引设置
2、创建索引模板
(1) ES 6.0之前的版本:

PUT _template/my_template
 {
     "template": "my*",       // 可以通过"my*"来适配
     "order": 0,                // 模板的权重, 多个模板的时候优先匹配用, 值越大, 权重越高
     "settings": {
         "number_of_shards": 1  // 分片数量, 可以定义其他配置项
         "number_of_replicas": "1",        // 副本数
         "refresh_interval": "5s"          // 刷新时间
     },
     "aliases": {
         "alias_1": {}          // 索引对应的别名
     },
     "mappings": {
         "_default": {          // 默认的配置, ES 6.0开始不再支持
             "_source": { "enabled": false },  // 是否保存字段的原始值
             "_all": { "enabled": false },     // 禁用_all字段
             "dynamic": "strict"               // 只用定义的字段, 关闭默认的自动类型推断
         },
         "type1": {             // 默认的文档类型设置为type1, ES 6.0开始只支持一种type, 所以这里不需要指出
         */
             "_source": {"enabled": false},
             "properties": {        // 字段的映射
                 "@timestamp": {    // 具体的字段映射
                     "type": "date",           
                     "format": "yyyy-MM-dd HH:mm:ss"
                 },
                 "@version": {
                     "doc_values": true,
                     "index": "not_analyzed",  // 不索引
                     "type": "string"          // string类型
                 },
                 "logLevel": {
                     "type": "long"
                 }
             }
         }
     }
 }


(2) ES 6.0之后的版本:

PUT _template/my_template
 {
     "index_patterns": ["my*", "index*"],       // 可以通过"my*"和"index*"来适配, template字段已过期
     "order": 0,                // 模板的权重, 多个模板的时候优先匹配用, 值越大, 权重越高
     "settings": {
         "number_of_shards": 1,  // 分片数量, 可以定义其他配置项
         "number_of_replicas": "1",        // 副本数
         "refresh_interval": "5s"          // 刷新时间
     },
     "aliases": {
         "alias_1": {}          // 索引对应的别名
     },
     "mappings": {
         // ES 6.0开始只支持一种type, 名称为“_doc”
         "_doc": {
             "_source": {            // 是否保存字段的原始值
                 "enabled": false
             },
             "properties": {        // 字段的映射
                 "@timestamp": {    // 具体的字段映射
                     "type": "date",           
                     "format": "yyyy-MM-dd HH:mm:ss"
                 },
                 "@version": {
                     "doc_values": true,
                     "index": "false",   // 设置为false, 不索引
                     "type": "text"      // text类型
                 },
                 "logLevel": {
                     "type": "long"
                 }
             }
         }
     }
 }


3、查看索引模板
(1) 查看示例:

GET _template                // 查看所有模板
 GET _template/temp*          // 查看与通配符相匹配的模板
 GET _template/temp1,temp2    // 查看多个模板
 GET _template/my_template  // 查看指定模板


(2) 判断模板是否存在:
判断示例:

HEAD _template/my_template


结果说明:
a) 如果存在, 响应结果是: 200 - OK
b) 如果不存在, 响应结果是: 404 - Not Found
4、删除索引模板
删除示例:

DELETE _template/my_template    // 删除上述创建的模板


如果模板不存在, 将抛出如下错误:

{
   "error" : {
     "root_cause" : [
       {
         "type" : "index_template_missing_exception",
         "reason" : "index_template [my_template] missing"
       }
     ],
     "type" : "index_template_missing_exception",
     "reason" : "index_template [my_template] missing"
   },
   "status" : 404
 }


5、修改(更新)索引模板
GET _template/my_template
复制结果,再PUT
PUT _template/my_template