Blame view

docs/Usage-Guide.md 18.4 KB
bc54124c   tangwang   docs
1
  # 使用指南 - saas-search(运行与运维)
38f530ff   tangwang   文档完善
2
  
bc54124c   tangwang   docs
3
4
5
  本文档聚焦运行与运维:服务启动/停止、日志查看、多环境切换、故障排查、Suggestion 索引运维。
  
  开发与配置(环境入口、配置体系、Provider 架构、模块扩展规范)请优先阅读 `docs/QUICKSTART.md`
38f530ff   tangwang   文档完善
6
7
8
9
10
  
  ## 目录
  
  1. [环境准备](#环境准备)
  2. [服务启动](#服务启动)
af7ee060   tangwang   service_ctl 简化为“显...
11
12
13
14
15
  3. [服务管理总览](#全盘串讲服务管理方式)
  4. [配置说明](#配置说明)
  5. [查看日志](#查看日志)
  6. [测试验证](#测试验证)
  7. [常见问题](#常见问题)
38f530ff   tangwang   文档完善
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  
  ---
  
  ## 环境准备
  
  ### 系统要求
  
  - **操作系统**: Linux (推荐 CentOS 7+ / Ubuntu 18.04+)
  - **Python**: 3.8+
  - **内存**: 建议 8GB+
  - **磁盘**: 10GB+ (包含模型文件)
  - **Elasticsearch**: 8.x (可通过Docker运行)
  
  ### 安装依赖
  
a7920e17   tangwang   项目名称和部署路径修改
31
32
  #### 1. 安装 Python 依赖与激活环境
  
ed948666   tangwang   tidy
33
  **推荐**:使用项目根目录的 `activate.sh` 激活环境(会加载 `.env`)。当前仅支持 venv(`.venv`),不存在 Conda 回退。系统要求、Python 环境、生产凭证与 `.env` 模板见 [QUICKSTART.md](./QUICKSTART.md) §1.4–1.8。
38f530ff   tangwang   文档完善
34
35
  
  ```bash
a7920e17   tangwang   项目名称和部署路径修改
36
  cd /data/saas-search
484adbfe   tangwang   adapt ubuntu; con...
37
38
39
40
  ./scripts/create_venv.sh   # 首次创建 venv(只需执行一次)
  source activate.sh
  ```
  
07cf5a93   tangwang   START_EMBEDDING=...
41
  模型服务采用独立环境(推荐):
484adbfe   tangwang   adapt ubuntu; con...
42
43
44
  
  ```bash
  cd /data/saas-search
07cf5a93   tangwang   START_EMBEDDING=...
45
46
47
  ./scripts/setup_embedding_venv.sh   # .venv-embedding
  ./scripts/setup_reranker_venv.sh    # .venv-reranker
  ./scripts/setup_cnclip_venv.sh      # .venv-cnclip
38f530ff   tangwang   文档完善
48
49
  ```
  
07cf5a93   tangwang   START_EMBEDDING=...
50
51
52
53
  TEI 文本向量服务使用 Docker 容器:
  
  ```bash
  # GPU(需 nvidia-container-toolkit)
af7ee060   tangwang   service_ctl 简化为“显...
54
  TEI_DEVICE=cuda ./scripts/start_tei_service.sh
07cf5a93   tangwang   START_EMBEDDING=...
55
56
  
  # CPU
af7ee060   tangwang   service_ctl 简化为“显...
57
  TEI_DEVICE=cpu ./scripts/start_tei_service.sh
07cf5a93   tangwang   START_EMBEDDING=...
58
  ```
a7920e17   tangwang   项目名称和部署路径修改
59
  
bc089b43   tangwang   refactor(reranker...
60
61
62
63
  专项说明:
  - `docs/TEI_SERVICE说明文档.md`
  - `docs/CNCLIP_SERVICE说明文档.md`
  
38f530ff   tangwang   文档完善
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  #### 2. 启动Elasticsearch
  
  **方式1: 使用Docker(推荐)**
  
  ```bash
  docker run -d \
    --name elasticsearch \
    -p 9200:9200 \
    -e "discovery.type=single-node" \
    -e "ES_JAVA_OPTS=-Xms2g -Xmx2g" \
    elasticsearch:8.11.0
  ```
  
  **方式2: 本地安装**
  
  参考 [Elasticsearch官方文档](https://www.elastic.co/guide/en/elasticsearch/reference/8.11/install-elasticsearch.html)
  
648cb4c2   tangwang   ES docs
81
  #### 3. 配置环境变量(支持多环境)
38f530ff   tangwang   文档完善
82
  
648cb4c2   tangwang   ES docs
83
  创建 `.env` 文件(以本地 / prod 环境为例):
38f530ff   tangwang   文档完善
84
85
86
87
88
89
90
91
92
93
94
95
  
  ```bash
  # MySQL配置
  DB_HOST=120.79.247.228
  DB_PORT=3316
  DB_DATABASE=saas
  DB_USERNAME=saas
  DB_PASSWORD=your_password
  
  # Elasticsearch配置
  ES_HOST=http://localhost:9200
  ES_USERNAME=essa
bc54124c   tangwang   docs
96
  ES_PASSWORD=your_es_password
38f530ff   tangwang   文档完善
97
98
99
100
  
  # Redis配置(可选,用于缓存)
  REDIS_HOST=localhost
  REDIS_PORT=6479
bc54124c   tangwang   docs
101
  REDIS_PASSWORD=your_redis_password
38f530ff   tangwang   文档完善
102
103
  
  # DeepL翻译API(可选)
bc54124c   tangwang   docs
104
  DEEPL_AUTH_KEY=your_deepl_auth_key
38f530ff   tangwang   文档完善
105
  
648cb4c2   tangwang   ES docs
106
107
108
109
110
  # 运行环境(用于区分 prod / uat / test / dev)
  RUNTIME_ENV=prod
  # ES 索引命名空间前缀(用于按环境隔离索引,prod 通常留空)
  ES_INDEX_NAMESPACE=
  
38f530ff   tangwang   文档完善
111
112
113
114
115
116
117
118
119
120
121
122
  # API服务配置
  API_HOST=0.0.0.0
  API_PORT=6002
  ```
  
  ---
  
  ## 服务启动
  
  ### 方式1: 一键启动(推荐)
  
  ```bash
a7920e17   tangwang   项目名称和部署路径修改
123
  cd /data/saas-search
7913e2fb   tangwang   服务管理和监控
124
  ./run.sh all
38f530ff   tangwang   文档完善
125
126
127
128
  ```
  
  这个脚本会自动:
  1. 创建日志目录
7b8d9e1a   tangwang   评估框架的启动脚本
129
  2. 按目标启动服务(`all`:`tei cnclip embedding embedding-image translator reranker reranker-fine backend indexer frontend eval-web`
d1d356f8   tangwang   脚本优化
130
131
  3. 写入 PID 到 `logs/*.pid`
  4. 执行健康检查
7913e2fb   tangwang   服务管理和监控
132
  5. 启动 monitor daemon(运行期连续失败自动重启)
38f530ff   tangwang   文档完善
133
134
135
  
  启动完成后,访问:
  - **前端界面**: http://localhost:6003
7b8d9e1a   tangwang   评估框架的启动脚本
136
  - **搜索评估 UI**: http://localhost:6010(`eval-web`,依赖 backend 提供实时搜索)
38f530ff   tangwang   文档完善
137
138
  - **后端API**: http://localhost:6002
  - **API文档**: http://localhost:6002/docs
d1d356f8   tangwang   脚本优化
139
  - **索引API**: http://localhost:6004/docs
38f530ff   tangwang   文档完善
140
  
7913e2fb   tangwang   服务管理和监控
141
  仅启动部分服务示例:`./run.sh backend indexer frontend`
d1d356f8   tangwang   脚本优化
142
143
144
145
146
147
148
  
  ### 方式2: 统一控制脚本(推荐)
  
  ```bash
  # 查看状态
  ./scripts/service_ctl.sh status
  
7913e2fb   tangwang   服务管理和监控
149
150
  # 推荐:一键启动 + 监控守护
  ./scripts/service_ctl.sh up all
d1d356f8   tangwang   脚本优化
151
152
  
  # 启动指定服务
7913e2fb   tangwang   服务管理和监控
153
154
155
  ./scripts/service_ctl.sh up backend indexer frontend
  
  # 启动指定服务(不自动拉起 monitor daemon)
7fbca0d7   tangwang   启动脚本优化
156
  ./scripts/service_ctl.sh start backend indexer frontend translator reranker tei cnclip
d1d356f8   tangwang   脚本优化
157
  
7913e2fb   tangwang   服务管理和监控
158
159
  # 停止全部服务(含 monitor daemon)
  ./scripts/service_ctl.sh down all
d1d356f8   tangwang   脚本优化
160
  
7913e2fb   tangwang   服务管理和监控
161
162
  # 重启全部服务
  ./scripts/service_ctl.sh restart all
d1d356f8   tangwang   脚本优化
163
164
  ```
  
c90f80ed   tangwang   相关性优化
165
166
167
168
169
  补充说明:
  
  - `./scripts/service_ctl.sh` 可以直接使用,不要求你手动先 `source ./activate.sh`;因为各个 `start_*.sh` 会自行加载项目虚拟环境。
  - 但如果你要运行 `pytest`、评估脚本或临时 Python 脚本,建议先执行 `source ./activate.sh`
  
d1d356f8   tangwang   脚本优化
170
  ### 方式3: 分步启动(单环境)
38f530ff   tangwang   文档完善
171
172
173
174
175
176
177
  
  #### 启动后端服务
  
  ```bash
  ./scripts/start_backend.sh
  ```
  
648cb4c2   tangwang   ES docs
178
  后端API会在 http://localhost:6002 启动(使用当前 `.env` 中的 ES_HOST / DB_HOST / RUNTIME_ENV / ES_INDEX_NAMESPACE)
38f530ff   tangwang   文档完善
179
  
c90f80ed   tangwang   相关性优化
180
181
182
183
184
185
186
187
  等价的手动命令是:
  
  ```bash
  cd /data/saas-search
  source ./activate.sh
  python main.py serve --host "${API_HOST:-0.0.0.0}" --port "${API_PORT:-6002}" --es-host "${ES_HOST:-http://localhost:9200}"
  ```
  
38f530ff   tangwang   文档完善
188
189
190
191
192
193
194
195
  #### 启动前端服务
  
  ```bash
  ./scripts/start_frontend.sh
  ```
  
  前端界面会在 http://localhost:6003 启动
  
c90f80ed   tangwang   相关性优化
196
197
198
199
200
201
202
203
204
  ## 本地测试与检索效果评估
  
  ```bash
  cd /data/saas-search
  source ./activate.sh
  python -m pytest -q tests/test_rerank_client.py tests/test_es_query_builder.py tests/test_search_rerank_window.py tests/test_query_parser_mixed_language.py
  ./scripts/service_ctl.sh restart backend
  sleep 3
  ./scripts/service_ctl.sh status backend
a345b01f   tangwang   eval framework
205
  ./scripts/evaluation/start_eval.sh.sh batch
c90f80ed   tangwang   相关性优化
206
207
  ```
  
3ac1f8d1   tangwang   评估标准优化
208
  离线批量评估会把标注与报表写到 `artifacts/search_evaluation/`(SQLite、`batch_reports/` 下的 JSON/Markdown 等)。说明与命令见 [scripts/evaluation/README.md](../scripts/evaluation/README.md)
c90f80ed   tangwang   相关性优化
209
  
d1d356f8   tangwang   脚本优化
210
  ### 方式4: 多环境示例(prod / uat)
648cb4c2   tangwang   ES docs
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
  
  假设有两套环境:
  
  - **prod**:正式环境,RUNTIME_ENV=prod,ES_INDEX_NAMESPACE 为空
  - **uat**:联调环境,RUNTIME_ENV=uat,ES_INDEX_NAMESPACE=uat_
  
  可以在项目根目录准备两个配置文件:
  
  - `.env.prod`
  
  ```bash
  RUNTIME_ENV=prod
  ES_INDEX_NAMESPACE=
  ES_HOST=http://prod-es:9200
  DB_HOST=prod-mysql
  ...
  ```
  
  - `.env.uat`
  
  ```bash
  RUNTIME_ENV=uat
  ES_INDEX_NAMESPACE=uat_
  ES_HOST=http://uat-es:9200
  DB_HOST=uat-mysql
  ...
  ```
  
  启动不同环境时:
  
  ```bash
  # 启动 UAT 后端 + 索引服务
  cp .env.uat .env
  ./scripts/start_backend.sh
  ./scripts/start_indexer.sh
  
  # 启动 PROD 后端 + 索引服务
  cp .env.prod .env
  ./scripts/start_backend.sh
  ./scripts/start_indexer.sh
  ```
  
d1d356f8   tangwang   脚本优化
253
  ### 方式5: 手动启动
38f530ff   tangwang   文档完善
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
  
  #### 启动后端API服务
  
  ```bash
  python -m api.app \
    --host 0.0.0.0 \
    --port 6002 \
    --es-host http://localhost:9200 \
    --reload
  ```
  
  #### 启动前端服务(可选)
  
  ```bash
  # 使用Python简单HTTP服务器
  cd frontend
  python -m http.server 6003
  ```
  
af7ee060   tangwang   service_ctl 简化为“显...
273
274
275
276
  ## 服务管理总览
  
  ### 1) 入口脚本职责
  
7913e2fb   tangwang   服务管理和监控
277
278
279
  - `./run.sh [all|service...]`:薄封装,直接调用 `./scripts/service_ctl.sh up [all|service...]`
  - `./restart.sh [all|service...]`:薄封装,直接调用 `./scripts/service_ctl.sh restart [all|service...]`
  - `./scripts/stop.sh [all|service...]`:薄封装,直接调用 `./scripts/service_ctl.sh down [all|service...]`
c6da6bca   tangwang   add status.sh
280
  - `./status.sh`:薄封装,直接调用 `./scripts/service_ctl.sh status`(包含进程/端口 + HTTP 健康检查结果)。
7913e2fb   tangwang   服务管理和监控
281
  - `./scripts/service_ctl.sh`:统一控制器,支持 `up/down/start/stop/restart/status/monitor*`(带参数行为完全由此脚本定义)。
af7ee060   tangwang   service_ctl 简化为“显...
282
283
284
  
  ### 2) `service_ctl.sh` 的默认行为
  
7913e2fb   tangwang   服务管理和监控
285
286
287
288
289
  - `up`:**必须显式指定** 服务或 `all`,并自动启动 monitor daemon。
  - `down`:**必须显式指定** 服务或 `all`,并停止 monitor daemon。
  - `start`:**必须显式指定** 服务或 `all`(不自动拉起 monitor daemon)。
  - `stop`:**必须显式指定** 服务或 `all`;若 monitor daemon 运行会先停止它。
  - `restart`:**必须显式指定** 服务或 `all`
c6da6bca   tangwang   add status.sh
290
  - `status`(不带服务名):显示全部已知服务状态 + monitor daemon 状态,并对支持的服务执行 HTTP/容器级健康检查。
af7ee060   tangwang   service_ctl 简化为“显...
291
292
293
294
  
  ### 3) 全量服务一键拉起
  
  ```bash
7913e2fb   tangwang   服务管理和监控
295
  ./scripts/service_ctl.sh up all
af7ee060   tangwang   service_ctl 简化为“显...
296
297
298
299
  ```
  
  说明:
  - `TEI_DEVICE` / `CNCLIP_DEVICE` 统一使用 `cuda|cpu`
7913e2fb   tangwang   服务管理和监控
300
  - `all` 内部已按依赖顺序处理(先 `tei/cnclip` 再 `embedding`)。
af7ee060   tangwang   service_ctl 简化为“显...
301
302
303
304
  
  ### 4) 常用运维命令
  
  ```bash
7913e2fb   tangwang   服务管理和监控
305
306
  # 一键拉起完整栈(推荐)
  ./scripts/service_ctl.sh up all         # 或 ./run.sh all
af7ee060   tangwang   service_ctl 简化为“显...
307
  
c6da6bca   tangwang   add status.sh
308
  # 查看全量状态 + 健康检查(进程/端口 + HTTP)
af7ee060   tangwang   service_ctl 简化为“显...
309
310
311
312
313
314
  ./scripts/service_ctl.sh status
  
  # 仅重启某个服务
  ./scripts/service_ctl.sh restart embedding
  
  # 停止全部
7913e2fb   tangwang   服务管理和监控
315
  ./scripts/service_ctl.sh down all       # 或 ./scripts/stop.sh all
af7ee060   tangwang   service_ctl 简化为“显...
316
317
  ```
  
38f530ff   tangwang   文档完善
318
319
320
  ### 停止服务
  
  ```bash
7913e2fb   tangwang   服务管理和监控
321
322
  # 推荐:统一停止(示例:全部)
  ./scripts/stop.sh all
d1d356f8   tangwang   脚本优化
323
324
  
  # 或使用统一控制脚本
7913e2fb   tangwang   服务管理和监控
325
  ./scripts/service_ctl.sh down all
38f530ff   tangwang   文档完善
326
327
328
329
330
331
332
333
  ```
  
  ### 服务端口
  
  | 服务 | 端口 | URL |
  |------|------|-----|
  | Elasticsearch | 9200 | http://localhost:9200 |
  | Backend API | 6002 | http://localhost:6002 |
d1d356f8   tangwang   脚本优化
334
  | Indexer API | 6004 | http://localhost:6004 |
38f530ff   tangwang   文档完善
335
  | Frontend Web | 6003 | http://localhost:6003 |
7b8d9e1a   tangwang   评估框架的启动脚本
336
  | Search eval web (`eval-web`) | 6010 | http://localhost:6010 |
5bac9649   tangwang   文本 embedding 与图片 ...
337
338
  | Embedding Text (optional) | 6005 | http://localhost:6005 |
  | Embedding Image (optional) | 6008 | http://localhost:6008 |
07cf5a93   tangwang   START_EMBEDDING=...
339
  | TEI (optional) | 8080 | http://localhost:8080 |
d1d356f8   tangwang   脚本优化
340
341
342
  | Translation (optional) | 6006 | http://localhost:6006 |
  | Reranker (optional) | 6007 | http://localhost:6007 |
  | API Docs | 6002 / 6004 | http://localhost:6002/docs / http://localhost:6004/docs |
38f530ff   tangwang   文档完善
343
344
345
346
347
348
349
350
351
352
353
354
355
  
  ---
  
  ## 配置说明
  
  ### 环境配置文件 (.env)
  
  主要配置项说明:
  
  ```bash
  # Elasticsearch配置
  ES_HOST=http://localhost:9200
  ES_USERNAME=essa
bc54124c   tangwang   docs
356
  ES_PASSWORD=your_es_password
38f530ff   tangwang   文档完善
357
358
359
360
361
362
363
364
365
366
367
  
  # MySQL配置
  DB_HOST=120.79.247.228
  DB_PORT=3316
  DB_DATABASE=saas
  DB_USERNAME=saas
  DB_PASSWORD=your_password
  
  # Redis配置(可选,用于缓存)
  REDIS_HOST=localhost
  REDIS_PORT=6479
bc54124c   tangwang   docs
368
  REDIS_PASSWORD=your_redis_password
38f530ff   tangwang   文档完善
369
370
  
  # DeepL翻译API
bc54124c   tangwang   docs
371
  DEEPL_AUTH_KEY=your_deepl_auth_key
38f530ff   tangwang   文档完善
372
373
374
375
  
  # API服务配置
  API_HOST=0.0.0.0
  API_PORT=6002
d1d356f8   tangwang   脚本优化
376
377
378
379
380
381
  
  # Indexer服务配置
  INDEXER_HOST=0.0.0.0
  INDEXER_PORT=6004
  
  # Optional service ports
af7ee060   tangwang   service_ctl 简化为“显...
382
  FRONTEND_PORT=6003
7b8d9e1a   tangwang   评估框架的启动脚本
383
  EVAL_WEB_PORT=6010
5bac9649   tangwang   文本 embedding 与图片 ...
384
385
  EMBEDDING_TEXT_PORT=6005
  EMBEDDING_IMAGE_PORT=6008
af7ee060   tangwang   service_ctl 简化为“显...
386
387
  TEI_PORT=8080
  CNCLIP_PORT=51000
d1d356f8   tangwang   脚本优化
388
389
  TRANSLATION_PORT=6006
  RERANKER_PORT=6007
38f530ff   tangwang   文档完善
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
  ```
  
  ### 修改配置
  
  1. 编辑 `.env` 文件
  2. 重启相关服务
  
  ---
  
  ## 查看日志
  
  ### 日志文件位置
  
  日志文件存储在 `logs/` 目录下:
  
28e57bb1   tangwang   日志体系优化
405
406
407
408
409
  - `logs/<service>-YYYY-MM-DD.log` - `service_ctl.sh` 统一管理的按天日志文件(真实文件)
  - `logs/<service>.log` - 指向当天日志文件的软链(兼容原有命令)
  - `logs/api.log` - backend 进程内日志(按天轮转)
  - `logs/backend_verbose.log` - backend 详细大对象日志(按天轮转,含 `Search response` / `ES query built` 完整内容)
  - `logs/indexer.log` - 索引结构化日志(JSON,按天轮转)
38f530ff   tangwang   文档完善
410
411
412
413
  
  ### 查看实时日志
  
  ```bash
28e57bb1   tangwang   日志体系优化
414
415
  # 查看后端日志(建议 -F,跨天自动跟随新文件)
  tail -F logs/backend.log
38f530ff   tangwang   文档完善
416
417
  
  # 查看前端日志
28e57bb1   tangwang   日志体系优化
418
  tail -F logs/frontend.log
38f530ff   tangwang   文档完善
419
  
28e57bb1   tangwang   日志体系优化
420
421
  # 查看 backend 详细大对象日志
  tail -F logs/backend_verbose.log
38f530ff   tangwang   文档完善
422
423
424
425
426
427
428
429
430
431
432
433
434
  ```
  
  ### 日志级别
  
  日志级别可以通过环境变量 `LOG_LEVEL` 设置:
  
  ```bash
  # 在 .env 文件中设置
  LOG_LEVEL=DEBUG  # DEBUG, INFO, WARNING, ERROR, CRITICAL
  ```
  
  ### 日志轮转
  
28e57bb1   tangwang   日志体系优化
435
  日志文件按天自动轮转,默认保留 30 天历史日志(可通过 `LOG_RETENTION_DAYS` 调整 `service_ctl.sh` 管理日志的保留天数)。
38f530ff   tangwang   文档完善
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
  
  ---
  
  ## 测试验证
  
  ### 1. 健康检查
  
  ```bash
  curl http://localhost:6002/admin/health
  ```
  
  **预期响应**:
  ```json
  {
    "status": "healthy",
    "elasticsearch": "connected"
  }
  ```
  
  ### 2. 索引统计
  
  ```bash
26b910bd   tangwang   refactor service ...
458
  curl http://localhost:6002/admin/stats -H "X-Tenant-ID: 162"
38f530ff   tangwang   文档完善
459
460
461
462
463
464
465
  ```
  
  ### 3. 简单搜索测试
  
  ```bash
  curl -X POST http://localhost:6002/search/ \
    -H "Content-Type: application/json" \
a10a89a3   tangwang   构造测试数据用于测试分类 和 三种...
466
    -H "X-Tenant-ID: 162" \
38f530ff   tangwang   文档完善
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
    -d '{
      "query": "玩具",
      "size": 10
    }'
  ```
  
  或者通过查询参数:
  
  ```bash
  curl -X POST "http://localhost:6002/search/?tenant_id=2" \
    -H "Content-Type: application/json" \
    -d '{
      "query": "玩具",
      "size": 10
    }'
  ```
  
  ### 4. 带过滤器的搜索
  
  ```bash
  curl -X POST http://localhost:6002/search/ \
    -H "Content-Type: application/json" \
a10a89a3   tangwang   构造测试数据用于测试分类 和 三种...
489
    -H "X-Tenant-ID: 162" \
38f530ff   tangwang   文档完善
490
491
492
493
    -d '{
      "query": "玩具",
      "size": 10,
      "filters": {
cadc77b6   tangwang   索引字段名、变量名、API数据结构...
494
        "category.keyword": ["玩具", "益智玩具"]
38f530ff   tangwang   文档完善
495
496
497
498
499
500
501
502
503
504
505
506
      },
      "range_filters": {
        "price": {"gte": 50, "lte": 200}
      }
    }'
  ```
  
  ### 5. 分面搜索测试
  
  ```bash
  curl -X POST http://localhost:6002/search/ \
    -H "Content-Type: application/json" \
a10a89a3   tangwang   构造测试数据用于测试分类 和 三种...
507
    -H "X-Tenant-ID: 162" \
38f530ff   tangwang   文档完善
508
509
510
511
    -d '{
      "query": "玩具",
      "size": 10,
      "facets": [
cadc77b6   tangwang   索引字段名、变量名、API数据结构...
512
513
        {"field": "category.keyword", "size": 15},
        {"field": "vendor.keyword", "size": 15}
38f530ff   tangwang   文档完善
514
515
516
517
518
519
520
521
522
      ]
    }'
  ```
  
  ### 6. 图片搜索测试
  
  ```bash
  curl -X POST http://localhost:6002/search/image \
    -H "Content-Type: application/json" \
a10a89a3   tangwang   构造测试数据用于测试分类 和 三种...
523
    -H "X-Tenant-ID: 162" \
38f530ff   tangwang   文档完善
524
525
526
527
528
529
530
531
532
533
534
535
536
537
    -d '{
      "image_url": "https://oss.essa.cn/example.jpg",
      "size": 10
    }'
  ```
  
  ### 7. 前端界面测试
  
  访问 http://localhost:6003 或 http://localhost:6002/ 进行可视化测试。
  
  **注意**: 所有搜索接口都需要通过 `X-Tenant-ID` 请求头或 `tenant_id` 查询参数指定租户ID。
  
  ---
  
f251cf2d   tangwang   suggestion全量索引程序跑通
538
539
  ## 8. Suggestion 索引与接口使用
  
ff9efda0   tangwang   suggest
540
  ### 8.1 构建 Suggestion 索引(Phase 2:全量 + 增量)
f251cf2d   tangwang   suggestion全量索引程序跑通
541
542
543
544
545
546
  
  Suggestion 索引会从:
  
  - ES 商品索引:`title.{lang}`, `qanchors.{lang}`
  - MySQL 日志表:`shoplazza_search_log.query`(含 `language`、`request_params`
  
ff9efda0   tangwang   suggest
547
  聚合生成版本化索引并发布 alias:
f251cf2d   tangwang   suggestion全量索引程序跑通
548
  
ff9efda0   tangwang   suggest
549
550
  - 物理索引:`{ES_INDEX_NAMESPACE}search_suggestions_tenant_{tenant_id}_v<timestamp>`
  - 读别名:`{ES_INDEX_NAMESPACE}search_suggestions_tenant_{tenant_id}_current`
f251cf2d   tangwang   suggestion全量索引程序跑通
551
  
ff9efda0   tangwang   suggest
552
  在项目根目录执行(以 tenant_id=162 为例):
648cb4c2   tangwang   ES docs
553
  
ff9efda0   tangwang   suggest
554
555
556
557
  ```bash
  # 1) 全量重建(版本化 + alias 发布)
  ./scripts/build_suggestions.sh 162 \
    --mode full \
f251cf2d   tangwang   suggestion全量索引程序跑通
558
    --days 30 \
ff9efda0   tangwang   suggest
559
560
    --publish-alias \
    --keep-versions 2
f251cf2d   tangwang   suggestion全量索引程序跑通
561
  
ff9efda0   tangwang   suggest
562
563
564
565
566
  # 2) 增量更新(watermark + overlap)
  ./scripts/build_suggestions.sh 162 \
    --mode incremental \
    --overlap-minutes 30
  ```
648cb4c2   tangwang   ES docs
567
  
f251cf2d   tangwang   suggestion全量索引程序跑通
568
569
570
571
572
  可选参数:
  
  - `--days`:回溯日志天数(默认 30)
  - `--batch-size`:扫描商品索引的批大小(默认 500)
  - `--min-query-len`:参与 suggestion 的最小查询长度(默认 1)
ff9efda0   tangwang   suggest
573
574
  - `--overlap-minutes`:增量窗口重叠分钟数(默认 30)
  - `--bootstrap-if-missing`:增量模式下若缺少 active index 则自动全量初始化(默认 true)
f251cf2d   tangwang   suggestion全量索引程序跑通
575
576
577
578
579
580
581
582
  
  > 建议在商品索引构建完成、日志正常写入一段时间后执行一次全量构建,然后按天/小时增加增量构建任务。
  
  ### 8.2 调用 Suggestion 接口
  
  全量构建完成后,可直接通过 `/search/suggestions` 获取自动补全结果:
  
  ```bash
648cb4c2   tangwang   ES docs
583
  # UAT 环境(本地或 UAT 集群)
ff9efda0   tangwang   suggest
584
  curl "http://localhost:6002/search/suggestions?q=iph&size=5&language=en" \
f251cf2d   tangwang   suggestion全量索引程序跑通
585
    -H "X-Tenant-ID: 162"
648cb4c2   tangwang   ES docs
586
587
  
  # PROD 环境(域名 / 端口按实际部署调整)
ff9efda0   tangwang   suggest
588
  curl "https://api.yourdomain.com/search/suggestions?q=iph&size=5&language=en" \
648cb4c2   tangwang   ES docs
589
590
591
592
593
594
595
596
597
598
    -H "X-Tenant-ID: 162"
  ```
  
  ### 8.3 商品索引构建(search_products,多环境例子)
  
  以 tenant_id=162 为例,分别在 UAT / PROD 构建商品索引。
  
  #### UAT 环境(索引前缀 uat_)
  
  ```bash
a7920e17   tangwang   项目名称和部署路径修改
599
  cd /data/saas-search
648cb4c2   tangwang   ES docs
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
  
  # 1. 切换到 UAT 环境配置
  cp .env.uat .env
  
  # 2. 启动 indexer 服务(如尚未启动)
  ./scripts/start_indexer.sh
  
  # 3. 可选:重建索引结构(会删除旧索引)
  ./scripts/create_tenant_index.sh 162
  # 实际创建的索引名为:uat_search_products_tenant_162
  
  # 4. 导入商品数据(全量索引)
  curl -X POST "http://localhost:6004/indexer/reindex" \
    -H "Content-Type: application/json" \
    -d '{
      "tenant_id": "162",
      "batch_size": 500
    }'
f251cf2d   tangwang   suggestion全量索引程序跑通
618
619
  ```
  
648cb4c2   tangwang   ES docs
620
621
622
  #### PROD 环境(无前缀)
  
  ```bash
a7920e17   tangwang   项目名称和部署路径修改
623
  cd /data/saas-search
648cb4c2   tangwang   ES docs
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
  
  # 1. 切换到 PROD 环境配置
  cp .env.prod .env
  
  # 2. 启动 indexer 服务
  ./scripts/start_indexer.sh
  
  # 3. 可选:重建索引结构(search_products_tenant_162)
  ./scripts/create_tenant_index.sh 162
  
  # 4. 导入商品数据
  curl -X POST "http://localhost:6004/indexer/reindex" \
    -H "Content-Type: application/json" \
    -d '{
      "tenant_id": "162",
      "batch_size": 500
    }'
  ```
  
  完成后:
  
  - UAT 环境商品索引:`uat_search_products_tenant_162`
  - PROD 环境商品索引:`search_products_tenant_162`
  
  两套环境的搜索 / suggestion API 调用完全一致,只是连接到各自的后端 / ES。
  
41345271   tangwang   文档更新
650
  接口返回结构详见 `docs/搜索API对接指南-02-搜索建议与即时搜索.md` 的「3.7 搜索建议接口」章节。
f251cf2d   tangwang   suggestion全量索引程序跑通
651
652
653
  
  ---
  
38f530ff   tangwang   文档完善
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
  ## 常见问题
  
  ### Q1: MySQL连接失败
  
  **症状**: `Failed to connect to MySQL`
  
  **解决方案**:
  ```bash
  # 检查MySQL服务状态
  mysql -h 120.79.247.228 -P 3316 -u saas -p -e "SELECT 1"
  
  # 检查配置
  cat .env | grep DB_
  ```
  
  ### Q2: Elasticsearch连接失败
  
  **症状**: `Failed to connect to Elasticsearch`
  
  **解决方案**:
  ```bash
  # 检查ES服务状态
  curl http://localhost:9200
  
  # 检查ES版本
  curl http://localhost:9200 | grep version
  
  # 确认配置
  cat .env | grep ES_
  ```
  
  ### Q3: 服务启动失败
  
  **症状**: `Address already in use` 或端口被占用
  
  **解决方案**:
  ```bash
  # 查看占用端口的进程
  lsof -i :6002  # 后端
  lsof -i :6003  # 前端
7b8d9e1a   tangwang   评估框架的启动脚本
694
  lsof -i :6010  # 搜索评估 UI(eval-web)
38f530ff   tangwang   文档完善
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
  lsof -i :9200  # ES
  
  # 杀掉进程
  kill -9 <PID>
  
  # 或修改端口配置
  ```
  
  ### Q4: 搜索无结果
  
  **症状**: 搜索返回空结果
  
  **解决方案**:
  ```bash
  # 检查ES中是否有数据
  curl http://localhost:9200/search_products/_count
  
  # 检查tenant_id过滤是否正确
  curl -X POST http://localhost:6002/search/ \
    -H "Content-Type: application/json" \
a10a89a3   tangwang   构造测试数据用于测试分类 和 三种...
715
    -H "X-Tenant-ID: 162" \
38f530ff   tangwang   文档完善
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
    -d '{"query": "*", "size": 10, "debug": true}'
  ```
  
  ### Q5: 前端无法连接后端
  
  **症状**: CORS错误
  
  **解决方案**:
  - 确保后端在 http://localhost:6002 运行
  - 检查浏览器控制台错误信息
  - 检查后端日志中的CORS配置
  
  ### Q6: 翻译不工作
  
  **症状**: 翻译返回原文
  
  **解决方案**:
  - 检查DEEPL_AUTH_KEY是否正确
  - 如果没有API key,系统会使用mock模式(返回原文)
  
  ---
  
  ## 相关文档
  
41345271   tangwang   文档更新
740
741
742
743
744
745
746
  - **项目概述与文档索引**: 仓库根目录 `README.md`
  - **搜索 API(分册)**: `docs/搜索API对接指南-00-总览与快速开始.md`(导航);各分册 `docs/搜索API对接指南-01`…`-10-*.md`
  - **搜索 API 速查**: `docs/搜索API对接指南-速查表.md`
  - **开发与规范**: `docs/DEVELOPER_GUIDE.md`
  - **环境与排障**: `docs/QUICKSTART.md`
  - **测试数据**: `scripts/mock_data.sh` 与 `docs/QUICKSTART.md` 中数据/ingest 相关章节
  - **索引字段**: `docs/索引字段说明v2.md`、`mappings/search_products.json`
38f530ff   tangwang   文档完善
747
748
749
  
  ---
  
41345271   tangwang   文档更新
750
751
  **文档版本**: v2.1  
  **最后更新**: 2026-04