debug_sort_query.py 2.76 KB
#!/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": "min_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()