分页 es全局搜索并且分页

 public PageUtils globalSearch(Map<String, Object> params) throws IOException {
        MapGet mapGet = MapGet.init(params);
        String[] indexName = {"test"};
        try{
            //创建搜索请求
            SearchRequest searchRequest = new SearchRequest(indexName);
            //创建构造器,构建搜索条件
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
            QueryStringQueryBuilder stringQuery = QueryBuilders.queryStringQuery((String) params.get("keyWord")).defaultOperator(Operator.AND);
            sourceBuilder.query(stringQuery);
            //得分排序
            //sourceBuilder.sort("_score", SortOrder.DESC);
            sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
            sourceBuilder.from();
            searchRequest.source(sourceBuilder);
            //最大返回结果数量
            sourceBuilder.size(10000);
            SearchResponse search = elasticsearchClient.search(searchRequest, RequestOptions.DEFAULT);
            SearchHits hits = search.getHits();
            SearchHit[] hits1 = hits.getHits();
            ArrayList<Object> obj = new ArrayList<>();

            for (SearchHit documentFields : hits1) {
                //获取到hits中的每一个asString
                String asString = documentFields.getSourceAsString();
                //数据封装
                JSONObject jsonObject = JSONObject.parseObject(asString);
                obj.add(jsonObject);
            }
            //分页
            int currPage = mapGet.getInt("page"); // 当前页
            int pageSize = mapGet.getInt("limit"); // 每页展示多少条数据

            if (currPage == 0) {
                currPage = 1;
            }
            int totalCount = obj.size(); // 一共多少条记录
            if(obj.size() != 0){
                int totalPage = totalCount % pageSize; // 一共多少页
                if (totalPage > 0) {
                    totalPage = totalCount / pageSize + 1;
                } else {
                    totalPage = totalCount / pageSize;
                }
                int fromIndex = 0; // 开始索引
                int toIndex = 0; // 结束索引

                if(currPage != totalPage){
                    fromIndex = (pageSize * (currPage - 1));
                    toIndex = fromIndex + pageSize;
                }else {
                    fromIndex = (pageSize * (currPage - 1));
                    toIndex = totalCount;
                }
                //当前页不存在数据就返回空列表
                ArrayList<Object> list = new ArrayList<>();
                if(toIndex >obj.size()){
                    return new PageUtils(list, totalCount, pageSize, currPage);
                }
                return new PageUtils(obj.subList(fromIndex, toIndex), totalCount, pageSize, currPage);
            }
            return new PageUtils(obj, totalCount, pageSize, currPage);

        }catch (Exception e){
            e.printStackTrace();
            throw new RRException(e.getMessage());
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值