|
@@ -2,6 +2,10 @@ package com.bowintek.practice.services.impl;
|
|
|
|
|
|
import co.elastic.clients.elasticsearch.ElasticsearchClient;
|
|
|
import co.elastic.clients.elasticsearch._types.FieldValue;
|
|
|
+import co.elastic.clients.elasticsearch._types.aggregations.Aggregate;
|
|
|
+import co.elastic.clients.elasticsearch._types.aggregations.Buckets;
|
|
|
+import co.elastic.clients.elasticsearch._types.aggregations.StringTermsAggregate;
|
|
|
+import co.elastic.clients.elasticsearch._types.aggregations.StringTermsBucket;
|
|
|
import co.elastic.clients.elasticsearch._types.mapping.Property;
|
|
|
import co.elastic.clients.elasticsearch._types.mapping.TextProperty;
|
|
|
import co.elastic.clients.elasticsearch._types.query_dsl.*;
|
|
@@ -165,14 +169,17 @@ public class EsQueryServiceImpl implements EsQueryService {
|
|
|
//==>高亮设置
|
|
|
searchRequest.highlight(h->h.fields("*", f->f.matchedFields("*")));
|
|
|
|
|
|
+ //查询后分组
|
|
|
+ searchRequest.aggregations("group_well",a->a.terms(t->t.field("well_id.keyword")));
|
|
|
//[5]Es发起查询
|
|
|
SearchRequest request = searchRequest.build();
|
|
|
- SearchResponse response = esClient.search(request, ObjectNode.class);
|
|
|
+ SearchResponse<ObjectNode> response = esClient.search(request, ObjectNode.class);
|
|
|
|
|
|
//[6]转换结果,可以对不同的index做出参数输出
|
|
|
List<Map<String, Object>> rows = searchResponse2List(response);
|
|
|
result.put("rows", rows);
|
|
|
result.put("total", response.hits().total().value());
|
|
|
+ result.put("agg",aggResponse2List(response));
|
|
|
|
|
|
//请求参数输出,方便调试
|
|
|
String[] jsonStrings = request.toString().split("typed_keys=true");
|
|
@@ -347,7 +354,18 @@ public class EsQueryServiceImpl implements EsQueryService {
|
|
|
JsonNode rootNode = objectMapper.readTree(jsonString);
|
|
|
return rootNode;
|
|
|
}
|
|
|
-
|
|
|
+ public List<Map<String, Object>> aggResponse2List(SearchResponse<ObjectNode> searchResponse)
|
|
|
+ {
|
|
|
+ List<StringTermsBucket> buckets =searchResponse.aggregations().get("group_well").sterms().buckets().array();
|
|
|
+ List<Map<String, Object>> aggs = new ArrayList<>();
|
|
|
+ for (StringTermsBucket bucket : buckets) {
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("key", bucket.key().stringValue());
|
|
|
+ map.put("doc_count", bucket.docCount());
|
|
|
+ aggs.add(map);
|
|
|
+ }
|
|
|
+ return aggs;
|
|
|
+ }
|
|
|
public List<Map<String, Object>> searchResponse2List(SearchResponse<ObjectNode> searchResponse) {
|
|
|
|
|
|
if (searchResponse == null) {return new ArrayList<>(0);}
|