产品决定:搜索建议只展示 ts_stat 高频词续接(如'美'→美国/美军/美国政府), 不要真实文章 id 提示(用户认为这种'文章#566871'是噪音,没连续性)。 改动: - SearchSuggestionsResponse 去 title,只剩 query + keywords - SearchService 只查 search_keywords,fallback 路径也只针对 keywords - Feed.vue: 删掉 suggestTitles 状态 + SuggestTitleOption 类型联合, renderSuggestion 简化成 '词' 标签 + 词文本 + 右侧 weight 数字 - 0011 迁移: 删 search_title_suggestions 表 + 3 索引 + trigger + 函数 (trigger 在每篇文章 INSERT/UPDATE 都会跑,删了能省掉无用性能损耗) - 删除: app/models/search_title_suggestion.py + backfill_search_suggestions.py 替换成: app/scripts/refresh_search_keywords.py(只跑一次词频刷新)
43 lines
1.6 KiB
Python
43 lines
1.6 KiB
Python
"""/api/v1/search/* — 搜索建议(autocomplete,纯 keyword 续接词)。
|
|
|
|
- GET /api/v1/search/suggestions?q=prefix
|
|
返回:{"query", "keywords": [...]}
|
|
- keywords: 词频续接词(按 weight DESC),输入"美国"→ ["美国", "美国政府", "美国签证", ...]
|
|
- 冷启动:search_keywords 表空时自动 fallback 到实时 ts_stat
|
|
- 鉴权:跟 articles 一致(需要登录)
|
|
|
|
性能:prefix_keys @> ARRAY['美'] 走 GIN 数组索引,亚毫秒。
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from fastapi import APIRouter, Depends, Query
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core.deps import get_current_user
|
|
from app.database import get_session
|
|
from app.models.user import User
|
|
from app.schemas.search import SearchKeywordItem, SearchSuggestionsResponse
|
|
from app.services.search import SearchService
|
|
|
|
router = APIRouter(prefix="/search", tags=["search"])
|
|
|
|
|
|
@router.get("/suggestions", response_model=SearchSuggestionsResponse)
|
|
async def get_suggestions(
|
|
q: str = Query(..., min_length=1, max_length=20, description="搜索前缀"),
|
|
limit: int = Query(10, ge=1, le=20, description="最多返回多少"),
|
|
_user: User = Depends(get_current_user), # 需要登录,跟 articles 一致
|
|
session: AsyncSession = Depends(get_session),
|
|
):
|
|
"""搜索建议:输入 prefix,返回高频词续接。
|
|
|
|
用法:前端搜索框 onChange 时调用,debounce 250ms。
|
|
选词 → 自动填入 q + 触发搜索。
|
|
"""
|
|
svc = SearchService(session)
|
|
raw = await svc.suggestions(q=q, limit=limit)
|
|
return SearchSuggestionsResponse(
|
|
query=raw["query"],
|
|
keywords=[SearchKeywordItem(**k) for k in raw["keywords"]],
|
|
)
|