ES 深分页

 //多次分页查询(scroll)@Testpublic void testSearchByPage3() throws IOException {//Integer currentPage=1;Integer pageSize=3;SearchRequest searchRequest=new SearchRequest();searchRequest.indices("kuangshen");searchRequest.scroll(TimeValue.timeValueMinutes(1L));//设置scroll失效时间为1分钟SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();//不需要传从第几条开始//searchSourceBuilder.from(currentPage-1);searchSourceBuilder.size(pageSize);searchSourceBuilder.sort("age", SortOrder.ASC);//排序,查出来的数据根据age排序searchRequest.source(searchSourceBuilder);SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);//System.out.println(JSON.toJSONString(response.getHits()));System.out.println("------------------------首页------------------------------");SearchHit[] hits = response.getHits().getHits();for (SearchHit hit:hits) {System.out.println(hit.getSourceAsString());}String scrollId=response.getScrollId();System.out.println("scrollId为: "+response.getScrollId());Boolean hasMore=true;while (hasMore){SearchScrollRequest searchScrollRequest=new SearchScrollRequest();searchScrollRequest.scroll(TimeValue.timeValueMinutes(1L));searchScrollRequest.scrollId(scrollId);SearchResponse scrollResponse = client.scroll(searchScrollRequest, RequestOptions.DEFAULT);System.out.println("------------------------下一页------------------------------");SearchHit[] hits1 = scrollResponse.getHits().getHits();for (SearchHit hit:hits1) {System.out.println(hit.getSourceAsString());}if(hits1.length==0){//返回没值时,则表示遍历完成hasMore=false;}scrollId = scrollResponse.getScrollId();System.out.println("scrollId为: "+scrollId);}System.out.println("全部查完");}

本文链接:https://my.lmcjl.com/post/3746.html

展开阅读全文

4 评论

留下您的评论.