kangda-robot-backend/ruoyi-fastapi-backend/test_search_service.py

84 lines
2.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
搜索服务测试脚本
"""
import asyncio
import sys
import os
# 添加项目路径到Python路径
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from module_admin.service.search_service import SearchService, SearchServiceError
async def test_search_service():
"""测试搜索服务"""
print("=" * 60)
print("搜索服务测试")
print("=" * 60)
# 测试用例
test_queries = [
"北京今天天气怎么样",
"最新的人工智能新闻",
"搜索Python编程",
]
for query in test_queries:
print(f"\n{'='*60}")
print(f"测试查询: {query}")
print(f"{'='*60}")
# 检查是否应该处理
should_handle = SearchService.should_handle(query)
print(f"✓ 关键词检测: {'通过' if should_handle else '未通过'}")
if should_handle:
try:
# 获取搜索结果不使用Redis缓存
result = await SearchService.get_search_answer(query, redis=None)
print(f"\n✓ 搜索成功!")
print(f"\n类型: {result.get('type')}")
print(f"查询: {result.get('query')}")
# 显示答案框(如果有)
answer_box = result.get('answer_box', {})
if answer_box:
answer = answer_box.get('answer') or answer_box.get('snippet', '')
if answer:
print(f"\n【快速答案】\n{answer}")
# 显示搜索结果
results = result.get('results', [])
if results:
print(f"\n【搜索结果】({len(results)}条)")
for idx, item in enumerate(results[:3], 1): # 只显示前3条
print(f"\n{idx}. {item.get('title', 'N/A')}")
print(f" 链接: {item.get('link', 'N/A')}")
snippet = item.get('snippet', '')
if snippet:
print(f" 摘要: {snippet[:100]}...")
# 显示格式化消息
print(f"\n【格式化消息】")
message = result.get('message', '')
# 只显示前500个字符
print(message[:500] + ('...' if len(message) > 500 else ''))
except SearchServiceError as e:
print(f"\n✗ 搜索失败: {e}")
except Exception as e:
print(f"\n✗ 未知错误: {type(e).__name__}: {e}")
print()
print("=" * 60)
print("测试完成")
print("=" * 60)
if __name__ == "__main__":
asyncio.run(test_search_service())