| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Threading; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | using Jellyfin.Data.Enums; |
| | | 7 | | using Jellyfin.Database.Implementations; |
| | | 8 | | using Jellyfin.Database.Implementations.Entities; |
| | | 9 | | using Jellyfin.Extensions; |
| | | 10 | | using MediaBrowser.Controller.Configuration; |
| | | 11 | | using MediaBrowser.Controller.Dto; |
| | | 12 | | using MediaBrowser.Controller.Entities; |
| | | 13 | | using MediaBrowser.Controller.Entities.Movies; |
| | | 14 | | using MediaBrowser.Controller.Library; |
| | | 15 | | using MediaBrowser.Controller.Persistence; |
| | | 16 | | using MediaBrowser.Model.Configuration; |
| | | 17 | | using Microsoft.EntityFrameworkCore; |
| | | 18 | | using BaseItemDto = MediaBrowser.Controller.Entities.BaseItem; |
| | | 19 | | |
| | | 20 | | namespace Emby.Server.Implementations.Library.SimilarItems; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Provides similar items for movies and trailers using weighted scoring. |
| | | 24 | | /// </summary> |
| | | 25 | | public sealed class MovieSimilarItemsProvider : ILocalSimilarItemsProvider<Movie>, ILocalSimilarItemsProvider<Trailer>, |
| | | 26 | | { |
| | | 27 | | private const int GenreWeight = 10; |
| | | 28 | | private const int TagWeight = 5; |
| | | 29 | | private const int StudioWeight = 5; |
| | | 30 | | private const int DirectorWeight = 50; |
| | | 31 | | private const int ActorWeight = 15; |
| | | 32 | | |
| | | 33 | | // Caps the batch fan-out so downstream IN-list sizes (per-source scores, accessible-id |
| | | 34 | | // load, navigation includes) stay bounded regardless of caller input. |
| | | 35 | | private const int MaxBatchSourceItems = 64; |
| | | 36 | | |
| | 0 | 37 | | private static readonly (ItemValueType Type, int Weight)[] _itemValueDimensions = |
| | 0 | 38 | | [ |
| | 0 | 39 | | (ItemValueType.Genre, GenreWeight), |
| | 0 | 40 | | (ItemValueType.Tags, TagWeight), |
| | 0 | 41 | | (ItemValueType.Studios, StudioWeight) |
| | 0 | 42 | | ]; |
| | | 43 | | |
| | 0 | 44 | | private static readonly Dictionary<string, int> _personTypeWeights = new(StringComparer.Ordinal) |
| | 0 | 45 | | { |
| | 0 | 46 | | [nameof(PersonKind.Director)] = DirectorWeight, |
| | 0 | 47 | | [nameof(PersonKind.Actor)] = ActorWeight, |
| | 0 | 48 | | [nameof(PersonKind.GuestStar)] = ActorWeight, |
| | 0 | 49 | | }; |
| | | 50 | | |
| | 0 | 51 | | private static readonly string[] _scoredPersonTypes = [.. _personTypeWeights.Keys]; |
| | | 52 | | |
| | | 53 | | private readonly IDbContextFactory<JellyfinDbContext> _dbProvider; |
| | | 54 | | private readonly IItemQueryHelpers _queryHelpers; |
| | | 55 | | private readonly IServerConfigurationManager _serverConfigurationManager; |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Initializes a new instance of the <see cref="MovieSimilarItemsProvider"/> class. |
| | | 59 | | /// </summary> |
| | | 60 | | /// <param name="dbProvider">The database context factory.</param> |
| | | 61 | | /// <param name="queryHelpers">The shared query helpers.</param> |
| | | 62 | | /// <param name="serverConfigurationManager">The server configuration manager.</param> |
| | | 63 | | public MovieSimilarItemsProvider( |
| | | 64 | | IDbContextFactory<JellyfinDbContext> dbProvider, |
| | | 65 | | IItemQueryHelpers queryHelpers, |
| | | 66 | | IServerConfigurationManager serverConfigurationManager) |
| | | 67 | | { |
| | 21 | 68 | | _dbProvider = dbProvider; |
| | 21 | 69 | | _queryHelpers = queryHelpers; |
| | 21 | 70 | | _serverConfigurationManager = serverConfigurationManager; |
| | 21 | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <inheritdoc/> |
| | 0 | 74 | | public string Name => "Local Genre/Tag"; |
| | | 75 | | |
| | | 76 | | /// <inheritdoc/> |
| | 0 | 77 | | public MetadataPluginType Type => MetadataPluginType.LocalSimilarityProvider; |
| | | 78 | | |
| | | 79 | | /// <inheritdoc/> |
| | | 80 | | public async Task<IReadOnlyList<BaseItemDto>> GetSimilarItemsAsync(Movie item, SimilarItemsQuery query, Cancellation |
| | | 81 | | { |
| | 0 | 82 | | var results = await GetBatchSimilarItemsAsync([item], query, cancellationToken).ConfigureAwait(false); |
| | 0 | 83 | | return results.TryGetValue(item.Id, out var items) ? items : []; |
| | 0 | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <inheritdoc/> |
| | | 87 | | public async Task<IReadOnlyList<BaseItemDto>> GetSimilarItemsAsync(Trailer item, SimilarItemsQuery query, Cancellati |
| | | 88 | | { |
| | 0 | 89 | | var results = await GetBatchSimilarItemsAsync([item], query, cancellationToken).ConfigureAwait(false); |
| | 0 | 90 | | return results.TryGetValue(item.Id, out var items) ? items : []; |
| | 0 | 91 | | } |
| | | 92 | | |
| | | 93 | | bool ILocalSimilarItemsProvider.Supports(Type itemType) |
| | 0 | 94 | | => typeof(Movie).IsAssignableFrom(itemType) || typeof(Trailer).IsAssignableFrom(itemType); |
| | | 95 | | |
| | | 96 | | Task<IReadOnlyList<BaseItem>> ILocalSimilarItemsProvider.GetSimilarItemsAsync(BaseItem item, SimilarItemsQuery query |
| | 0 | 97 | | => item switch |
| | 0 | 98 | | { |
| | 0 | 99 | | Movie movie => GetSimilarItemsAsync(movie, query, cancellationToken), |
| | 0 | 100 | | Trailer trailer => GetSimilarItemsAsync(trailer, query, cancellationToken), |
| | 0 | 101 | | _ => throw new ArgumentException($"Unsupported item type {item.GetType()}", nameof(item)) |
| | 0 | 102 | | }; |
| | | 103 | | |
| | | 104 | | /// <inheritdoc/> |
| | | 105 | | public async Task<Dictionary<Guid, IReadOnlyList<BaseItemDto>>> GetBatchSimilarItemsAsync( |
| | | 106 | | IReadOnlyList<BaseItemDto> sourceItems, |
| | | 107 | | SimilarItemsQuery query, |
| | | 108 | | CancellationToken cancellationToken) |
| | | 109 | | { |
| | 0 | 110 | | var includeItemTypes = new List<BaseItemKind> { BaseItemKind.Movie }; |
| | 0 | 111 | | if (_serverConfigurationManager.Configuration.EnableExternalContentInSuggestions) |
| | | 112 | | { |
| | 0 | 113 | | includeItemTypes.Add(BaseItemKind.Trailer); |
| | 0 | 114 | | includeItemTypes.Add(BaseItemKind.LiveTvProgram); |
| | | 115 | | } |
| | | 116 | | |
| | 0 | 117 | | var limit = query.Limit ?? 50; |
| | 0 | 118 | | var dtoOptions = query.DtoOptions ?? new DtoOptions(); |
| | | 119 | | |
| | 0 | 120 | | if (sourceItems.Count > MaxBatchSourceItems) |
| | | 121 | | { |
| | 0 | 122 | | sourceItems = sourceItems.Take(MaxBatchSourceItems).ToList(); |
| | | 123 | | } |
| | | 124 | | |
| | 0 | 125 | | var context = await _dbProvider.CreateDbContextAsync(cancellationToken).ConfigureAwait(false); |
| | 0 | 126 | | await using (context.ConfigureAwait(false)) |
| | | 127 | | { |
| | | 128 | | // Phase 1: Score all candidates per source item |
| | 0 | 129 | | var sourceIds = sourceItems.Select(i => i.Id).ToList(); |
| | 0 | 130 | | var perSourceScores = await ComputeBatchScoresAsync(sourceIds, context, cancellationToken).ConfigureAwait(fa |
| | | 131 | | |
| | 0 | 132 | | var allCandidateIds = new HashSet<Guid>(); |
| | 0 | 133 | | foreach (var (_, scores) in perSourceScores) |
| | | 134 | | { |
| | 0 | 135 | | allCandidateIds.UnionWith( |
| | 0 | 136 | | scores.OrderByDescending(kvp => kvp.Value) |
| | 0 | 137 | | .Take(limit * 3) |
| | 0 | 138 | | .Select(kvp => kvp.Key)); |
| | | 139 | | } |
| | | 140 | | |
| | 0 | 141 | | var result = new Dictionary<Guid, IReadOnlyList<BaseItemDto>>(); |
| | 0 | 142 | | if (allCandidateIds.Count == 0) |
| | | 143 | | { |
| | 0 | 144 | | return result; |
| | | 145 | | } |
| | | 146 | | |
| | | 147 | | // Phase 2: One access filter for all candidates |
| | 0 | 148 | | var filter = new InternalItemsQuery(query.User) |
| | 0 | 149 | | { |
| | 0 | 150 | | IncludeItemTypes = [.. includeItemTypes], |
| | 0 | 151 | | ExcludeItemIds = [.. query.ExcludeItemIds], |
| | 0 | 152 | | DtoOptions = dtoOptions, |
| | 0 | 153 | | EnableGroupByMetadataKey = true, |
| | 0 | 154 | | EnableTotalRecordCount = false, |
| | 0 | 155 | | IsMovie = true, |
| | 0 | 156 | | IsPlayed = false |
| | 0 | 157 | | }; |
| | | 158 | | |
| | 0 | 159 | | _queryHelpers.PrepareFilterQuery(filter); |
| | 0 | 160 | | var baseQuery = _queryHelpers.PrepareItemQuery(context, filter); |
| | 0 | 161 | | baseQuery = _queryHelpers.TranslateQuery(baseQuery, context, filter); |
| | | 162 | | |
| | 0 | 163 | | var allCandidateIdsList = allCandidateIds.ToList(); |
| | 0 | 164 | | var accessibleItems = await baseQuery |
| | 0 | 165 | | .WhereOneOrMany(allCandidateIdsList, e => e.Id) |
| | 0 | 166 | | .Select(e => new { e.Id, e.PresentationUniqueKey }) |
| | 0 | 167 | | .ToListAsync(cancellationToken).ConfigureAwait(false); |
| | | 168 | | |
| | | 169 | | // Phase 3: Pick top IDs per source, dedup by PresentationUniqueKey |
| | 0 | 170 | | var allOrderedIds = new HashSet<Guid>(); |
| | 0 | 171 | | var perSourceOrderedIds = new Dictionary<Guid, List<Guid>>(); |
| | | 172 | | |
| | 0 | 173 | | foreach (var item in sourceItems) |
| | | 174 | | { |
| | 0 | 175 | | if (!perSourceScores.TryGetValue(item.Id, out var scores)) |
| | | 176 | | { |
| | | 177 | | continue; |
| | | 178 | | } |
| | | 179 | | |
| | 0 | 180 | | var orderedIds = accessibleItems |
| | 0 | 181 | | .Where(x => scores.ContainsKey(x.Id)) |
| | 0 | 182 | | .OrderByDescending(x => scores.GetValueOrDefault(x.Id)) |
| | 0 | 183 | | .DistinctBy(x => x.PresentationUniqueKey) |
| | 0 | 184 | | .Take(limit) |
| | 0 | 185 | | .Select(x => x.Id) |
| | 0 | 186 | | .ToList(); |
| | | 187 | | |
| | 0 | 188 | | if (orderedIds.Count > 0) |
| | | 189 | | { |
| | 0 | 190 | | perSourceOrderedIds[item.Id] = orderedIds; |
| | 0 | 191 | | allOrderedIds.UnionWith(orderedIds); |
| | | 192 | | } |
| | | 193 | | } |
| | | 194 | | |
| | 0 | 195 | | if (allOrderedIds.Count == 0) |
| | | 196 | | { |
| | 0 | 197 | | return result; |
| | | 198 | | } |
| | | 199 | | |
| | | 200 | | // Phase 4: One entity load for all results |
| | 0 | 201 | | var allOrderedIdsList = allOrderedIds.ToList(); |
| | 0 | 202 | | var entities = await _queryHelpers.ApplyNavigations( |
| | 0 | 203 | | context.BaseItems.AsNoTracking().WhereOneOrMany(allOrderedIdsList, e => e.Id), |
| | 0 | 204 | | filter) |
| | 0 | 205 | | .AsSplitQuery() |
| | 0 | 206 | | .ToListAsync(cancellationToken).ConfigureAwait(false); |
| | | 207 | | |
| | 0 | 208 | | var entitiesById = entities |
| | 0 | 209 | | .Select(e => _queryHelpers.DeserializeBaseItem(e, filter.SkipDeserialization)) |
| | 0 | 210 | | .Where(dto => dto is not null) |
| | 0 | 211 | | .ToDictionary(i => i!.Id); |
| | | 212 | | |
| | | 213 | | // Phase 5: Split by source, preserving score order |
| | 0 | 214 | | foreach (var (sourceId, orderedIds) in perSourceOrderedIds) |
| | | 215 | | { |
| | 0 | 216 | | var items = orderedIds |
| | 0 | 217 | | .Where(entitiesById.ContainsKey) |
| | 0 | 218 | | .Select(id => entitiesById[id]!) |
| | 0 | 219 | | .ToList(); |
| | | 220 | | |
| | 0 | 221 | | if (items.Count > 0) |
| | | 222 | | { |
| | 0 | 223 | | result[sourceId] = items; |
| | | 224 | | } |
| | | 225 | | } |
| | | 226 | | |
| | 0 | 227 | | return result; |
| | | 228 | | } |
| | 0 | 229 | | } |
| | | 230 | | |
| | | 231 | | private static async Task<Dictionary<Guid, Dictionary<Guid, int>>> ComputeBatchScoresAsync(List<Guid> sourceIds, Jel |
| | | 232 | | { |
| | 0 | 233 | | var result = new Dictionary<Guid, Dictionary<Guid, int>>(); |
| | 0 | 234 | | foreach (var id in sourceIds) |
| | | 235 | | { |
| | 0 | 236 | | result[id] = []; |
| | | 237 | | } |
| | | 238 | | |
| | 0 | 239 | | foreach (var (valueType, weight) in _itemValueDimensions) |
| | | 240 | | { |
| | 0 | 241 | | var sourceRows = await context.ItemValuesMap.AsNoTracking() |
| | 0 | 242 | | .Where(m => sourceIds.Contains(m.ItemId) && m.ItemValue.Type == valueType) |
| | 0 | 243 | | .Select(m => new { m.ItemId, Key = m.ItemValue.CleanValue }) |
| | 0 | 244 | | .ToListAsync(cancellationToken).ConfigureAwait(false); |
| | | 245 | | |
| | 0 | 246 | | var sourceMap = sourceRows.GroupBy(r => r.ItemId).ToDictionary(g => g.Key, g => g.Select(x => x.Key).ToHashS |
| | 0 | 247 | | var allKeys = sourceMap.Values.SelectMany(v => v).Distinct().ToList(); |
| | 0 | 248 | | if (allKeys.Count == 0) |
| | | 249 | | { |
| | | 250 | | continue; |
| | | 251 | | } |
| | | 252 | | |
| | 0 | 253 | | var candidateRows = await context.ItemValuesMap.AsNoTracking() |
| | 0 | 254 | | .Where(m => m.ItemValue.Type == valueType && allKeys.Contains(m.ItemValue.CleanValue)) |
| | 0 | 255 | | .Select(m => new { m.ItemId, Key = m.ItemValue.CleanValue }) |
| | 0 | 256 | | .ToListAsync(cancellationToken).ConfigureAwait(false); |
| | | 257 | | |
| | 0 | 258 | | var keyToCandidates = candidateRows.GroupBy(r => r.Key).ToDictionary(g => g.Key, g => g.Select(x => x.ItemId |
| | 0 | 259 | | ApplyDimensionScores(sourceIds, sourceMap, keyToCandidates, weight, result); |
| | 0 | 260 | | } |
| | | 261 | | |
| | 0 | 262 | | var personSourceRows = await context.PeopleBaseItemMap.AsNoTracking() |
| | 0 | 263 | | .Where(m => sourceIds.Contains(m.ItemId) && _scoredPersonTypes.Contains(m.People.PersonType)) |
| | 0 | 264 | | .Select(m => new { m.ItemId, m.PeopleId, m.People.PersonType }) |
| | 0 | 265 | | .ToListAsync(cancellationToken).ConfigureAwait(false); |
| | | 266 | | |
| | 0 | 267 | | if (personSourceRows.Count > 0) |
| | | 268 | | { |
| | 0 | 269 | | var personCandidateRows = await context.PeopleBaseItemMap.AsNoTracking() |
| | 0 | 270 | | .Where(m => context.PeopleBaseItemMap |
| | 0 | 271 | | .Where(s => sourceIds.Contains(s.ItemId) && _scoredPersonTypes.Contains(s.People.PersonType)) |
| | 0 | 272 | | .Select(s => s.PeopleId) |
| | 0 | 273 | | .Contains(m.PeopleId)) |
| | 0 | 274 | | .Select(m => new { m.ItemId, m.PeopleId }) |
| | 0 | 275 | | .ToListAsync(cancellationToken).ConfigureAwait(false); |
| | | 276 | | |
| | 0 | 277 | | var personToCandidates = personCandidateRows |
| | 0 | 278 | | .GroupBy(r => r.PeopleId) |
| | 0 | 279 | | .ToDictionary(g => g.Key, g => g.Select(x => x.ItemId).ToList()); |
| | | 280 | | |
| | 0 | 281 | | foreach (var weightGroup in personSourceRows.GroupBy(r => _personTypeWeights[r.PersonType!])) |
| | | 282 | | { |
| | 0 | 283 | | var sourceMap = weightGroup |
| | 0 | 284 | | .GroupBy(r => r.ItemId) |
| | 0 | 285 | | .ToDictionary(g => g.Key, g => g.Select(x => x.PeopleId).ToHashSet()); |
| | 0 | 286 | | ApplyDimensionScores(sourceIds, sourceMap, personToCandidates, weightGroup.Key, result); |
| | | 287 | | } |
| | | 288 | | } |
| | | 289 | | |
| | 0 | 290 | | foreach (var sourceId in sourceIds) |
| | | 291 | | { |
| | 0 | 292 | | var scoreMap = result[sourceId]; |
| | 0 | 293 | | scoreMap.Remove(sourceId); |
| | 0 | 294 | | if (scoreMap.Count == 0) |
| | | 295 | | { |
| | 0 | 296 | | result.Remove(sourceId); |
| | | 297 | | } |
| | | 298 | | } |
| | | 299 | | |
| | 0 | 300 | | return result; |
| | 0 | 301 | | } |
| | | 302 | | |
| | | 303 | | private static void ApplyDimensionScores<TKey>( |
| | | 304 | | List<Guid> sourceIds, |
| | | 305 | | Dictionary<Guid, HashSet<TKey>> sourceMap, |
| | | 306 | | Dictionary<TKey, List<Guid>> keyToCandidates, |
| | | 307 | | int weight, |
| | | 308 | | Dictionary<Guid, Dictionary<Guid, int>> result) |
| | | 309 | | where TKey : notnull |
| | | 310 | | { |
| | 0 | 311 | | foreach (var sourceId in sourceIds) |
| | | 312 | | { |
| | 0 | 313 | | if (!sourceMap.TryGetValue(sourceId, out var sourceKeys)) |
| | | 314 | | { |
| | | 315 | | continue; |
| | | 316 | | } |
| | | 317 | | |
| | 0 | 318 | | var scoreMap = result[sourceId]; |
| | 0 | 319 | | foreach (var key in sourceKeys) |
| | | 320 | | { |
| | 0 | 321 | | if (!keyToCandidates.TryGetValue(key, out var candidates)) |
| | | 322 | | { |
| | | 323 | | continue; |
| | | 324 | | } |
| | | 325 | | |
| | 0 | 326 | | foreach (var candidateId in candidates) |
| | | 327 | | { |
| | 0 | 328 | | scoreMap[candidateId] = scoreMap.GetValueOrDefault(candidateId) + weight; |
| | | 329 | | } |
| | | 330 | | } |
| | | 331 | | } |
| | 0 | 332 | | } |
| | | 333 | | } |