1. 程式人生 > 其它 >Elasticsearch報錯Result window is too large

Elasticsearch報錯Result window is too large

具體報錯如下

{Type: search_phase_execution_exception Reason: "all shards failed" CausedBy: "Type: illegal_argument_exception Reason: "Result window is too large, from + size must be less than or equal to: [10000] but was [16000]. 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." CausedBy: "Type: illegal_argument_exception Reason: "Result window is too large, from + size must be less than or equal to: [10000] but was [16000]. 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."""}

根據ElasticSearch: Result window is too large,有三種解決方案:

1.設定index的max_result_window

http://你的ES IP地址:9200/index名稱/_settings
PUT
Body:
{
    "max_result_window": "10000000"
}

返回結果

{
    "acknowledged": true
}

2.使用Scroll API(官方不推薦)

類似遊標
第一次查詢帶上?scroll=1m

http://127.0.0.1:9200/indexName/_search?scroll=1m
POST
{
    "size": 100,
    查詢條件
}

返回結果

{
    "_scroll_id": "FGluY2x1ZGVfY29udGV4dF91dWlkDXF1ZXJ5QW5kRmV0Y2gBFkJnSGFwelc1UXg2RkR0amFINVdvYncAAAAAAAB2vRY2enRNYVU4QVN6Q3ZHZG1hWnNMVmtB",
    其他資料
}

之後呼叫/_search/scroll介面,每次帶上查詢返回的_scroll_id進行查詢

http://127.0.0.1:9200/_search/scroll/獲得的_scroll_id?scroll=1m
GET

返回結果

{
    "_scroll_id": "FGluY2x1ZGVfY29udGV4dF91dWlkDXF1ZXJ5QW5kRmV0Y2gBFkJnSGFwelc1UXg2RkR0amFINVdvYncAAAAAAAB2zBY2enRNYVU4QVN6Q3ZHZG1hWnNMVmtB",
    分頁的資料集合
}

根據Scroll API,不同版本的ES Scroll API使用略有不同,可以根據版本查閱

參考資料

Paginate search results
Scroll API

3.使用Search after引數

參考資料

Search API
Paginate search results