代码示例:
说明:
1 version: 2.x
2 设置size大小 size为每个分片的大小,例如: 设置200,有5个shards,每次提取200×5
3 TimeValue.timeValueMinutes(1) 这个设置为查询超时时间
4 setSearchType(SearchType.Scan) 告诉ES不需要排序只要结果返回即可
5 setScroll(new TimeValue(600000)) 设置滚动的时间
SearchResponse response = client.prepareSearch("index")
.setTypes("type") .setQuery(QueryBuilders.matchAllQuery()).setSize(200).setScroll(TimeValue.timeValueMinutes(1)).setSearchType(SearchType.SCAN).execute().actionGet();
String scrollid = response.getScrollId();
SearchResponse response2 = client.prepareSearchScroll(scrollid).setScroll(TimeValue.timeValueMinutes(8)).execute().actionGet();
SearchHits searchHit = response2.getHits();
//再次查询不到数据时跳出循环
if (searchHit.getHits().length == 0) {
break;
}
System.out.println("查询数量 :" + searchHit.getHits().length);
for (int i = 0; i < searchHit.getHits().length; i++) {
String json = searchHit.getHits()[i].getSourceAsString();
//获取原始对象
JSONObject obj = new JSONObject(json);