mirror of
https://github.com/MrSleeps/jellyfin-scene-search.git
synced 2026-11-19 00:03:36 +00:00
228 lines
7.4 KiB
C#
228 lines
7.4 KiB
C#
using System.Runtime.CompilerServices;
|
|
using Jellyfin.Data.Enums;
|
|
using Jellyfin.Plugin.SceneSearch.Configuration;
|
|
using MediaBrowser.Controller.Library;
|
|
using MediaBrowser.Model.Configuration;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Jellyfin.Plugin.SceneSearch;
|
|
|
|
/// <summary>
|
|
/// External Jellyfin search provider backed by jellyfin-scene-search.
|
|
/// </summary>
|
|
public sealed class SceneSearchProvider : IExternalSearchProvider
|
|
{
|
|
// Jellyfin merges all external provider results by descending score and
|
|
// truncates to query.Limit. Normal Meilisearch relevance is in 0..1, so
|
|
// simply returning our website results at the same scores does NOT preserve
|
|
// any of them: another provider can push every Scene Search hit beyond the
|
|
// cap. Reserve up to half the limit (at most 10 items) for Scene Search,
|
|
// leaving the other half available for normal title matches.
|
|
// Prioritise in website/API ORDER rather than re-ranking by SigLIP scores.
|
|
private const int MaxBlendedSceneSlots = 10;
|
|
private const float BlendedSceneScoreCeiling = 1.20f;
|
|
private const float BlendedSceneScoreStep = 0.01f;
|
|
|
|
// The optional scene: prefix continues to prefer Scene Search results
|
|
// more strongly than normal queries (which reserve only a limited slice).
|
|
private const float ExplicitSceneScoreFloor = 1.1f;
|
|
private const float ExplicitSceneScoreRange = 0.9f;
|
|
|
|
private static readonly HashSet<BaseItemKind> SupportedKinds =
|
|
[
|
|
BaseItemKind.Movie,
|
|
BaseItemKind.Episode,
|
|
BaseItemKind.Video
|
|
];
|
|
|
|
private readonly SceneSearchClient _client;
|
|
private readonly ILogger<SceneSearchProvider> _logger;
|
|
|
|
public SceneSearchProvider(
|
|
SceneSearchClient client,
|
|
ILogger<SceneSearchProvider> logger)
|
|
{
|
|
_client = client;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public string Name => "Scene Search";
|
|
|
|
/// <inheritdoc />
|
|
public MetadataPluginType Type => MetadataPluginType.SearchProvider;
|
|
|
|
/// <inheritdoc />
|
|
public int Priority => 20;
|
|
|
|
/// <inheritdoc />
|
|
public bool CanSearch(SearchProviderQuery query)
|
|
{
|
|
if (!_client.IsConfigured || string.IsNullOrWhiteSpace(query.SearchTerm))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (query.IncludeItemTypes.Length > 0
|
|
&& !query.IncludeItemTypes.Any(SupportedKinds.Contains))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async IAsyncEnumerable<SearchResult> SearchAsync(
|
|
SearchProviderQuery query,
|
|
[EnumeratorCancellation] CancellationToken cancellationToken)
|
|
{
|
|
if (!CanSearch(query))
|
|
{
|
|
yield break;
|
|
}
|
|
|
|
var config = Configuration;
|
|
var explicitSceneQuery = HasPrefix(query.SearchTerm, config.SearchPrefix);
|
|
var searchTerm = StripPrefix(query.SearchTerm, config);
|
|
if (string.IsNullOrWhiteSpace(searchTerm))
|
|
{
|
|
yield break;
|
|
}
|
|
|
|
var limit = Math.Min(query.Limit ?? config.MaxResults, config.MaxResults);
|
|
_logger.LogInformation(
|
|
"Scene Search handling Jellyfin query '{SearchTerm}' as '{BackendQuery}' (limit {Limit}, explicit scene preference {ExplicitSceneQuery})",
|
|
query.SearchTerm,
|
|
searchTerm,
|
|
Math.Max(limit, 1),
|
|
explicitSceneQuery);
|
|
|
|
var results = await _client.SearchAsync(
|
|
searchTerm,
|
|
Math.Max(limit, 1),
|
|
cancellationToken).ConfigureAwait(false);
|
|
|
|
var yielded = 0;
|
|
var invalidIds = 0;
|
|
var typeFiltered = 0;
|
|
var promoted = 0;
|
|
var blendedSceneSlots = Math.Min(
|
|
MaxBlendedSceneSlots,
|
|
Math.Max(1, (Math.Max(limit, 1) + 1) / 2));
|
|
|
|
foreach (var result in results)
|
|
{
|
|
if (!Guid.TryParse(result.JellyfinItemId, out var itemId) || itemId == Guid.Empty)
|
|
{
|
|
invalidIds++;
|
|
_logger.LogDebug(
|
|
"Ignoring Scene Search result with invalid Jellyfin item id {ItemId}",
|
|
result.JellyfinItemId);
|
|
continue;
|
|
}
|
|
|
|
if (!MatchesRequestedType(result, query))
|
|
{
|
|
typeFiltered++;
|
|
continue;
|
|
}
|
|
|
|
var score = (float)Math.Clamp(
|
|
result.RankingScore ?? result.SiglipScore ?? 0.5,
|
|
0.0,
|
|
1.0);
|
|
|
|
if (explicitSceneQuery)
|
|
{
|
|
score = ExplicitSceneScoreFloor + (ExplicitSceneScoreRange * score);
|
|
}
|
|
else if (promoted < blendedSceneSlots)
|
|
{
|
|
// The backend already groups by Jellyfin item and places
|
|
// caption-text matches first. Keep its exact ordering for a
|
|
// bounded number of Scene Search matches, above Meilisearch's
|
|
// 0..1 scores. The rest compete normally with other providers.
|
|
score = BlendedSceneScoreCeiling - (promoted * BlendedSceneScoreStep);
|
|
promoted++;
|
|
}
|
|
|
|
yielded++;
|
|
yield return new SearchResult(itemId, score);
|
|
}
|
|
|
|
_logger.LogInformation(
|
|
"Scene Search API returned {ApiCount} grouped item(s); yielded {Yielded}, reserved {Promoted}/{ReservedSlots} blended scene slots, invalid ids {InvalidIds}, type-filtered {TypeFiltered}",
|
|
results.Count,
|
|
yielded,
|
|
promoted,
|
|
explicitSceneQuery ? 0 : blendedSceneSlots,
|
|
invalidIds,
|
|
typeFiltered);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
async Task<IReadOnlyList<SearchResult>> ISearchProvider.SearchAsync(
|
|
SearchProviderQuery query,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var results = new List<SearchResult>();
|
|
await foreach (var result in SearchAsync(query, cancellationToken).ConfigureAwait(false))
|
|
{
|
|
results.Add(result);
|
|
}
|
|
|
|
return results;
|
|
}
|
|
|
|
private static PluginConfiguration Configuration
|
|
=> Plugin.Instance?.Configuration ?? new PluginConfiguration();
|
|
|
|
private static bool HasPrefix(string term, string prefix)
|
|
=> !string.IsNullOrWhiteSpace(prefix)
|
|
&& term.TrimStart().StartsWith(prefix, StringComparison.OrdinalIgnoreCase);
|
|
|
|
private static string StripPrefix(string term, PluginConfiguration config)
|
|
{
|
|
var trimmed = term.Trim();
|
|
if (string.IsNullOrWhiteSpace(config.SearchPrefix))
|
|
{
|
|
return trimmed;
|
|
}
|
|
|
|
return trimmed.StartsWith(config.SearchPrefix, StringComparison.OrdinalIgnoreCase)
|
|
? trimmed[config.SearchPrefix.Length..].Trim()
|
|
: trimmed;
|
|
}
|
|
|
|
private static bool MatchesRequestedType(
|
|
SceneSearchItem result,
|
|
SearchProviderQuery query)
|
|
{
|
|
if (!Enum.TryParse<BaseItemKind>(
|
|
result.JellyfinItemType,
|
|
ignoreCase: true,
|
|
out var kind))
|
|
{
|
|
return query.IncludeItemTypes.Length == 0;
|
|
}
|
|
|
|
if (!SupportedKinds.Contains(kind))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (query.IncludeItemTypes.Length > 0 && !query.IncludeItemTypes.Contains(kind))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (query.IncludeItemTypes.Length == 0 && query.ExcludeItemTypes.Contains(kind))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|