题解:AtCoder AT_awc0047_b Road Closure on a One-Way Street
2026/5/4 16:48:27
命令curl -X DELETE "localhost:9200/my_first_index"是通过 HTTP DELETE 请求删除 Elasticsearch 中名为my_first_prototype的索引的标准操作。
curl-X DELETE"localhost:9200/my_first_index"| 部分 | 作用 |
|---|---|
curl | HTTP 客户端工具 |
-X DELETE | 指定 HTTP 方法为DELETE(默认是 GET) |
"localhost:9200/my_first_index" | 目标 URL: - 主机: localhost- 端口: 9200(ES 默认 HTTP 端口)- 路径: /my_first_index(索引名) |
✅等效 REST API:
DELETE /my_first_index
当 ES 收到该请求,执行以下步骤:
delete_index权限)my_first_index元数据).cfs,.doc,.tim等)⚠️关键点:
删除是物理删除,数据无法恢复(除非有快照备份)。
| 资源 | 是否被删除 |
|---|---|
| 索引数据 | ✅ 全部文档永久删除 |
| 索引映射(Mapping) | ✅ 字段定义、分析器配置丢失 |
| 索引设置(Settings) | ✅ 如number_of_shards,refresh_interval |
| 别名(Aliases) | ✅ 绑定到该索引的别名自动解除 |
| 跨索引关联 | ❌ 其他索引不受影响 |
| 快照(Snapshot) | ❌ 已备份的快照仍存在(需单独删除) |
💡验证删除结果:
curl-X GET"localhost:9200/_cat/indices?v"# 不再显示 my_first_index
action.destructive_requires_name: true(ES 默认开启)DELETE /*)delete_index权限给特定角色| 场景 | 推荐命令 |
|---|---|
| 仅清空数据,保留映射 | POST /my_first_index/_delete_by_query?conflicts=proceed { "query": { "match_all": {} } } |
| 临时禁用索引 | POST /my_first_index/_close(可 reopen) |
| 按条件删除 | DELETE /my_first_index/_doc/<id>(删除单文档) |
✅最佳实践:
- 删除前先创建快照:
PUT /_snapshot/my_backup/snapshot_1?wait_for_completion=true- 使用索引别名切流,避免直接操作物理索引
删除后,ES 数据目录(如/var/lib/elasticsearch/nodes/0/indices/)中对应索引 ID 的文件夹被移除:
Before: /var/lib/elasticsearch/nodes/0/indices/ └── abc123/ # my_first_index 的内部 UUID ├── 0/ # 分片 0 └── _state/ After: abc123/ folder gone.🔍注意:
索引名my_first_index只是逻辑名称,实际存储以内部 UUID为目录名。
delete_index权限。