作者: whooyun发表于: 2023-09-21 15:25
@Override public List<ESAdsSaleGdsOrgAmtNd> querySaleOrgGdsNoLimit(SingleAnalysisConditionDto condition, CommonPageDto page) { List<ESAdsSaleGdsOrgAmtNd> resultList = new ArrayList<>(); // 替换为您的索引名称 String indexName = "ads_sale_gds_org_amt_nd"; // 每页的文档数 int pageSize = 1000; // 上一页的排序字段值 Object[] lastSortValues = null; try { while (true) { // 创建搜索请求 SearchRequest searchRequest = new SearchRequest(indexName); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); String[] include = {"own_org_id", "sku_id"}; searchSourceBuilder.fetchSource(include, null); // 构建查询 // 使用 constant_score 查询,忽略 _score searchSourceBuilder.query(QueryBuilders.constantScoreQuery(this.createQueryBuilder(condition, null))); searchRequest.source(searchSourceBuilder); // 设置分页参数 searchSourceBuilder.size(pageSize); // 使用search_after来设置上一页的排序字段值 if (lastSortValues != null) { searchSourceBuilder.searchAfter(lastSortValues); } // 创建排序规则 // 设置排序字段和排序方式 searchSourceBuilder.sort(new FieldSortBuilder("day_cnt").order(SortOrder.ASC)); searchSourceBuilder.sort(new FieldSortBuilder("sku_id").order(SortOrder.ASC)); searchSourceBuilder.sort(new FieldSortBuilder("stat_dtm").order(SortOrder.ASC)); searchRequest.source(searchSourceBuilder); // 执行查询 SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); // 处理查询结果 SearchHits hits = searchResponse.getHits(); for (SearchHit hit : searchResponse.getHits().getHits()) { // 获取文档的数据 Map<String, Object> sourceAsMap = hit.getSourceAsMap(); // 使用json将sourceAsMap转换为ESAdsSaleGdsOrgAmtNd对象 ESAdsSaleGdsOrgAmtNd gds = JSON.parseObject(JSON.toJSONString(sourceAsMap), ESAdsSaleGdsOrgAmtNd.class); resultList.add(gds); } SearchHit[] sortHits = searchResponse.getHits().getHits(); // 获取上一页的排序字段值,用于下一页查询 if (sortHits.length > 0) { lastSortValues = sortHits[sortHits.length - 1].getSortValues(); } else { // 如果没有更多数据,退出循环 break; } } } catch (IOException e) { e.printStackTrace(); } return resultList; }