Blame view

offline_tasks/QUICKSTART_NEW.md 6.84 KB
b57c6eb4   tangwang   offline tasks: fi...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
  # 快速开始 - 新版本
  
  ## 🚀 5分钟快速上手
  
  ### 1. 安装依赖
  
  ```bash
  cd /home/tw/recommendation
  pip install -r requirements.txt
  ```
  
  **新增依赖**: `elasticsearch>=8.0.0`
  
  ### 2. 测试ES连接
  
  ```bash
  cd offline_tasks
  python scripts/test_es_connection.py
  ```
  
  如果看到 ✓ 表示测试通过。
  
  ### 3. 运行所有任务
  
  ```bash
  python run_all.py
  ```
  
  就这么简单!不需要任何参数。
  
  ### 4. 加载到Redis
  
  ```bash
  python scripts/load_index_to_redis.py
  ```
  
  ## 📋 运行单个任务
  
  ### i2i相似索引
  
  ```bash
  # Swing算法
  python scripts/i2i_swing.py --lookback_days 30 --top_n 50 --time_decay
  
  # Session W2V
  python scripts/i2i_session_w2v.py --lookback_days 30 --top_n 50 --save_model
  
  # DeepWalk
  python scripts/i2i_deepwalk.py --lookback_days 30 --top_n 50 --save_model
  
  # 内容相似(ES向量)- 无需参数!
  python scripts/i2i_content_similar.py
  ```
  
  ### 兴趣聚合
  
  ```bash
  python scripts/interest_aggregation.py --lookback_days 30 --top_n 1000
  ```
  
  ## 🎯 主要变化
  
  ### 简化!简化!简化!
  
  #### 之前 (v1.0)
  ```bash
  python run_all.py \
    --lookback_days 30 \
    --top_n 50 \
    --skip-interest \
    --only-content \
    --debug
  ```
  
  #### 现在 (v2.0)
  ```bash
  python run_all.py
  # 或
  python run_all.py --debug  # 启用debug模式
  ```
  
  ### 内容相似索引
  
  #### 之前
  - 1个索引: `i2i_content_hybrid_*.txt`
  - 基于: 商品属性(分类、供应商等)
  - 参数: `--method hybrid --top_n 50`
  
  #### 现在
  - **2个索引**: 
    - `i2i_content_name_*.txt` (名称向量)
    - `i2i_content_pic_*.txt` (图片向量)
  - 基于: Elasticsearch深度学习向量
  - 参数: **无需参数!**
  
  ## 📊 输出文件
  
  ### 文件位置
  ```
  offline_tasks/output/
  ├── i2i_swing_20251017.txt          # Swing相似索引
  ├── i2i_session_w2v_20251017.txt    # Session W2V相似索引
  ├── i2i_deepwalk_20251017.txt       # DeepWalk相似索引
  ├── i2i_content_name_20251017.txt   # 名称向量相似索引 ⭐新
  ├── i2i_content_pic_20251017.txt    # 图片向量相似索引 ⭐新
  ├── interest_aggregation_hot_20251017.txt    # 热门商品
  ├── interest_aggregation_cart_20251017.txt   # 加购商品
  ├── interest_aggregation_new_20251017.txt    # 新品
  └── interest_aggregation_global_20251017.txt # 全局热门
  ```
  
  ### 文件格式
  ```
  item_id \t item_name \t similar_id1:score1,similar_id2:score2,...
  ```
  
  ## 🔍 查询示例
  
  ### Python查询
  
  ```python
  import redis
  import json
  
  # 连接Redis
  r = redis.Redis(host='localhost', port=6379, db=0, decode_responses=True)
  
  # 1. 获取Swing相似商品
  similar = json.loads(r.get('item:similar:swing:123456'))
  # 返回: [[234567, 0.8523], [345678, 0.7842], ...]
  
  # 2. 获取名称向量相似商品 ⭐新
  similar = json.loads(r.get('item:similar:content_name:123456'))
  # 返回: [[234567, 0.9234], [345678, 0.8756], ...]
  
  # 3. 获取图片向量相似商品 ⭐新
  similar = json.loads(r.get('item:similar:content_pic:123456'))
  # 返回: [[567890, 0.8123], [678901, 0.7856], ...]
  
  # 4. 获取热门商品
  hot_items = json.loads(r.get('interest:hot:platform:PC'))
  # 返回: [123456, 234567, 345678, ...]
  ```
  
  ### Redis CLI查询
  
  ```bash
  # 连接Redis
  redis-cli
  
  # 查看Swing相似商品
  GET item:similar:swing:123456
  
  # 查看名称向量相似商品 ⭐新
  GET item:similar:content_name:123456
  
  # 查看图片向量相似商品 ⭐新
  GET item:similar:content_pic:123456
  
  # 查看热门商品
  GET interest:hot:platform:PC
  ```
  
  ## ⚙️ 配置说明
  
  ### ES配置 (i2i_content_similar.py)
  
  ```python
  ES_CONFIG = {
      'host': 'http://localhost:9200',
      'index_name': 'spu',
      'username': 'essa',
      'password': '4hOaLaf41y2VuI8y'
  }
  ```
  
  ### 算法参数 (i2i_content_similar.py)
  
  ```python
  TOP_N = 50          # 每个商品返回50个相似商品
  KNN_K = 100         # KNN查询返回100个候选
  KNN_CANDIDATES = 200  # 候选池大小200
  ```
  
  ### 全局配置 (offline_config.py)
  
  ```python
  DEFAULT_LOOKBACK_DAYS = 30    # 回看天数
  DEFAULT_I2I_TOP_N = 50        # i2i Top N
  DEFAULT_INTEREST_TOP_N = 1000 # 兴趣聚合 Top N
  ```
  
  ## 🔧 故障排查
  
  ### ES连接失败
  
  ```bash
  # 1. 检查ES是否运行
  curl -u essa:4hOaLaf41y2VuI8y http://localhost:9200
  
  # 2. 运行测试脚本
  python scripts/test_es_connection.py
  
  # 3. 检查配置
  # 编辑 scripts/i2i_content_similar.py 中的 ES_CONFIG
  ```
  
  ### 商品ID不存在
  
  测试脚本默认使用 `item_id = "3302275"`,如果不存在:
  
  ```python
  # 编辑 test_es_connection.py
  test_item_id = "你的商品ID"
  ```
  
  ### Redis连接失败
  
  ```bash
  # 检查Redis配置
  cat offline_tasks/config/offline_config.py | grep REDIS
  
  # 测试Redis连接
  redis-cli ping
  ```
  
  ### 文件不存在
  
  ```bash
  # 检查output目录
  ls -lh offline_tasks/output/
  
  # 查看最新生成的文件
  ls -lht offline_tasks/output/ | head -10
  ```
  
  ## 📚 详细文档
  
  - **ES向量相似度**: `scripts/ES_VECTOR_SIMILARITY.md`
  - **更新说明**: `CONTENT_SIMILARITY_UPDATE.md`
  - **变更总结**: `CHANGES_SUMMARY.md`
  - **Redis规范**: `REDIS_DATA_SPEC.md`
  
  ## 🎓 学习路径
  
  ### 新用户
  1. 阅读本文档 ✓
  2. 运行 `test_es_connection.py`
  3. 运行 `run_all.py`
  4. 查看 `output/` 目录
  5. 加载到Redis并查询
  
  ### 进阶使用
  1. 阅读 `ES_VECTOR_SIMILARITY.md`
  2. 了解向量相似度原理
  3. 优化ES查询性能
  4. 自定义算法参数
  
  ### 开发者
  1. 阅读 `CONTENT_SIMILARITY_UPDATE.md`
  2. 了解技术架构
  3. 阅读源代码注释
  4. 贡献代码改进
  
  ## 🚨 注意事项
  
  ### ⚠️ 破坏性变化
  
  1. **i2i_content_similar.py 参数全部改变**
     - 旧: `--method`, `--top_n`, `--debug`
     - 新: 无参数
  
  2. **Redis Key格式改变**
     - 旧: `item:similar:content:{item_id}`
     - 新: `item:similar:content_name:{item_id}` 和 `item:similar:content_pic:{item_id}`
  
  3. **输出文件改变**
     - 旧: `i2i_content_hybrid_*.txt`
     - 新: `i2i_content_name_*.txt` 和 `i2i_content_pic_*.txt`
  
  ### ✅ 向后兼容
  
  - Swing、W2V、DeepWalk 算法不受影响
  - 兴趣聚合不受影响
  - Redis加载器向后兼容
  - 其他i2i索引继续工作
  
  ## 💡 最佳实践
  
  ### 运行频率
  - **行为相似** (Swing, W2V, DeepWalk): 每天
  - **内容相似** (名称向量, 图片向量): 每周
  - **兴趣聚合**: 每天
  
  ### Redis TTL
  - **行为相似**: 7天
  - **内容相似**: 30天
  - **兴趣聚合**: 3-7天
  
  ### 性能优化
  1. 使用 `--debug` 模式调试
  2. 先用小数据集测试
  3. 定期清理过期数据
  4. 监控ES查询性能
  
  ## 🎉 总结
  
  新版本大幅简化了使用,主要改进:
  
  1.**无需参数**: `run_all.py` 和 `i2i_content_similar.py` 无需参数
  2.**更强大**: 基于深度学习向量,更准确
  3.**多维度**: 名称 + 图片两个维度
  4.**更快**: ES KNN查询性能优秀
  5.**易维护**: 代码简洁,配置清晰
  
  开始使用新版本,享受更简单、更强大的推荐系统!
  
  ---
  
  **问题反馈**: 如有问题请查看详细文档或联系开发团队