Coverage for src/semware/api/search.py: 70%
30 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-09 02:16 -0700
« prev ^ index » next coverage.py v7.10.6, created at 2025-09-09 02:16 -0700
1"""API endpoints for semantic search operations."""
3from fastapi import APIRouter, Depends, HTTPException, status
4from loguru import logger
6from ..models.requests import SearchResponse, SimilaritySearchRequest, TopKSearchRequest
7from ..services.search import search_service
8from .auth import api_key_auth
10router = APIRouter()
13@router.post(
14 "/{table_name}/search/similarity",
15 response_model=SearchResponse,
16 summary="Search by similarity threshold",
17 description="Find all records with similarity score >= threshold. Results are sorted by similarity (descending).",
18)
19async def similarity_search(
20 table_name: str,
21 request: SimilaritySearchRequest,
22 api_key: str = Depends(api_key_auth),
23) -> SearchResponse:
24 """Search for records by similarity threshold."""
25 try:
26 logger.info(f"Similarity search request for table '{table_name}'")
28 result = search_service.similarity_search(table_name, request)
29 return result
31 except ValueError as e:
32 logger.error(f"Similarity search validation error: {e}")
33 raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=str(e))
34 except Exception as e:
35 logger.exception(f"Similarity search failed: {e}")
36 raise HTTPException(
37 status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
38 detail="Similarity search failed",
39 )
42@router.post(
43 "/{table_name}/search/top-k",
44 response_model=SearchResponse,
45 summary="Search for top-k most similar records",
46 description="Find the k most similar records. Results are sorted by similarity (descending).",
47)
48async def top_k_search(
49 table_name: str, request: TopKSearchRequest, api_key: str = Depends(api_key_auth)
50) -> SearchResponse:
51 """Search for top-k most similar records."""
52 try:
53 logger.info(f"Top-k search request for table '{table_name}'")
55 result = search_service.top_k_search(table_name, request)
56 return result
58 except ValueError as e:
59 logger.error(f"Top-k search validation error: {e}")
60 raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=str(e))
61 except Exception as e:
62 logger.exception(f"Top-k search failed: {e}")
63 raise HTTPException(
64 status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
65 detail="Top-k search failed",
66 )