索引模板
索引模板允许用户使用预定义的映射和设置初始化新索引。例如,如果用户持续索引日志数据,可以定义一个索引模板,以便所有这些索引具有相同的分片和副本数量。
创建模板
要创建索引模板,请使用 PUT 或 POST 请求:
PUT _index_template/<template name>
POST _index_template/<template name>
此命令创建一个名为 daily_logs 的模板,并将其应用于任何名称匹配模式 logs-2020-01-* 的新索引,并将其添加到 my_logs 别名:
PUT _index_template/daily_logs
{
"index_patterns": [
"logs-2020-01-*"
],
"template": {
"aliases": {
"my_logs": {}
},
"settings": {
"number_of_shards": 2,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"timestamp": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
},
"value": {
"type": "double"
}
}
}
}
}
用户应该看到以下响应:
{
"acknowledged": true
}
如果用户创建一个名为 logs-2020-01-01 的索引,用户可以看到它具有模板中的映射和设置:
PUT logs-2020-01-01
GET logs-2020-01-01
{
"logs-2020-01-01": {
"aliases": {
"my_logs": {}
},
"mappings": {
"properties": {
"timestamp": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
},
"value": {
"type": "double"
}
}
},
"settings": {
"index": {
"replication": {
"type": "DOCUMENT"
},
"number_of_shards": "2",
"provided_name": "logs-2020-01-01",
"creation_date": "1764042201208",
"number_of_replicas": "1",
"uuid": "35w7hDAqQl2oh2_r6DTIbg",
"version": {
"created": "136467827"
}
}
}
}
}
任何匹配此模式的额外索引——logs-2020-01-02、logs-2020-01-03 等——将继承相同的映射和设置。
索引模式不能包含以下任何字符::, ", +, /, \, |, ?, #, >, 和 <。
检索模板
要列出所有索引模板:
GET _cat/templates
GET /_index_template
要按名称查找模板:
GET _index_template/daily_logs
要获取匹配模式的模板列表:
GET _index_template/daily*
要检查特定模板是否存在:
HEAD _index_template/<name>
配置多个模板
用户可以为索引创建多个索引模板。如果索引名称匹配多个模板,UDB-SX 将采用具有最高优先级的模板的映射和设置,并将其应用于索引。
例如,假用户有以下两个模板,它们都匹配 logs-2020-01-02 索引,并且在 number_of_shards 字段中存在冲突:
模板 1
PUT _index_template/template-01
{
"index_patterns": [
"logs*"
],
"priority": 0,
"template": {
"settings": {
"number_of_shards": 2,
"number_of_replicas": 2
}
}
}
模板 2
PUT _index_template/template-02
{
"index_patterns": [
"logs-2020-01-*"
],
"priority": 1,
"template": {
"settings": {
"number_of_shards": 3
}
}
}
priority 数值越大优先级越高, template-02 具有更大的 priority 值,优先于 template-01。logs-2020-01-02 索引的 number_of_shards 值为 3,number_of_replicas 为默认值 1。
删除模板
用户可以使用名称删除索引模板:
DELETE _index_template/daily_logs
可组合索引模板
管理多个索引模板存在以下挑战:
如果用户的索引模板之间存在重复,存储这些索引模板会导致更大的集群状态。
如果用户想在所有索引模板中进行更改,则必须手动为每个模板进行更改。
用户可以使用可组合索引模板来克服这些挑战。可组合索引模板允许用户将常见的设置、映射和别名抽象为一个可重用的构建块,称为组件模板。
用户可以组合组件模板来组成一个索引模板。
用户在创建索引请求中直接指定的设置和映射会覆盖索引模板及其组件模板中指定的任何设置或映射。
创建组件模板
定义两个组件模板——component_template_1 和 component_template_2:
组件模板 1
PUT _component_template/component_template_1
{
"template": {
"mappings": {
"properties": {
"@timestamp": {
"type": "date"
}
}
}
}
}
组件模板 2
PUT _component_template/component_template_2
{
"template": {
"mappings": {
"properties": {
"ip_address": {
"type": "ip"
}
}
}
}
}
使用组件模板创建索引模板
创建索引模板时,需要在 composed_of 列表中包含组件模板。
UDB-SX 按照用户在索引模板中指定的顺序应用组件模板。用户在索引模板内直接指定的设置、映射和别名最后应用。
PUT _index_template/daily_logs
{
"index_patterns": [
"logs-2020-01-*"
],
"template": {
"aliases": {
"my_logs": {}
},
"settings": {
"number_of_shards": 2,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"timestamp": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
},
"value": {
"type": "double"
}
}
}
},
"priority": 200,
"composed_of": [
"component_template_1",
"component_template_2"
],
"version": 3,
"_meta": {
"description": "using component templates"
}
}
如果用户创建一个名为 logs-2020-01-01 的索引,用户可以看到它从两个组件模板中继承了映射和设置:
PUT logs-2020-01-02
GET logs-2020-01-02
示例响应
{
"logs-2020-01-02": {
"aliases": {
"my_logs": {}
},
"mappings": {
"properties": {
"@timestamp": {
"type": "date"
},
"ip_address": {
"type": "ip"
},
"timestamp": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
},
"value": {
"type": "double"
}
}
},
"settings": {
"index": {
"replication": {
"type": "DOCUMENT"
},
"number_of_shards": "2",
"provided_name": "logs-2020-01-02",
"creation_date": "1764052786236",
"number_of_replicas": "1",
"uuid": "itg2h14BTGeaYMsUGYbUnw",
"version": {
"created": "136467827"
}
}
}
}
}
索引模板选项
用户可以指定以下模板选项:
| 选项 | 类型 | 描述 | 必需 |
|---|---|---|---|
template |
Object |
指定索引设置、映射和别名。 | 否 |
priority |
Integer |
索引模板的优先级。 | 否 |
composed_of |
String array |
与新索引一起应用的组件模板名称列表。 | 否 |
version |
Integer |
指定版本号以简化模板管理。默认值为 null。 |
否 |
_meta |
Object |
指定关于模板的元信息。 | 否 |