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

创建、获取、删除可组合索引模板(Composable Index Templates)。

API #

PUT /_index_template/{name}
POST /_index_template/{name}
GET /_index_template
GET /_index_template/{name}
DELETE /_index_template/{name}

API 的作用 #

索引模板允许您定义在创建新索引时自动应用的设置、映射和别名。

可组合索引模板 #

可组合索引模板是 Easysearch 推荐的模板管理方式,具有以下特点:

  • 模块化:由组件模板(component templates)组成
  • 可组合:多个模板可以组合在一起
  • 优先级:通过 priority 控制模板应用顺序
  • 版本控制:支持版本号管理

API 的参数 #

创建/更新索引模板 #

路由参数 #

参数类型是否必填描述
{name}字符串必需索引模板名称

Query String 参数 #

参数类型是否必填默认值描述
cause字符串api创建/更新索引模板的原因
create布尔值false是否只创建新模板,不允许更新已存在的模板
master_timeout时间值30s连接到主节点的超时时间

请求体参数 #

参数类型是否必填描述
index_patterns数组必需匹配的索引模式列表,如 ["logs-*", "metrics-*"]
priority整数模板优先级,数值越大优先级越高
template对象模板配置,包含 settings、mappings、aliases
version整数模板版本号
_meta对象模板元数据

获取索引模板 #

路由参数 #

参数类型是否必填描述
{name}字符串索引模板名称,支持逗号分隔的多个名称。不指定则获取所有模板

Query String 参数 #

参数类型是否必填默认值描述
flat_settings布尔值false是否以扁平格式返回设置
local布尔值false是否只从本地节点获取信息
master_timeout时间值30s连接到主节点的超时时间

删除索引模板 #

路由参数 #

参数类型是否必填描述
{name}字符串必需要删除的索引模板名称

Query String 参数 #

参数类型是否必填默认值描述
master_timeout时间值30s连接到主节点的超时时间
timeout时间值默认超时操作超时时间

示例 #

创建索引模板 #

PUT /_index_template/logs_template
{
  "index_patterns": ["logs-*"],
  "priority": 100,
  "template": {
    "settings": {
      "number_of_shards": 3,
      "number_of_replicas": 1
    },
    "mappings": {
      "properties": {
        "@timestamp": { "type": "date" },
        "message": { "type": "text" },
        "level": { "type": "keyword" }
      }
    },
    "aliases": {
      "logs_current": {}
    }
  }
}

响应示例:

{
  "acknowledged": true
}

只创建不更新 #

PUT /_index_template/logs_template?create=true
{
  "index_patterns": ["logs-*"],
  "template": { ... }
}

获取所有模板 #

GET /_index_template

获取特定模板 #

GET /_index_template/logs_template

获取多个模板 #

GET /_index_template/logs_template,metrics_template

以扁平格式获取 #

GET /_index_template/logs_template?flat_settings=true

删除模板 #

DELETE /_index_template/logs_template

响应示例:

{
  "acknowledged": true
}

使用组件模板 #

# 先创建组件模板
PUT /_component_template/logs_settings
{
  "template": {
    "settings": {
      "number_of_shards": 3,
      "number_of_replicas": 1
    }
  }
}

# 在索引模板中引用组件模板
PUT /_index_template/logs_template
{
  "index_patterns": ["logs-*"],
  "priority": 100,
  "composed_of": ["logs_settings"],
  "template": {
    "mappings": {
      "properties": {
        "@timestamp": { "type": "date" }
      }
    }
  }
}

设置优先级 #

PUT /_index_template/default_logs
{
  "index_patterns": ["logs-*"],
  "priority": 50,
  "template": {
    "settings": {
      "number_of_replicas": 0
    }
  }
}

PUT /_index_template/production_logs
{
  "index_patterns": ["logs-prod-*"],
  "priority": 100,
  "template": {
    "settings": {
      "number_of_replicas": 2
    }
  }
}

添加版本和元数据 #

PUT /_index_template/logs_template
{
  "index_patterns": ["logs-*"],
  "version": 1,
  "_meta": {
    "description": "Logs index template",
    "owner": "data-team"
  },
  "template": { ... }
}

模板优先级 #

当多个模板匹配同一个索引时:

  1. priority 降序应用
  2. 相同优先级时按名称字典序
  3. 后应用的模板设置会覆盖先应用的
优先级应用顺序
高值优先应用
相同按名称排序

索引模式 #

支持通配符模式:

模式匹配示例
logs-*logs-2024, logs-app
*_prodapp_prod, db_prod
test-*test-1, test-data

错误处理 #

模板已存在(create=true) #

{
  "error": {
    "type": "resource_already_exists_exception",
    "reason": "index_template [logs_template] already exists"
  }
}

模板不存在 #

{
  "error": {
    "type": "index_template_missing_exception",
    "reason": "index_template [logs_template] missing"
  }
}

使用场景 #

场景 1:时间序列索引 #

PUT /_index_template/metrics_template
{
  "index_patterns": ["metrics-*"],
  "priority": 100,
  "template": {
    "settings": {
      "number_of_shards": 3,
      "number_of_replicas": 1,
      "refresh_interval": "30s"
    }
  }
}

场景 2:环境特定配置 #

# 开发环境
PUT /_index_template/dev_logs
{
  "index_patterns": ["logs-dev-*"],
  "priority": 100,
  "template": {
    "settings": {
      "number_of_replicas": 0
    }
  }
}

# 生产环境
PUT /_index_template/prod_logs
{
  "index_patterns": ["logs-prod-*"],
  "priority": 200,
  "template": {
    "settings": {
      "number_of_replicas": 2
    }
  }
}

注意事项 #

  1. 全局模板限制:全局模板(使用 * 模式)不能设置 index.hidden
  2. 不可变设置:某些设置(如 number_of_shards)在索引创建后不能修改
  3. 模板验证:模板定义会进行严格验证
  4. 向后兼容:保留对旧版模板(/_template)的支持
  5. 优先级管理:合理设置优先级避免意外覆盖