Mix strong caption text matches into semantic scene candidates

This commit is contained in:
Sleeps
2026-10-08 10:30:32 +01:00
parent 262cec6fec
commit 5b7f82d89f
+52
View File
@@ -16,6 +16,7 @@ class SearchService:
MAX_SEARCH_CANDIDATES = 500
SCENES_PER_ITEM = 5
PRIORITY_CAPTION_ITEMS = 2
def __init__(
self,
@@ -123,6 +124,7 @@ class SearchService:
"title": hit.get("title"),
"timestamp_ms": int(hit.get("timestamp_ms") or 0),
"caption": hit.get("caption"),
"caption_text_match": bool(hit.get("_caption_text_match")),
"caption_model": hit.get("caption_model"),
"caption_route": hit.get("caption_route"),
"caption_window_id": hit.get("caption_window_id"),
@@ -160,6 +162,7 @@ class SearchService:
"tags": scene.get("tags") or [],
"best_timestamp_ms": scene.get("timestamp_ms"),
"best_caption": scene.get("caption"),
"caption_text_match": scene.get("caption_text_match", False),
"ranking_score": scene.get("ranking_score"),
"siglip_score": scene.get("siglip_score"),
"scene_matches": 1,
@@ -237,6 +240,54 @@ class SearchService:
self.MAX_SEARCH_CANDIDATES,
)
# Caption windows are indexed on their representative frame.
# With millions of mostly visual-only frames, hybrid vector
# retrieval can bury a literal caption match below the candidate
# cutoff. Fetch exact text matches in the caption field separately
# and reserve just two *distinct items* for them, preserving most
# of the normal semantic/topical search order.
caption_hits: list[dict[str, Any]] = []
try:
caption_results = self.meili.search_caption_text(
query,
min(max(item_limit * 4, 20), 100),
filter_expression=filter_expression,
)
caption_hits = list(caption_results.get("hits") or [])
except Exception as exc:
# Old/incompatible Meili instances should still serve the
# existing semantic path instead of failing every search.
print(f"WARN caption-only search unavailable: {exc}")
priority_hits: list[dict[str, Any]] = []
priority_items: set[str] = set()
for hit in caption_hits:
item_id = hit.get("jellyfin_item_id")
if (
not item_id
or not hit.get("id")
or not str(hit.get("caption") or "").strip()
or str(item_id) in priority_items
):
continue
priority_items.add(str(item_id))
priority_hits.append({**hit, "_caption_text_match": True})
if len(priority_hits) >= min(
self.PRIORITY_CAPTION_ITEMS, item_limit
):
break
priority_frame_ids = {
str(hit["id"]) for hit in priority_hits
}
hits = (
priority_hits
+ [
hit for hit in hits
if str(hit.get("id")) not in priority_frame_ids
]
)[:self.MAX_SEARCH_CANDIDATES]
frame_ids = [
str(hit["id"])
for hit in hits
@@ -267,6 +318,7 @@ class SearchService:
return {
"query": query,
"semantic_ratio": self.semantic_ratio,
"caption_text_item_count": len(priority_hits),
"scene_count": len(display_scenes),
"candidate_scene_count": len(scenes),
"item_count": len(items),