首页/数据/NoSQL 速查/索引模板与组件模板
Elasticsearch89 条

索引模板与组件模板

索引模板按名称模式把 settings、mappings、aliases 自动应用到新建索引;组合式模板(composable)支持组件模板复用,并按 priority 决定冲突时谁生效。

NoSQL 速查数据更新于 2026-10-12
接口PUT /_index_template/<name>
匹配index_patterns 支持通配符;priority 大者优先
组成template 里写 settings/mappings/aliases;composed_of 引用组件模板

示例

示例
PUT /_index_template/logs_template
{
  "index_patterns": ["logs-*"],
  "priority": 500,
  "template": {
    "settings": { "number_of_shards": 3, "number_of_replicas": 1 },
    "mappings": {
      "properties": {
        "@timestamp": { "type": "date" },
        "message": { "type": "text" }
      }
    },
    "aliases": { "logs": {} }
  }
}
模板只影响创建时刻:已存在的索引不会被补写配置,需要滚动到新索引或用 rollover 生效。

易错点

  • template 与 legacy 模板混用容易冲突,新集群统一用 composable 模板
  • 模板里写错 mapping 类型,新索引会自动继承错误定义,排查先 GET /_index_template 核对

数据来源与参考

  • Elasticsearch Guide: Index templates
  • Elasticsearch Guide: Composable templates
​‌‌​​‌​​​‌‌​‌‌‌‌​‌‌​​​‌‌​‌‌‌​​‌‌​​‌​‌‌‌​​‌‌‌​​​‌​‌‌​‌​​‌​‌‌‌​​​‌​‌‌​‌​​‌​‌‌​​​‌‌​‌‌‌​‌​​​‌‌​‌‌‌‌​​‌​‌‌‌​​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌​‌