執行搜尋查詢時拋出異常:
QueryPhaseExecutionException[Result window is too large, from + size must be less than or equal to: [10000] but was [10200].
See the scroll api for a more efficient way to request large data sets.
This limit can be set by changing the [index.max_result_window] index level parameter.]
ES 預設限制 from + size ≤ 10000,請求 10200 筆超出上限。
index.max_result_windowcurl -XPUT "https://localhost:9200/your_index/_settings" -H 'Content-Type: application/json' -d'
{
"index": {
"max_result_window": 100000
}
}'
深分頁(大 from)需在每個分片收集 from + size 文檔,再合併排序,消耗大量 CPU/記憶體/網路,易導致 OOM 或長尾延遲。
| 場景 | 推薦方案 | 優勢 |
|---|---|---|
| 需遍歷海量數據(導出、重建索引) | Scroll API / Search After | 游標式分頁,效能穩定,不受 10k 限制 |
| 用戶翻頁查看(如電商列表) | Search After (ES 5.0+) | 基於排序鍵的無狀態分頁,效能優於 from/size |
| 聚合統計、分析 | Aggregations | 不返回文檔內容,僅計算指標,極快 |
GET /products/_search
{
"size": 10,
"query": { "match_all": {} },
"sort": [ { "price": "asc" }, { "_id": "asc" } ],
"search_after": [ 100, "product_50" ] // 上一頁最後一筆的排序值
}
「治標」調大 max_result_window,「治本」改用 Scroll 或 Search After。根據業務場景選擇正確分頁策略,才是 ES 高效穩定的關鍵。