使用elasticsearch的时候,size的值太大的话会报以下错误。
"type":"query_phase_execution_exception","reason":"Result window is too large, from + size must be less than or equal to: [10000] but was [1000000]. 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默认限制size是10000。
解决方法:
一种是调整index.max_result_window的大小,调整到尽量大的值。
这里使用另一种方式,使用scroll api读取数据量超过10000的数据。
scroll api是专门为了读取大量数据设计的
首先设置每次读取的数量size和这批请求的有效时间scroll。
POST /index名字/_search?scroll=1m # 这里设置scroll为1m,意味着这批请求有效读取的时间是一分钟 { "size": 100, # 每次请求查询的大小 "query": { # 你的查询条件 } }
首次读取会返回一个scroll_id,后续读取只需要通过scroll_id不断请求读取剩余部分的数据,直到最后一批数据读取结束,后续每批数据返回的数量小于等于第一次请求时设置的size值。
POST /_search/scroll # 不需要填写index名字 { "scroll" : "1m", # 可选项,填写将会把scroll_id的有效时间更新 "scroll_id" : "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAD4WYm9laVYtZndUQlNsdDcwakFMNjU1QQ==" # 首次提交的scroll_id }
scroll api的官方文档https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-scroll