fix(analytics): 词库单字MySQL预览拒扫+IntegrityError 409+MATCH短语净化

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
浅念
2026-09-04 19:48:30 +08:00
parent 0c14be0777
commit c7b3ce9d1c
3 changed files with 55 additions and 5 deletions
+12 -2
View File
@@ -3,6 +3,7 @@ from __future__ import annotations
from typing import Annotated, Any, Literal
import sqlalchemy as sa
from fastapi import APIRouter, Depends, HTTPException, Query
from pydantic import BaseModel
from sqlalchemy.ext.asyncio import AsyncSession
@@ -107,12 +108,21 @@ async def create_word(session: SessionDep, body: WordIn) -> dict:
return {"id": w.id, "word": w.word, "enabled": w.enabled}
except ValueError as exc:
raise HTTPException(409, str(exc)) from exc
except sa.exc.IntegrityError as exc:
# 并发撞 word UNIQUE(两请求同词,预检都未见)→ 409,不裸 500。
raise HTTPException(409, "词已存在") from exc
@router.post("/sensitive/words/preview")
async def preview_word(session: SessionDep, body: WordPreviewIn) -> dict:
"""加词前命中量预览(count 为 MySQL 上限语义;warn=超阈值建议收窄)。"""
n = await words_svc.preview_count(session, body.word, body.exclude_words)
"""加词前命中量预览(count 为 MySQL 上限语义;warn=超阈值建议收窄)。
MySQL 单字词拒扫(400)——LIKE 前导通配全扫 ~16s,加词后从命中列表观察。
"""
try:
n = await words_svc.preview_count(session, body.word, body.exclude_words)
except ValueError as exc:
raise HTTPException(400, str(exc)) from exc
return {"count": n, "warn": n >= words_svc.PREVIEW_WARN_THRESHOLD}