浏览代码

update search module

reghao 10 月之前
父节点
当前提交
22c1ab7cd6

+ 3 - 3
search/search-api/src/main/java/cn/reghao/tnb/search/api/dto/VideoSummary.java

@@ -21,12 +21,12 @@ public class VideoSummary implements Serializable {
     private String videoId;
     private String title;
     private String description;
-    private Boolean vip;
+    private Integer scope;
 
-    public VideoSummary(String videoId, String title, Boolean vip) {
+    public VideoSummary(String videoId, String title, int scope) {
         this.videoId = videoId;
         this.title = title;
         this.description = title;
-        this.vip = vip;
+        this.scope = scope;
     }
 }

+ 22 - 0
search/search-service/src/main/java/cn/reghao/tnb/search/app/config/SiteProperties.java

@@ -0,0 +1,22 @@
+package cn.reghao.tnb.search.app.config;
+
+import lombok.Getter;
+import lombok.Setter;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.context.annotation.Configuration;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author reghao
+ * @date 2025-03-20 11:13:33
+ */
+@Configuration
+@ConfigurationProperties(prefix = "site")
+@Setter
+@Getter
+public class SiteProperties {
+    private String indexDir;
+    private String storeDir;
+}

+ 38 - 0
search/search-service/src/main/java/cn/reghao/tnb/search/app/config/SpringLifecycle.java

@@ -0,0 +1,38 @@
+package cn.reghao.tnb.search.app.config;
+
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.DisposableBean;
+import org.springframework.boot.ApplicationArguments;
+import org.springframework.boot.ApplicationRunner;
+import org.springframework.stereotype.Component;
+
+import java.io.File;
+
+/**
+ * @author reghao
+ * @date 2022-03-28 11:54:49
+ */
+@Slf4j
+@Component
+public class SpringLifecycle implements ApplicationRunner, DisposableBean {
+    private final SiteProperties siteProperties;
+
+    public SpringLifecycle(SiteProperties siteProperties) {
+        this.siteProperties = siteProperties;
+    }
+
+    @Override
+    public void run(ApplicationArguments args) {
+        String indexDir = siteProperties.getIndexDir();
+        File dir = new File(indexDir);
+        if (!dir.exists()) {
+            dir.mkdirs();
+        }
+        log.info("SearchService 启动...");
+    }
+
+    @Override
+    public void destroy() {
+        log.info("SearchService 停止...");
+    }
+}

+ 1 - 1
search/search-service/src/main/java/cn/reghao/tnb/search/app/es/MappingService.java

@@ -75,7 +75,7 @@ public class MappingService {
         propertyMap.put("video", keywordProp);
         propertyMap.put("title", textPropIk);
         propertyMap.put("description", textPropIk);
-        propertyMap.put("vip", booleanProp);
+        propertyMap.put("scope", intProp);
         return propertyMap;
     }
 }

+ 1 - 1
search/search-service/src/main/java/cn/reghao/tnb/search/app/lucene/LuceneDocument.java

@@ -54,7 +54,7 @@ public class LuceneDocument {
         doc.add(new StringField("videoId", videoText.getVideoId(), org.apache.lucene.document.Field.Store.YES));
         doc.add(new TextField("title", videoText.getTitle(), org.apache.lucene.document.Field.Store.YES));
         doc.add(new TextField("description", videoText.getDescription(), org.apache.lucene.document.Field.Store.NO));
-        doc.add(new StringField("vip", ""+videoText.getVip(), org.apache.lucene.document.Field.Store.YES));
+        doc.add(new StringField("scope", ""+videoText.getScope(), org.apache.lucene.document.Field.Store.YES));
         return doc;
     }
 }

+ 2 - 2
search/search-service/src/main/java/cn/reghao/tnb/search/app/lucene/LuceneQuery.java

@@ -87,8 +87,8 @@ public class LuceneQuery {
                 String videoId = document.get("videoId");
                 String title = document.get("title");
                 String htmlTitle = highlighter.getBestFragment(luceneAnalyzer, field, title);
-                boolean vip = Boolean.parseBoolean(document.get("vip"));
-                list.add(new VideoText(videoId, htmlTitle, vip));
+                int scope = Integer.parseInt(document.get("scope"));
+                list.add(new VideoText(videoId, htmlTitle, scope));
             }
 
             PageRequest pageRequest = PageRequest.of(pn-1, ps);

+ 4 - 4
search/search-service/src/main/java/cn/reghao/tnb/search/app/model/po/VideoText.java

@@ -17,19 +17,19 @@ public class VideoText {
     private String videoId;
     private String title;
     private String description;
-    private Boolean vip;
+    private Integer scope;
 
     public VideoText(VideoSummary videoSummary) {
         this.videoId = videoSummary.getVideoId();
         this.title = videoSummary.getTitle();
         this.description = videoSummary.getDescription();
-        this.vip = videoSummary.getVip();
+        this.scope = videoSummary.getScope();
     }
 
-    public VideoText(String videoId, String title, Boolean vip) {
+    public VideoText(String videoId, String title, int scope) {
         this.videoId = videoId;
         this.title = title;
         this.description = title;
-        this.vip = vip;
+        this.scope = scope;
     }
 }

+ 2 - 2
search/search-service/src/main/java/cn/reghao/tnb/search/app/rpc/DataSearchServiceImpl.java

@@ -100,8 +100,8 @@ public class DataSearchServiceImpl implements DataSearchService {
             String videoId = videoText.getVideoId();
             String title = videoText.getTitle();
             String description = videoText.getDescription();
-            boolean vip = videoText.getVip();
-            return new VideoSummary(videoId, title, description, vip);
+            int scope = videoText.getScope();
+            return new VideoSummary(videoId, title, description, scope);
         }).collect(Collectors.toList());
         return PageList.pageList(pn, ps, (int) page.getTotalElements(), list);
     }

+ 56 - 0
search/search-service/src/main/java/cn/reghao/tnb/search/app/util/BaseEntity.java

@@ -0,0 +1,56 @@
+package cn.reghao.tnb.search.app.util;
+
+import lombok.Getter;
+import lombok.Setter;
+import org.hibernate.annotations.CreationTimestamp;
+import org.hibernate.annotations.UpdateTimestamp;
+
+import javax.persistence.*;
+import java.io.Serializable;
+import java.time.LocalDateTime;
+
+/**
+ * @author reghao
+ * @date 2019-10-18 14:42:48
+ */
+@MappedSuperclass
+@Getter
+@Setter
+public class BaseEntity implements Serializable {
+    private static final long serialVersionUID = 1L;
+    @Id
+    // 采用 id 字段自增方式
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    // 使用额外的 seq 表
+    //@SequenceGenerator(name = "seqGen", sequenceName = "seq")
+    protected Integer id;
+    // 逻辑删除
+    @Column(nullable = false)
+    private Boolean deleted;
+    @CreationTimestamp
+    @Column(nullable = false, updatable = false)
+    protected LocalDateTime createTime;
+    @UpdateTimestamp
+    @Column(nullable = false)
+    protected LocalDateTime updateTime;
+
+    public BaseEntity() {
+        this.deleted = false;
+        this.createTime = LocalDateTime.now();
+        this.updateTime = LocalDateTime.now();
+    }
+
+    public void setNull() {
+        //this.setId(null);
+        this.setDeleted(null);
+        this.setCreateTime(null);
+        this.setUpdateTime(null);
+    }
+
+    public void setEntity(BaseEntity entity) {
+        this.id = entity.getId();
+        this.deleted = entity.getDeleted();
+        this.createTime = entity.getCreateTime();
+        this.updateTime = LocalDateTime.now();
+    }
+}

+ 6 - 1
search/search-service/src/main/resources/application.yml

@@ -42,9 +42,14 @@ spring:
       hibernate.format_sql: true
       hibernate.naming.physical-strategy: org.hibernate.scripts.model.naming.PhysicalNamingStrategyStandardImpl
       hibernate.cache.use_second_level_cache: false
+      hibernate.search.backend.directory.type: local-filesystem
+      hibernate.search.backend.directory.root: ${site.indexDir}
+      hibernate.search.backend.analysis.configurer: class:cn.reghao.tnb.search.app.config.MyLuceneAnalysisConfigurer
 eureka:
   instance:
     prefer-ip-address: true
   client:
     register-with-eureka: true
-    fetch-registry: true
+    fetch-registry: true
+site:
+  indexDir: /opt/data/searchdata