115047ee
tangwang
为一个租户灌入测试数据;实例的启动...
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
# 使用指南 - SearchEngine
## 快速启动(推荐)
### 一键启动所有服务
```bash
cd /data/tw/SearchEngine
./start_all.sh
```
这个脚本会自动完成:
1. 设置conda环境
2. 检查并导入测试数据(如果需要)
3. 启动后端API服务(后台运行)
4. 启动前端Web界面
启动完成后,访问:
|
2a76641e
tangwang
config
|
19
20
21
|
- **前端界面**: http://localhost:6003
- **后端API**: http://localhost:6002
- **API文档**: http://localhost:6002/docs
|
115047ee
tangwang
为一个租户灌入测试数据;实例的启动...
|
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
|
### 停止服务
```bash
# 停止后端
kill $(cat logs/backend.pid)
# 前端按 Ctrl+C
```
---
## 分步启动(自定义)
### 1. 环境设置
```bash
cd /data/tw/SearchEngine
./setup.sh
```
这会:
- 创建/激活conda环境 `searchengine`
- 加载配置文件
- 检查Elasticsearch连接
### 2. 数据导入
#### 快速测试(1000条,不生成embedding)
```bash
./scripts/ingest.sh 1000 true
```
#### 完整导入(10000条,包含embedding)
```bash
./scripts/ingest.sh 10000 false
```
**注意**: 首次运行会下载模型文件(BGE-M3和CN-CLIP),大约需要10-30分钟。
### 3. 启动后端
```bash
./scripts/start_backend.sh
```
|
2a76641e
tangwang
config
|
68
|
后端API会在 http://localhost:6002 启动
|
115047ee
tangwang
为一个租户灌入测试数据;实例的启动...
|
69
70
71
72
73
74
75
|
### 4. 启动前端
```bash
./scripts/start_frontend.sh
```
|
2a76641e
tangwang
config
|
76
|
前端界面会在 http://localhost:6003 启动
|
115047ee
tangwang
为一个租户灌入测试数据;实例的启动...
|
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
|
---
## 配置说明
### 环境配置文件 (.env)
```bash
# Elasticsearch配置
ES_HOST=http://localhost:9200
ES_USERNAME=essa
ES_PASSWORD=4hOaLaf41y2VuI8y
# Redis配置(可选,用于缓存)
REDIS_HOST=localhost
REDIS_PORT=6479
REDIS_PASSWORD=BMfv5aI31kgHWtlx
# DeepL翻译API
DEEPL_AUTH_KEY=c9293ab4-ad25-479b-919f-ab4e63b429ed
# 客户配置
CUSTOMER_ID=customer1
# API服务配置
API_HOST=0.0.0.0
|
2a76641e
tangwang
config
|
103
|
API_PORT=6002
|
115047ee
tangwang
为一个租户灌入测试数据;实例的启动...
|
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
|
```
### 修改配置
1. 编辑 `.env` 文件
2. 重启相关服务
---
## 使用Web界面
### 搜索功能
1. **简单搜索**: 直接输入关键词
- 中文: "芭比娃娃"
- 英文: "fire control set"
- 俄文: "Наборы для пожаротушения"
2. **布尔搜索**: 使用操作符
- AND: "toy AND barbie"
- OR: "barbie OR doll"
- ANDNOT: "toy ANDNOT cheap"
- 组合: "toy AND (barbie OR doll) ANDNOT cheap"
3. **域搜索**: 指定搜索域
- 品牌: "brand:ZHU LIN"
- 类别: "category:玩具"
### 搜索选项
- **启用翻译**: 自动翻译查询到其他语言
- **启用语义搜索**: 使用embedding进行语义匹配
- **启用自定义排序**: 使用配置的ranking表达式
- **结果数量**: 10/20/50条
---
## API使用
### 搜索接口
```bash
|
2a76641e
tangwang
config
|
146
|
curl -X POST http://localhost:6002/search/ \
|
115047ee
tangwang
为一个租户灌入测试数据;实例的启动...
|
147
148
149
150
151
152
153
154
155
156
157
158
|
-H "Content-Type: application/json" \
-d '{
"query": "芭比娃娃",
"size": 10,
"enable_translation": true,
"enable_embedding": true
}'
```
### 图片搜索
```bash
|
2a76641e
tangwang
config
|
159
|
curl -X POST http://localhost:6002/search/image \
|
115047ee
tangwang
为一个租户灌入测试数据;实例的启动...
|
160
161
162
163
164
165
166
167
168
169
|
-H "Content-Type: application/json" \
-d '{
"image_url": "https://oss.essa.cn/example.jpg",
"size": 10
}'
```
### 健康检查
```bash
|
2a76641e
tangwang
config
|
170
|
curl http://localhost:6002/admin/health
|
115047ee
tangwang
为一个租户灌入测试数据;实例的启动...
|
171
172
173
174
175
|
```
### 查看配置
```bash
|
2a76641e
tangwang
config
|
176
|
curl http://localhost:6002/admin/config
|
115047ee
tangwang
为一个租户灌入测试数据;实例的启动...
|
177
178
179
180
181
|
```
### 索引统计
```bash
|
2a76641e
tangwang
config
|
182
|
curl http://localhost:6002/admin/stats
|
115047ee
tangwang
为一个租户灌入测试数据;实例的启动...
|
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
|
```
---
## 常见问题
### 1. Elasticsearch连接失败
**问题**: `Failed to connect to Elasticsearch`
**解决**:
```bash
# 检查ES是否运行
curl http://localhost:9200
# 检查配置
cat .env | grep ES_
```
### 2. 导入数据时内存不足
**问题**: `Out of memory`
**解决**:
```bash
# 减少batch size或跳过embedding
./scripts/ingest.sh 1000 true
```
### 3. 模型下载失败
**问题**: 模型文件下载超时
**解决**:
- 检查网络连接
- 使用国内镜像源
- 手动下载模型到指定目录
### 4. 翻译不工作
**问题**: 翻译返回原文
**解决**:
- 检查DEEPL_AUTH_KEY是否正确
- 如果没有API key,系统会使用mock模式(返回原文)
### 5. 前端无法连接后端
**问题**: CORS错误
**解决**:
|
2a76641e
tangwang
config
|
234
|
- 确保后端在 http://localhost:6002 运行
|
115047ee
tangwang
为一个租户灌入测试数据;实例的启动...
|
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
- 检查浏览器控制台错误信息
---
## 开发和调试
### 查看日志
```bash
# 后端日志
tail -f logs/backend.log
# 实时日志(如果前台运行)
./scripts/start_backend.sh
```
### Python命令行测试
```bash
# 激活环境
source /home/tw/miniconda3/etc/profile.d/conda.sh
conda activate searchengine
# 测试搜索
python -c "
from config import ConfigLoader
from utils import ESClient
from search import Searcher
from config.env_config import get_es_config
config_loader = ConfigLoader('config/schema')
config = config_loader.load_customer_config('customer1')
es_config = get_es_config()
es_client = ESClient(hosts=[es_config['host']],
username=es_config.get('username'),
password=es_config.get('password'))
searcher = Searcher(config, es_client)
result = searcher.search('芭比娃娃', size=5)
print(f'找到 {result.total} 个结果')
for hit in result.hits:
print(f' - {hit[\"_source\"][\"name\"]} (分数: {hit[\"_score\"]:.4f})')
"
```
### 重新导入数据
```bash
# 删除现有索引并重新导入
./scripts/ingest.sh 1000 true
```
---
## 性能优化
### 1. 使用embedding缓存
首次生成embedding后会自动缓存到 `.cache/` 目录,后续导入会更快。
### 2. 批量大小调整
```bash
# 修改批量大小(在ingest_customer1.py中)
--batch-size 200 # 默认100
```
### 3. GPU加速
确保CUDA可用以加速embedding生成:
```bash
python -c "import torch; print(f'CUDA available: {torch.cuda.is_available()}')"
```
---
## 项目结构
```
SearchEngine/
├── .env # 环境配置
├── setup.sh # 环境设置脚本
├── start_all.sh # 一键启动脚本
├── scripts/ # 运行脚本
│ ├── ingest.sh # 数据导入
│ ├── start_backend.sh # 启动后端
│ └── start_frontend.sh # 启动前端
├── frontend/ # Web前端
│ ├── index.html
│ └── static/
├── logs/ # 日志文件
├── config/ # 配置模块
├── indexer/ # 数据导入
├── query/ # 查询处理
├── search/ # 搜索引擎
├── embeddings/ # 向量模型
└── api/ # REST API
```
---
## 支持
遇到问题请查看:
- **日志**: `logs/backend.log`
|
2a76641e
tangwang
config
|
342
|
- **API文档**: http://localhost:6002/docs
|
115047ee
tangwang
为一个租户灌入测试数据;实例的启动...
|
343
|
- **配置**: `config/schema/customer1_config.yaml`
|