摘要:elasticsearch升級到7.X后,與之前版本修改的細節還挺多的,首先就是返回文檔的total字段格式調整,然后就是當文檔數大于10000時,只顯示10000條數據...
elasticsearch升級到7.X后,與之前版本修改的細節還挺多的,首先就是返回文檔的total字段格式調整,然后就是當文檔數大于10000時,只顯示10000,這使我們在獲取文檔的真實數據時變的麻煩了一些。
解決total最大為10000方案,在查詢中添加: track_total_hits:true
例如:
$params = [ 'index' => $indice, 'body' => [ 'query' => [ 'bool' => [ 'must' => $must, 'should' => $should, 'must_not' => $must_not ] ], 'sort' => [ 'id' =>['order'=>'asc'] ] ], 'track_total_hits' => true, //elasticsearch7 顯示真實數據總數 'from' => 0, 'size' => 10 ];
此時可以顯示文檔的真實數了,但是在分頁查詢的時候,大于 10000 條還是報錯,錯誤如下:
Result window is too large, from + size must be less than or equal to: [10000] but was [1278620]. 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.
原因分析:
es對 from + size 的大小進行限制,必須小于等于10000。
解決方案:
在業務中限制分頁大小,使 from+size<=10000; 動態更改索引設置,為 max_result_window 參數賦值足夠大的值,因為es的優勢在于搜索,不在于分頁,對此做限制就是為不影響其性。就es的默認配置,假設每頁10條記錄,也有1000頁,如果業務上實在不妥協,則可以設置修改其最大值:
PUT index/_settings { "index":{ "max_result_window":100000000 } }