ElasticSearch中的索引模板详解

发布时间:2026/7/18 22:44:59
ElasticSearch中的索引模板详解 标准化配置统一同一类索引的分片数、副本数、刷新间隔等设置自动映射提前定义字段类型、分词器、是否索引等属性避免动态映射产生错误简化操作创建索引时自动应用配置无需每次重复写参数生命周期管理配合ILM索引生命周期管理实现索引的自动轮转、删除等操作从Elasticsearch 7.8版本开始引入了可组合索引模板Composable Index Template 和 组件模板Component Template同时兼容旧版模板Legacy Index Template类型版本支持核心结构优势劣势可组合模板Composable7.8推荐组件模板 索引模板配置复用、易维护、支持多租户、适配数据流学习成本略高旧版模板Legacy7.x 兼容单一配置结构简单直接无法复用、复杂场景难维护、8.x 逐步淘汰⚠️ 注意旧版模板在8.x版本中已经被移除建议7.x版本开始使用新的可组合模板体系为什么需要索引模板ES的索引创建默认会自动推断字段类型动态mapping但生产环境往往需要统一规范所有日志类索引统一设置1分片1副本不需要每次创建索引手动指定所有字符串类型字段默认同时创建text全文检索和keyword精确匹配两个类型所有时间字段统一用date类型指定时间格式业务专有字段统一使用IK分词器如果不用模板每次创建新索引都要手动写一遍配置不仅麻烦还容易出错尤其适合按时间滚动的索引场景比如日志、监控指标每天/每小时生成一个新索引log-2026.04.20、metric-2026.04.20。回到顶部旧版索引模板创建实战案例旧版索引模板比较简单粗暴它是直接为一类索引定义了一个模板后续直接使用即可创建一个日志场景的索引模板# 请求PUT _template/log_template_test{index_patterns: [log-*],order: 1,settings: {number_of_shards: 3,number_of_replicas: 1,refresh_interval: 30s},mappings: {properties: {timestamp: {type: date,format: yyyy-MM-dd HH:mm:ss||epoch_millis},message: {type: text,analyzer: ik_max_word,search_analyzer: ik_smart},level: { type: keyword },service_name: { type: keyword }}},aliases: {logs_all: {}}}# 参数解析log_template_test索引模板名称index_patterns指定模板匹配规则所有名字以 log- 开头的索引比如 log-2025-01-01、log-app-123自动应用这个模板order模板优先级数字越大优先级越高。如果多个模板匹配同一个索引order 大的覆盖小的settings指定索引设置number_of_shards索引主分片数量number_of_replicas索引副本分片数量refresh_interval数据写入后30 秒后才可以被搜索到调大提升写入性能调小提升实时性mappings指定字段映射规则定义每个字段的类型、分词、格式相当于给数据建表结构aliases给所有匹配该模板的索引自动绑定别名不用管索引是 log-2025-01 还是 log-2025-02直接查询 logs_all 就能查所有日志查看旧版本索引模板# 查看索引的索引模板GET _template# 查看指定的索引模板GET _template/log_template_test# 响应{log_template_test : {order : 1,index_patterns : [log-*],settings : {index : {number_of_shards : 3,number_of_replicas : 1,refresh_interval : 30s}},mappings : {properties : {timestamp : {format : yyyy-MM-dd HH:mm:ss||epoch_millis,type : date},level : {type : keyword},service_name : {type : keyword},message : {search_analyzer : ik_smart,analyzer : ik_max_word,type : text}}},aliases : {logs_all : { }}}}旧版本索引模板的删除和更新// 更新模板和创建语法一样覆盖原有配置即可PUT _template/log_template_test{// 新的配置}// 删除模板DELETE _template/log_template_test创建索引测试# 创建一个符合规则的索引测试PUT log-2026.04.20# 查看索引结构GET log-2026.04.20# 响应能够看到符合我们的索引模板结构{log-2026.04.20 : {aliases : {logs_all : { }},mappings : {properties : {timestamp : {type : date,format : yyyy-MM-dd HH:mm:ss||epoch_millis},level : {type : keyword},message : {type : text,analyzer : ik_max_word,search_analyzer : ik_smart},service_name : {type : keyword}}},settings : {index : {routing : {allocation : {include : {_tier_preference : data_content}}},refresh_interval : 30s,number_of_shards : 3,provided_name : log-2026.04.20,creation_date : 1776751447902,number_of_replicas : 1,uuid : DPR1wHFMRJuOUdHs-ZI-Hw,version : {created : 7172699}}}}}回到顶部新版本索引模板实战案例新版本索引模板是在ES7.8版本开始引入的新版本索引模板又称为可组合索引模板其主要有以下部分组成组件模板可组合索引模板优先级规则接下来我们一个一个讲解组件模板组件模板是可复用的配置单元通常按功能拆分比如通用设置组件配置分片数、副本数通用映射组件配置的类型和分词器别名组件配置统一的查询别名创建各类型组件模板通用设置组件PUT _component_template/settings-component-test{template: {settings: {number_of_shards: 3,number_of_replicas: 1,refresh_interval: 30s}}}# 参数解析settings-component-test组件模板名称number_of_shards主分片数量number_of_replicas副本分片数量refresh_interval数据写入后30 秒后才可以被搜索到调大提升写入性能调小提升实时性通用映射组件PUT _component_template/mappings-component-test{template: {mappings: {properties: {timestamp: { type: date },message: { type: text, analyzer: standard },host: { type: keyword },level: { type: keyword, index: false }}}}}通用别名组件PUT _component_template/aliases-component-test{template: {aliases: {all_logs: {}}}}组件模板其它运维操作查询组件模板# 查询所有组件模板GET _component_template# 查询指定的组件模板GET _component_template/settings-component-test# 响应结果{component_templates : [{name : settings-component-test,component_template : {template : {settings : {index : {number_of_shards : 3,number_of_replicas : 1,refresh_interval : 30s}}}}}]}删除组件模板# 删除组件模板DELETE _component_template/settings-component-test修改组件模板# 修改组件模板PUT _component_template/settings-component-test{template: {settings: {number_of_shards: 3,number_of_replicas: 1,refresh_interval: 20s}}}可组合索引模板可组合索引模板是真正给创建索引时使用的其由三部分组成匹配规则index_patterns 定义哪些索引会匹配这个模板支持通配符比如logs-*优先级priority 数值越大优先级越高当多个模板匹配同一个索引时高优先级的模板生效配置来源composed_of引用的组件模板列表按顺序叠加配置template直接在索引模板中定义的配置优先级高于引用的组件模板创建一个可组合索引模板创建日志索引模板匹配 logs-* 索引组合上述组件模板并补充个性化配置。PUT _index_template/logs-template-test{index_patterns: [logs-*],priority: 200,composed_of: [settings-component-test, mappings-component-test, aliases-component-test],template: {mappings: {properties: {error_code: { type: integer }}}},version: 1,_meta: {description: 日志索引模板组合通用组件模板补充错误码字段}}# 参数解析index_patterns匹配索引名称的通配符规则priority类似旧版本的order字段模板优先级数字越大优先级越高。如果多个模板匹配同一个索引priority 大的覆盖小的默认值100.composed_of引用的组件模板名称列表。template直接定义索引模板里的配置与组件模板互为补充但是其优先级比引用的组件模板高。version定义索引模板的版本_meta索引模板的元数据信息查看新版本索引模板# 查看所有的GET _index_template# 查看指定的GET _index_template/logs-template-test# 响应{index_templates : [{name : logs-template-test,index_template : {index_patterns : [logs-*],template : {mappings : {properties : {error_code : {type : integer}}}},composed_of : [settings-component-test,mappings-component-test,aliases-component-test],priority : 200,version : 1,_meta : {description : 日志索引模板组合通用组件模板补充错误码字段}}}]}新版本索引模板的删除和更新// 更新模板和创建语法一样覆盖原有配置即可PUT _index_template/logs-template-test{// 新的配置}// 删除模板DELETE _index_template/logs-template-test创建一个索引测试# 创建索引PUT logs-20260423# 查看索引GET logs-20260423# 响应{logs-20260423 : {aliases : {all_logs : { }},mappings : {properties : {timestamp : {type : date},error_code : {type : integer},host : {type : keyword},level : {type : keyword,index : false},message : {type : text,analyzer : standard}}},settings : {index : {routing : {allocation : {include : {_tier_preference : data_content}}},refresh_interval : 20s,number_of_shards : 3,provided_name : logs-20260423,creation_date : 1776913760315,number_of_replicas : 1,uuid : x8sk4JoCQ4C4QFcQe0YnkQ,version : {created : 7172699}}}}}回到顶部动态模板动态模板是自定义未知字段的映射规则解决默认动态映射的不确定性如字符串被自动设为 text keyword浪费资源创建动态模板示例所有字符串字段自动映射为 keyword以 env_ 开头的字段不索引减少存储开销。# 在索引模板的 mappings 中定义动态模板PUT _index_template/dynamic-logs-template-test{index_patterns: [logs-*],template: {mappings: {dynamic_templates: [{string_as_keyword: {match_mapping_type: string,mapping: { type: keyword }}},{env_fields: {match: env_*,mapping: { type: keyword, index: false }}}],properties: {timestamp: { type: date }}}}}匹配规则说明匹配规则说明示例match_mapping_type匹配字段类型match_mapping_type: stringmatch匹配字段名前缀 / 后缀match: env_*匹配 env_name、env_typeunmatch排除字段名unmatch: env_descpath_match匹配对象路径path_match: user.*匹配 user.name、user.agematch_pattern匹配模式regex/wildcardmatch_pattern: regex, match: user_[0-9]回到顶部索引模板与索引生命周期管理ILM集成索引生命周期管理ILM详解ILMIndex Lifecycle Management 是 ES 7.x 用于自动管理时序类索引日志、指标、事件 从创建、写入、查询、归档到删除全流程的核心功能核心价值是自动实现热温冷架构、节省存储成本、优化性能、免人工运维。ILM 通过定义 生命周期策略Policy让 ES 自动判断索引 “年龄、大小、文档数”驱动索引在不同阶段流转并执行滚动、收缩、合并、迁移、删除等操作适用于日志logs-、监控指标metrics-、事件events-*等只追加、少更新、查询热度随时间递减的数据。ILM核心三要素生命周期策略PolicyILM 的 “规则手册”定义阶段、触发条件、执行动作。索引模板Index Template将 Policy 绑定到索引新创建的匹配索引自动应用。索引 / 数据流Data Stream被管理的对象数据流更适合时序场景7.x 推荐ILM 五大生命周期阶段Phases详解ILM把索引的生命周期分为五个阶段 Hot → Warm → Cold → Frozen → Delete 顺序流转每个阶段有明确定位、触发条件和允许动作每个阶段之间的流转是有固定规则的流转依据min_age最小年龄从 索引创建时间 或 Rollover 时间 计算。流转条件当前阶段所有动作执行完成 达到下一阶段 min_age。顺序固定只能按 Hot→Warm→Cold→Frozen→Delete 单向流转不可回退Hot阶段热阶段—— 核心写入 高频查询这个阶段是索引刚创建完成且数据正在写入、实时高频查询数据最活跃定位索引正在写入、实时高频查询数据最活跃。硬件高性能 SSD 节点、高内存、高 CPU。默认触发索引创建即进入min_age: 0ms。核心目标保障写入性能、稳定查询、触发滚动Rollover。常用动作rollover核心索引达阈值自动切新索引set_priority设高优先级如 100unfollow跨集群复制场景取消跟随Warm 阶段温阶段—— 只读 中频查询这个阶段索引中数据不再写入仅用于查询定位停止写入、仍频繁查询数据不再更新。硬件普通 SATA/HDD 节点、中等配置。触发Rollover 后或达到指定时长如 7 天。核心目标优化存储、提升查询效率、降资源占用。常用动作readonly设为只读防误写forcemerge合并段文件删删除标记shrink收缩主分片如 5→1allocate迁移到温节点、降副本数set_priority降优先级如 50Cold 阶段冷阶段—— 极少查询 归档这个阶段查询很少但是数据仍需保留一段时间定位不写、极少查询需保留但允许慢查询。硬件廉价大容量存储、低配置节点。触发进入 Warm 后 N 天如 30 天。核心目标极致省存储、最小化资源占用。常用动作allocate迁冷节点、副本数设 0freeze冻结索引卸载内存、只读searchable_snapshot转为可搜索快照7.10set_priority最低优先级如 0Frozen 阶段冻结阶段7.7—— 超冷归档定位几乎不查询、必须留存查询极慢也可接受。硬件对象存储 / 远程仓库几乎不占本地资源。触发进入 Cold 后 N 天如 60 天。核心动作searchable_snapshot基于快照搜索本地几乎不占资源Delete 阶段删除阶段—— 数据销毁定位数据过期、无需留存安全删除。触发进入前一阶段后 N 天如 90 天。核心动作delete永久删除索引 数据。可选wait_for_snapshot先备份快照再删实战案例创建ILM策略描述Hot50GB 或 7 天 RolloverWarmRollover 后 1 天进入缩分片、合并段ColdWarm 后 30 天进入降副本、迁冷节点DeleteCold 后 60 天删除总留存 90 天PUT _ilm/policy/logs_ilm_policy{policy: {phases: {// 1. Hot 阶段写入滚动hot: {min_age: 0ms,actions: {set_priority: { priority: 100 }, // 高优先级rollover: {max_primary_shard_size: 50gb, // 主分片达50GB滚动max_age: 7d // 或7天滚动}}},// 2. Warm 阶段只读优化warm: {min_age: 1d, // Rollover后1天进入actions: {set_priority: { priority: 50 },readonly: {}, // 设为只读shrink: { number_of_shards: 1 }, // 主分片缩为1forcemerge: { max_num_segments: 1 }, // 合并为1段allocate: {require: { node_type: warm } // 迁到温节点}}},// 3. Cold 阶段归档cold: {min_age: 30d, // 进入Warm后30天actions: {set_priority: { priority: 0 },allocate: {require: { node_type: cold },number_of_replicas: 0 // 冷数据无副本},freeze: {} // 冻结索引}},// 4. Delete 阶段删除delete: {min_age: 60d, // 进入Cold后60天actions: {delete: {}}}}}}创建可组合索引模板创建组件模板// 组件模板1Settings含ILMPUT _component_template/logs_settings{template: {settings: {number_of_shards: 3,number_of_replicas: 1,index.lifecycle.name: logs_ilm_policy, // 绑定ILM策略index.lifecycle.rollover_alias: logs-app-write // 写入别名}}}// 组件模板2MappingsPUT _component_template/logs_mappings{template: {mappings: {properties: {timestamp: { type: date },message: { type: text },app: { type: keyword },level: { type: keyword }}}}}创建可组合索引模板// 可组合索引模板PUT _index_template/logs_app_template{index_patterns: [logs-app-*],composed_of: [logs_settings, logs_mappings],priority: 200,version: 1,_meta: { description: 应用日志索引模板绑定ILM策略 }}ILM常用管理命令查看ILM# 查看指定策略GET _ilm/policy/logs_ilm_policy# 查看所有策略GET _ilm/policy查看索引 ILM 状态# 查看单个索引ILM详情GET logs-app-000001/_ilm/explain# 查看所有索引ILM状态GET /*/_ilm/explain?humantrue手动触发 Rollover# 手动触发滚动POST logs-app-write/_rollover删除 ILM 策略DELETE _ilm/policy/logs_ilm_policy