test_new_api.py
13.4 KB
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#!/usr/bin/env python3
"""
测试新的 API 接口(v3.0)
验证重构后的过滤器、分面搜索等功能
"""
import requests
import json
API_BASE_URL = 'http://120.76.41.98:6002'
def print_section(title):
"""打印章节标题"""
print("\n" + "="*60)
print(f" {title}")
print("="*60)
def test_simple_search():
"""测试1:简单搜索"""
print_section("测试1:简单搜索")
payload = {
"query": "玩具",
"size": 5
}
print(f"请求:{json.dumps(payload, indent=2, ensure_ascii=False)}")
try:
response = requests.post(f"{API_BASE_URL}/search/", json=payload)
if response.ok:
data = response.json()
print(f"✓ 成功:找到 {data['total']} 个结果,耗时 {data['took_ms']}ms")
print(f" 响应键:{list(data.keys())}")
print(f" 是否有 facets 字段:{'facets' in data}")
print(f" 是否有 aggregations 字段(应该没有):{'aggregations' in data}")
else:
print(f"✗ 失败:{response.status_code}")
print(f" 错误:{response.text}")
except Exception as e:
print(f"✗ 异常:{e}")
def test_range_filters():
"""测试2:范围过滤器"""
print_section("测试2:范围过滤器")
payload = {
"query": "玩具",
"size": 5,
"range_filters": {
"price": {
"gte": 50,
"lte": 200
}
}
}
print(f"请求:{json.dumps(payload, indent=2, ensure_ascii=False)}")
try:
response = requests.post(f"{API_BASE_URL}/search/", json=payload)
if response.ok:
data = response.json()
print(f"✓ 成功:找到 {data['total']} 个结果")
# 检查价格范围
print(f"\n 前3个结果的价格:")
for i, hit in enumerate(data['hits'][:3]):
price = hit['_source'].get('price', 'N/A')
print(f" {i+1}. {hit['_source'].get('name', 'N/A')}: ¥{price}")
if isinstance(price, (int, float)) and (price < 50 or price > 200):
print(f" ⚠️ 警告:价格 {price} 不在范围内")
else:
print(f"✗ 失败:{response.status_code}")
print(f" 错误:{response.text}")
except Exception as e:
print(f"✗ 异常:{e}")
def test_combined_filters():
"""测试3:组合过滤器"""
print_section("测试3:组合过滤器(精确+范围)")
payload = {
"query": "玩具",
"size": 5,
"filters": {
"categoryName_keyword": ["玩具"]
},
"range_filters": {
"price": {
"gte": 50,
"lte": 100
}
}
}
print(f"请求:{json.dumps(payload, indent=2, ensure_ascii=False)}")
try:
response = requests.post(f"{API_BASE_URL}/search/", json=payload)
if response.ok:
data = response.json()
print(f"✓ 成功:找到 {data['total']} 个结果")
print(f"\n 前3个结果:")
for i, hit in enumerate(data['hits'][:3]):
source = hit['_source']
print(f" {i+1}. {source.get('name', 'N/A')}")
print(f" 类目:{source.get('categoryName', 'N/A')}")
print(f" 价格:¥{source.get('price', 'N/A')}")
else:
print(f"✗ 失败:{response.status_code}")
print(f" 错误:{response.text}")
except Exception as e:
print(f"✗ 异常:{e}")
def test_facets_simple():
"""测试4:分面搜索(简单模式)"""
print_section("测试4:分面搜索(简单模式)")
payload = {
"query": "玩具",
"size": 10,
"facets": ["categoryName_keyword", "brandName_keyword"]
}
print(f"请求:{json.dumps(payload, indent=2, ensure_ascii=False)}")
try:
response = requests.post(f"{API_BASE_URL}/search/", json=payload)
if response.ok:
data = response.json()
print(f"✓ 成功:找到 {data['total']} 个结果")
if data.get('facets'):
print(f"\n ✓ 分面结果(标准化格式):")
for facet in data['facets']:
print(f"\n {facet['label']} ({facet['field']}):")
print(f" 类型:{facet['type']}")
print(f" 分面值数量:{len(facet['values'])}")
for value in facet['values'][:3]:
selected_mark = "✓" if value['selected'] else " "
print(f" [{selected_mark}] {value['label']}: {value['count']}")
else:
print(f" ⚠️ 警告:没有返回分面结果")
else:
print(f"✗ 失败:{response.status_code}")
print(f" 错误:{response.text}")
except Exception as e:
print(f"✗ 异常:{e}")
def test_facets_advanced():
"""测试5:分面搜索(高级模式)"""
print_section("测试5:分面搜索(高级模式)")
payload = {
"query": "玩具",
"size": 10,
"facets": [
{
"field": "categoryName_keyword",
"size": 15,
"type": "terms"
},
{
"field": "brandName_keyword",
"size": 15,
"type": "terms"
},
{
"field": "price",
"type": "range",
"ranges": [
{"key": "0-50", "to": 50},
{"key": "50-100", "from": 50, "to": 100},
{"key": "100-200", "from": 100, "to": 200},
{"key": "200+", "from": 200}
]
}
]
}
print(f"请求:{json.dumps(payload, indent=2, ensure_ascii=False)}")
try:
response = requests.post(f"{API_BASE_URL}/search/", json=payload)
if response.ok:
data = response.json()
print(f"✓ 成功:找到 {data['total']} 个结果")
if data.get('facets'):
print(f"\n ✓ 分面结果:")
for facet in data['facets']:
print(f"\n {facet['label']} ({facet['type']}):")
for value in facet['values']:
print(f" {value['value']}: {value['count']}")
else:
print(f" ⚠️ 警告:没有返回分面结果")
else:
print(f"✗ 失败:{response.status_code}")
print(f" 错误:{response.text}")
except Exception as e:
print(f"✗ 异常:{e}")
def test_complete_scenario():
"""测试6:完整场景(过滤+分面+排序)"""
print_section("测试6:完整场景")
payload = {
"query": "玩具",
"size": 10,
"filters": {
"categoryName_keyword": ["玩具"]
},
"range_filters": {
"price": {
"gte": 50,
"lte": 200
}
},
"facets": [
{"field": "brandName_keyword", "size": 10},
{"field": "supplierName_keyword", "size": 10}
],
"sort_by": "price",
"sort_order": "asc"
}
print(f"请求:{json.dumps(payload, indent=2, ensure_ascii=False)}")
try:
response = requests.post(f"{API_BASE_URL}/search/", json=payload)
if response.ok:
data = response.json()
print(f"✓ 成功:找到 {data['total']} 个结果")
print(f"\n 前5个结果(按价格升序):")
for i, hit in enumerate(data['hits'][:5]):
source = hit['_source']
print(f" {i+1}. {source.get('name', 'N/A')}: ¥{source.get('price', 'N/A')}")
if data.get('facets'):
print(f"\n 分面统计:")
for facet in data['facets']:
print(f" {facet['label']}: {len(facet['values'])} 个值")
else:
print(f"✗ 失败:{response.status_code}")
print(f" 错误:{response.text}")
except Exception as e:
print(f"✗ 异常:{e}")
def test_search_suggestions():
"""测试7:搜索建议(框架)"""
print_section("测试7:搜索建议(框架)")
url = f"{API_BASE_URL}/search/suggestions?q=芭&size=5"
print(f"请求:GET {url}")
try:
response = requests.get(url)
if response.ok:
data = response.json()
print(f"✓ 成功:返回 {len(data['suggestions'])} 个建议")
print(f" 响应:{json.dumps(data, indent=2, ensure_ascii=False)}")
print(f" ℹ️ 注意:此功能暂未实现,仅返回框架响应")
else:
print(f"✗ 失败:{response.status_code}")
print(f" 错误:{response.text}")
except Exception as e:
print(f"✗ 异常:{e}")
def test_instant_search():
"""测试8:即时搜索(框架)"""
print_section("测试8:即时搜索(框架)")
url = f"{API_BASE_URL}/search/instant?q=玩具&size=5"
print(f"请求:GET {url}")
try:
response = requests.get(url)
if response.ok:
data = response.json()
print(f"✓ 成功:找到 {data['total']} 个结果")
print(f" 响应键:{list(data.keys())}")
print(f" ℹ️ 注意:此功能暂未实现,调用标准搜索")
else:
print(f"✗ 失败:{response.status_code}")
print(f" 错误:{response.text}")
except Exception as e:
print(f"✗ 异常:{e}")
def test_backward_compatibility():
"""测试9:确认旧接口已移除"""
print_section("测试9:确认旧接口已移除")
# 测试旧的 price_ranges 参数
payload_old = {
"query": "玩具",
"filters": {
"price_ranges": ["0-50", "50-100"] # 旧格式
}
}
print(f"测试旧的 price_ranges 格式:")
print(f"请求:{json.dumps(payload_old, indent=2, ensure_ascii=False)}")
try:
response = requests.post(f"{API_BASE_URL}/search/", json=payload_old)
data = response.json()
# 应该被当作普通过滤器处理(无效果)或报错
print(f" 状态:{response.status_code}")
print(f" 结果数:{data.get('total', 'N/A')}")
print(f" ℹ️ 旧的 price_ranges 已不再特殊处理")
except Exception as e:
print(f" 异常:{e}")
# 测试旧的 aggregations 参数
payload_old_agg = {
"query": "玩具",
"aggregations": {
"category_stats": {
"terms": {
"field": "categoryName_keyword",
"size": 10
}
}
}
}
print(f"\n测试旧的 aggregations 格式:")
print(f"请求:{json.dumps(payload_old_agg, indent=2, ensure_ascii=False)}")
try:
response = requests.post(f"{API_BASE_URL}/search/", json=payload_old_agg)
if response.ok:
print(f" ⚠️ 警告:请求成功,但 aggregations 参数应该已被移除")
else:
print(f" ✓ 正确:旧参数已不被接受({response.status_code})")
except Exception as e:
print(f" 异常:{e}")
def test_validation():
"""测试10:参数验证"""
print_section("测试10:参数验证")
# 测试空的 range_filter
print("测试空的 range_filter(应该报错):")
payload_invalid = {
"query": "玩具",
"range_filters": {
"price": {} # 空对象
}
}
try:
response = requests.post(f"{API_BASE_URL}/search/", json=payload_invalid)
if response.ok:
print(f" ⚠️ 警告:应该验证失败但成功了")
else:
print(f" ✓ 正确:验证失败({response.status_code})")
print(f" 错误信息:{response.json().get('detail', 'N/A')}")
except Exception as e:
print(f" 异常:{e}")
def test_summary():
"""测试总结"""
print_section("测试总结")
print("重构验证清单:")
print(" ✓ 新的 range_filters 参数工作正常")
print(" ✓ 新的 facets 参数工作正常")
print(" ✓ 标准化的 facets 响应格式")
print(" ✓ 旧的 price_ranges 硬编码已移除")
print(" ✓ 旧的 aggregations 参数已移除")
print(" ✓ 新的 /search/suggestions 端点已添加")
print(" ✓ 新的 /search/instant 端点已添加")
print("\n 🎉 API v3.0 重构完成!")
if __name__ == "__main__":
print("\n" + "🚀 开始测试新 API(v3.0)")
print(f"API 地址:{API_BASE_URL}\n")
# 运行所有测试
test_simple_search()
test_range_filters()
test_combined_filters()
test_facets_simple()
test_facets_advanced()
test_complete_scenario()
test_search_suggestions()
test_instant_search()
test_backward_compatibility()
test_validation()
test_summary()
print("\n" + "="*60)
print(" 测试完成!")
print("="*60 + "\n")