# Redis 在线索引契约 本文描述 `scripts/load_index_to_redis.py` 当前实际写入的 Redis 契约。在线服务、诊断脚本和 Agent 工具均应以本文及加载器代码为准。 ## 1. 权威约定 - 加载器取离线 TSV 的第一列作为 key 后缀,最后一列作为 value。 - value 原样写入 Redis,不会转换成 JSON。 - 当前标准 value 是按排名降序的文本:`item_id:score,item_id:score,...`。 - 所有索引的默认 TTL 都由 `--expire-days` 控制,当前默认值为 60 天。 - Redis 地址、端口、数据库和认证信息由部署配置决定,文档不固定数据库编号,也不保存凭据。 - 商品 ID 在接口中建议使用字符串,避免跨语言传递时的数值精度问题。 示例: ```text Key: item:similar:swing:3141390 Value: 3264320:1.2656,3264284:1.2656,3562128:0.4000 ``` value 不是下面这种 JSON: ```json [[3264320, 1.2656], [3264284, 1.2656]] ``` 历史环境可能仍保留 JSON pair 数组、JSON ID 数组或旧的 `item:similar:w2v:*` key。新代码不应继续写这些格式;`agent_tools` 的读取器仅为平滑迁移而兼容它们。 ## 2. I2I 索引 | 离线模块 | 输入文件 | Redis key | value | | --- | --- | --- | --- | | C++ Swing | `collaboration/output/swing_similar.txt` | `item:similar:swing_cpp:{item_id}` | `item_id:score,...` | | Python Swing | `output/i2i_swing_YYYYMMDD.txt` | `item:similar:swing:{item_id}` | `item_id:score,...` | | Session W2V | `output/i2i_session_w2v_YYYYMMDD.txt` | `item:similar:session_w2v:{item_id}` | `item_id:score,...` | | DeepWalk | `output/i2i_deepwalk_YYYYMMDD.txt` | `item:similar:deepwalk:{item_id}` | `item_id:score,...` | | 名称内容相似 | `output/i2i_content_name_YYYYMMDD.txt` | `item:similar:content_name:{item_id}` | `item_id:score,...` | | 图片内容相似 | `output/i2i_content_pic_YYYYMMDD.txt` | `item:similar:content_pic:{item_id}` | `item_id:score,...` | | 行为相似 | `output/i2i_item_behavior_YYYYMMDD.txt` | `item:similar:item_behavior:{item_id}` | `item_id:score,...` | `item:similar:w2v:{item_id}` 是历史别名,不是当前加载器的写入目标。迁移期间可以读它作为 `session_w2v` 的后备 key,但新生产者和新调用方必须使用 `item:similar:session_w2v:{item_id}`。 各算法原始 score 的量纲不同,例如 Swing 原始分数、向量分数和行为聚合分数不能直接相加。跨算法融合应使用排名融合或先逐路校准;Agent 工具默认使用加权 RRF。 ## 3. 兴趣聚合索引 完整 key 格式为: ```text interest:{list_type}:{dimension_key} ``` `list_type` 有四种: | 类型 | 数据范围 | | --- | --- | | `hot` | 最近窗口内的多行为加权结果 | | `cart` | 加购、入池行为结果 | | `new` | 最近上架商品结果 | | `global` | 完整回看窗口内的多行为加权结果 | `global` 是列表类型,不是自动生成的 dimension。因此现有生产器会生成 `interest:global:platform:essaone` 等 key,不会生成 `interest:global:global`。若业务需要真正的无条件全局兜底,必须在离线侧新增 `global` dimension 后再建立该契约。 单维度: ```text platform:{platform} client_platform:{client_platform} supplier:{supplier_id} category_level1:{category_id} category_level2:{category_id} category_level3:{category_id} category_level4:{category_id} ``` 组合维度: ```text platform_client:{platform}_{client_platform} platform_category2:{platform}_{category_level2_id} platform_category3:{platform}_{category_level3_id} client_category2:{client_platform}_{category_level2_id} platform:{platform};country_code:{country_code} platform:{platform};country_code:{country_code};customer_type:{customer_type} ``` 国家及客户类型组合当前只为 `essa`、`essaone` 生成。具体数据来源和语义限制见 [新维度说明.md](./新维度说明.md)。 兴趣索引同样保留 score: ```text Key: interest:hot:platform_category2:essaone_29 Value: 3482467:156.2000,2708138:143.0000,1008982:132.5000 ``` ## 4. 标准解析 仅消费当前契约时可使用: ```python def parse_ranked_value(value): if not value: return [] if isinstance(value, bytes): value = value.decode('utf-8') result = [] for token in value.split(','): token = token.strip() if not token or ':' not in token: continue item_id, score = token.rsplit(':', 1) try: result.append((item_id, float(score))) except ValueError: continue return result ``` 需要兼容历史 JSON 格式时,统一调用: ```python from agent_tools.parsing import parse_ranked_items items = parse_ranked_items(redis_client.get(key)) ``` 不要在每个在线召回分支各写一套解析逻辑。 ## 5. 加载与验证 加载指定日期的全部索引: ```bash cd offline_tasks python3 scripts/load_index_to_redis.py --date YYYYMMDD ``` 该命令会连接外部服务并写 Redis。执行前必须确认 `config/offline_config.py` 或显式参数中的目标地址、数据库、TTL 和日期。不要为了加载新数据执行 `--flush-db`,除非已确认整个目标数据库都可删除。 在线抽样: ```bash redis-cli --scan --pattern 'item:similar:swing:*' | head redis-cli --scan --pattern 'interest:hot:platform:*' | head redis-cli GET 'item:similar:swing:3141390' redis-cli TTL 'item:similar:swing:3141390' ``` 认证信息通过 `REDISCLI_AUTH` 或安全的凭据注入方式传递,不要写入命令历史、脚本或文档。 建议按索引族记录以下指标,而不是依赖文档中的静态数量: - 生成文件行数、合法行比例和空 value 比例; - Redis 写入成功数、跳过数和 TTL; - key 覆盖商品数、每个 key 的候选数量分布; - 抽样解析成功率和候选商品实时可售率; - 在线每路 hit rate、召回候选数和延迟。 ## 6. 变更规则 以下任一变化都属于在线契约变更,必须同步修改加载器、读取器、测试和本文: - key 前缀或算法名; - value 序列化格式; - dimension key 的拼接方式; - score 含义或排序方向; - TTL、数据库或双写/迁移策略。 更新日期:2026-08-23。