es scroll 深度分页查询 感觉很慢,

听说 scroll查询速度会挺快,然后试了一下,感觉速度好慢,
1000条返回需要8s,虽然页数往后速度还是一样,
但是感觉速度还是太慢了吧,是我哪里做的不对吗,请大佬指导,
用kibana控制台速度不算慢,但是java去调用 就特别慢。

https://blog.csdn.net/u011228889/article/details/79760167

java 可以使用spring data或者bboss框架,性能挺好的,我使用过。

bboss并行方式执行slice检索实例如下,restful方式调用es服务器,查询语句可以类似mybatis在配置文件中配置:

//用来存放实际slice检索总记录数
long realTotalSize ;
//辅助方法,用来累计每次scroll获取到的记录数
synchronized void incrementSize(int size){
    this.realTotalSize = this.realTotalSize + size;
}
/**
 * 并行方式执行slice scroll操作
 */
@Test
public void testParralSliceScroll() {
    final ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/scroll.xml");
    final List<String> scrollIds = new ArrayList<>();
    long starttime = System.currentTimeMillis();
    //scroll slice分页检索
    final int max = 6;
    final CountDownLatch countDownLatch = new CountDownLatch(max);//线程任务完成计数器,每个线程对应一个sclice,每运行完一个slice任务,countDownLatch计数减去1

    for (int j = 0; j < max; j++) {//启动max个线程,并行处理每个slice任务
        final int i = j;
        Thread sliceThread = new Thread(new Runnable() {//多线程并行执行scroll操作做,每个线程对应一个sclice

            @Override
            public void run() {
                Map params = new HashMap();
                params.put("id", i);
                params.put("max", max);//最多6个slice,不能大于share数
                params.put("size", 100);//每页100条记录
                ESDatas<Map> sliceResponse = clientUtil.searchList("agentstat-*/_search?scroll=1m",
                        "scrollSliceQuery", params,Map.class);
                List<Map> sliceDatas = sliceResponse.getDatas();
                incrementSize( sliceDatas.size());//统计实际处理的文档数量
                long totalSize = sliceResponse.getTotalSize();
                String scrollId = sliceResponse.getScrollId();
                if (scrollId != null)
                    scrollIds.add(scrollId);
                System.out.println("totalSize:" + totalSize);
                System.out.println("scrollId:" + scrollId);
                if (sliceDatas != null && sliceDatas.size() >= 100) {//每页100条记录,迭代scrollid,遍历scroll分页结果
                    do {
                        sliceResponse = clientUtil.searchScroll("1m", scrollId, Map.class);
                        String sliceScrollId = sliceResponse.getScrollId();
                        if (sliceScrollId != null)
                            scrollIds.add(sliceScrollId);
                        sliceDatas = sliceResponse.getDatas();
                        if (sliceDatas == null || sliceDatas.size() < 100) {
                            break;
                        }
                        incrementSize( sliceDatas.size());//统计实际处理的文档数量
                    } while (true);
                }
                countDownLatch.countDown();//slice检索完毕后计数器减1
            }

        });
        sliceThread.start();//启动线程
    }
    try {
        countDownLatch.await();//等待所有的线程执行完毕,计数器变成0
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
      //打印处理耗时和实际检索到的数据
    long endtime = System.currentTimeMillis();
    System.out.println("耗时:"+(endtime - starttime)+",realTotalSize:"+realTotalSize);
    //查询存在es服务器上的scroll上下文信息
    String scrolls = clientUtil.executeHttp("_nodes/stats/indices/search", ClientUtil.HTTP_GET);
//      System.out.println(scrolls);
    //处理完毕后清除scroll上下文信息
    if(scrollIds.size() > 0) {
        scrolls = clientUtil.deleteScrolls(scrollIds);
//          System.out.println(scrolls);
    }
    //清理完毕后查看scroll上下文信息
    scrolls = clientUtil.executeHttp("_nodes/stats/indices/search", ClientUtil.HTTP_GET);
//      System.out.println(scrolls);
}

我今天测试scroll,一开始是远程访问Elaticsearch,scroll也很慢,比from、size慢很多,而且页数达到300就死活没有数据回来了。但是我发现我把Elasticsearch变成localhost之后,响应速度确实比from、size快了不止一个数量级啊!