api.py
3.78 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
import argparse
import json
import uvicorn
from fastapi import FastAPI, Request
from app.core.config import get_config
from app.core.log import setup_logging
from app.search.models import MerchantQuery, BrandQuery
from app.search.searcher import search_merchants, search_brands
logger = setup_logging('search_api')
_config = get_config()
def _resolve_limit(limit_from_request):
"""如果请求未提供 limit,返回配置默认值"""
if limit_from_request is not None:
return limit_from_request
return _config.getint("search", "default_limit", fallback=10)
app = FastAPI(title="门店全文搜索接口")
@app.post("/ask_data/api/findMerchantIdByFt5")
async def find_merchant_id_by_ft5(query_data: MerchantQuery, request: Request):
limit = _resolve_limit(query_data.limit)
client_host = request.client.host
logger.info(
f"收到 API 请求 - 客户端: {client_host}, "
f"关键词: '{query_data.merchantName}', 限制: {limit}"
)
result = search_merchants(query_data.merchantName, limit)
if "error" in result:
logger.error(f"API 请求失败: {result['error']}")
else:
matched_names = [item['merchant_name'] for item in result.get('data', [])]
logger.info(
f"API 请求成功 - 找到 {result['count']} 条匹配记录: "
f"{', '.join(matched_names)}"
)
return result
@app.post("/ask_data/api/findBrandIdByFt5")
async def find_brand_id_by_ft5(query_data: BrandQuery, request: Request):
limit = _resolve_limit(query_data.limit)
client_host = request.client.host
logger.info(
f"收到品牌搜索 API 请求 - 客户端: {client_host}, "
f"关键词: '{query_data.brandName}', 限制: {limit}"
)
result = search_brands(query_data.brandName, limit)
if "error" in result:
logger.error(f"品牌搜索 API 请求失败: {result['error']}")
else:
matched_names = [item['brand_name'] for item in result.get('data', [])]
logger.info(
f"品牌搜索 API 请求成功 - 找到 {result['count']} 条匹配记录: "
f"{', '.join(matched_names)}"
)
return result
def run_server(host=None, port=None):
config = get_config()
host = host or config.get("api", "host", fallback="0.0.0.0")
port = port or config.getint("api", "port", fallback=8000)
logger.info(f"正在启动 API 服务: http://{host}:{port}")
uvicorn.run(app, host=host, port=port)
def run_cli(query, limit=None):
if limit is None:
limit = _resolve_limit(None)
result = search_merchants(query, limit)
output = json.dumps(result, ensure_ascii=False, indent=2)
logger.info(f"命令行搜索结果:\n{output}")
print(output)
if __name__ == "__main__":
default_limit = _resolve_limit(None)
parser = argparse.ArgumentParser(description="门店全文搜索工具")
parser.add_argument("--query", help="要搜索的门店名称关键词")
parser.add_argument("--limit", type=int, default=default_limit, help="返回结果数量限制")
parser.add_argument("--server", action="store_true", help="启动 FastAPI 服务器")
parser.add_argument("--host", type=str, help="服务器监听地址")
parser.add_argument("--port", type=int, help="服务器端口")
args = parser.parse_args()
if args.server:
run_server(host=args.host, port=args.port)
elif args.query:
run_cli(args.query, args.limit)
else:
test_queries = ["岚图深圳南山塘朗门店"]
logger.info("=== 门店全文搜索测试演示 ===")
for q in test_queries:
result = search_merchants(q, limit=default_limit)
logger.info(
f"查询关键词: '{q}', "
f"结果: {json.dumps(result, ensure_ascii=False)}"
)