Elasticsearch 索引管理 是运维和开发人员必须掌握的核心技能。一个高效的索引管理策略,不仅能提升搜索性能、降低资源消耗,还能保障数据生命周期的可控性。
本文将全面详解 Elasticsearch 索引管理的 核心概念、操作命令、模板设计、生命周期管理(ILM)、别名使用、监控与最佳实践。
一、索引管理的核心目标
| 目标 | 说明 |
|---|---|
| ✅ 高效创建 | 快速部署新索引,避免手动配置错误 |
| ✅ 自动维护 | 自动滚动、归档、删除过期数据 |
| ✅ 资源优化 | 控制分片大小、副本数,避免资源浪费 |
| ✅ 无缝切换 | 使用别名实现读写分离与版本切换 |
| ✅ 高可用 | 通过副本、多可用区保障数据安全 |
| ✅ 可观测性 | 监控索引状态、性能、容量 |
二、索引管理的核心操作
1. 创建索引(Create Index)
PUT /logs-2024-06-01
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1,
"refresh_interval": "30s"
},
"mappings": {
"properties": {
"message": { "type": "text" },
"level": { "type": "keyword" },
"@timestamp": { "type": "date" }
}
}
}
⚠️ 索引名必须小写,不能包含特殊字符(
_、-可用)。
2. 查看索引信息
GET /logs-2024-06-01
返回 settings 和 mappings。
查看所有索引:
GET /_cat/indices?v
3. 删除索引
DELETE /logs-2024-06-01
⚠️ 删除不可恢复,请谨慎操作。
4. 打开/关闭索引
关闭索引可节省内存,但仍保留数据:
POST /logs-2024-06-01/_close
POST /logs-2024-06-01/_open
5. 清空缓存
POST /logs-2024-06-01/_cache/clear
用于测试或释放内存。
三、索引模板(Index Templates)
用于自动应用 settings 和 mappings 到匹配名称的索引。
1. Legacy Template(旧版,7.x 及以下)
PUT /_template/template-logs
{
"index_patterns": ["logs-*"],
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"host": { "type": "keyword" }
}
}
}
当创建
logs-app-2024时,自动应用此模板。
2. Composable Template(8.x 推荐)
支持多模板组合,更灵活。
步骤 1:创建组件模板(Component Template)
PUT /_component_template/logs-mapping
{
"template": {
"mappings": {
"properties": {
"message": { "type": "text" },
"service": { "type": "keyword" }
}
}
}
}
PUT /_component_template/logs-settings
{
"template": {
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
}
}
}
步骤 2:创建组合模板(Index Template)
PUT /_index_template/logs-template
{
"index_patterns": ["logs-*"],
"composed_of": ["logs-settings", "logs-mapping"],
"template": {
"settings": {
"refresh_interval": "30s"
}
},
"priority": 100
}
priority控制模板优先级。
四、索引别名(Aliases)
别名 是指向一个或多个索引的逻辑名称,用于:
- 实现读写分离;
- 无缝切换索引(如滚动更新);
- 联合多个索引查询。
示例:读写别名
POST /_aliases
{
"actions": [
{ "add": { "index": "logs-2024-06-01", "alias": "logs-write" } },
{ "add": { "index": "logs-2024-06-01", "alias": "logs-read" } }
]
}
- 写入:
POST /logs-write/_doc - 查询:
GET /logs-read/_search
滚动更新(Rollover)场景
# 创建初始索引
PUT /logs-000001
{
"aliases": {
"logs-write": {
"is_write_index": true
},
"logs-read": {}
}
}
# 当满足条件时滚动
POST /logs-write/_rollover
{
"conditions": {
"max_age": "1d",
"max_docs": 10000000
}
}
滚动后,
logs-write指向新索引logs-000002。
五、索引生命周期管理(ILM)
ILM(Index Lifecycle Management) 自动管理索引从创建到删除的全过程。
四个阶段:
| 阶段 | 说明 |
|---|---|
| Hot | 活跃写入,高资源(SSD、高副本) |
| Warm | 不再写入,只读,降低副本、禁用刷新 |
| Cold | 极少访问,迁移到低性能节点 |
| Delete | 过期删除 |
示例策略
PUT /_ilm/policy/logs-policy
{
"policy": {
"phases": {
"hot": {
"actions": {
"rollover": { "max_size": "50gb", "max_age": "1d" }
}
},
"warm": {
"min_age": "1d",
"actions": {
"forcemerge": { "max_num_segments": 1 },
"shrink": { "number_of_shards": 1 },
"migrate": { "destination": "warm_nodes" }
}
},
"delete": {
"min_age": "30d",
"actions": { "delete": {} }
}
}
}
}
应用到模板
PUT /_index_template/logs-template
{
"index_patterns": ["logs-*"],
"template": {
"settings": {
"index.lifecycle.name": "logs-policy",
"index.lifecycle.rollover_alias": "logs-write"
}
}
}
六、索引状态监控
1. 查看索引状态
# 基本信息
GET /_cat/indices/logs-*?v
# 分片分布
GET /_cat/shards/logs-*?v
# 统计信息
GET /logs-2024-06-01/_stats
2. 关键指标
| 指标 | 说明 |
|---|---|
docs.count | 文档数量 |
store.size | 存储大小 |
search.query_time | 查询延迟 |
indexing.index_time | 写入延迟 |
query_cache.hit_count | 缓存命中 |
七、索引优化建议 ✅
| 场景 | 建议 |
|---|---|
| 分片大小 | 控制在 10~50GB |
| 主分片数 | 根据未来数据量预估,避免后期 reindex |
| 副本数 | 生产环境 ≥1,读多可增至 2~3 |
| refresh_interval | 写密集场景设为 30s |
| 动态映射 | 生产环境设为 strict |
| 字段类型 | 聚合用 keyword,全文搜索用 text |
| 别名管理 | 使用 write / read 别名实现解耦 |
八、常见问题与解决方案
Q1:如何修改主分片数?
❌ 不能直接修改。
✅ 解决方案:使用 reindex 创建新索引并迁移数据。
POST /_reindex
{
"source": { "index": "old-index" },
"dest": { "index": "new-index" }
}
Q2:索引 unassigned 分片怎么办?
GET /_cluster/allocation/explain
常见原因:
- 磁盘满;
- 节点离线;
cluster.routing.allocation.enable被禁用。
Q3:如何批量操作多个索引?
使用通配符:
POST /logs-*/_rollover
POST /metrics-2024-*/_shrink/new-%index%-shrinked
九、最佳实践 checklist ✅
| 项目 | 建议 |
|---|---|
| 使用索引模板 | 避免手动配置错误 |
| 启用 ILM | 自动管理生命周期 |
| 使用别名 | 实现读写分离 |
| 控制分片大小 | 10~50GB |
| 监控索引状态 | 定期检查 _cat/indices |
| 定期快照备份 | 防止数据丢失 |
| 禁用动态映射 | 设为 strict |
5019

被折叠的 条评论
为什么被折叠?



