📣 极限科技诚招搜索运维工程师(Elasticsearch/Easysearch)- 全职/北京 👉 : 立即申请加入

创建、获取、删除可重用的组件模板(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时间值默认超时操作超时时间

示例 #

创建组件模板(设置) #

PUT /_component_template/logs_settings
{
  "template": {
    "settings": {
      "number_of_shards": 3,
      "number_of_replicas": 1,
      "index.refresh_interval": "5s"
    }
  }
}

响应示例:

{
  "acknowledged": true
}

创建组件模板(映射) #

PUT /_component_template/logs_mappings
{
  "template": {
    "mappings": {
      "properties": {
        "@timestamp": { "type": "date" },
        "level": { "type": "keyword" },
        "message": { "type": "text" },
        "labels": { "type": "keyword" }
      }
    }
  }
}

创建组件模板(别名) #

PUT /_component_template/logs_aliases
{
  "template": {
    "aliases": {
      "logs_current": {},
      "logs_read": {}
    }
  }
}

创建包含完整配置的组件模板 #

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": {}
    }
  }
}

获取所有组件模板 #

GET /_component_template

响应示例:

{
  "component_templates": [
    {
      "name": "logs_settings",
      "component_template": {
        "version": 1,
        "template": {
          "settings": {
            "number_of_shards": "3",
            "number_of_replicas": "1"
          }
        }
      }
    }
  ]
}

获取特定组件模板 #

GET /_component_template/logs_settings

获取多个组件模板 #

GET /_component_template/logs_settings,logs_mappings

以扁平格式获取 #

GET /_component_template/logs_settings?flat_settings=true

删除组件模板 #

DELETE /_component_template/logs_settings

响应示例:

{
  "acknowledged": true
}

只创建不更新 #

PUT /_component_template/logs_settings?create=true
{
  "template": {
    "settings": { ... }
  }
}

在索引模板中使用组件模板 #

# 创建组件模板
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"]
}

更新组件模板 #

PUT /_component_template/logs_settings
{
  "version": 2,
  "template": {
    "settings": {
      "number_of_shards": 5,
      "number_of_replicas": 2
    }
  }
}

组件模板结构 #

{
  "version": 1,
  "_meta": {
    "description": "组件模板描述",
    "owner": "团队名称"
  },
  "template": {
    "settings": { ... },
    "mappings": { ... },
    "aliases": { ... }
  }
}

组件模板 vs 索引模板 #

特性组件模板索引模板
作用域可重用的构建块直接应用于索引
索引模式不包含必须包含 index_patterns
组合性可被索引模板引用可引用组件模板
优先级有 priority

使用场景 #

场景 1:通用设置模板 #

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:按功能分离 #

# 设置组件
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:环境配置 #

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. 组合顺序:当多个组件模板有冲突设置时,后应用的会覆盖先应用的