--- title: "管理组件模板" date: 2026-01-15 lastmod: 2026-01-15 description: "创建、获取、删除可重用的组件模板" tags: ["索引模板", "组件模板", "配置管理"] summary: "创建、获取、删除可重用的组件模板(Component Templates)。 API # PUT /_component_template/{name} POST /_component_template/{name} GET /_component_template GET /_component_template/{name} DELETE /_component_template/{name} API 的作用 # 组件模板是可重用的构建块,包含索引的设置、映射或别名配置。它们可以被索引模板引用,实现模块化的模板管理。 组件模板的优势 # 可重用:多个索引模板可以引用同一个组件模板 模块化:将配置拆分为独立的、可管理的部分 版本控制:支持版本号管理 易于维护:集中管理通用配置 API 的参数 # 创建/更新组件模板 # 路由参数 # 参数 类型 是否必填 描述 {name} 字符串 必需 组件模板名称 Query String 参数 # 参数 类型 是否必填 默认值 描述 cause 字符串 否 api 操作原因描述 create 布尔值 否 false 是否只创建新模板 master_timeout 时间值 否 30s 连接到主节点的超时时间 timeout 时间值 否 默认超时 操作超时时间 请求体参数 # 参数 类型 是否必填 描述 template 对象 必需 模板定义对象 version 整数 否 模板版本号 _meta 对象 否 模板元数据 template 对象可以包含:" --- 创建、获取、删除可重用的组件模板(Component Templates)。 ## API ``` PUT /_component_template/{name} POST /_component_template/{name} GET /_component_template GET /_component_template/{name} DELETE /_component_template/{name} ``` ## API 的作用 组件模板是可重用的构建块,包含索引的设置、映射或别名配置。它们可以被索引模板引用,实现模块化的模板管理。 ### 组件模板的优势 - **可重用**:多个索引模板可以引用同一个组件模板 - **模块化**:将配置拆分为独立的、可管理的部分 - **版本控制**:支持版本号管理 - **易于维护**:集中管理通用配置 ## API 的参数 ### 创建/更新组件模板 #### 路由参数 | 参数 | 类型 | 是否必填 | 描述 | |------|------|----------|------| | `{name}` | 字符串 | 必需 | 组件模板名称 | #### Query String 参数 | 参数 | 类型 | 是否必填 | 默认值 | 描述 | |------|------|----------|--------|------| | `cause` | 字符串 | 否 | api | 操作原因描述 | | `create` | 布尔值 | 否 | false | 是否只创建新模板 | | `master_timeout` | 时间值 | 否 | 30s | 连接到主节点的超时时间 | | `timeout` | 时间值 | 否 | 默认超时 | 操作超时时间 | #### 请求体参数 | 参数 | 类型 | 是否必填 | 描述 | |------|------|----------|------| | `template` | 对象 | 必需 | 模板定义对象 | | `version` | 整数 | 否 | 模板版本号 | | `_meta` | 对象 | 否 | 模板元数据 | `template` 对象可以包含: | 参数 | 类型 | 描述 | |------|------|------| | `settings` | 对象 | 索引设置 | | `mappings` | 对象 | 字段映射定义 | | `aliases` | 对象 | 索引别名 | ### 获取组件模板 #### 路由参数 | 参数 | 类型 | 是否必填 | 描述 | |------|------|----------|------| | `{name}` | 字符串 | 否 | 组件模板名称,支持逗号分隔的多个名称。不指定则获取所有模板 | #### Query String 参数 | 参数 | 类型 | 是否必填 | 默认值 | 描述 | |------|------|----------|--------|------| | `flat_settings` | 布尔值 | 否 | false | 是否以扁平格式返回设置 | | `local` | 布尔值 | 否 | false | 是否只从本地节点获取信息 | | `master_timeout` | 时间值 | 否 | 30s | 连接到主节点的超时时间 | ### 删除组件模板 #### 路由参数 | 参数 | 类型 | 是否必填 | 描述 | |------|------|----------|------| | `{name}` | 字符串 | 必需 | 要删除的组件模板名称 | #### Query String 参数 | 参数 | 类型 | 是否必填 | 默认值 | 描述 | |------|------|----------|--------|------| | `master_timeout` | 时间值 | 否 | 30s | 连接到主节点的超时时间 | | `timeout` | 时间值 | 否 | 默认超时 | 操作超时时间 | ## 示例 ### 创建组件模板(设置) ```bash PUT /_component_template/logs_settings { "template": { "settings": { "number_of_shards": 3, "number_of_replicas": 1, "index.refresh_interval": "5s" } } } ``` **响应示例:** ```json { "acknowledged": true } ``` ### 创建组件模板(映射) ```bash PUT /_component_template/logs_mappings { "template": { "mappings": { "properties": { "@timestamp": { "type": "date" }, "level": { "type": "keyword" }, "message": { "type": "text" }, "labels": { "type": "keyword" } } } } } ``` ### 创建组件模板(别名) ```bash PUT /_component_template/logs_aliases { "template": { "aliases": { "logs_current": {}, "logs_read": {} } } } ``` ### 创建包含完整配置的组件模板 ```bash PUT /_component_template/complete_logs { "version": 1, "_meta": { "description": "Complete logs component template", "owner": "data-team" }, "template": { "settings": { "number_of_shards": 3, "number_of_replicas": 1 }, "mappings": { "properties": { "@timestamp": { "type": "date" }, "message": { "type": "text" } } }, "aliases": { "logs_main": {} } } } ``` ### 获取所有组件模板 ```bash GET /_component_template ``` **响应示例:** ```json { "component_templates": [ { "name": "logs_settings", "component_template": { "version": 1, "template": { "settings": { "number_of_shards": "3", "number_of_replicas": "1" } } } } ] } ``` ### 获取特定组件模板 ```bash GET /_component_template/logs_settings ``` ### 获取多个组件模板 ```bash GET /_component_template/logs_settings,logs_mappings ``` ### 以扁平格式获取 ```bash GET /_component_template/logs_settings?flat_settings=true ``` ### 删除组件模板 ```bash DELETE /_component_template/logs_settings ``` **响应示例:** ```json { "acknowledged": true } ``` ### 只创建不更新 ```bash PUT /_component_template/logs_settings?create=true { "template": { "settings": { ... } } } ``` ### 在索引模板中使用组件模板 ```bash # 创建组件模板 PUT /_component_template/logs_settings { "template": { "settings": { "number_of_shards": 3, "number_of_replicas": 1 } } } PUT /_component_template/logs_mappings { "template": { "mappings": { "properties": { "@timestamp": { "type": "date" }, "message": { "type": "text" } } } } } # 创建索引模板并引用组件模板 PUT /_index_template/logs_template { "index_patterns": ["logs-*"], "priority": 100, "composed_of": ["logs_settings", "logs_mappings"] } ``` ### 更新组件模板 ```bash PUT /_component_template/logs_settings { "version": 2, "template": { "settings": { "number_of_shards": 5, "number_of_replicas": 2 } } } ``` ## 组件模板结构 ```json { "version": 1, "_meta": { "description": "组件模板描述", "owner": "团队名称" }, "template": { "settings": { ... }, "mappings": { ... }, "aliases": { ... } } } ``` ## 组件模板 vs 索引模板 | 特性 | 组件模板 | 索引模板 | |------|----------|----------| | **作用域** | 可重用的构建块 | 直接应用于索引 | | **索引模式** | 不包含 | 必须包含 index_patterns | | **组合性** | 可被索引模板引用 | 可引用组件模板 | | **优先级** | 无 | 有 priority | ## 使用场景 ### 场景 1:通用设置模板 ```bash PUT /_component_template/standard_settings { "template": { "settings": { "number_of_shards": 3, "number_of_replicas": 1, "index.refresh_interval": "5s" } } } # 多个索引模板可以共用 PUT /_index_template/logs { "index_patterns": ["logs-*"], "composed_of": ["standard_settings"] } PUT /_index_template/metrics { "index_patterns": ["metrics-*"], "composed_of": ["standard_settings"] } ``` ### 场景 2:按功能分离 ```bash # 设置组件 PUT /_component_template/hot_settings { "template": { "settings": { "number_of_replicas": 0 } } } # 映射组件 PUT /_component_template/common_mappings { "template": { "mappings": { "properties": { "@timestamp": { "type": "date" }, "level": { "type": "keyword" } } } } } # 别名组件 PUT /_component_template/read_alias { "template": { "aliases": { "read": {} } } } # 组合使用 PUT /_index_template/logs_hot { "index_patterns": ["logs-hot-*"], "composed_of": ["hot_settings", "common_mappings", "read_alias"] } ``` ### 场景 3:环境配置 ```bash PUT /_component_template/dev_settings { "template": { "settings": { "number_of_replicas": 0 } } } PUT /_component_template/prod_settings { "template": { "settings": { "number_of_replicas": 2 } } } ``` ## 注意事项 1. **引用验证**:索引模板引用的组件模板必须存在 2. **版本管理**:使用 version 字段跟踪模板版本 3. **删除影响**:删除被引用的组件模板会影响使用它的索引模板 4. **更新影响**:组件模板更新不会影响已存在的索引 5. **组合顺序**:当多个组件模板有冲突设置时,后应用的会覆盖先应用的