Преглед изворни кода

视频匹配增加 匹配到的文本返回

wangyunpeng пре 12 часа
родитељ
комит
2cfbd5bdb2

+ 11 - 0
core/src/main/java/com/tzld/videoVector/model/entity/VideoMatch.java

@@ -17,6 +17,9 @@ public class VideoMatch {
     /** 向量点索引(多点模式下区分同一视频的不同向量点) */
     private Integer pointIndex;
 
+    /** 向量化原文 */
+    private String text;
+
     public VideoMatch(Long videoId, double score) {
         this.videoId = videoId;
         this.score = score;
@@ -66,4 +69,12 @@ public class VideoMatch {
     public void setPointIndex(Integer pointIndex) {
         this.pointIndex = pointIndex;
     }
+
+    public String getText() {
+        return text;
+    }
+
+    public void setText(String text) {
+        this.text = text;
+    }
 }

+ 5 - 1
core/src/main/java/com/tzld/videoVector/model/vo/VideoMatchResult.java

@@ -20,6 +20,9 @@ public class VideoMatchResult {
     /** 余弦相似度分值 */
     private Double score;
 
+    /** 向量化原文 */
+    private String text;
+
     /**
      * 视频基础信息(从大数据表同步至Redis)
      * 内嵌 deconstruct 子对象 = { topic, 灵感点, 灵感点-实质, 关键点, 关键点-实质, 目的点, 目的点-实质 }
@@ -30,9 +33,10 @@ public class VideoMatchResult {
     public VideoMatchResult() {
     }
 
-    public VideoMatchResult(String configCode, Long videoId, Double score) {
+    public VideoMatchResult(String configCode, Long videoId, Double score, String text) {
         this.configCode = configCode;
         this.videoId = videoId;
         this.score = score;
+        this.text = text;
     }
 }

+ 13 - 1
core/src/main/java/com/tzld/videoVector/model/vo/VideoVectorSearchResult.java

@@ -14,6 +14,9 @@ public class VideoVectorSearchResult {
     /** 余弦相似度得分 */
     private Double score;
 
+    /** 向量化原文 */
+    private String text;
+
     public Long getVideoId() {
         return videoId;
     }
@@ -38,10 +41,19 @@ public class VideoVectorSearchResult {
         this.score = score;
     }
 
+    public String getText() {
+        return text;
+    }
+
+    public void setText(String text) {
+        this.text = text;
+    }
+
     @Override
     public String toString() {
         return "VideoVectorSearchResult{videoId=" + videoId +
                 ", pointIndex=" + pointIndex +
-                ", score=" + score + "}";
+                ", score=" + score +
+                ", text='" + text + "'}";
     }
 }

+ 3 - 0
core/src/main/java/com/tzld/videoVector/model/vo/recall/VideoMatchEnrichedVO.java

@@ -63,4 +63,7 @@ public class VideoMatchEnrichedVO {
      * 来源 Redis recall:vid_decode:{vid}
      */
     private Map<String, Object> videoDetail;
+
+    /** 向量化原文 */
+    private String text;
 }

+ 1 - 0
core/src/main/java/com/tzld/videoVector/service/impl/PgVectorStoreServiceImpl.java

@@ -268,6 +268,7 @@ public class PgVectorStoreServiceImpl implements VectorStoreService {
                 .map(vv -> {
                     VideoMatch m = new VideoMatch(vv.getVideoId(), vv.getScore() != null ? vv.getScore() : 0.0);
                     m.setPointIndex(vv.getPointIndex());
+                    m.setText(vv.getText());
                     return m;
                 })
                 .collect(Collectors.toList());

+ 6 - 4
core/src/main/java/com/tzld/videoVector/service/impl/VideoSearchServiceImpl.java

@@ -664,7 +664,7 @@ public class VideoSearchServiceImpl implements VideoSearchService {
 
                 // 转化为强类型返回格式
                 for (VideoMatch match : filteredMatches) {
-                    result.add(new VideoMatchResult(cfgCode, match.getVideoId(), match.getScore()));
+                    result.add(new VideoMatchResult(cfgCode, match.getVideoId(), match.getScore(), match.getText()));
                 }
 
                 log.info("配置 {} 搜索完成,返回 {} 条结果", cfgCode, filteredMatches.size());
@@ -698,7 +698,7 @@ public class VideoSearchServiceImpl implements VideoSearchService {
         List<VideoMatch> filteredMatches = filterByAuditStatus(matches, topN);
         List<VideoMatchResult> result = new ArrayList<>(filteredMatches.size());
         for (VideoMatch match : filteredMatches) {
-            result.add(new VideoMatchResult(configCode, match.getVideoId(), match.getScore()));
+            result.add(new VideoMatchResult(configCode, match.getVideoId(), match.getScore(), match.getText()));
         }
         // 填充视频详情
         enrichVideoDetail(result);
@@ -777,7 +777,9 @@ public class VideoSearchServiceImpl implements VideoSearchService {
             Long videoId = match.getVideoId();
             VideoMatch existing = deduped.get(videoId);
             if (existing == null || match.getScore() > existing.getScore()) {
-                deduped.put(videoId, new VideoMatch(videoId, match.getScore(), configCode));
+                VideoMatch best = new VideoMatch(videoId, match.getScore(), configCode);
+                best.setText(match.getText());
+                deduped.put(videoId, best);
             }
         }
 
@@ -791,7 +793,7 @@ public class VideoSearchServiceImpl implements VideoSearchService {
     private List<DeconstructVectorConfig> getEnabledConfigs() {
         try {
             DeconstructVectorConfigExample example = new DeconstructVectorConfigExample();
-            example.createCriteria().andEnabledEqualTo((short) 1).andSourceFieldEqualTo("result_json");
+            example.createCriteria().andEnabledEqualTo((short) 1);
             example.setOrderByClause("priority ASC");
             List<DeconstructVectorConfig> configs = deconstructVectorConfigMapper.selectByExample(example);
             return configs != null ? configs : Collections.emptyList();

+ 3 - 0
core/src/main/java/com/tzld/videoVector/service/recall/impl/VectorRecallTestServiceImpl.java

@@ -236,6 +236,7 @@ public class VectorRecallTestServiceImpl implements VectorRecallTestService {
             mi.configCode = r.getConfigCode();
             mi.score = r.getScore();
             mi.videoDetail = r.getVideoDetail();
+            mi.text = r.getText();
             matches.add(mi);
         }
 
@@ -327,6 +328,7 @@ public class VectorRecallTestServiceImpl implements VectorRecallTestService {
 
             // 填充 videoDetail (已由 VideoSearchServiceImpl 嵌入 deconstruct 子对象)
             vo.setVideoDetail(m.videoDetail);
+            vo.setText(m.text);
 
             items.add(vo);
         }
@@ -411,5 +413,6 @@ public class VectorRecallTestServiceImpl implements VectorRecallTestService {
         String configCode;
         Double score;
         Map<String, Object> videoDetail;
+        String text;
     }
 }

+ 4 - 3
core/src/main/resources/mapper/pgVector/ext/VideoVectorMapperExt.xml

@@ -15,11 +15,12 @@
     <result column="updated_at" jdbcType="TIMESTAMP" property="updatedAt" />
   </resultMap>
 
-  <!-- 余弦相似度搜索 Top-N(返回 videoId + pointIndex + score) -->
+  <!-- 余弦相似度搜索 Top-N(返回 videoId + pointIndex + score + text) -->
   <resultMap id="SearchResultMap" type="com.tzld.videoVector.model.vo.VideoVectorSearchResult">
     <result column="video_id" jdbcType="BIGINT" property="videoId" />
     <result column="point_index" jdbcType="INTEGER" property="pointIndex" />
     <result column="score" jdbcType="DOUBLE" property="score" />
+    <result column="text" jdbcType="VARCHAR" property="text" />
   </resultMap>
 
   <!-- ==================== 自定义向量操作 SQL ==================== -->
@@ -88,11 +89,11 @@
 
   <select id="searchTopNByCosine" resultMap="SearchResultMap">
     WITH filtered AS MATERIALIZED (
-      SELECT video_id, point_index, embedding
+      SELECT video_id, point_index, embedding, "text"
       FROM video_vectors
       WHERE config_code = #{configCode}
     )
-    SELECT video_id, point_index, 1 - (embedding &lt;=&gt; #{queryVector}::vector) AS score
+    SELECT video_id, point_index, 1 - (embedding &lt;=&gt; #{queryVector}::vector) AS score, "text"
     FROM filtered
     ORDER BY embedding &lt;=&gt; #{queryVector}::vector
     LIMIT #{topN}