fix: include matching body sections despite nonempty semantic recall

This commit is contained in:
2026-10-17 20:15:54 +08:00
parent eb8c545090
commit 93ff9c8c8d
3 changed files with 46 additions and 4 deletions
+7 -2
View File
@@ -391,11 +391,16 @@ def retrieve_targets(db, *, course_version_id: int, project_id: int, targets: li
substantive_only=target.get("use") != "structure",
semantic_pool=SEMANTIC_POOL, keyword_pool=KEYWORD_POOL)
hits = [normalize_hit(hit) for hit in (raw_hits or [])]
if target.get("use") == "content" and not hits and navigator is not None:
if target.get("use") == "content" and navigator is not None:
# The contents list may only be used to navigate: its headings are matched
# against headings that really occur in body text, and those sections and
# their neighbouring pages are searched. The quotation still lands on body.
hits = [dict(hit, via="toc_navigation") for hit in (navigator(target) or [])]
# Semantic recall always returns neighbours, even for a short vague
# query. Supplement it with matching body sections rather than waiting
# for zero hits, and keep those sections ahead of incidental matches.
navigated = [normalize_hit(hit) for hit in (navigator(target) or [])]
seen_ids = {hit["chunk_id"] for hit in navigated}
hits = navigated + [hit for hit in hits if hit["chunk_id"] not in seen_ids]
per_target[target["target_id"]] = hits
for hit in hits:
if hit["chunk_id"] not in seen: