Browse Source

井筒-领域检索

xiaoqiao 1 year ago
parent
commit
719d9f6e26

+ 20 - 2
src/main/java/com/bowintek/practice/services/impl/EsQueryServiceImpl.java

@@ -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);}

+ 56 - 3
vue/src/views/esdomain/result.vue

@@ -22,6 +22,24 @@
       </template>
     </a-divider>
     <a-spin :spinning="pageState.loading">
+      <div class="search-group" :style="{ height: pageState.showMoreKey? '150px':'38px' }">
+        <div class="search-group-title">检索井名:</div>
+        <div class="search-group-item">
+          <template v-for="it in keyList">
+            <span>{{it.key}}</span>
+          </template>
+        </div>
+        <div class="search-group-expend" @click="pageState.showMoreKey=!pageState.showMoreKey">
+          <p v-if="!pageState.showMoreKey">
+            展 开
+            <DoubleRightOutlined rotate="90"/>
+          </p>
+          <p v-else>
+            收 起
+            <DoubleLeftOutlined rotate="90"/>
+          </p>
+        </div>
+      </div>
       <div class="search-container">
         <div class="search-body">
           <div class="search-order">
@@ -136,6 +154,7 @@ export default defineComponent({
     const tabsViewStore = useTabsViewStore();
     const dataList = ref<any>([]);
     const indexList = ref<any>([]);
+    const keyList = ref<any>([]);
     const formState = reactive({
       page: 1, rows: 10, subjectName: '', tabName: '', tabCode: null, total: 0
     });
@@ -145,7 +164,8 @@ export default defineComponent({
       showMoreQuery: false,
       defaultOrder: '',
       timeOrder: '',
-      loading: false
+      loading: false,
+      showMoreKey:false
     });
     const pageParams = ref({
       total: 0,
@@ -193,6 +213,7 @@ export default defineComponent({
         dataList.value = (result as any).rows;
         pageParams.value.total = (result as any).total;
 
+        keyList.value = (result as any).agg;
         getIndexSetting();
       });
     }
@@ -230,7 +251,8 @@ export default defineComponent({
       loadData,
       dataList,
       handleChange,
-      pageParams
+      pageParams,
+      keyList
     };
   },
   created() {
@@ -244,7 +266,38 @@ export default defineComponent({
 });
 </script>
 
-<style lang="less">
+<style lang="less" scoped>
+.search-group{
+  display: flex;
+  width: 100%;
+  border: 1px solid #f0f0f0;
+  padding:10px;
+  margin-bottom: 10px;
+  overflow-y: hidden;
+
+  .search-group-title{
+    width: 120px;
+    font-weight: bold;
+    border-right: 1px solid #f0f0f0;
+    text-align: center;
+  }
+  .search-group-item{
+    flex: 1;
+    display: flex;
+    flex-wrap: wrap;
+    span {
+      padding: 0px 10px 5px 10px;
+      cursor: pointer;
+    }
+  }
+  .search-group-expend{
+    border-left: 1px solid #f0f0f0;
+    width: 120px;
+    cursor: pointer;
+    text-align: center;
+  }
+}
+
 .search-container {
   display: flex;