Files
diary-news/backend/app/models/search_keyword.py

46 lines
1.7 KiB
Python
Raw Normal View History

"""搜索建议候选词表(固化,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}>"