Group search results by Jellyfin item

This commit is contained in:
Sleeps
2026-10-07 17:02:24 +01:00
parent 092112e132
commit b03a006436
+55 -17
View File
@@ -14,6 +14,9 @@ from .meili import Meili
class SearchService: class SearchService:
"""Shared hybrid search implementation for CLI and HTTP consumers.""" """Shared hybrid search implementation for CLI and HTTP consumers."""
MAX_SEARCH_CANDIDATES = 500
SCENES_PER_ITEM = 5
def __init__( def __init__(
self, self,
*, *,
@@ -160,9 +163,13 @@ class SearchService:
"ranking_score": scene.get("ranking_score"), "ranking_score": scene.get("ranking_score"),
"siglip_score": scene.get("siglip_score"), "siglip_score": scene.get("siglip_score"),
"scene_matches": 1, "scene_matches": 1,
"matching_scenes": [scene],
} }
else: else:
existing["scene_matches"] = int(existing["scene_matches"]) + 1 existing["scene_matches"] = int(existing["scene_matches"]) + 1
matching_scenes = existing["matching_scenes"]
if len(matching_scenes) < SearchService.SCENES_PER_ITEM:
matching_scenes.append(scene)
return list(grouped.values())[:item_limit] return list(grouped.values())[:item_limit]
@@ -186,18 +193,49 @@ class SearchService:
embedder = self.get_embedder() embedder = self.get_embedder()
text_vector = embedder.embed_text(query) text_vector = embedder.embed_text(query)
result = self.meili.search_vector(
text_vector,
query,
scene_limit,
self.semantic_ratio,
filter_expression = ( filter_expression = (
'source_kind = "jellyfin"' 'source_kind = "jellyfin"'
if jellyfin_only if jellyfin_only
else None else None
), )
# Jellyfin consumes item-level results, while Meilisearch ranks scene
# frames. A single film can occupy many of the best scene hits, so a
# fixed scene limit can collapse to only a handful of unique items.
# Expand the candidate pool until we have enough unique Jellyfin
# items, Meilisearch is exhausted, or a bounded safety ceiling is hit.
candidate_limit = min(
max(scene_limit, item_limit * 2),
self.MAX_SEARCH_CANDIDATES,
)
hits: list[dict[str, Any]] = []
while True:
result = self.meili.search_vector(
text_vector,
query,
candidate_limit,
self.semantic_ratio,
filter_expression=filter_expression,
) )
hits = list(result.get("hits") or []) hits = list(result.get("hits") or [])
unique_item_ids = {
str(hit["jellyfin_item_id"])
for hit in hits
if hit.get("jellyfin_item_id")
}
if (
len(unique_item_ids) >= item_limit
or len(hits) < candidate_limit
or candidate_limit >= self.MAX_SEARCH_CANDIDATES
):
break
candidate_limit = min(
candidate_limit * 2,
self.MAX_SEARCH_CANDIDATES,
)
frame_ids = [ frame_ids = [
str(hit["id"]) str(hit["id"])
@@ -223,18 +261,18 @@ class SearchService:
for hit, score in zip(hits, scores, strict=True) for hit, score in zip(hits, scores, strict=True)
] ]
items = self._group_items(scenes, item_limit)
display_scenes = scenes[:scene_limit]
return { return {
"query": query, "query": query,
"semantic_ratio": self.semantic_ratio, "semantic_ratio": self.semantic_ratio,
"scene_count": len(scenes), "scene_count": len(display_scenes),
"item_count": min( "candidate_scene_count": len(scenes),
len({ "item_count": len(items),
scene.get("jellyfin_item_id") "items": items,
for scene in scenes # Kept for backward compatibility with scene-level consumers. The
if scene.get("jellyfin_item_id") # dashboard now treats items as canonical and exposes matching scenes
}), # underneath each item instead of repeating the same title.
item_limit, "scenes": display_scenes,
),
"items": self._group_items(scenes, item_limit),
"scenes": scenes,
} }