NoSQL 与搜索 / 系统课时
索引模板
索引模板
索引模板用于保证新索引按统一规则创建,避免每次手工重复设置分片、映射和别名。本课将拆分可复用的组件模板,再组装索引模板,并通过模拟 API 与自动创建策略验证最终配置。
学习目标
- 区分组件模板与索引模板的职责
- 复用设置、映射和别名并理解配置覆盖顺序
- 模拟模板结果并控制匹配索引是否允许自动创建
模板如何生效
组件模板是可复用的配置片段,可以保存设置、映射和别名;索引模板通过 index_patterns 匹配新索引,再用 composed_of 组合多个组件模板。模板只在索引或数据流创建时应用,修改模板不会自动更新已经存在的索引。:contentReference[oaicite:2]{index=2}
当多个索引模板同时匹配时,优先级最高的模板生效。组件模板按 composed_of 顺序合并,后面的组件覆盖前面的同名配置;随后合并索引模板自身的 template;创建索引请求中显式提供的配置优先级最高。:contentReference[oaicite:3]{index=3}

创建组件模板
先把所有日志索引共享的分片设置和字段映射拆成两个组件。以下请求可在 Kibana Dev Tools 中执行,调用者需要具备管理索引模板的集群权限。
PUT _component_template/ct-app-logs-settings
{
"template": {
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1,
"index.refresh_interval": "5s"
}
},
"_meta": {
"description": "应用日志通用设置"
}
}
PUT _component_template/ct-app-logs-mappings
{
"template": {
"mappings": {
"dynamic": "strict",
"properties": {
"@timestamp": {
"type": "date"
},
"service": {
"properties": {
"name": {
"type": "keyword"
}
}
},
"message": {
"type": "text"
}
}
}
},
"_meta": {
"description": "应用日志通用字段"
}
}预期结果:两个请求都返回同样的确认响应。
{
"acknowledged": true
}dynamic: strict 会拒绝未声明字段,适合字段治理要求明确的日志模型。若上游字段经常变化,应先设计动态模板或改为更宽松的策略,避免正常写入被阻断。
组装索引模板
索引模板匹配 app-logs-*,组合前面的两个组件,并为每个新索引添加只读查询别名。priority 用于解决多个模板同时匹配时的选择问题,version 仅供外部发布流程管理,不会自动递增。
PUT _index_template/it-app-logs
{
"index_patterns": [
"app-logs-*"
],
"priority": 200,
"version": 1,
"composed_of": [
"ct-app-logs-settings",
"ct-app-logs-mappings"
],
"template": {
"aliases": {
"app-logs-read": {}
}
},
"allow_auto_create": false,
"_meta": {
"owner": "search-platform"
}
}预期结果:
{
"acknowledged": true
}这里把 allow_auto_create 设为 false,因此任何匹配该模板的索引都必须显式创建。该字段可以覆盖集群级自动创建设置,适合防止拼写错误意外生成新索引。:contentReference[oaicite:4]{index=4}
模拟最终配置
在真正创建索引前,使用模拟 API 查看名称 app-logs-2026.08.03 会匹配到什么配置。模拟不会创建索引,适合在发布模板前检查合并结果和冲突模板。:contentReference[oaicite:5]{index=5}
POST _index_template/_simulate_index/app-logs-2026.08.03?filter_path=template.settings,template.mappings,template.aliases预期结果节选:
{
"template": {
"settings": {
"index": {
"number_of_shards": "1",
"number_of_replicas": "1",
"refresh_interval": "5s"
}
},
"mappings": {
"dynamic": "strict",
"properties": {
"@timestamp": {
"type": "date"
},
"service": {
"properties": {
"name": {
"type": "keyword"
}
}
},
"message": {
"type": "text"
}
}
},
"aliases": {
"app-logs-read": {}
}
}
}这里使用 filter_path 只保留合并后的关键配置。需要排查模板冲突时移除该参数,再检查响应中的 overlapping;若字段类型或设置不符合预期,则按“组件顺序 → 索引模板自身配置 → 创建索引请求”的顺序定位覆盖来源。
控制自动创建边界
集群级 action.auto_create_index 可以使用允许与拒绝模式控制自动创建。下面的测试配置只允许 app-logs-*,拒绝其他普通索引;生产环境使用前必须补充 Elastic Stack 所需的系统索引模式,不能直接照搬。
PUT _cluster/settings
{
"persistent": {
"action.auto_create_index": "+app-logs-*,-*"
}
}预期结果:
{
"persistent": {
"action": {
"auto_create_index": "+app-logs-*,-*"
}
},
"transient": {}
}即使集群允许 app-logs-* 自动创建,本课模板中的 allow_auto_create: false 仍会要求显式创建。若业务确实依赖首次写入自动建索引,应删除该字段或明确设为 true,并在模拟和测试环境验证匹配范围。
本课总结
- 组件模板负责复用配置片段,索引模板负责匹配名称、组合组件并应用到新索引
- 配置覆盖顺序必须明确,创建索引请求中的显式设置优先级最高
- 发布前应使用模拟 API 检查最终结果,并同时治理模板级与集群级自动创建策略
下一课预告
下一课将进一步把索引模板与生命周期策略结合,让索引按容量或时间自动滚动、迁移和清理。