mirror of
https://github.com/MrSleeps/jellyfin-scene-search.git
synced 2026-11-20 00:33:36 +00:00
Group search results by Jellyfin item
This commit is contained in:
@@ -14,6 +14,9 @@ from .meili import Meili
|
||||
class SearchService:
|
||||
"""Shared hybrid search implementation for CLI and HTTP consumers."""
|
||||
|
||||
MAX_SEARCH_CANDIDATES = 500
|
||||
SCENES_PER_ITEM = 5
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
@@ -160,9 +163,13 @@ class SearchService:
|
||||
"ranking_score": scene.get("ranking_score"),
|
||||
"siglip_score": scene.get("siglip_score"),
|
||||
"scene_matches": 1,
|
||||
"matching_scenes": [scene],
|
||||
}
|
||||
else:
|
||||
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]
|
||||
|
||||
@@ -186,18 +193,49 @@ class SearchService:
|
||||
embedder = self.get_embedder()
|
||||
text_vector = embedder.embed_text(query)
|
||||
|
||||
result = self.meili.search_vector(
|
||||
text_vector,
|
||||
query,
|
||||
scene_limit,
|
||||
self.semantic_ratio,
|
||||
filter_expression=(
|
||||
'source_kind = "jellyfin"'
|
||||
if jellyfin_only
|
||||
else None
|
||||
),
|
||||
filter_expression = (
|
||||
'source_kind = "jellyfin"'
|
||||
if jellyfin_only
|
||||
else None
|
||||
)
|
||||
hits = list(result.get("hits") or [])
|
||||
|
||||
# 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 [])
|
||||
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 = [
|
||||
str(hit["id"])
|
||||
@@ -223,18 +261,18 @@ class SearchService:
|
||||
for hit, score in zip(hits, scores, strict=True)
|
||||
]
|
||||
|
||||
items = self._group_items(scenes, item_limit)
|
||||
display_scenes = scenes[:scene_limit]
|
||||
|
||||
return {
|
||||
"query": query,
|
||||
"semantic_ratio": self.semantic_ratio,
|
||||
"scene_count": len(scenes),
|
||||
"item_count": min(
|
||||
len({
|
||||
scene.get("jellyfin_item_id")
|
||||
for scene in scenes
|
||||
if scene.get("jellyfin_item_id")
|
||||
}),
|
||||
item_limit,
|
||||
),
|
||||
"items": self._group_items(scenes, item_limit),
|
||||
"scenes": scenes,
|
||||
"scene_count": len(display_scenes),
|
||||
"candidate_scene_count": len(scenes),
|
||||
"item_count": len(items),
|
||||
"items": items,
|
||||
# Kept for backward compatibility with scene-level consumers. The
|
||||
# dashboard now treats items as canonical and exposes matching scenes
|
||||
# underneath each item instead of repeating the same title.
|
||||
"scenes": display_scenes,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user