• es 深度分页查询


    依赖

     <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>elasticsearch-rest-high-level-client</artifactId>
                <version>6.8.5</version>
            </dependency>

    配置

    代码

    package com.elasticsearch.controller;
    
    import org.elasticsearch.action.search.SearchRequest;
    import org.elasticsearch.action.search.SearchResponse;
    import org.elasticsearch.client.RequestOptions;
    import org.elasticsearch.client.RestHighLevelClient;
    import org.elasticsearch.index.query.QueryBuilders;
    import org.elasticsearch.search.SearchHit;
    import org.elasticsearch.search.SearchHits;
    import org.elasticsearch.search.builder.SearchSourceBuilder;
    import org.elasticsearch.search.sort.FieldSortBuilder;
    import org.elasticsearch.search.sort.SortOrder;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.io.IOException;
    
    @RestController
    public class ElasticsearchController {
    
        @Autowired
        private RestHighLevelClient client;
    
        @GetMapping("/page")
        public String page(int page,int size) throws IOException {
            SearchRequest searchRequest = new SearchRequest( "app_list" );
            searchRequest.types( "_doc" );
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query( QueryBuilders.matchAllQuery() );
    
            searchSourceBuilder.sort(new FieldSortBuilder("_id").order( SortOrder.ASC));
            int from = (page - 1) * size;
            int sum = page * size;
            if(sum > 10000){
                searchSourceBuilder.from(0);
                searchSourceBuilder.size(10000);
    
                searchRequest.source( searchSourceBuilder );
                System.out.println(searchSourceBuilder);
    
                SearchResponse searchResponse = client.search( searchRequest ,RequestOptions.DEFAULT);
                SearchHits hits = searchResponse.getHits();
                if(hits.getTotalHits() < sum - size){
                    System.out.println("数据不够--");
                    return null;
                }
    
                SearchHit[] hitsHits = hits.getHits();
    
                if(hitsHits.length > 0){
                    int eachSize = sum / 10000;
                    if(eachSize * 10000 < sum){
                        eachSize++;
                    }
    
                    SearchHit last = hitsHits[hitsHits.length - 1];
                    for (int i = 1; i < eachSize; i++) {
    
                        searchSourceBuilder.searchAfter(last.getSortValues());
    
                        searchRequest.source( searchSourceBuilder );
                        System.out.println(searchSourceBuilder);
    
                        searchResponse = client.search( searchRequest ,RequestOptions.DEFAULT);
                        hits = searchResponse.getHits();
                        hitsHits = hits.getHits();
                        if(hitsHits.length == 0){// 没有那么多数据
                            break;
                        }
                        last = hitsHits[hitsHits.length - 1];
                    }
    
                    int skipSize = (10000) * (eachSize - 1);
                    int resultSum = sum - skipSize;
                    int returnSize = resultSum / size;
                    int mod = resultSum % size;
                    if(mod == 0){
                        SearchHit[] result = new SearchHit[size];
                        int start = hitsHits.length - size;
                        for (int i = start; i < hitsHits.length; i++) {
                            result[i - start] = hitsHits[i];
                        }
                        System.out.println(2);
                    }else {
                        int resultsize = hitsHits.length - (returnSize * size);
                        SearchHit[] result = new SearchHit[size];
                        int start = hitsHits.length - resultsize;
                        for (int i = start; i < hitsHits.length; i++) {
                            result[i - start] = hitsHits[i];
                        }
                        System.out.println(1);
    
    
                    }
    
    
    
    
    
                    System.out.println("超过10000---");
    
                }
    
            }else {
                searchSourceBuilder.from(from);
                searchSourceBuilder.size(size);
                searchRequest.source( searchSourceBuilder );
                System.out.println(searchSourceBuilder);
    
                SearchResponse searchResponse = client.search( searchRequest ,RequestOptions.DEFAULT);
    
                SearchHits hits = searchResponse.getHits();
                SearchHit[] hitsHits = hits.getHits();
    
    
    
                System.out.println(" not over 10000");
            }
    
            return "page";
        }
    }
  • 相关阅读:
    PageControl1
    IE6 发送奇数个汉字出现的BUG机器解决之道
    十 C# Socket协议的形象描述
    深入浅出URL编码
    认识session
    Servlet过滤器介绍之原理分析
    web开发标准
    JS 截取字符串及字符与数字转换
    Servet和JSP学习总结(转载)
    端口、系统服务、系统进程概念
  • 原文地址:https://www.cnblogs.com/dongma/p/14111968.html
Copyright © 2020-2023  润新知