es深分页查询

/**
	 * 利用游标进行深分页查询 注:此方法可支持跳转页,但是翻页越往后越慢
	 * @param type es查询类型
	 * @param order 排序字段
	 * @param map 查询条件
	 * @param pageNo 当前页码
	 * @param pageSize 每页显示数量
	 * @return 结果集
	 */
public static Map cascadeESQueryByScroll(String type,String order,Map map,int pageNo, int pageSize){
        Client client = ElasticSearchManager.getClient();
        SearchRequestBuilder searchRequestBuilder = client.prepareSearch("cems").setTypes(type);
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        for (String key : map.keySet()) {
            if (key.indexOf("*") != -1) {
                boolQueryBuilder.must(QueryBuilders.termsQuery(key.replace("*","").trim(), map.get(key)));
            } else {
                boolQueryBuilder.must(QueryBuilders.wildcardQuery(key, "*"+map.get(key)+"*"));
            }
        }
        searchRequestBuilder.setQuery(boolQueryBuilder);
        
        if (StringUtils.isNotBlank(order)) {
            searchRequestBuilder.addSort(order, SortOrder.DESC);
        }
        searchRequestBuilder.setFrom((pageNo-1)*pageSize).setSize(pageSize);
        
        JSONArray array = new JSONArray();
        Map dataMap = new HashMap();
        try {
            //SearchResponse response = searchRequestBuilder.get();
            
            // 当前页数据开始下标
            int beginIndex = (pageNo - 1) * pageSize;
            // 当前页数据结束下标
            int endIndex = beginIndex + pageSize;
            
            int size = 1000;
            if(beginIndex > 100000){
                size = 8000;
            } else if (beginIndex > 5000){
                size = 1000;
            } else if (beginIndex > 1000){
                size = 500;
            }
            
            long start = System.currentTimeMillis();
            // 设置ScrollId的有效时间为两分钟
            SearchResponse response = searchRequestBuilder.setFrom(0).setScroll(TimeValue.timeValueMinutes(2)).setSize(size).setExplain(true)
                    .execute().actionGet();
            long total = response.getHits().getTotalHits();

            log.info("[cascadeESQueryByScroll] TotalHits:" + total);
            dataMap.put("total", total);
            int indexRecord = 0;
            do {
                // 解析结果
                SearchHits hits = response.getHits();
                for (int i = 0; i < hits.getHits().length; i++) {
                    if (beginIndex <= indexRecord && indexRecord < endIndex) {
                        SearchHit searchHit = hits.getHits()[i];
                        JSONObject object = JSONObject.fromObject(searchHit.getSourceAsString());
                        array.add(object);
                    } else if (indexRecord == endIndex) {
                        dataMap.put("data", array);
                        return dataMap;
                    }
                    indexRecord++;
                }
                response = client.prepareSearchScroll(response.getScrollId()).setScroll(TimeValue.timeValueMinutes(2))
                        .execute().actionGet();
            } while (response.getHits().getHits().length != 0);
            
            long end = System.currentTimeMillis();
            
            System.out.println("cost--->" + (end - start));
            
            dataMap.put("data", array);
           
        } catch (Exception e) {
            log.error("[cascadeESQueryByScroll] error:" + e.getMessage());
        }
        return dataMap;
    }

附上翻页时间:每页30条数据,总数据量15W

页码--时间

300--5.79s
400--7.06s
500--10.50s
600--10.55s
700--44.81s
800--34.35s
900--40.53s
1200--55.49s

对大数据量的跳转翻页查询,见另一篇文章。

你可能感兴趣的:(Java)