# 搜索阶段执行异常：所有分片失败：[search_phase_execution_exception] 原因：等待搜索结果超时 [30000ms]

- **ID:** `elasticsearch/search-phase-execution-timeout`
- **领域:** elasticsearch
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

搜索请求超时，因为一个或多个分片响应时间超过了配置的超时时间，通常是由于慢查询或资源争用。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| elasticsearch 7.17 | active | — | — |
| elasticsearch 8.10 | active | — | — |
| elasticsearch 8.12 | active | — | — |

## 解决方案

1. ```
   增加特定请求的搜索超时时间：POST /my_index/_search { "timeout": "60s", "query": { "match": { "field": "value" } } }
   ```
2. ```
   通过添加索引过滤器或减少聚合复杂度来优化查询：对非评分过滤器使用 'filter' 上下文而非 'query'，并限制 'size' 和 'aggs'。
   ```
3. ```
   对长时间运行的查询使用异步搜索：POST /my_index/_async_search { "size": 0, "query": { "match_all": {} }, "aggs": { "my_agg": { "terms": { "field": "field", "size": 10000 } } }, "wait_for_completion_timeout": "10s" }
   ```

## 无效尝试

- **** — This only delays the failure; slow queries still consume resources and may eventually cause cluster instability. (60% 失败率)
- **** — This changes the search semantics and returns inaccurate results, breaking application functionality. (75% 失败率)
- **** — Restarting does not fix the underlying slow query or resource issue; the timeout will recur after the cluster recovers. (70% 失败率)
