【项目经验】解决ES查询只能查一万条数据问题
一、问题背景
在使用es写日志系统的时候发现es只能获取一万条日志数据, 而实际有38万多条.
使用的分页查询工具是es自带的PageRequest
Query query = new CriteriaQuery(criteria); query.setTrackTotalHits(true); //先根据criteria条件中的from和to 筛选出时间范围内的数据,然后根据日期降序, query.setPageable(PageRequest.of((int) documentDto.getCurrPage()-1,(int) documentDto.getPageSize(), Sort.by(Sort.Direction.DESC,"@timestamp")));
二、问题解决
2.1 步骤1 :修改索引配置中的max_result_window
由于在logstash中配置索引的时候使用es的默认的索引模板,
默认的索引模板配置信息中的from+size最大值为10000,原因见官网描述:
修改索引配置文件中的max_result_window的值:
通过请求:
put http://ip:9200/index/_settings {"index":{"max_result_window":10000000}}
得到结果:
{ "acknowledged":true }
说明配置成功
查看索引信息:
http://ip:9200/index
在结果中看到参数已经改变
2.2 修改
改了上述的配置之后查询还是只有一万条, 经过查询发现还需要配置track_total_hits=true才能返回真实数据.
http://ip:port/index/_search?track_total_hits=true
之后进行查询即可