Commit be0b3c5a50bb4255d2b9772aeb8304e4439e1d23
1 parent
6dbcb70e
/home/tw/recommendation/offline_tasks 的截断从「3 年 / 行业 2 年」收成 30 天。i2i、兴趣聚合、灌 Redis、内容相似都会走这个窗口。
Showing
5 changed files
with
52 additions
and
40 deletions
Show diff stats
offline_tasks/config/offline_config.py
| ... | ... | @@ -83,19 +83,19 @@ I2I_CONFIG = { |
| 83 | 83 | } |
| 84 | 84 | } |
| 85 | 85 | |
| 86 | -# 商品新鲜度配置(离线推荐索引源头打压老品) | |
| 87 | -# 默认 3 年截断;行业/地域/平台维度更严,2 年截断。 | |
| 86 | +# 商品新鲜度配置(离线推荐索引源头只进近 1 个月上架商品) | |
| 87 | +# 在线首页最终过滤会放宽到 2 个月,消化离线周更滞后。 | |
| 88 | 88 | ITEM_FRESHNESS_CONFIG = { |
| 89 | - 'max_item_age_years': 3, # 超过该年数的商品不进入索引 | |
| 90 | - 'strict_item_age_years': 2, # 行业/地域/平台维度截断年数 | |
| 89 | + 'max_item_age_days': 30, # 超过该天数的商品不进入索引 | |
| 90 | + 'strict_item_age_days': 30, # 行业/地域/平台同样只保留近 1 个月 | |
| 91 | 91 | 'keep_unknown_age': True, # 无创建时间的商品保留,但降权 |
| 92 | 92 | 'unknown_age_weight': 0.4, # 未知上架时间的新鲜度权重 |
| 93 | 93 | 'min_freshness_weight': 0.05, # 新鲜度权重下限,避免完全归零 |
| 94 | - # 半衰期(天):越小越打压老品。行业(类目)、地域(国家)、平台更激进 | |
| 95 | - 'default_half_life_days': 365, | |
| 96 | - 'category_half_life_days': 180, | |
| 97 | - 'region_half_life_days': 180, | |
| 98 | - 'platform_half_life_days': 180, | |
| 94 | + # 半衰期(天):越小越打压老品。窗口已收紧到 1 个月,半衰期同步缩短 | |
| 95 | + 'default_half_life_days': 15, | |
| 96 | + 'category_half_life_days': 10, | |
| 97 | + 'region_half_life_days': 10, | |
| 98 | + 'platform_half_life_days': 10, | |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | # 兴趣点聚合配置 | ... | ... |
offline_tasks/scripts/i2i_content_similar.py
| ... | ... | @@ -12,6 +12,7 @@ from datetime import datetime, timedelta |
| 12 | 12 | from elasticsearch import Elasticsearch |
| 13 | 13 | from db_service import create_db_connection |
| 14 | 14 | from config.offline_config import DB_CONFIG, OUTPUT_DIR, ITEM_FRESHNESS_CONFIG |
| 15 | +from scripts.item_freshness import get_max_item_age_days | |
| 15 | 16 | from scripts.debug_utils import ( |
| 16 | 17 | setup_debug_logger, log_processing_step, |
| 17 | 18 | save_readable_index, load_item_name_mappings, sanitize_index_field, |
| ... | ... | @@ -116,7 +117,7 @@ def find_similar_by_vector(es, vector, field_name, k=KNN_K, num_candidates=KNN_C |
| 116 | 117 | Returns: |
| 117 | 118 | List of (item_id, boosted_score, name_zh) tuples |
| 118 | 119 | """ |
| 119 | - max_age_days = int(ITEM_FRESHNESS_CONFIG['max_item_age_years'] * 365) | |
| 120 | + max_age_days = get_max_item_age_days() | |
| 120 | 121 | cutoff_date = (datetime.now() - timedelta(days=max_age_days)).strftime('%Y-%m-%d') |
| 121 | 122 | knn_body = { |
| 122 | 123 | "field": field_name, | ... | ... |
offline_tasks/scripts/interest_aggregation.py
| ... | ... | @@ -295,12 +295,12 @@ def main(): |
| 295 | 295 | help=f'Recent days for hot items (default: {DEFAULT_RECENT_DAYS})') |
| 296 | 296 | parser.add_argument('--new_days', type=int, default=DEFAULT_RECENT_DAYS, |
| 297 | 297 | help=f'Days for new items (default: {DEFAULT_RECENT_DAYS})') |
| 298 | - parser.add_argument('--max_item_age_years', type=float, | |
| 299 | - default=ITEM_FRESHNESS_CONFIG['max_item_age_years'], | |
| 300 | - help='Drop items older than this many years before indexing (default: 3)') | |
| 301 | - parser.add_argument('--strict_item_age_years', type=float, | |
| 302 | - default=ITEM_FRESHNESS_CONFIG.get('strict_item_age_years', 2), | |
| 303 | - help='Category/region/platform dimension cutoff years (default: 2)') | |
| 298 | + parser.add_argument('--max_item_age_days', type=int, | |
| 299 | + default=ITEM_FRESHNESS_CONFIG['max_item_age_days'], | |
| 300 | + help='Drop items older than this many days before indexing (default: 30)') | |
| 301 | + parser.add_argument('--strict_item_age_days', type=int, | |
| 302 | + default=ITEM_FRESHNESS_CONFIG.get('strict_item_age_days', 30), | |
| 303 | + help='Category/region/platform dimension cutoff days (default: 30)') | |
| 304 | 304 | parser.add_argument('--decay_factor', type=float, default=INTEREST_AGGREGATION_CONFIG['time_decay_factor'], |
| 305 | 305 | help='Time decay factor') |
| 306 | 306 | parser.add_argument('--output_prefix', type=str, default='interest_aggregation', |
| ... | ... | @@ -319,8 +319,8 @@ def main(): |
| 319 | 319 | 'lookback_days': args.lookback_days, |
| 320 | 320 | 'recent_days': args.recent_days, |
| 321 | 321 | 'new_days': args.new_days, |
| 322 | - 'max_item_age_years': args.max_item_age_years, | |
| 323 | - 'strict_item_age_years': args.strict_item_age_years, | |
| 322 | + 'max_item_age_days': args.max_item_age_days, | |
| 323 | + 'strict_item_age_days': args.strict_item_age_days, | |
| 324 | 324 | 'decay_factor': args.decay_factor, |
| 325 | 325 | 'debug': args.debug |
| 326 | 326 | } |
| ... | ... | @@ -340,11 +340,11 @@ def main(): |
| 340 | 340 | start_date, end_date = get_time_range(args.lookback_days) |
| 341 | 341 | recent_start_date, _ = get_time_range(args.recent_days) |
| 342 | 342 | new_start_date, _ = get_time_range(args.new_days) |
| 343 | - item_cutoff_date = get_item_age_cutoff_date(args.max_item_age_years) | |
| 344 | - strict_cutoff_date = get_item_age_cutoff_date(args.strict_item_age_years) | |
| 343 | + item_cutoff_date = get_item_age_cutoff_date(args.max_item_age_days) | |
| 344 | + strict_cutoff_date = get_item_age_cutoff_date(args.strict_item_age_days) | |
| 345 | 345 | |
| 346 | 346 | logger.info(f"获取数据范围:{start_date} 到 {end_date}") |
| 347 | - logger.info(f"商品新鲜度截断:默认 >= {item_cutoff_date} ({args.max_item_age_years} 年);行业/地域/平台 >= {strict_cutoff_date} ({args.strict_item_age_years} 年)") | |
| 347 | + logger.info(f"商品新鲜度截断:默认 >= {item_cutoff_date} ({args.max_item_age_days} 天);行业/地域/平台 >= {strict_cutoff_date} ({args.strict_item_age_days} 天)") | |
| 348 | 348 | logger.debug(f"热门商品起始日期:{recent_start_date}") |
| 349 | 349 | logger.debug(f"新品起始日期:{new_start_date}") |
| 350 | 350 | |
| ... | ... | @@ -422,7 +422,7 @@ def main(): |
| 422 | 422 | fresh_mask = df['item_create_time'] >= item_cutoff_ts |
| 423 | 423 | df = df[fresh_mask].copy() |
| 424 | 424 | logger.info( |
| 425 | - f"按商品创建时间过滤 {args.max_item_age_years} 年前商品: {before_fresh_filter} -> {len(df)} " | |
| 425 | + f"按商品创建时间过滤 {args.max_item_age_days} 天前商品: {before_fresh_filter} -> {len(df)} " | |
| 426 | 426 | f"(dropped={before_fresh_filter - len(df)}, cutoff={item_cutoff_date})" |
| 427 | 427 | ) |
| 428 | 428 | strict_cutoff_ts = pd.Timestamp(strict_cutoff_date) |
| ... | ... | @@ -431,7 +431,7 @@ def main(): |
| 431 | 431 | else: |
| 432 | 432 | df['is_strict_fresh'] = df['item_create_time'] >= strict_cutoff_ts |
| 433 | 433 | logger.info( |
| 434 | - f"行业/地域/平台 {args.strict_item_age_years} 年可用行: {int(df['is_strict_fresh'].sum())}/{len(df)} " | |
| 434 | + f"行业/地域/平台 {args.strict_item_age_days} 天可用行: {int(df['is_strict_fresh'].sum())}/{len(df)} " | |
| 435 | 435 | f"(cutoff={strict_cutoff_date})" |
| 436 | 436 | ) |
| 437 | 437 | df = add_item_freshness_weights(df, logger=logger) | ... | ... |
offline_tasks/scripts/item_freshness.py
| ... | ... | @@ -11,14 +11,17 @@ from config.offline_config import ITEM_FRESHNESS_CONFIG, OUTPUT_DIR |
| 11 | 11 | ITEM_CREATE_TIMES_FILENAME = 'item_create_times.json' |
| 12 | 12 | |
| 13 | 13 | |
| 14 | -def get_max_item_age_days(max_item_age_years=None): | |
| 15 | - years = ITEM_FRESHNESS_CONFIG['max_item_age_years'] if max_item_age_years is None else max_item_age_years | |
| 16 | - return int(years * 365) | |
| 14 | +def get_max_item_age_days(max_item_age_days=None): | |
| 15 | + if max_item_age_days is None: | |
| 16 | + if 'max_item_age_days' in ITEM_FRESHNESS_CONFIG: | |
| 17 | + return int(ITEM_FRESHNESS_CONFIG['max_item_age_days']) | |
| 18 | + return int(float(ITEM_FRESHNESS_CONFIG.get('max_item_age_years', 30 / 365.0)) * 365) | |
| 19 | + return int(max_item_age_days) | |
| 17 | 20 | |
| 18 | 21 | |
| 19 | -def get_item_age_cutoff_date(max_item_age_years=None, reference_time=None): | |
| 22 | +def get_item_age_cutoff_date(max_item_age_days=None, reference_time=None): | |
| 20 | 23 | reference_time = reference_time or datetime.now() |
| 21 | - return (reference_time - timedelta(days=get_max_item_age_days(max_item_age_years))).strftime('%Y-%m-%d') | |
| 24 | + return (reference_time - timedelta(days=get_max_item_age_days(max_item_age_days))).strftime('%Y-%m-%d') | |
| 22 | 25 | |
| 23 | 26 | |
| 24 | 27 | def item_create_times_path(output_dir=None): |
| ... | ... | @@ -83,7 +86,7 @@ def freshness_weight(age_days, half_life_days, min_weight=None, unknown=False): |
| 83 | 86 | |
| 84 | 87 | |
| 85 | 88 | def is_strong_freshness_dimension(dim_key): |
| 86 | - """行业(类目)、地域(国家)、平台维度使用 2 年截断和更激进的新鲜度权重。""" | |
| 89 | + """行业(类目)、地域(国家)、平台维度使用更短半衰期。""" | |
| 87 | 90 | key = str(dim_key) |
| 88 | 91 | if 'category' in key: |
| 89 | 92 | return True |
| ... | ... | @@ -96,8 +99,16 @@ def is_strong_freshness_dimension(dim_key): |
| 96 | 99 | return False |
| 97 | 100 | |
| 98 | 101 | |
| 102 | +def get_strict_item_age_days(): | |
| 103 | + if 'strict_item_age_days' in ITEM_FRESHNESS_CONFIG: | |
| 104 | + return int(ITEM_FRESHNESS_CONFIG['strict_item_age_days']) | |
| 105 | + if 'strict_item_age_years' in ITEM_FRESHNESS_CONFIG: | |
| 106 | + return int(float(ITEM_FRESHNESS_CONFIG['strict_item_age_years']) * 365) | |
| 107 | + return get_max_item_age_days() | |
| 108 | + | |
| 109 | + | |
| 99 | 110 | def get_strict_item_age_years(): |
| 100 | - return ITEM_FRESHNESS_CONFIG.get('strict_item_age_years', 2) | |
| 111 | + return get_strict_item_age_days() / 365.0 | |
| 101 | 112 | |
| 102 | 113 | |
| 103 | 114 | def cutoff_date_for_dimension(dim_key, default_cutoff, strict_cutoff): | ... | ... |
offline_tasks/scripts/load_index_to_redis.py
| ... | ... | @@ -303,12 +303,12 @@ def main(): |
| 303 | 303 | help='Load interest indices') |
| 304 | 304 | parser.add_argument('--flush-db', action='store_true', |
| 305 | 305 | help='Flush database before loading (危险操作!)') |
| 306 | - parser.add_argument('--max-item-age-years', type=float, | |
| 307 | - default=ITEM_FRESHNESS_CONFIG['max_item_age_years'], | |
| 308 | - help='Skip items older than this many years when writing Redis (default 3)') | |
| 309 | - parser.add_argument('--strict-item-age-years', type=float, | |
| 310 | - default=ITEM_FRESHNESS_CONFIG.get('strict_item_age_years', 2), | |
| 311 | - help='Category/region/platform dimension cutoff years (default 2)') | |
| 306 | + parser.add_argument('--max-item-age-days', type=int, | |
| 307 | + default=ITEM_FRESHNESS_CONFIG['max_item_age_days'], | |
| 308 | + help='Skip items older than this many days when writing Redis (default 30)') | |
| 309 | + parser.add_argument('--strict-item-age-days', type=int, | |
| 310 | + default=ITEM_FRESHNESS_CONFIG.get('strict_item_age_days', 30), | |
| 311 | + help='Category/region/platform dimension cutoff days (default 30)') | |
| 312 | 312 | parser.add_argument('--skip-freshness-filter', action='store_true', |
| 313 | 313 | help='Disable the item age filter while loading Redis') |
| 314 | 314 | |
| ... | ... | @@ -343,12 +343,12 @@ def main(): |
| 343 | 343 | strict_cutoff_date = None |
| 344 | 344 | if not args.skip_freshness_filter: |
| 345 | 345 | create_times = load_item_create_times(logger=logger) |
| 346 | - cutoff_date = get_item_age_cutoff_date(args.max_item_age_years) | |
| 347 | - strict_cutoff_date = get_item_age_cutoff_date(args.strict_item_age_years) | |
| 346 | + cutoff_date = get_item_age_cutoff_date(args.max_item_age_days) | |
| 347 | + strict_cutoff_date = get_item_age_cutoff_date(args.strict_item_age_days) | |
| 348 | 348 | if create_times: |
| 349 | 349 | logger.info( |
| 350 | - f"Redis freshness cutoff: default >= {cutoff_date} ({args.max_item_age_years} years); " | |
| 351 | - f"category/region/platform >= {strict_cutoff_date} ({args.strict_item_age_years} years)" | |
| 350 | + f"Redis freshness cutoff: default >= {cutoff_date} ({args.max_item_age_days} days); " | |
| 351 | + f"category/region/platform >= {strict_cutoff_date} ({args.strict_item_age_days} days)" | |
| 352 | 352 | ) |
| 353 | 353 | else: |
| 354 | 354 | logger.warning("Item create times unavailable, Redis load will skip freshness filtering") | ... | ... |