一、前言
工作遇到一個(gè)ES深度分頁查詢時(shí)出現(xiàn)報(bào)錯(cuò),報(bào)錯(cuò)內(nèi)容如下
ElasticsearchException[Elasticsearch exception [type=illegal_argument_exception, reason=Result window is too large, from + size must be less than or equal to: [10000] but was [10001].This limit can be set by changing the [index.max_result_window] index level setting.
出現(xiàn)這個(gè)問題的原因是:
ES為了避免用戶的過大分頁請(qǐng)求造成ES服務(wù)所在機(jī)器內(nèi)存溢出,默認(rèn)對(duì)深度分頁的條數(shù)進(jìn)行了限制,默認(rèn)的最大條數(shù)是10000條
二、解決方法
2.1、修改max_result_window參數(shù)
修改es的默認(rèn)窗口大小 — index.max_result_window
或者代碼中動(dòng)態(tài)修改參數(shù),如下
//更新索引的max_result_window參數(shù)
private boolean updateIndexs(TransportClient client, String indices, int records, int size) {
int records = from * size + size;
if (records <= 10000) return true;
UpdateSettingsResponse indexResponse = client.admin().indices()
.prepareUpdateSettings(indices)
.setSettings(Settings.builder()
.put("index.max_result_window", records)
.build()
).get();
return indexResponse.isAcknowledged();
}
2.2、修改track_total_hits 參數(shù)
出現(xiàn)上面的問題,實(shí)際是ES查詢時(shí)的totalHits返回值>10000。為什么會(huì)出現(xiàn)這種情況呢?Elasticsearch 的 track_total_hits 參數(shù)是指追蹤查詢結(jié)果的總命中數(shù)。默認(rèn)情況下,當(dāng)查詢結(jié)果總命中數(shù)大于 10,000 時(shí),Elasticsearch 將停止精確地計(jì)算命中數(shù),以避免對(duì)集群造成過大的壓力。
track_total_hits 為 true,則 Elasticsearch 將返回精確的命中數(shù),這些命中數(shù)會(huì)大于10000,但可能會(huì)對(duì)性能產(chǎn)生影響。
track_total_hits 為 false,則 Elasticsearch 可能會(huì)返回近似的命中數(shù),并且不會(huì)超過10000,但可以提高查詢性能。文章來源:http://www.zghlxwxcb.cn/news/detail-726028.html
2.3、結(jié)論
最佳的解決方案是track_total_hits 為false,在不影響性能下同時(shí)也滿足我們的需要。文章來源地址http://www.zghlxwxcb.cn/news/detail-726028.html
到了這里,關(guān)于【ES】--track_total_hits參數(shù)影響ES分頁數(shù)據(jù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!