修复搜索bug

This commit is contained in:
Miyamizu-MitsuhaSang 2025-10-15 00:30:57 +08:00
parent 340b2a7b6b
commit 54cc21806f
2 changed files with 21 additions and 17 deletions

View File

@ -77,7 +77,8 @@ async def search(request: Request, body: SearchRequest, user=Depends(get_current
# 修改freq # 修改freq
first_word = word_contents[0].word first_word = word_contents[0].word
current_freq = first_word.freq current_freq = first_word.freq
await first_word.update(freq=current_freq + 1) first_word.freq = current_freq + 1
await first_word.save()
pos_seen = set() pos_seen = set()
pos_contents = [] pos_contents = []
@ -111,7 +112,10 @@ async def search(request: Request, body: SearchRequest, user=Depends(get_current
raise HTTPException(status_code=404, detail="Word not found") raise HTTPException(status_code=404, detail="Word not found")
first_def = word_content[0] first_def = word_content[0]
pos_list = await first_def.pos.all() first_word = first_def.word
first_word.freq = first_word.freq + 1
await first_word.save()
pos_list = await first_def.pos
pos_contents = [p.pos_type for p in pos_list] pos_contents = [p.pos_type for p in pos_list]
contents: List[SearchItemJp] = [] contents: List[SearchItemJp] = []

View File

@ -11,7 +11,7 @@ from app.utils.textnorm import normalize_text
from settings import TORTOISE_ORM from settings import TORTOISE_ORM
async def suggest_autocomplete(query: SearchRequest, limit: int = 10) -> List[str]: async def suggest_autocomplete(query: SearchRequest, limit: int = 10) -> List[Tuple[str, str]]:
""" """
:param query: 当前用户输入的内容 :param query: 当前用户输入的内容
@ -60,10 +60,10 @@ async def suggest_autocomplete(query: SearchRequest, limit: int = 10) -> List[st
.get_or_none( .get_or_none(
text=query.query text=query.query
) )
.only("text", "freq") .only("text", "hiragana", "freq")
) )
if exact: if exact:
exact_word = [(exact.text, exact.freq)] exact_word = [(exact.text, exact.hiragana, exact.freq)]
else: else:
exact_word = [] exact_word = []
@ -71,34 +71,34 @@ async def suggest_autocomplete(query: SearchRequest, limit: int = 10) -> List[st
WordlistJp WordlistJp
.filter(Q(hiragana__startswith=query_word) | Q(text__startswith=query.query)) .filter(Q(hiragana__startswith=query_word) | Q(text__startswith=query.query))
.exclude(text=query.query) .exclude(text=query.query)
.only("text", "freq") .only("text", "hiragana", "freq")
) )
prefix_objs = await qs_prefix[:limit] prefix_objs = await qs_prefix[:limit]
prefix: List[Tuple[str, int]] = [(o.text, o.freq) for o in prefix_objs] prefix: List[Tuple[str, str, int]] = [(o.text, o.hiragana, o.freq) for o in prefix_objs]
need = max(0, limit - len(prefix)) need = max(0, limit - len(prefix))
contains: List[Tuple[str, int]] = [] contains: List[Tuple[str, str, int]] = []
if need > 0: if need > 0:
qs_contain = await ( qs_contain = await (
WordlistJp WordlistJp
.filter(Q(hiragana__icontains=query_word) | Q(text__icontains=query.query)) .filter(Q(hiragana__icontains=query_word) | Q(text__icontains=query.query))
.exclude(Q(hiragana__startswith=query_word) | Q(text__startswith=query.query) | Q(text=query.query)) .exclude(Q(hiragana__startswith=query_word) | Q(text__startswith=query.query) | Q(text=query.query))
.only("text", "freq") .only("text", "hiragana", "freq")
.only("text", "freq")
) )
contains_objs = qs_contain[:need * 2] contains_objs = qs_contain[:need * 2]
contains: List[Tuple[str, int]] = [(o.text, o.freq) for o in contains_objs] contains: List[Tuple[str, str, int]] = [(o.text, o.hiragana, o.freq) for o in contains_objs]
seen_text, out = set(), [] seen_text, out = set(), []
for text, freq in list(exact_word) + list(prefix) + list(contains): for text, hiragana, freq in list(exact_word) + list(prefix) + list(contains):
if text not in seen_text: key = (text, hiragana)
seen_text.add(text) if key not in seen_text:
out.append((text, freq)) seen_text.add(key)
out.append((text, hiragana, freq))
if len(out) >= limit: if len(out) >= limit:
break break
out = sorted(out, key=lambda w: (w[1], len(w[0]), w[0])) out = sorted(out, key=lambda w: (-w[2], len(w[0]), w[0]))
return [text for text, _ in out] return [(text, hiragana) for text, hiragana, _ in out]
async def __test(): async def __test():