/**
* 分组去重
*/
public static Map<String, Object> searchGroupHistoryQuery(SearchHistoryQuery historyQuery, String esIndex) {
RestHighLevelClient client = getRestHighLevelClient();
Map<String, Object> result = new HashMap<>();
try {
List<Map<String, Object>> list = new ArrayList<>();
SearchRequest searchRequest = new SearchRequest(esIndex);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
int currentPage = 1, pageSize = 10;
if (historyQuery.getCurrentPage() != null) {
currentPage = historyQuery.getCurrentPage();
}
if (historyQuery.getPageSize() != null) {
pageSize = historyQuery.getPageSize();
}
sourceBuilder.from((currentPage - 1) * pageSize);
sourceBuilder.size(pageSize);
sourceBuilder.sort("createTime", SortOrder.DESC);
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
.must(QueryBuilders.termQuery("userCode", historyQuery.getUserCode()))
.must(QueryBuilders.termQuery("status", 1))
.mustNot(QueryBuilders.termQuery("query.keyword",""));
if (historyQuery.getSearchType() != null) {
TermQueryBuilder matchQueryBuilder = QueryBuilders.termQuery("searchType", historyQuery.getSearchType());
boolQueryBuilder.must(matchQueryBuilder);
}
sourceBuilder.query(boolQueryBuilder);
// 将重复数据折叠
CollapseBuilder collapseBuilder = new CollapseBuilder("query.keyword");
sourceBuilder.collapse(collapseBuilder);
AggregationBuilder aggregation = AggregationBuilders.cardinality("total_size").field("query.keyword");
sourceBuilder.aggregation(aggregation);
// 设置聚合(去重)
// 创建 terms 聚合
// TermsAggregationBuilder termsAggregation = AggregationBuilders.terms("unique_values")
// .field("query.keyword")
// .size(pageSize);
//
// // 在 terms 聚合内部创建 cardinality 聚合
//// CardinalityAggregationBuilder cardinalityAggregation = AggregationBuilders.cardinality("distinct_count")
//// .field("query.keyword");
//
// // 创建 top_hits 子聚合
// TopHitsAggregationBuilder topHitsAggregation = AggregationBuilders.topHits("top_docs")
// .size(pageSize)
// .fetchSource(new String[]{"searchHistoryID","ip","productCode","productName","query","queryJson","resCode","resName","searchRange","searchType","uf","userCode","uv"}, null);
//
// // 将 cardinality 聚合添加到 terms 聚合中
// termsAggregation.subAggregation(topHitsAggregation);
//
// // 将 terms 聚合添加到搜索源构建器中
// sourceBuilder.aggregation(termsAggregation);
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
// SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
// 处理结果
// Aggregation aggregation = searchResponse.getAggregations().get("unique_values");
// if (aggregation instanceof ParsedStringTerms) {
// ParsedStringTerms parsedTerms = (ParsedStringTerms) aggregation;
// for (Terms.Bucket bucket : parsedTerms.getBuckets()) {
// Map<String, Object> sourceAsMap = new HashMap<>();
// String term = bucket.getKeyAsString();
// // 处理每个分组的信息
// ParsedTopHits topHits = bucket.getAggregations().get("top_docs");
// for (SearchHit hit : topHits.getHits()) {
// sourceAsMap = hit.getSourceAsMap();
// }
// sourceAsMap.put("query", term);
// list.add(sourceAsMap);
// }
// }
for (SearchHit doc : searchResponse.getHits().getHits()) {
Map<String, Object> sourceAsMap = doc.getSourceAsMap();
list.add(sourceAsMap);
}
// int total = (int) searchResponse.getHits().getTotalHits().value;
// int total = getTotal(historyQuery, esIndex);
assert searchResponse != null;
ParsedCardinality parsedCardinality = (ParsedCardinality) searchResponse.getAggregations().asList().get(0);
int total = (int)parsedCardinality.getValue();
result.put("data", list);
result.put("total", total);
result.put("totalPage", total == 0 ? 0 : (total % pageSize == 0 ? total / pageSize : (total / pageSize) + 1));
result.put("pageSize", pageSize);
result.put("currentPage", currentPage);
} catch (Exception ex) {
log.error("getSearchHistory error: ", ex);
} finally {
//closeClient(client);
}
return result;
}
注意:本文归作者所有,未经作者允许,不得转载