• Java客户端调用elasticsearch进行深度分页查询 (search_after)


    Java客户端调用elasticsearch进行深度分页查询 (search_after)

    前言
    这是我在这个网站整理的笔记,有错误的地方请指出,关注我,接下来还会持续更新。
    作者:神的孩子都在歌唱

    具体的Search_after解释,可以看我这篇文章 elasticsearch 深度分页查询 Search_after(图文教程)

    image-20240306154454909

    一. 代码

    public class Test {
    
        public static void main(String[] args) throws IOException {
    
            // 创建客户端
            final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(AuthScope.ANY,
                    new UsernamePasswordCredentials("elastic", "chenyunzhi"));
    
            // 跳过ssl验证
            SSLFactory sslFactory = SSLFactory.builder()
                    .withUnsafeTrustMaterial()
                    .withUnsafeHostnameVerifier()
                    .build();
    
            RestClientBuilder builder = RestClient.builder(
                            new HttpHost("192.168.1.47", 9200,"https"))
                    .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                        @Override
                        public HttpAsyncClientBuilder customizeHttpClient(
                                HttpAsyncClientBuilder httpClientBuilder) {
                            return httpClientBuilder
                                    .setSSLContext(sslFactory.getSslContext())
                                    .setSSLHostnameVerifier(sslFactory.getHostnameVerifier())
                                    .setDefaultCredentialsProvider(credentialsProvider);
                        }
                    });
    
            // Create the transport with a Jackson mapper
            ElasticsearchTransport transport = new RestClientTransport(builder.build(), new JacksonJsonpMapper());
            // 客户端连接
            ElasticsearchClient client = new ElasticsearchClient(transport);
    
            // 创建pit
            OpenPointInTimeRequest.Builder build = new OpenPointInTimeRequest.Builder();
            OpenPointInTimeRequest.Builder builder1 = build.index("test-000005").keepAlive(new Time.Builder().time("1m").build());
            OpenPointInTimeResponse openPointInTimeResponse = client.openPointInTime(builder1.build());
            String id = openPointInTimeResponse.id();
    
            // 构造查询条件
            SearchRequest.Builder sort = new SearchRequest.Builder().size(5)
                    .pit(p->p.id(id).keepAlive(k->k.time("1m")))
                    .sort(s -> s.field(f -> f.field("name.keyword").order(SortOrder.Desc)));
    
            // 获取第一次查询的结果
            List<Hit<HashMap>> hits = client.search(sort.build(), HashMap.class).hits().hits();
    
            // 检查 hits 列表是否为空
            if (!hits.isEmpty()) {
                //  获取最后一个值的sort
                List<String> sort1 = hits.get(hits.size() - 1).sort();
    
                // 再次构造查询条件 加上searchAfter
                sort = new SearchRequest.Builder().size(5)
                        .pit(p->p.id(id).keepAlive(k->k.time("1m")))
                        .searchAfter(sort1)
                        .sort(s -> s.field(f -> f.field("name.keyword").order(SortOrder.Desc)));
    
                List<Hit<HashMap>> hits1 = client.search(sort.build(), HashMap.class).hits().hits();
    
                System.out.println("-------------------------第一次查询结果-------------------------");
                System.out.println(hits);
    
                System.out.println("-------------------------最后一个值的sort-------------------------");
                System.out.println(sort1);
    
                System.out.println("-------------------------第二次查询结果-------------------------");
                System.out.println(hits1);
    
            }
    
            // 关闭
            client.closePointInTime(c->c.id(id));
            transport.close();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76

    二. 测试结果

    image-20240312174440180

    参考:https://blog.csdn.net/qq_44056652/article/details/126341810

    作者:神的孩子都在歌唱
    本人博客:https://blog.csdn.net/weixin_46654114
    转载说明:务必注明来源,附带本人博客连接。

  • 相关阅读:
    Vue Router的进阶
    【顺序表和链表】
    软件卸载quickuninstall
    使用图像处理跟踪瞳孔(Matlab代码实现)
    12种爱自己的方式
    【Linux 】向Shell脚本传递参数、getopts、getopt
    面试题之数组的去重方式
    生成对抗网络 – Generative Adversarial Networks | GAN
    使用 vue + vant 开发移动端网页
    Javascript 基础知识学习
  • 原文地址:https://blog.csdn.net/weixin_46654114/article/details/136664785