解決 Elasticsearch 超過 10000 條無法查詢的問題
問題描述
分頁查詢場景,當查詢記錄數超過 10000 條時,會報錯。
使用 Kibana 的 Dev Tools 工具查詢 從第 10001 條到 10010 條數據。
查詢語句如下:
GET alarm/_search { "from": 10000, "size": 10 }
查詢結果,截圖如下:
報錯信息如下:
{ "error": { "root_cause": [ { "type": "query_phase_execution_exception", "reason": "Result window is too large, from + size must be less than or equal to: [10000] but was [10010]. 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 setting." } ], "type": "search_phase_execution_exception", "reason": "all shards failed", "phase": "query", "grouped": true, "failed_shards": [ { "shard": 0, "index": "alarm", "node": "hdLJanxRTbmF52eK6-FFgg", "reason": { "type": "query_phase_execution_exception", "reason": "Result window is too large, from + size must be less than or equal to: [10000] but was [10010]. 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 setting." } } ] }, "status": 500 }
原因分析
Elasticsearch 默認查詢結果最多展示前 10000 條數據。
解決方案
按照報錯信息里的提示,可以看到,通過設置 max_result_window 的值來調整顯示數據的大小:
This limit can be set by changing the [index.max_result_window] index level setting.
兩種方式可以實現:
【方式一】(修改完配置文件,需要重啟集群中的 ES 服務)
修改Elasticsearch 集群中的 配置文件 config/elasticsearch.yml
在配置文件最后增加一行,如下:
max_result_window: 200000000
【方式二】(推薦)
具體操作命令,如下(比如,設置可查詢 200000000 條數據,其中 alarm 是index名稱):
PUT alarm/_settings { "max_result_window" : 200000000 }
命令執行效果,截圖如下:
再次執行查詢語句,即可正常查詢,效果截圖如下: