Browse Source

搜索记录

da-xian 3 months ago
parent
commit
31f0d0b1f0

+ 5 - 0
src/main/java/com/bowintek/smartsearch/controller/QueryLogController.java

@@ -25,6 +25,11 @@ public class QueryLogController {
         return RespGenerstor.success(queryLogService.getQueryList(keyString));
     }
 
+    @GetMapping("/getCurrentUserListByKeyString")
+    public BaseResponse getCurrentUserListByKeyString(String keyString) {
+        return RespGenerstor.success(queryLogService.getCurrentUserListByKeyString(keyString, accountService.getLoginUserID()));
+    }
+
     @PostMapping("/save")
     public BaseResponse save(@RequestBody EsQueryLogVo model) {
         return RespGenerstor.success(queryLogService.save(model.getQueryText(), accountService.getLoginUserID()));

+ 1 - 0
src/main/java/com/bowintek/smartsearch/mapper/cquery/EsQueryLogCQuery.java

@@ -6,4 +6,5 @@ import java.util.List;
 
 public interface EsQueryLogCQuery {
     List<EsQueryLogVo> getQueryGroupList(String queryText);
+    List<EsQueryLogVo> getCurrentUserQueryGroupList(String queryText, String userId);
 }

+ 40 - 8
src/main/java/com/bowintek/smartsearch/services/impl/QueryLogServiceImpl.java

@@ -1,10 +1,12 @@
 package com.bowintek.smartsearch.services.impl;
 
+import ch.qos.logback.core.joran.node.ComponentNode;
 import com.bowintek.smartsearch.mapper.EsQueryLogMapper;
 import com.bowintek.smartsearch.mapper.EsRelationLogMapper;
 import com.bowintek.smartsearch.mapper.cquery.EsQueryLogCQuery;
 import com.bowintek.smartsearch.model.*;
 import com.bowintek.smartsearch.services.service.QueryLogService;
+import com.bowintek.smartsearch.util.DictionaryUtils;
 import com.bowintek.smartsearch.vo.EsQueryLogVo;
 import com.github.pagehelper.PageHelper;
 import com.github.pagehelper.PageInfo;
@@ -12,9 +14,12 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.cache.annotation.Cacheable;
 import org.springframework.stereotype.Component;
 
-import java.util.Date;
-import java.util.List;
+import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.function.Function;
+import java.util.function.Predicate;
 import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 @Component
 public class QueryLogServiceImpl implements QueryLogService {
@@ -45,12 +50,11 @@ public class QueryLogServiceImpl implements QueryLogService {
     }
 
     @Override
-    @Cacheable(value = "esQueryLogList")
     public List<EsQueryLogVo> getQueryList(String queryText) {
-        /*synchronized (lockObject) {
+        synchronized (lockObject) {
             //用户输入查询时,从本地缓存加载数据
             long timeSpan = (new Date()).getTime() - cacheTime;
-            if (timeSpan > 30 * 1000 || queryTextCache == null) {
+            if (timeSpan > 5 * 60 * 1000 || queryTextCache == null) {
                 queryTextCache = getQueryGroupList(0, 10000, null).getList();
                 cacheTime = (new Date()).getTime();
             }
@@ -58,9 +62,37 @@ public class QueryLogServiceImpl implements QueryLogService {
 
         List<EsQueryLogVo> rtnList = queryTextCache.stream()
                 .filter(log -> log.getQueryText().contains(queryText)).toList();
-        return rtnList;*/
-        List<EsQueryLogVo> rtnList = getQueryGroupList(0, 10000, null).getList();
-        return rtnList.stream().filter(log -> log.getQueryText().contains(queryText)).toList();
+        return rtnList;
+    }
+
+    @Override
+    public List<EsQueryLogVo> getCurrentUserListByKeyString(String queryText, String userId) {
+        List<EsQueryLogVo> resList = new ArrayList<>();
+        List<EsQueryLogVo> firstList = esQueryLogCQuery.getCurrentUserQueryGroupList(queryText, userId);
+        resList.addAll(firstList);
+
+        if(firstList.size() < 30){
+            //不足30条,则从搜索次数最多的前10000条数据中补充
+            List<EsQueryLogVo> lastList = getQueryList(queryText).stream().filter(log -> log.getQueryText().contains(queryText)).toList();
+            int  poorNum = 30 - firstList.size();
+            int  poorNumSize = lastList.size() >= poorNum ? poorNum : lastList.size();
+            if(poorNumSize>0){
+                //合并
+                resList.addAll(lastList.subList(0, poorNumSize));
+                //去重
+                resList = resList.stream()
+                        .filter(distinctByKey(d->d.getQueryText()))
+                        .sorted(Comparator.comparing(EsQueryLogVo::getIsSelf))
+                        .collect(Collectors.toList());
+            }
+        }
+
+        return resList;
+    }
+
+    static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
+        Map<Object, Boolean> seen = new ConcurrentHashMap<>();
+        return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
     }
 
     @Override

+ 1 - 0
src/main/java/com/bowintek/smartsearch/services/service/QueryLogService.java

@@ -12,4 +12,5 @@ public interface QueryLogService {
     List<EsQueryLogVo> getQueryList(String queryText);
     int save(String queryText, String userId);
     List<EsRelationLog> getRelationLogList(String well_common_name);
+    List<EsQueryLogVo> getCurrentUserListByKeyString(String queryText, String userId);
 }

+ 1 - 0
src/main/java/com/bowintek/smartsearch/vo/EsQueryLogVo.java

@@ -6,4 +6,5 @@ import lombok.Data;
 public class EsQueryLogVo {
     private String queryText;
     private int maxCount;
+    private int isSelf;
 }

+ 15 - 1
src/main/resources/mapping/cquery/EsQueryLogCQuery.xml

@@ -2,7 +2,7 @@
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
 <mapper namespace="com.bowintek.smartsearch.mapper.cquery.EsQueryLogCQuery">
     <select id="getQueryGroupList" resultType="com.bowintek.smartsearch.vo.EsQueryLogVo">
-        SELECT queryText, COUNT(*) maxCount
+        SELECT queryText, COUNT(*) maxCount, 1 as isSelf
         FROM es_query_log eql
         where 1=1
         <if test="queryText!='' and queryText!=null">
@@ -11,4 +11,18 @@
         GROUP BY queryText
         ORDER BY COUNT(*) desc
     </select>
+    <select id="getCurrentUserQueryGroupList" resultType="com.bowintek.smartsearch.vo.EsQueryLogVo">
+        SELECT queryText, 0 as isSelf
+        FROM es_query_log eql
+        where 1=1
+        <if test="queryText!='' and queryText!=null">
+            and eql.queryText like Concat('%',#{queryText},'%')
+        </if>
+        <if test="userId!='' and userId!=null">
+            and eql.createdBy = #{userId}
+        </if>
+        GROUP BY queryText
+        ORDER by max(createTime) desc
+        LIMIT 10
+    </select>
 </mapper>

+ 19 - 8
vue/src/components/basic/querylog/history-complete.vue

@@ -16,7 +16,8 @@ export default defineComponent({
     return {
       topOptions:[] as any,
       options:[] as any,
-      loading: false
+      loading: false,
+      timeout: {} as any
     }
   },
   setup(props, context) {
@@ -28,8 +29,13 @@ export default defineComponent({
   },
   mounted() {
     this.getHotspotList();
+    this.getInitQuery();
   },
   methods: {
+    getInitQuery: async function () {
+      //初始化搜索记录
+      await get('queryLog/getListByKeyString', {keyString:"----------"});
+    },
     getHotspotList: async function () {
       const data = await get('queryLog/getHotspotList', {maxRows:10});
       this.topOptions = data.map((item) => {
@@ -41,17 +47,22 @@ export default defineComponent({
     },
     onSearch:async function (searchText){
       console.log("onSearch",searchText);
-      if(searchText.length==0){
+      if(searchText==undefined || searchText.length==0){
         this.options = this.topOptions;
         return;
       }
 
-      const data = await get('queryLog/getListByKeyString', {keyString:searchText});
-      this.options = data.map((item) => {
-        return {
-          value : item.queryText
-        }
-      });
+      if (this.timeout) clearTimeout(this.timeout); // 清除之前的定时器
+
+      this.timeout = setTimeout(() => {
+        get('queryLog/getCurrentUserListByKeyString', {keyString:searchText}).then(x=>{
+          this.options = x.map((item) => {
+            return {
+              value : item.queryText
+            }
+          });
+        });
+      }, 500); // 延迟调用
     },
     saveHistory:function (queryText){
       save('queryLog/save', {queryText:queryText}).then(result => {