debug_sort_query.py
2.75 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
#!/usr/bin/env python3
"""
Debug script to check the generated ES query for sorting
"""
import requests
import json
def test_simple_sort():
"""Test simple sort functionality via API"""
print("Testing simple sort via API...")
# Test without sort first
request_no_sort = {
"query": "芭比",
"size": 1
}
print("\n1. Search without sort:")
try:
response = requests.post("http://120.76.41.98:6002/search/", json=request_no_sort)
print(f"Status: {response.status_code}")
if response.ok:
data = response.json()
print(f"Results: {data.get('total', 0)} found in {data.get('took_ms', 0)}ms")
else:
print(f"Error: {response.text}")
except Exception as e:
print(f"Exception: {e}")
# Test with sort
request_with_sort = {
"query": "芭比",
"size": 1,
"sort_by": "create_time",
"sort_order": "desc"
}
print("\n2. Search with sort:")
print(f"Request: {json.dumps(request_with_sort, ensure_ascii=False, indent=2)}")
try:
response = requests.post("http://120.76.41.98:6002/search/", json=request_with_sort)
print(f"Status: {response.status_code}")
if response.ok:
data = response.json()
print(f"Results: {data.get('total', 0)} found in {data.get('took_ms', 0)}ms")
if 'hits' in data and data['hits']:
hit = data['hits'][0]
source = hit.get('_source', {})
print(f"Sample result: {source.get('name', 'N/A')}")
print(f"Create time: {source.get('create_time', 'N/A')}")
else:
print(f"Error: {response.text}")
except Exception as e:
print(f"Exception: {e}")
# Test with different sort field
request_price_sort = {
"query": "芭比",
"size": 1,
"sort_by": "price",
"sort_order": "asc"
}
print("\n3. Search with price sort:")
print(f"Request: {json.dumps(request_price_sort, ensure_ascii=False, indent=2)}")
try:
response = requests.post("http://120.76.41.98:6002/search/", json=request_price_sort)
print(f"Status: {response.status_code}")
if response.ok:
data = response.json()
print(f"Results: {data.get('total', 0)} found in {data.get('took_ms', 0)}ms")
if 'hits' in data and data['hits']:
hit = data['hits'][0]
source = hit.get('_source', {})
print(f"Sample result: {source.get('name', 'N/A')}")
print(f"Price: {source.get('price', 'N/A')}")
else:
print(f"Error: {response.text}")
except Exception as e:
print(f"Exception: {e}")
if __name__ == "__main__":
test_simple_sort()