模板类型
模板有两种类型:索引模板和组件模板。
- 组件模板是可重用的构建块,用于配置映射,设置和别名;它们不会直接应用于一组索引。
索引模板可以包含组件模板的集合,也可以直接指定设置,映射和别名。
索引模板中的优先级
可组合模板优先于旧模板。如果没有可组合模板匹配给定索引,则旧版模板可能仍匹配并被应用。
- 如果使用显式设置创建索引并且该索引也与索引模板匹配,则创建索引请求中的设置将优先于索引模板及其组件模板中指定的设置。
如果新数据流或索引与多个索引模板匹配,则使用优先级最高的索引模板。
内置索引模板
Elasticsearch具有内置索引模板,每个索引模板的优先级为100,适用于以下索引模式:
logs--
- metrics--
- synthetics--
所以在涉及内建索引模板时,要避免索引模式冲突。更多可以参考这里
索引模板
- 索引模板,主要用于在新建索引时自动应用预先设定的配置,简化索引创建的操作步骤
- 可以设定索引的setting和mapping
- 可以有多个模板
- 索引模板API,endpoint为 _template
# 创建索引模板,匹配 test-index-map 开头的索引
PUT _template/template_1
{
"index_patterns": ["test-index-map*"],
"priority" : 1, # 指定优先级, 数值越大优先级越高, 这个模板就越先被应用
"settings": {
"number_of_shards": 1
},
"mappings": {
"doc": {
"_source": {
"enabled": false
},
"properties": {
"name": {
"type": "keyword"
},
"created_at": {
"type": "date",
"format": "YYYY/MM/dd HH:mm:ss"
}
}
}
}
}
# 插入一个文档
POST test-index-map_1/doc
{
"name" : "小旋锋",
"created_at": "2018/08/16 20:11:11"
}
# 获取该索引的信息,可以发现 settings 和 mappings 和索引模板里设置的一样
GET test-index-map_1
# 删除
DELETE /_template/template_1
# 查询
GET /_template/template_1
参数 | 参数类型 | 是否必传 | 描述 |
---|---|---|---|
index_patterns | 字符串数组 | 是 | 通配符表达式数组,用于在创建过程中匹配数据流和索引的名称。 |
data_stream | 对象 | 否 | 指示模板是否用于创建数据流及其支持索引 |
template | 对象 | 否 | 应用的模板。它可任选地包括一个aliases,mappings或 settings配置,这些参数和创建索引的时候配置的一样 |
composed_of | 字符串数组 | 否 | 组件模板名称的有序数组。组件模板按照指定的顺序合并,这意味着指定的最后一个组件模板具有最高优先级 |
priority | 整数 | 否 | 确定索引模板优先级的优先级,如果未指定优先级,则将模板视为优先级为0(最低优先级) |
version | 整数 | 否 | 用于从外部管理索引模板的版本号,用户自定义 |
_meta | 对象 | 否 | 有关索引模板的可选用户元数据,用户自定义 |
获取所有索引模板
GET http://172.16.1.236:9201/_index_template
索引模板topic_template是否存在 | # 返回200 代表存在,404 代表不存在
HEAD http://172.16.1.236:9201/_index_template/topic_template | | —- |删除topic_template索引模板 | DELETE http://172.16.1.236:9201/_index_template/topic_template
| | —- |
组件模板
- 首先创建两个索引组件模板:
```java
PUT _component_template/component_template1
{
“template”: {
“mappings”: {
} } }"properties": { "@timestamp": { "type": "date" } }
PUT _component_template/runtime_component_template { “template”: { “mappings”: { “runtime”: { “day_of_week”: { “type”: “keyword”, “script”: { “source”: “emit(doc[‘@timestamp’].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT))” } } } } } }
执行结果如下<br />
- **创建使用组件模板的索引模板**
```java
PUT _index_template/template_1
{
"index_patterns": ["bar*"],
"template": {
"settings": {
"number_of_shards": 1
},
"mappings": {
"_source": {
"enabled": true
},
"properties": {
"host_name": {
"type": "keyword"
},
"created_at": {
"type": "date",
"format": "EEE MMM dd HH:mm:ss Z yyyy"
}
}
},
"aliases": {
"mydata": { }
}
},
"priority": 500,
"composed_of": ["component_template1", "runtime_component_template"],
"version": 3,
"_meta": {
"description": "my custom"
}
}
- 创建一个匹配bar*的索引bar-test ```java PUT /bar-test
然后获取mapping
```java
GET /bar-test/_mapping
模拟某个索引结果
比如上面的template_1, 我们不用创建bar*的索引(这里模拟bar-pdai-test),也可以模拟计算出索引的配置:
POST /_index_template/_simulate_index/bar-pdai-test
模拟组件模板结果
当然,由于template_1模板是由两个组件模板组合的,我们也可以模拟出template_1被组合后的索引配置:
POST /_index_template/_simulate/template_1
执行结果如下:
{
"template" : {
"settings" : {
"index" : {
"number_of_shards" : "1"
}
},
"mappings" : {
"runtime" : {
"day_of_week" : {
"type" : "keyword",
"script" : {
"source" : "emit(doc['@timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ROOT))",
"lang" : "painless"
}
}
},
"properties" : {
"@timestamp" : {
"type" : "date"
},
"created_at" : {
"type" : "date",
"format" : "EEE MMM dd HH:mm:ss Z yyyy"
},
"host_name" : {
"type" : "keyword"
}
}
},
"aliases" : {
"mydata" : { }
}
},
"overlapping" : [ ]
}
模拟组件模板和自身模板结合后的结果
- 新建两个模板
```java
PUT /_component_template/ct1
{
“template”: {
“settings”: {
} } }"index.number_of_shards": 2
PUT /_component_template/ct2 { “template”: { “settings”: { “index.number_of_replicas”: 0 }, “mappings”: { “properties”: { “@timestamp”: { “type”: “date” } } } } }
模拟在两个组件模板的基础上,添加自身模板的配置
```java
POST /_index_template/_simulate
{
"index_patterns": ["my*"],
"template": {
"settings" : {
"index.number_of_shards" : 3
}
},
"composed_of": ["ct1", "ct2"]
}
执行的结果如下
{
"template" : {
"settings" : {
"index" : {
"number_of_shards" : "3",
"number_of_replicas" : "0"
}
},
"mappings" : {
"properties" : {
"@timestamp" : {
"type" : "date"
}
}
},
"aliases" : { }
},
"overlapping" : [ ]
}