新维度说明.md 5.99 KB

兴趣聚合新维度说明

📋 新增维度

从2026年1月12日起,兴趣聚合索引新增以下维度:

1. 平台 + 国家代码

格式:interest:{类型}:platform:{平台};country_code:{国家代码}

示例:

  • interest:hot:platform:essa;country_code:US
  • interest:hot:platform:essaone;country_code:CN
  • interest:cart:platform:essa;country_code:GB
  • interest:new:platform:essaone;country_code:US

适用平台:仅 essa 和 essaone

  • ✅ essa:有国家维度
  • ✅ essaone:有国家维度
  • ❌ essabuy:无国家维度

2. 平台 + 国家代码 + 客户类型

格式:interest:{类型}:platform:{平台};country_code:{国家代码};customer_type:{客户类型}

示例:

  • interest:hot:platform:essa;country_code:US;customer_type:目标客户
  • interest:hot:platform:essaone;country_code:CN;customer_type:普通客户
  • interest:cart:platform:essa;country_code:GB;customer_type:VIP客户

适用平台:仅 essa 和 essaone

  • ✅ essa:有国家和客户类型维度
  • ✅ essaone:有国家和客户类型维度
  • ❌ essabuy:无这些维度

🔍 数据来源

新维度的数据通过以下方式获取:

-- 通过sensors_events的login_id关联
sensors_events.login_id -> buy_account.id
buy_account.buyer_id -> buy_buyer.id
buy_buyer.id -> buy_buyer_extend.buyer_id

-- 获取国家代码
buy_buyer_extend_option.code = 'country_code'
-> pub_dict_item.name (例如: US, CN, GB)

-- 获取客户类型
buy_buyer_extend_option.code = 'customer_type'
-> pub_dict_item.name (例如: 目标客户, 普通客户, VIP客户)

📊 索引类型(list_type)

新维度适用于所有现有索引类型:

  1. hot(热门):最近N天的热门商品
  2. cart(加购):加购行为商品
  3. new(新品):新上架商品
  4. global(全局):所有历史数据聚合

💡 使用示例

Redis查询

# 查询essa平台美国的热门商品
redis-cli -h localhost -p 6479 -a "BMfv5aI31kgHWtlx" GET "interest:hot:platform:essa;country_code:US"

# 查询essaone平台中国目标客户的热门商品
redis-cli -h localhost -p 6479 -a "BMfv5aI31kgHWtlx" GET "interest:hot:platform:essaone;country_code:CN;customer_type:目标客户"

# 查询所有包含国家代码的hot索引
redis-cli -h localhost -p 6479 -a "BMfv5aI31kgHWtlx" KEYS "interest:hot:platform:*;country_code:*"

# 查询所有包含国家和客户类型的hot索引
redis-cli -h localhost -p 6479 -a "BMfv5aI31kgHWtlx" KEYS "interest:hot:platform:*;country_code:*;customer_type:*"

输出文件查询

# 查看生成的索引文件
grep "platform:essa;country_code:" /data/tw/recommendation/offline_tasks/output/interest_aggregation_hot_20260112.txt

# 查看特定平台和国家的索引
grep "platform:essaone;country_code:CN" /data/tw/recommendation/offline_tasks/output/interest_aggregation_hot_20260112.txt

# 统计新维度数量
grep -c "platform:essa;country_code:" /data/tw/recommendation/offline_tasks/output/interest_aggregation_hot_20260112.txt
grep -c "platform:essaone;country_code:.*;customer_type:" /data/tw/recommendation/offline_tasks/output/interest_aggregation_hot_20260112.txt

🎯 完整的索引列表

essa平台

interest:hot:platform:essa
interest:hot:platform:essa;country_code:{国家代码}
interest:hot:platform:essa;country_code:{国家代码};customer_type:{客户类型}

essaone平台

interest:hot:platform:essaone
interest:hot:platform:essaone;country_code:{国家代码}
interest:hot:platform:essaone;country_code:{国家代码};customer_type:{客户类型}

essabuy平台

interest:hot:platform:essabuy
(无国家和客户类型维度)

⚙️ 代码修改

修改的文件

  • /data/tw/recommendation/offline_tasks/scripts/interest_aggregation.py

备份文件

  • /data/tw/recommendation/offline_tasks/scripts/interest_aggregation.py.backup

主要修改内容

  1. SQL查询(第268-312行)

    • 增加 login_id 字段
    • 增加 country_code_pd.name AS country_code
    • 增加 customer_type_pd.name AS customer_type
    • 新增6个LEFT JOIN关联表获取用户信息
  2. 聚合函数(第142-156行)

    • 新增 platform:{平台};country_code:{国家代码} 维度
    • 新增 platform:{平台};country_code:{国家代码};customer_type:{客户类型} 维度
    • 限制只为essa和essaone平台生成这些维度

📝 注意事项

  1. 数据完整性

    • 如果用户没有login_id,country_code和customer_type将为NULL
    • 这些用户不会出现在新维度索引中
    • 但仍会出现在基础platform索引中
  2. 性能影响

    • 新增了多个表关联,SQL查询时间可能增加
    • 建议监控查询性能,必要时添加索引
  3. 索引数量

    • 新维度会显著增加索引数量
    • 每个平台×每个国家×每个客户类型 = 一个索引
    • 请确保Redis有足够的存储空间
  4. 数据准确性

    • country_code和customer_type依赖于buy_buyer_extend_option表的数据质量
    • 如果code字段不是'country_code'或'customer_type',将无法获取数据

🚀 运行方式

cd /data/tw/recommendation/offline_tasks

# 标准运行(使用默认参数)
python3 scripts/interest_aggregation.py

# 调试模式(查看详细信息)
python3 scripts/interest_aggregation.py --debug

# 自定义参数
python3 scripts/interest_aggregation.py \
    --lookback_days 730 \
    --top_n 1000 \
    --recent_days 180 \
    --debug

📈 预期结果

运行后将在输出文件中看到类似内容:

platform:essa;country_code:US   1001:150.5,1002:145.2,1003:138.9,...
platform:essa;country_code:CN   1004:142.1,1005:139.8,...
platform:essa;country_code:US;customer_type:目标客户    1001:85.3,1006:72.1,...
platform:essaone;country_code:CN;customer_type:普通客户 2001:123.4,2002:118.7,...

在Redis中:

redis-cli -h localhost -p 6479 -a "BMfv5aI31kgHWtlx" KEYS "interest:*" | grep "country_code"

更新日期:2026-01-12 版本:v2.0