فهرست منبع

智能搜索,阶段2;结合界面,增加限制条件,多检索内容条件组装

周壕 9 ماه پیش
والد
کامیت
539429372f

+ 7 - 3
src/main/java/com/bowintek/practice/controller/EsQueryController.java

@@ -9,6 +9,7 @@ import com.bowintek.practice.model.SrSaerchtemp;
 import com.bowintek.practice.model.SrTempData;
 import com.bowintek.practice.services.service.*;
 import com.bowintek.practice.vo.Analyze.ComparisonResult;
+import com.bowintek.practice.vo.Analyze.EsQueryModel;
 import com.bowintek.practice.vo.SaerchtempVo;
 import com.bowintek.practice.vo.system.RoleModel;
 import com.bowintek.practice.vo.temp.GenSqlStringResult;
@@ -30,9 +31,12 @@ public class EsQueryController {
     @Autowired
     private EsQueryService esQueryService;
 
-    @GetMapping("/getQuery")
-    public BaseResponse getQuery(String text, int page, int limit) {
-        Map<String, Object> results = esQueryService.query(text, page, limit);
+    @PostMapping("/Query")
+    public BaseResponse getQuery(@RequestBody EsQueryModel model) {
+        Map<String, Object> results = esQueryService.query(model.getQueryList(),
+                model.getLimiters(),
+                model.getPage(),
+                model.getLimit());
         return RespGenerstor.success(results);
     }
 }

+ 79 - 24
src/main/java/com/bowintek/practice/services/impl/EsQueryServiceImpl.java

@@ -16,10 +16,13 @@ import com.bowintek.practice.services.service.AnalyzeService;
 import com.bowintek.practice.services.service.EsQueryService;
 import com.bowintek.practice.util.StringUtils;
 import com.bowintek.practice.vo.Analyze.ComparisonResult;
+import com.bowintek.practice.vo.Analyze.EsQueryText;
 import com.bowintek.practice.vo.EsIndexVo;
 import com.bowintek.practice.vo.EsIndexfieldVo;
 import com.bowintek.practice.vo.EsQueryLogVo;
+import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.node.ObjectNode;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -74,34 +77,76 @@ public class EsQueryServiceImpl implements EsQueryService {
         return pathCache;
     }
 
+    public List<Query> getComparisonQueryList(EsQueryText queryText){
+        List<Query> queryList = new ArrayList<>();
+        //分析查询字符串,有的字符串需要变成条件查询
+        List<ComparisonResult> cmpList = analyzeService.analyzeJavas(queryText.getKeyString());
+        //二级查询的路径信息
+        List<String> pathList = getPathCache();
+
+        for (int i = 0; i < cmpList.size(); i++) {
+            ComparisonResult cmp = cmpList.get(i);
+            //检查查询、对比的值是否为空
+            if(StringUtils.IsNullEmpty(cmp.getKeyString())) continue;
+
+            if (cmp.getSearchType().equals("comparison")) { //对比查询,有字段,有对比符号,有值
+                //第一级,对比搜索
+                queryList.addAll(getRangeQueryByComparison(cmp));
+                //第二级,对比搜索
+                for(String path : pathList) {
+                    queryList.addAll(getNestedRangeQueryByComparison(cmp, path));
+                }
+            } else if(StringUtils.IsNullEmpty(queryText.getField())){//全文查询,没有限制字段
+                //第一级,文字搜索
+                queryList.add(getMultiMatchQuery(cmp.getKeyString()));
+                //第二级,嵌套类型,文字搜索
+                for(String path : pathList) {
+                    queryList.add(getNestedMultiMatchQuery(path, cmp.getKeyString()));
+                }
+            } else { //全文查询,限定了字段
+                //第一级,文字搜索
+                queryList.add(getMultiMatchQuery(new String[]{queryText.getField()} ,cmp.getKeyString()));
+                //第二级,嵌套类型,文字搜索
+                for(String path : pathList) {
+                    queryList.add(getNestedMultiMatchQuery(path, new String[]{queryText.getField()}, cmp.getKeyString()));
+                }
+            }
+        }
+        return queryList;
+    }
+
     @Override
-    public Map<String, Object> query(String text, int page, int limit) {
+    public Map<String, Object> query(List<EsQueryText> queryList, List<ComparisonResult> limiters, int page, int limit) {
         //[1]需要返回的结果map
         Map<String, Object> result = new HashMap<>();
         result.put("total", 0);
 
         try {
-            //[2]分析查询字符串,有的字符串需要变成条件查询
-            List<ComparisonResult> cmpList = analyzeService.analyzeJavas(text);
-            //[3]有查询条件分析内容,组装查询条件
+            //二级查询的路径信息
             List<String> pathList = getPathCache();
-            List<Query> queryList = new ArrayList<>();
-            for (int i = 0; i < cmpList.size(); i++) {
-                ComparisonResult cmp = cmpList.get(i);
-                if (cmp.getSearchType().equals("comparison")) {
-                    //第一级,对比搜索
-                    queryList.addAll(getRangeQueryByComparison(cmp));
-                    //第二级,对比搜索
-                    for(String path : pathList) {
-                        queryList.addAll(getNestedRangeQueryByComparison(cmp, path));
-                    }
-                } else {
-                    //第一级,文字搜索
-                    queryList.add(getMultiMatchQuery(cmp.getKeyString()));
-                    //第二级,嵌套类型,文字搜索
-                    for(String path : pathList) {
-                        queryList.add(getNestedMultiMatchQuery(path, cmp.getKeyString()));
-                    }
+
+            List<Query> queryMustList = new ArrayList<>();
+            //[2]分析查询字符串,有的字符串需要变成条件查询
+            for(EsQueryText queryText : queryList){
+                List<Query> shouldQuerys = getComparisonQueryList(queryText);
+                if(shouldQuerys.size()>0){
+                    queryMustList.add(Query.of(q->q.bool(b->b.should(shouldQuerys))));
+                }
+            }
+            //[3]限定查询条件,都是有条件的查询
+            for(ComparisonResult cmp:limiters){
+                List<Query> cmpQuerys = new ArrayList<>();
+                //检查查询、对比的值是否为空
+                if(StringUtils.IsNullEmpty(cmp.getValue())) continue;
+
+                //第一级,对比搜索
+                cmpQuerys.addAll(getRangeQueryByComparison(cmp));
+                //第二级,对比搜索
+                for(String path : pathList) {
+                    cmpQuerys.addAll(getNestedRangeQueryByComparison(cmp, path));
+                }
+                if(cmpQuerys.size()>0){
+                    queryMustList.add(Query.of(q->q.bool(b->b.should(cmpQuerys))));
                 }
             }
 
@@ -113,8 +158,8 @@ public class EsQueryServiceImpl implements EsQueryService {
             searchRequest.size(limit);
             searchRequest.from(page * limit);
             //==>设置查询条件
-            if (queryList.size() > 0) {
-                searchRequest.query(q -> q.bool(b -> b.should(queryList)));
+            if (queryMustList.size() > 0) {
+                searchRequest.query(q -> q.bool(b -> b.must(queryMustList)));
             }
             //==>高亮设置
             searchRequest.highlight(h->h.fields("*", f->f.matchedFields("*")));
@@ -127,7 +172,11 @@ public class EsQueryServiceImpl implements EsQueryService {
             List<Map<String, Object>> rows = searchResponse2List(response);
             result.put("rows", rows);
             result.put("total", response.hits().total().value());
-            result.put("QueryString", request.toString());
+
+            //请求参数输出,方便调试
+            String[] jsonStrings = request.toString().split("typed_keys=true");
+            result.put("SearchUrl", jsonStrings[0]);
+            result.put("SearchRequest", stringToNodeJson(jsonStrings[1]));
             System.out.println(response.hits().total() + " " + request.toString());
         }
         catch (Exception ex){
@@ -286,6 +335,12 @@ public class EsQueryServiceImpl implements EsQueryService {
         System.out.println("update mappings ack: " + acknowledged);
     }
 
+    public JsonNode stringToNodeJson(String jsonString) throws JsonProcessingException {
+        ObjectMapper objectMapper = new ObjectMapper();
+        JsonNode rootNode = objectMapper.readTree(jsonString);
+        return rootNode;
+    }
+
     public List<Map<String, Object>> searchResponse2List(SearchResponse<ObjectNode> searchResponse) {
 
         if (searchResponse == null) {return new ArrayList<>(0);}

+ 2 - 1
src/main/java/com/bowintek/practice/services/service/EsQueryService.java

@@ -2,10 +2,11 @@ package com.bowintek.practice.services.service;
 
 import co.elastic.clients.elasticsearch.indices.AnalyzeResponse;
 import com.bowintek.practice.vo.Analyze.ComparisonResult;
+import com.bowintek.practice.vo.Analyze.EsQueryText;
 
 import java.util.List;
 import java.util.Map;
 
 public interface EsQueryService {
-    Map<String, Object> query(String text, int page, int limit);
+    Map<String, Object> query(List<EsQueryText> queryList, List<ComparisonResult> limiters, int page, int limit);
 }

+ 13 - 0
src/main/java/com/bowintek/practice/vo/Analyze/EsQueryModel.java

@@ -0,0 +1,13 @@
+package com.bowintek.practice.vo.Analyze;
+
+import lombok.Data;
+
+import java.util.List;
+
+@Data
+public class EsQueryModel {
+    private List<ComparisonResult> limiters;
+    private List<EsQueryText> queryList;
+    private int page;
+    private int limit;
+}

+ 11 - 0
src/main/java/com/bowintek/practice/vo/Analyze/EsQueryText.java

@@ -0,0 +1,11 @@
+package com.bowintek.practice.vo.Analyze;
+
+import lombok.Data;
+
+import java.util.List;
+
+@Data
+public class EsQueryText {
+    private String keyString;
+    private String field;
+}

+ 48 - 27
vue/src/views/esdomain/index.vue

@@ -9,47 +9,43 @@
     >
       <a-divider orientation="left"><b style="color:#4E61D0">1.输入检索控制条件</b></a-divider>
       <a-row :gutter="24">
-        <a-col :span="22">
+        <a-col :span="24">
           <div class="col-title">组织单位:</div>
-          <a-input v-model:value="formState.subjectName" style="width: 200px"></a-input>
+          <a-input v-model:value="formState.text" style="width: 400px"></a-input>
         </a-col>
         <a-col :span="24">
-          <div class="col-title">
-          作业区/项目部:
-           </div>
-          <a-input v-model:value="formState.tabCode" style="width: 200px"></a-input>
+          <div class="col-title">作业区/项目部:</div>
+          <a-input v-model:value="formState.text" style="width: 400px"></a-input>
         </a-col>
         <a-col :span="24">
-          <div class="col-title">
-          井名:
-           </div>
-          <a-input v-model:value="formState.tabName" style="width: 200px"></a-input>
+          <div class="col-title">井名:</div>
+          <a-input v-model:value="formState.well_common_name" style="width: 400px"></a-input>
         </a-col>
         <a-col :span="24">
-          <div class="col-title">
-          地层名称:
-           </div>
-          <a-input v-model:value="formState.tabName" style="width: 200px"></a-input>
+          <div class="col-title">地层名称:</div>
+          <a-input v-model:value="formState.formation_name" style="width: 400px"></a-input>
         </a-col>
       </a-row>
       <a-divider orientation="left"><b style="color:#4E61D0">2.输入检索内容条件</b></a-divider>
       <a-row :gutter="24">
         <a-col :span="24" class="query-row">
-          <div class="operTexts">&nbsp;</div>
-          <a-select v-model:value="formState.tabName" :options="fieldList" class="operTexts"></a-select>
-          <a-input v-model:value="formState.subjectName" style="width: 200px"></a-input>
+          <div class="col-title"></div>
+          <a-select v-model:value="formState.defaultField" :options="fieldList" class="operTexts"></a-select>
+          <a-input v-model:value="formState.defaultKeyString" style="width: 285px"></a-input>
           <img src="~@/assets/images/add.png" @click="addQuery"/>
         </a-col>
         <a-col :span="24" class="query-row" v-for="(it,index) in queryList">
-          <a-select v-model:value="it.oper" :options="operTexts" class="operTexts"></a-select>
+          <div class="col-title"></div>
+          <!--a-select v-model:value="it.oper" :options="operTexts" class="operTexts"></a-select-->
           <a-select v-model:value="it.field" :options="fieldList" class="operTexts"></a-select>
-          <a-input v-model:value="it.key" style="width: 200px"></a-input>
+          <a-input v-model:value="it.keyString" style="width: 285px"></a-input>
           <img src="~@/assets/images/sub.png" @click="onDelete(index)"/>
         </a-col>
 
       </a-row>
-      <a-row>
-        <a-col :span="4" style="text-align: right">
+      <a-row :gutter="24">
+        <a-col :span="24" class="query-row">
+          <div class="col-title"></div>
           <a-button type="primary" html-type="submit" @click="onQuery">查询</a-button>
           <a-button style="margin: 0 8px" @click="() => {formRef.resetFields();}">重置</a-button>
         </a-col>
@@ -65,18 +61,23 @@ import {useRoute, useRouter} from "vue-router";
 import {DownOutlined, UpOutlined} from "@ant-design/icons-vue";
 import type {FormInstance, SelectProps} from "ant-design-vue";
 import {useTabsViewStore} from "@/store/modules/tabsView";
+import {save} from "@/api/common";
 
 export default defineComponent({
   name: 'domainESIndex',
   components: {DownOutlined, UpOutlined},
   setup() {
-
     const tabsViewStore = useTabsViewStore();
     const route = useRoute();
     const router = useRouter();
     const formRef = ref<FormInstance>();
     const formState = reactive({
-      page: 1, rows: 10, subjectName: '', tabName: '', tabCode: null, total: 0
+      page: 1, rows: 10,
+      well_common_name: '',
+      formation_name: '',
+      text:'',
+      defaultField:'',
+      defaultKeyString:''
     });
     const queryList= ref<any>([]);
 
@@ -89,15 +90,35 @@ export default defineComponent({
 
     const fieldList = [
       {value: '', label: '不限'},
-      {value: 'wellId', label: '井号'},
-      {value: 'wellName', label: '井名'}
+      {value: 'well_id', label: '井号'},
+      {value: 'well_common_name', label: '井名'}
     ] as SelectProps['options'];
 
     const onQuery = () => {
-      tabsViewStore.addTabByPath('/domain/result', {});
+      if(false) tabsViewStore.addTabByPath('/domain/result', {});
+      //组装查询条件
+      let queryObject = {
+        queryList : JSON.parse(JSON.stringify(queryList.value)),
+        limiters : [] as any,
+        page : 0,
+        limit : 10
+      };
+      //第一条默认查询条件
+      queryObject.queryList.splice(0, 0, {
+        field:formState.defaultField,
+        keyString:formState.defaultKeyString});
+      //限定条件写入
+      //{"fieldName":"开钻日期", "fields":["spud_date"],"opreation":"大于","value":"2022-03-28"}
+      queryObject.limiters.push({ fields:["well_common_name.keyword"], opreation:'等于', value:formState.well_common_name });
+
+      console.log(queryObject);
+
+      save('esquery/Query', queryObject).then(result => {
+        console.log(result);
+      });
     }
     function addQuery(){
-      queryList.value.push({});
+      queryList.value.push({field:'', keyString:''});
     }
     function onDelete(index) {
       queryList.value.splice(index, 1);

+ 0 - 5
vue/src/views/position/test.vue

@@ -62,7 +62,6 @@ import QueryHistoryComplete from '@/components/basic/querylog/history-complete.v
 import ChartCell from '@/components/basic/chart/chart-cell.vue'
 import ChartProdDynmics from '@/components/basic/chart/chart-prod-dynamics.vue'
 import ChartMap from '@/components/basic/chart/chart-map.vue'
-import {get} from "@/api/common";
 
 export default defineComponent({
   name: 'QueryTest',
@@ -84,10 +83,6 @@ export default defineComponent({
       if(true || formState.value.keyString && formState.value.keyString.trim().length>0) {
         //记录搜索关键字日志
         (queryHistoryComplete.value as any).saveHistory(formState.value.keyString);
-        //查询搜索结果
-        get('esquery/getQuery', {text: formState.value.keyString, page:0, limit:100}).then(result => {
-          console.log(result);
-        });
       }
     };