当前位置: 首页 > news >正文

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. ✅ 必须全部为小写字母。
  3. ⚠️ 生产环境中应避免使用通配符(如 *),尤其是在删除操作中,防止误删。

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-07service-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

http://www.lryc.cn/news/604801.html

相关文章:

  • Redis 面试全解析:从数据结构到集群架构(含实战解决方案)
  • 设计模式之单例模式及其在多线程下的使用
  • 【C#】DevExpress.XtraEditors.MemoEdit memoEditLog控件讲解
  • Rabbitmq中常见7种模式介绍
  • pytorch小记(三十三):PyTorch 使用 TensorBoard 可视化训练过程(含完整示例)
  • 用 Go Typed Client 快速上手 Elasticsearch —— 从建索引到聚合的完整实战
  • 8.Linux : 日志的管理与时钟同步的配置
  • Rabbit MQ的消息模式-Java原生代码
  • YOLO-01目标检测基础
  • 02 基于sklearn的机械学习-特征降维(特征选择、PCA)、KNN算法、模型选择与调优(交叉验证、朴素贝叶斯算法、拉普拉斯平滑)
  • Android调用python库和方法的实现
  • YOLOv5u:无锚点检测的革命性进步
  • android-PMS-创建新用户流程
  • 舆情监测专员需要哪些常用软件工具?
  • 基于 Hadoop 生态圈的数据仓库实践 —— OLAP 与数据可视化(一)
  • 论文Review 3DGSSLAM S3PO-GS | ICCV 2025 港科广出品!| 高效快速的3DGSSLAM!
  • sqli-labs:Less-1关卡详细解析
  • CMS框架漏洞
  • 3D Web轻量化引擎HOOPS Communicator数据处理与流式加载能力概述
  • 【音视频】WebRTC-Web 音视频采集与播放
  • 【预判一手面试问题:排序】
  • 依托客户满意度分析协助企业精准把握市场趋势​(满意度调查)
  • 智能AI医疗物资/耗材管理系统升级改造方案分析
  • InfluxDB 与 Java 框架集成:Spring Boot 实战(二)
  • VSCode插件开发完整教程:从零开始创建文件导出插件
  • Python 程序设计讲义(37):字符串的处理方法——设置字符串居中显示:center() 方法
  • 图像平滑处理
  • 9.项目起步(3)
  • OpenCV学习day1
  • 实习小记(个人中心的编辑模块)