# 分片锁获取失败：[my_index][0] 获取分片锁失败

- **ID:** `elasticsearch/primary-shard-not-allocated-due-to-shard-lock`
- **领域:** elasticsearch
- **类别:** system_error
- **验证级别:** ai_generated
- **修复率:** 82%

## 根因

分片锁无法获取，因为分片仍在恢复中，或者之前的节点崩溃在磁盘上留下了过期的锁文件。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 7.17.10 | active | — | — |
| 8.6.2 | active | — | — |
| 8.11.0 | active | — | — |

## 解决方案

1. ```
   手动删除过期的分片锁文件：找到 ES_PATH_CONF/data/nodes/0/indices/<索引UUID>/0/ 目录下的 'index.lock' 或 'shard.lock' 文件，删除后重启节点。
   ```
2. ```
   使用 Cluster Reroute API 重新分配未分配的分片：POST /_cluster/reroute { "commands": [{ "allocate_stale_primary": { "index": "my_index", "shard": 0, "node": "my_node", "accept_data_loss": true } }] }
   ```

## 无效尝试

- **** — Deleting the index loses all data and may not resolve the underlying lock issue if the file system is corrupted. (70% 失败率)
- **** — Restarting does not remove stale lock files; the lock file persists and the same error occurs again. (85% 失败率)
