ES系列--索引模板

前言

索引模板定义了 在创建新索引时可以自动应用的设置和映射。这里来介绍下索引模板的使用。
官网文档入口:https://www.elastic.co/guide/en/elasticsearch/reference/7.8/index-templates.html

正文

注意:7.8版本以后使用_index_template ,_component_template 来代替 _template。文中出现的旧模板指的就是 _template。
但是用法还是一致的。

其它注意点:
1.索引模板仅在创建索引期间应用。索引模板的更改不会影响现有索引。
2.在创建索引API请求中指定的设置和映射会覆盖在索引模板中指定的所有设置或映射。
3.可组合模板始终优先于旧模板。如果没有可组合模板与新索引匹配,则根据其顺序应用匹配的旧模板。

7.8版本以前的创建或更新模板

PUT _template/template_1?create=false&order=0
{
  "index_patterns": [ "te*", "bar*" ],
  "settings": {
    "number_of_shards": 1
  },
  "order": 0,
  "mappings": {
    "_source": {
      "enabled": false
    },
    "properties": {
      "host_name": {
        "type": "keyword"
      },
      "created_at": {
        "type": "date",
        "format": "EEE MMM dd HH:mm:ss Z yyyy"
      }
    }
  },
  "aliases" : {
    "alias1" : {},
    "alias2" : {
      "filter" : {
        "term" : {"user.id" : "kimchy" }
      },
      "routing" : "shard-1"
    },
    "{index}-alias": {} 
  }
}

这里只有 index_patterns 是必选参数,其它参数都是可选的。下面介绍下它们的用法:

Query parameters
create: 如果为true,则此请求无法替换或更新现有索引模板。默认为false。
order: 如果索引与多个模板匹配,则Elasticsearch应用此模板的顺序。较低order值的模板将首先合并。较高order值的模板稍后合并,覆盖较低值的模板。order相同的情况下是最终结果是随机的。 也可以放在请求正文里。 默认为0。
master_timeout: 指定等待连接到主节点的时间段。如果在超时到期之前未收到任何响应,则请求将失败并返回错误。默认为30s。

Request body
index_patterns: 用于在创建过程中匹配索引名称的通配符表达式数组。
aliases: 索引别名。 在上面示例中,占位符{index}将会在创建索引的时候由实际的索引名称替换掉。
mappings: 索引映射。
settings: 索引配置。
version: 索引版本。这个不是es自动生成的,是由我们自己传参的。

7.8版本以后:

创建和更新模板

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": "EEE MMM dd HH:mm:ss Z yyyy"
        }
      }
    },
    "aliases": {
      "mydata": { }
    }
  },
  "priority": 10,
  "composed_of": ["component_template1", "other_component_template"],
  "version": 3,
  "_meta": {
    "description": "my custom"
  }
}

优先级:
1.如果新索引匹配多个索引模板,则使用优先级最高的索引模板。(_index_template是 priority;_template是order)
2.当可组合模板与给定索引匹配时,它始终优先于旧模板。如果没有可组合模板匹配,则旧版模板可能仍匹配并被应用。
3.如果使用显式设置创建索引并且该索引也与索引模板匹配,则创建索引请求中的设置将优先于索引模板及其组件模板中指定的设置。

注意:
index_template 创建时,如果它包含的 component_template与component_template 或者 component_template与index_template 中properties里存在重复的属性,则index_template创建会报错。

查看模板信息

查看模板信息

# 查看所有的模板
GET _index_template
GET _index_template/template_1
GET _component_template/component_template1

删除模板

DELETE _index_template/template_1
DELETE _component_template/component_template1  

想要了解更多,可以通过 elk相关知识地图 来找到你想要了解的部分进行查看。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值