Jelajahi Sumber

数据维度时间支持传参

wangyunpeng 5 hari lalu
induk
melakukan
6b3d72a3d9

+ 6 - 0
core/src/main/java/com/tzld/videoVector/common/constant/VectorConstants.java

@@ -1,5 +1,8 @@
 package com.tzld.videoVector.common.constant;
 
+import java.util.Arrays;
+import java.util.List;
+
 /**
  * 向量化相关常量
  */
@@ -80,4 +83,7 @@ public interface VectorConstants {
 
     /** recallK 上限,防止 displayK 调大时候选爆炸 */
     int RECALL_K_CAP = 200;
+
+    /** 支持的指标日期维度列表(天) */
+    List<Integer> SUPPORTED_DATE_RANGES = Arrays.asList(3, 7, 15, 30, 180, 365);
 }

+ 2 - 5
core/src/main/java/com/tzld/videoVector/job/VideoDetailSyncJob.java

@@ -43,9 +43,6 @@ public class VideoDetailSyncJob {
     /** ODPS SQL IN 子句批次大小 */
     private static final int ODPS_BATCH_SIZE = 1000;
 
-    /** 支持的日期维度列表(同步时查询所有维度并存储,使用时通过Apollo配置选择) */
-    private static final List<Integer> SUPPORTED_DATE_RANGES = Arrays.asList(3, 7, 15, 365);
-
     // ========================== 维度字段(取最新日期) ==========================
 
     /** 维度字段列表(参照 query_video_dimension_metrics.py 的 GROUP_FIELDS,排除视频id) */
@@ -109,7 +106,7 @@ public class VideoDetailSyncJob {
             String dtMax = LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE);
             // 维度查询使用最大范围(365天)
             String dtMin = LocalDate.now().minusDays(365).format(DateTimeFormatter.BASIC_ISO_DATE);
-            log.info("ODPS 查询 dt 范围: {} ~ {}, 指标日期维度: {}", dtMin, dtMax, SUPPORTED_DATE_RANGES);
+            log.info("ODPS 查询 dt 范围: {} ~ {}, 指标日期维度: {}", dtMin, dtMax, VectorConstants.SUPPORTED_DATE_RANGES);
 
             // 2. 分批查询大数据表并流式写入 Redis(拆分为维度查询 + 多维度指标查询)
             AtomicInteger totalSuccess = new AtomicInteger(0);
@@ -147,7 +144,7 @@ public class VideoDetailSyncJob {
             log.info("批次 {}-{} 维度查询完成(365d范围),获取 {} 条记录", batchStart, batchEnd, dimensionMap.size());
 
             // 2. 每个日期维度单独查询指标,合并维度字段后写入对应 Redis key
-            for (int days : SUPPORTED_DATE_RANGES) {
+            for (int days : VectorConstants.SUPPORTED_DATE_RANGES) {
                 String dtMinForMetrics = LocalDate.now().minusDays(days).format(DateTimeFormatter.BASIC_ISO_DATE);
                 String metricsSql = buildMetricsSql(idsStr, dtMinForMetrics, dtMax);
 

+ 3 - 0
core/src/main/java/com/tzld/videoVector/model/param/MatchTopNVideoParam.java

@@ -21,4 +21,7 @@ public class MatchTopNVideoParam {
 
     /** 返回 Top-N 结果数量,默认 10 */
     private Integer topN = 10;
+
+    /** 指标数据日期维度(天),不传则使用 video.detail.metrics.days 配置 */
+    private Integer days;
 }

+ 3 - 0
core/src/main/java/com/tzld/videoVector/model/param/RecallVideoScoreParam.java

@@ -40,4 +40,7 @@ public class RecallVideoScoreParam {
 
     /** 相似度下界(粗筛阈值),默认 0.7 */
     private Double simMin;
+
+    /** 指标数据日期维度(天),不传则使用 video.detail.metrics.days 配置 */
+    private Integer days;
 }

+ 6 - 0
core/src/main/java/com/tzld/videoVector/service/VideoSearchService.java

@@ -80,6 +80,12 @@ public interface VideoSearchService {
      */
     RecallVideoScoreVO recallWithScore(RecallVideoScoreParam param);
 
+    /**
+     * 获取支持的指标日期维度列表
+     * @return 支持的日期维度(天),如 [3, 7, 15, 365]
+     */
+    List<Integer> getSupportedDateRanges();
+
     /**
      * 查询渠道需求匹配结果
      * 按日期、渠道、人群、维度查询匹配到的视频ID及分数

+ 13 - 5
core/src/main/java/com/tzld/videoVector/service/impl/VideoSearchServiceImpl.java

@@ -735,7 +735,7 @@ public class VideoSearchServiceImpl implements VideoSearchService {
 
         if (withDetail) {
             // 从 Redis 获取视频基础信息并填充到结果中
-            enrichVideoDetail(result);
+            enrichVideoDetail(result, param.getDays());
             // 从 Redis 获取视频解构(选题 + 高价值实质点)并填充
             enrichDeconstruct(result);
         }
@@ -760,7 +760,7 @@ public class VideoSearchServiceImpl implements VideoSearchService {
             result.add(new VideoMatchResult(configCode, match.getVideoId(), match.getScore(), match.getText()));
         }
         // 填充视频详情
-        enrichVideoDetail(result);
+        enrichVideoDetail(result, param.getDays());
         // 填充视频解构
         enrichDeconstruct(result);
         return result;
@@ -1089,15 +1089,17 @@ public class VideoSearchServiceImpl implements VideoSearchService {
      * 根据 Apollo 配置 video.detail.metrics.days 读取对应日期维度的 Redis key
      * key格式: video:detail:{days}d:{videoId}
      */
-    private void enrichVideoDetail(List<VideoMatchResult> results) {
+    private void enrichVideoDetail(List<VideoMatchResult> results, Integer days) {
         if (results == null || results.isEmpty()) {
             return;
         }
 
+        int effectiveDays = days != null ? days : metricsDays;
+
         try {
-            // 根据 Apollo 配置构建对应日期维度的 Redis keys
+            // 根据传入 days 或 Apollo 配置构建对应日期维度的 Redis keys
             List<String> keys = results.stream()
-                    .map(r -> VectorConstants.VIDEO_DETAIL_DAYS_KEY_PREFIX + metricsDays + "d:" + r.getVideoId())
+                    .map(r -> VectorConstants.VIDEO_DETAIL_DAYS_KEY_PREFIX + effectiveDays + "d:" + r.getVideoId())
                     .collect(Collectors.toList());
 
             List<String> values = redisUtils.mGet(keys);
@@ -1403,6 +1405,7 @@ public class VideoSearchServiceImpl implements VideoSearchService {
         matchParam.setQueryText(param.getQueryText());
         matchParam.setQueryVector(param.getQueryVector());
         matchParam.setTopN(param.getTopN() != null && param.getTopN() > 0 ? param.getTopN() : 10);
+        matchParam.setDays(param.getDays());
 
         List<VideoMatchResult> rawMatches = matchTopNVideo(matchParam, true);
         if (rawMatches == null || rawMatches.isEmpty()) {
@@ -1515,6 +1518,11 @@ public class VideoSearchServiceImpl implements VideoSearchService {
         return vo;
     }
 
+    @Override
+    public List<Integer> getSupportedDateRanges() {
+        return VectorConstants.SUPPORTED_DATE_RANGES;
+    }
+
     @Override
     public PageResult<ChannelDemandMatchVO> queryDemandMatchResult(ChannelDemandMatchQueryParam param) {
         int pageNum = (param.getPageNum() != null && param.getPageNum() > 0) ? param.getPageNum() : 1;

+ 9 - 0
server/src/main/java/com/tzld/videoVector/controller/VideoSearchController.java

@@ -65,6 +65,15 @@ public class VideoSearchController {
         return CommonResponse.success(videoSearchService.recallWithScore(param));
     }
 
+    /**
+     * 获取支持的指标日期维度列表
+     * GET /videoSearch/supportedDateRanges
+     */
+    @GetMapping("/supportedDateRanges")
+    public CommonResponse<List<Integer>> getSupportedDateRanges() {
+        return CommonResponse.success(videoSearchService.getSupportedDateRanges());
+    }
+
     /**
      * 查询渠道需求匹配结果
      * 按日期、渠道、人群、维度查询匹配到的视频ID及分数