新增功能: - execution_log 模块:自动记录引擎调用耗时、成功/失败、错误详情 - 所有搜索引擎自动跟踪每次调用的性能指标 - CLI 搜索后自动显示执行摘要 - health 命令使用 ParallelSearch 检测所有引擎连通性 - 新增 log 命令查看历史执行日志
110 lines
3.2 KiB
Python
110 lines
3.2 KiB
Python
"""
|
|
Unified multi-source search — URL-based engines.
|
|
|
|
Architecture:
|
|
Primary: URL engines (Baidu, Bing, Google, DuckDuckGo, Sogou)
|
|
No external service dependency.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import time
|
|
from typing import Any, Optional
|
|
|
|
from dataclasses import dataclass
|
|
|
|
from scripts.models import SearchQuery, SearchResponse, SourceRef, SearchMetadata, EngineTiming
|
|
|
|
|
|
@dataclass
|
|
class SearchResult:
|
|
title: str
|
|
url: str
|
|
content: str
|
|
source: str = ""
|
|
engine: str = ""
|
|
score: float = 0.0
|
|
category: str = ""
|
|
published: str = ""
|
|
|
|
|
|
class UnifiedSearch:
|
|
"""Unified search across multiple open-source engines."""
|
|
|
|
def __init__(self):
|
|
self._start_time = 0.0
|
|
|
|
async def search(self, query: str, max_results: int = 5,
|
|
language: str = "zh-CN") -> SearchResponse:
|
|
"""Search using URL-based engines."""
|
|
self._start_time = time.time()
|
|
metadata = SearchMetadata()
|
|
metadata.total_engines = 1
|
|
|
|
all_results, timings = await self._search_urls(query, max_results)
|
|
|
|
elapsed_ms = int((time.time() - self._start_time) * 1000)
|
|
metadata.duration_ms = elapsed_ms
|
|
metadata.engine_timings = timings
|
|
metadata.total_raw_results = len(all_results)
|
|
|
|
sources = []
|
|
seen_urls = set()
|
|
for i, r in enumerate(all_results[:max_results]):
|
|
if r.url and r.url not in seen_urls:
|
|
seen_urls.add(r.url)
|
|
sources.append(SourceRef(
|
|
index=i + 1,
|
|
title=r.title or r.content[:50],
|
|
url=r.url or "",
|
|
source=r.source,
|
|
))
|
|
|
|
metadata.engines_used = list(set(s.source for s in sources))
|
|
|
|
return SearchResponse(
|
|
query=query,
|
|
results=list(all_results[:max_results]),
|
|
sources=sources,
|
|
metadata=metadata,
|
|
)
|
|
|
|
async def _search_urls(self, query: str, max_results: int
|
|
) -> tuple[list[SearchResult], list[EngineTiming]]:
|
|
"""Build search URLs for major engines."""
|
|
timings = []
|
|
engines = {
|
|
"baidu": f"https://www.baidu.com/s?wd={query}",
|
|
"bing": f"https://cn.bing.com/search?q={query}",
|
|
"google": f"https://www.google.com/search?q={query}",
|
|
"duckduckgo": f"https://html.duckduckgo.com/html/?q={query}",
|
|
"sogou": f"https://www.sogou.com/web?query={query}",
|
|
}
|
|
results = []
|
|
for name, url in engines.items():
|
|
results.append(SearchResult(
|
|
title=f"{name}: {query}",
|
|
url=url,
|
|
content=f"Search {query} on {name}",
|
|
source=name,
|
|
))
|
|
|
|
timings.append(EngineTiming(
|
|
engine_name="url_fallback",
|
|
success=True,
|
|
duration_ms=1,
|
|
result_count=len(results),
|
|
))
|
|
return results, timings
|
|
|
|
async def health(self) -> dict:
|
|
"""Check status."""
|
|
return {
|
|
"status": "ok",
|
|
"engines": ["baidu", "bing", "google", "duckduckgo", "sogou"],
|
|
}
|
|
|
|
async def close(self):
|
|
pass
|