后端: - alembic 0009: 两张固化表 + GIN prefix_keys 索引 + articles trigger - /api/v1/search/suggestions: 混合 A(高频词 ts_stat) + B(真实标题) + 冷启动 fallback - worker 每日 03:00 + 启动时刷新 search_keywords - 顺便填 commit 11 TODO: articles.title_zh_tsv + GIN 索引(未来 FTS 基础) 前端: - NInput -> NAutoComplete + debounce 250ms - 选标题 -> 跳详情;选关键词 -> 填入 + 触发搜索 - AbortController 防 race condition 性能: prefix_keys @> ARRAY[prefix] 走 GIN 亚毫秒,100w 行也稳
46 lines
1.7 KiB
Python
46 lines
1.7 KiB
Python
"""搜索建议候选词表(固化,worker 每日 ts_stat 刷新)。
|
|
|
|
- 数据源:articles.title_zh + body_zh_text + commentary + commentary_meituan
|
|
- 用途:/api/v1/search/suggestions 返回"高频词"建议(A 方案)
|
|
- 刷新:每日凌晨 worker 调 refresh_search_keywords() 全量重建
|
|
- 查询:prefix_keys @> ARRAY['美'] 走 GIN 索引(亚毫秒)
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
|
|
from sqlalchemy import BigInteger, DateTime, Integer, String, Text, func
|
|
from sqlalchemy.dialects.postgresql import ARRAY
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
|
|
from app.database import Base
|
|
|
|
|
|
class SearchKeyword(Base):
|
|
__tablename__ = "search_keywords"
|
|
|
|
id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
|
|
|
|
keyword: Mapped[str] = mapped_column(Text, nullable=False)
|
|
# ts_stat / title_extract / manual
|
|
source: Mapped[str] = mapped_column(String(32), nullable=False)
|
|
# 词频或文章数(权重,排序用)
|
|
weight: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
|
|
# 预计算前缀数组,['美','美联储','美联储宣'] for '美联储宣布...'
|
|
prefix_keys: Mapped[list[str]] = mapped_column(ARRAY(Text), nullable=False)
|
|
|
|
last_seen_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), server_default=func.now(), nullable=False
|
|
)
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), server_default=func.now(), nullable=False
|
|
)
|
|
|
|
__table_args__ = (
|
|
# db-level unique 留给 alembic 迁移创建(__table_args__ 只是 ORM 侧参考)
|
|
# 实际 UNIQUE 约束在 0009 迁移里建
|
|
)
|
|
|
|
def __repr__(self) -> str:
|
|
return f"<SearchKeyword {self.keyword!r} src={self.source} weight={self.weight}>"
|