首页 > 其他分享 >ElasticSearch高级用法之滚动查询

ElasticSearch高级用法之滚动查询

时间:2022-08-27 17:34:08浏览次数:55  
标签:滚动 查询 hitsScroll ElasticSearch searchResponse scrollId 用法 scroll

由于es的限制,普通查询最多查询10000条数据,那么需要查询数据量大的情况怎么办呢?这个时候就可以使用滚动查询。代码如下:

 //设置查询超时时间
        Scroll scroll = new Scroll(TimeValue.timeValueMillis(5L));
        //滚动查询
        searchRequest.scroll(scroll);
        //记录要滚动的id
        String scrollId = searchResponse.getScrollId();
        //滚动查询部分,将从第1001笔数据开始取
        SearchHit[] hitsScroll = hits.getHits();
        while (hitsScroll != null && hitsScroll.length > 0) {
            //构造滚动查询条件
            SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId);
            scrollRequest.scroll(scroll);
            try {
                searchResponse = client.restHighLevelClient().scroll(scrollRequest, RequestOptions.DEFAULT);
            } catch (IOException e) {
                e.printStackTrace();
            }
            scrollId = searchResponse.getScrollId();
            System.out.println(scrollId);
            hits = searchResponse.getHits();
            hitsScroll = hits.getHits();
            //对结果进行操作
            for (SearchHit hit : hits) {
                CntdDataDeliver cntdDataDeliver=JSON.parseObject(hit.getSourceAsString(), CntdDataDeliver.class);
                list.add(cntdDataDeliver);
            }
        }
        //清除滚动
        ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
        clearScrollRequest.addScrollId(scrollId);
        try {
            client.restHighLevelClient().clearScroll(clearScrollRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }

标签:滚动,查询,hitsScroll,ElasticSearch,searchResponse,scrollId,用法,scroll
From: https://www.cnblogs.com/Johnson8888/p/16630968.html

相关文章

  • RestTemplate用法
    RestTemplate用法RestTemplate简介RestTemplate是一个同步的webhttp客户端请求模板工具,spring框架做的抽象模板,常见的http客户端请求工具有:JDK的HttpURLConne......
  • break用法详解
    在执行while循环或者for循环时,只要循环条件满足,程序会一直执行循环体。但在某些场景,我们希望在循环结束前就强制结束循环。Python中有两种强制结束循环的方法:1.continue语......
  • smarty模板引擎中变量及变量修饰器用法实例
    smarty模板引擎中变量及变量修饰器用法实例_php实例_脚本之家 https://www.jb51.net/article/60243.htm本文实例讲述了smarty变量及变量修饰器的应用。分享给大家供大家......
  • 【c++多线程】互斥量概念、用法、死锁演示以及unique_lock
    第5节互斥量概念、用法、死锁演示及解决详解(1)互斥量(mutex)的基本概念(2)互斥量的用法(2.1)lock(),unlock()(2.2)std::lock_guard类模板(3)死锁(3.1)......
  • 算法题python用法
    算法题python用法大写变小写往后移动一位chr(ord(v.lower())+1)大写、小写、数字i.isalpha():#英文i.isspace()#空格​ifitem.isupper():#大写     a......
  • R语言中apply函数的用法
     001、dat<-data.frame(a=c(3,8,2,1),b=c(8,4,2,6),c=c(2,7,6,9))##测试数据狂datapp......
  • Python枚举用法_Enum
    #-*-coding:utf-8-*-fromenumimportEnum,unique#1.枚举的定义#首先,定义枚举要导入enum模块。#枚举定义用class关键字,继承Enum类。#2.如果要限制定......
  • 用Linux安装Docker详细步骤以及Linux下使用docker安装elasticsearch
    https://blog.csdn.net/Saionyy/article/details/124243491 https://blog.csdn.net/weixin_42361442/article/details/121511762?spm=1001.2101.3001.6650.5&utm_medium......
  • pySpark RDD基本用法
    pySparkRDD基本用法RDD的全称是:ResilientDistributedDataset(弹性分布式数据集),它有几个关键的特性:RDD是只读的,表示它的不可变性。可以并行的操作分区集合上的所有元......
  • 【ElasticSearch】索引生命周期管理(三) 避坑指南
    背景主要是针对在使用索引生命周期的去管理索引的过程中,记录所踩到坑,避免同样的问题再次发生问题1. 索引生命周期中设置各个阶段的市场以及索引rollover的时间......