Elasticsearch 索引管理 API 实战:涵盖创建、查看、修改、删除及别名
1. 索引管理 API 实战
建议使用 Postman 或其他 API 测试工具进行以下操作。
1.1 索引创建
01. 创建索引使用默认配置
PUT http://192.168.130.61:9200/applog-default
此操作将使用 Elasticsearch 默认设置(1 个主分片,1 个副本)创建索引。
02. 创建索引并指定分片数量
PUT http://192.168.130.61:9200/webdata-logs-v1
{"settings": {"number_of_shards": 3}
}
指定该索引有 3 个主分片。
03. 指定分片和副本创建索引
PUT http://192.168.130.61:9200/userindex-session-temp
{"settings": {"number_of_shards": 3,"number_of_replicas": 0}
}
设置 3 个主分片,0 个副本(适用于临时数据或写多读少场景)。
04. 创建索引的命名规范
- ❌ 索引名称不能以
.
或_
开头。 - ✅ 必须全部为小写字母。
- ⚠️ 生产环境中应避免使用通配符(如
*
),尤其是在删除操作中,防止误删。
1.2 查看索引
01. 查看所有索引列表
GET http://192.168.130.61:9200/_cat/indices?v
返回所有索引的状态、健康度、文档数、存储大小等信息。
02. 查看指定索引的基本信息
GET http://192.168.130.61:9200/_cat/indices/userindex-session-temp
查看单个索引的简要统计信息。
03. 查看指定索引的详细配置信息
GET http://192.168.130.61:9200/freed_2025_07
响应示例:
{"freed_2025_07": {"aliases": {},"mappings": {},"settings": {"index": {"routing": {"allocation": {"_tier_preference": "data_content"}},"number_of_shards": "3","provided_name": "freed_2025_07","creation_date": "1753779573507","number_of_replicas": "2","uuid": "xlxtIYx1TGmX4v2SMhKv3w","version": {"created": "8020299"}}}}
}
字段解释:
字段名 | 说明 |
---|---|
number_of_shards | 主分片数量,此处为 3 |
provided_name | 索引名称:freed_2025_07 |
creation_date | 创建时间戳(毫秒级 Unix 时间):1753779573507 → 对应 2025-07-29 左右 |
number_of_replicas | 副本数:2 |
uuid | 索引唯一标识符 |
version.created | 创建时使用的 ES 版本号(8.2.2) |
1.3 索引修改
01. 修改索引副本数量
PUT http://192.168.130.61:9200/userindex-session-temp/_settings
{"number_of_replicas": 1
}
✅ 可修改副本数;❌ 不可修改主分片数(一旦创建无法更改,除非重建索引)。
索引的关闭与打开
关闭索引(释放资源)
POST http://192.168.130.61:9200/userindex-session-temp/_close
POST http://192.168.130.61:9200/userindex-*/_close
关闭后无法读写,但数据仍保留。
打开索引
POST http://192.168.130.61:9200/userindex-session-temp/_open
POST http://192.168.130.61:9200/userindex-*/_open
重新启用索引,恢复读写能力。
💡 默认状态下索引为打开状态。
1.4 索引删除
删除指定索引
DELETE http://192.168.130.61:9200/userindex-session-temp
使用通配符批量删除
DELETE http://192.168.130.61:9200/webdata-logs-*
⚠️ 谨慎使用通配符删除,建议先用
GET _cat/indices
确认目标。
📌 特别说明:
DELETE / *
不会删除隐藏索引(以.
开头的索引,例如.kibana-8.2.2
)。
1.5 索引模板(Index Templates)
强烈推荐使用“可组合索引模板”(Composable Index Templates),因为:
- 传统模板(Legacy Templates)在 8.x 中已被 弃用(Deprecated)
/_template
接口虽然仍可用,但未来可能被移除- 官方推荐使用
/_component_template
+/_index_template
的组合方式
01.什么是索引模板?
在 Elasticsearch 8.x 中,索引模板用于为匹配命名模式的新建索引自动应用预设的:
settings
(如分片、副本、刷新间隔等)mappings
(字段类型、动态策略等)aliases
(别名)
⚠️ 若不指定模板且创建索引时未配置,则默认为:1 主分片 + 1 副本。
02.创建索引模板
创建索引模板
POST http://192.168.130.61:9200/_template/logs-appserver
{"aliases": {"app_admin": {},"dev_ops": {},"monitoring_team": {}},"index_patterns": ["logs-appserver*"],"settings": {"index": {"number_of_shards": 5,"number_of_replicas": 0}},"mappings": {"properties": {"client_ip": {"type": "ip"},"request_time": {"type": "date"},"message": {"type": "text"},"service_name": {"type": "keyword"}}}
}
创建索引测试(不指定分片数和副本数)
PUT http://192.168.130.61:9200/logs-appserver-2025
创建索引测试(指定分片数但不指定副本数)
PUT http://192.168.130.61:9200/logs-appserver-2025-001
{"settings": {"number_of_shards": 3}
}
创建测试索引(指定分片数和副本数)
PUT http://192.168.130.61:9200/logs-appserver-2025-002
{"settings": {"number_of_shards": 4,"number_of_replicas": 1}
}
创建测试索引(仅指定副本数)
PUT http://192.168.130.61:9200/logs-appserver-2025-003
{"settings": {"number_of_replicas": 2}
}
03.查看索引模板
查看所有索引模板
GET http://192.168.130.61:9200/_template
查看单个索引模板
GET http://192.168.130.61:9200/_template/logs-appserver
04.查看现有模板
1. 查看所有 组件模板(Component Templates)
GET http://192.168.130.61:9200/_component_template
组件模板仅包含可复用的部分(settings/mappings/aliases),不能直接绑定索引。
2. 查看所有 可组合索引模板(Composable Index Templates)
GET http://192.168.130.61:9200/_index_template
这是 8.x 中管理模板的主入口,取代了旧版
_template
。
3. (可选)查看传统模板(不推荐用于新项目)
GET http://192.168.130.61:9200/_template
仅用于兼容旧系统,新项目请避免使用。
05.创建组件模板(Component Template)— 复用配置单元
组件模板用于封装可复用的 settings、mappings 或 aliases。
示例:创建通用设置组件模板
PUT http://192.168.130.61:9200/_component_template/std-settings-default
{"template": {"settings": {"number_of_shards": 5,"number_of_replicas": 1,"refresh_interval": "30s","codec": "best_compression"}},"meta": {"description": "Standard settings for production logs: 5 shards, 1 replica, compressed storage"}
}
💡 提示:可在
meta
字段添加描述信息,便于团队维护。
示例:创建通用映射组件模板
PUT http://192.168.130.61:9200/_component_template/logs-mapping-common
{"template": {"mappings": {"properties": {"@timestamp": { "type": "date" },"message": { "type": "text" },"level": { "type": "keyword" },"service": { "type": "keyword" },"host": { "type": "keyword" },"trace_id": { "type": "keyword" }},"dynamic_templates": [{"strings_as_keyword": {"match_mapping_type": "string","mapping": {"type": "keyword"}}}]}},"meta": {"description": "Common field mappings for log data"}
}
06.创建可组合索引模板(Composable Index Template)
将一个或多个组件模板组合起来,应用于匹配模式的索引。
示例:创建日志类索引模板
PUT http://192.168.130.61:9200/_index_template/logs-template-v1
{"index_patterns": ["app-logs-*", "service-trace-*"],"composed_of": ["std-settings-default","logs-mapping-common"],"priority": 100,"template": {"aliases": {"all-logs": {}}},"meta": {"version": "1.0","maintainer": "dev-team","description": "Template for all application and service logs"}
}
参数 | 说明 |
---|---|
index_patterns | 匹配索引名模式 |
composed_of | 引用的组件模板列表 |
priority | 优先级(数字越大优先级越高),解决多个模板匹配冲突 |
template.aliases | 可额外定义别名 |
meta | 自定义元数据 |
✅ 当创建
app-logs-2025-07
或service-trace-api
时,将自动应用上述配置。
07.测试:创建匹配模板的索引
PUT http://192.168.130.61:9200/app-logs-2025-07
✅ 该索引将自动拥有:
- 5 个主分片
- 1 个副本
- 指定的字段映射
all-logs
别名- 压缩存储与 30s 刷新间隔
08.修改索引模板(更新配置)
可随时更新组件模板或索引模板,仅影响后续新建的索引。
更新组件模板
PUT http://192.168.130.61:9200/_component_template/std-settings-default
{"template": {"settings": {"number_of_shards": 8,"number_of_replicas": 2,"refresh_interval": "60s"}},"meta": {"description": "Updated: more shards and replicas for high-load services"}
}
❗ 已存在的索引不会自动更新,需重建或使用
_settings
API 单独修改。
验证更新效果
PUT http://192.168.130.61:9200/app-logs-2025-08
新索引
app-logs-2025-08
将使用更新后的配置(8 分片,2 副本)。
09.删除索引模板(ES 8.x 安全流程)
删除前需确保无依赖,否则会报错。
1. 删除索引模板
DELETE http://192.168.130.61:9200/_index_template/logs-template-v1
2. 删除组件模板(顺序不能反!)
DELETE http://192.168.130.61:9200/_component_template/std-settings-default
DELETE http://192.168.130.61:9200/_component_template/logs-mapping-common
⚠️ 如果组件模板正在被某个索引模板引用,删除会失败。
10. 附加:查看模板实际应用效果
查看某个索引创建时应用了哪些模板
GET http://192.168.130.61:9200/_index_template/logs-template-v1
预览模板匹配结果(调试用)
POST http://192.168.130.61:9200/_index_template/_simulate
{"index_patterns": ["app-logs-*"]
}
返回模拟应用后的最终 settings、mappings、aliases,用于验证配置是否正确。
11.总结:模板使用最佳实践
项目 | 推荐做法 |
---|---|
✅ 模板类型 | 使用 /_component_template + /_index_template |
❌ 避免使用 | /_template (已弃用) |
🧩 配置拆分 | settings / mappings / aliases 分开管理 |
🔢 优先级 | 使用 priority 解决多模板匹配冲突 |
📝 元信息 | 添加 meta 字段便于团队协作 |
🔁 更新策略 | 修改模板 → 新建索引生效,老索引需手动更新 |
🧹 删除顺序 | 先删 _index_template ,再删 _component_template |
🔗 官方文档参考(ES 8.2.2):
https://www.elastic.co/guide/en/elasticsearch/reference/8.2/index-templates.html
https://www.elastic.co/guide/en/elasticsearch/reference/8.2/component-templates.html
1.6 索引别名(Index Aliases)
什么是索引别名?
索引别名是一个逻辑名称,可指向一个或多个实际索引。
用途包括:
- 实现无缝索引轮转(rollover)
- 多个索引统一查询
- 解耦应用与物理索引名
✅ 一个别名可对应多个索引
✅ 一个索引可拥有多个别名
查看索引别名
查看指定索引的别名
GET http://192.168.130.61:9200/user-trace-data/_alias
查看所有别名信息
GET http://192.168.130.61:9200/_aliases
创建索引别名
为多个索引添加相同别名
POST http://192.168.130.61:9200/_aliases
{"actions": [{"add": {"index": "user-trace-data","alias": "app-tracing-all"}},{"add": {"index": "applog-prod-2025-07","alias": "app-tracing-all"}},{"add": {"index": "service-logs-api-gateway","alias": "app-tracing-all"}}]
}
可通过
app-tracing-all
同时查询这三个索引的数据。
为多个索引添加不同别名
POST http://192.168.130.61:9200/_aliases
{"actions": [{"add": {"index": "user-trace-data","alias": "trace-source-a"}},{"add": {"index": "applog-prod-2025-07","alias": "trace-source-b"}},{"add": {"index": "service-logs-api-gateway","alias": "trace-source-c"}}]
}
移除索引别名
POST http://192.168.130.61:9200/_aliases
{"actions": [{"remove": {"index": "user-trace-data","alias": "app-tracing-all"}},{"remove": {"index": "applog-prod-2025-07","alias": "app-tracing-all"}},{"remove": {"index": "service-logs-api-gateway","alias": "app-tracing-all"}}]
}
修改索引别名(先删后增)
POST http://192.168.130.61:9200/_aliases
{"actions": [{"remove": {"index": "applog-prod-2025-07","alias": "trace-source-b"}},{"add": {"index": "applog-prod-2025-07","alias": "trace-source-updated"}}]
}
实现别名的“重命名”效果。
🔗 官方文档参考:
https://www.elastic.co/guide/en/elasticsearch/reference/8.2/aliases.html
1.7 索引的其他操作
🔗推荐阅读:
https://www.elastic.co/guide/en/elasticsearch/reference/current/indices.html