< Summary - Jellyfin

Information
Class: Emby.Server.Implementations.Library.SearchEngine
Assembly: Emby.Server.Implementations
File(s): /srv/git/jellyfin/Emby.Server.Implementations/Library/SearchEngine.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 91
Coverable lines: 91
Total lines: 200
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 50
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
GetSearchHints(...)0%4260%
AddIfMissing(...)0%620%
GetSearchHints(...)0%1806420%

File(s)

/srv/git/jellyfin/Emby.Server.Implementations/Library/SearchEngine.cs

#LineLine coverage
 1#pragma warning disable CS1591
 2
 3using System;
 4using System.Collections.Generic;
 5using System.Linq;
 6using Jellyfin.Data.Enums;
 7using Jellyfin.Database.Implementations.Entities;
 8using Jellyfin.Database.Implementations.Enums;
 9using Jellyfin.Extensions;
 10using MediaBrowser.Controller.Dto;
 11using MediaBrowser.Controller.Entities;
 12using MediaBrowser.Controller.Library;
 13using MediaBrowser.Model.Querying;
 14using MediaBrowser.Model.Search;
 15
 16namespace Emby.Server.Implementations.Library
 17{
 18    public class SearchEngine : ISearchEngine
 19    {
 20        private readonly ILibraryManager _libraryManager;
 21        private readonly IUserManager _userManager;
 22
 23        public SearchEngine(ILibraryManager libraryManager, IUserManager userManager)
 24        {
 025            _libraryManager = libraryManager;
 026            _userManager = userManager;
 027        }
 28
 29        public QueryResult<SearchHintInfo> GetSearchHints(SearchQuery query)
 30        {
 031            User? user = null;
 032            if (!query.UserId.IsEmpty())
 33            {
 034                user = _userManager.GetUserById(query.UserId);
 35            }
 36
 037            var results = GetSearchHints(query, user);
 038            var totalRecordCount = results.Count;
 39
 040            if (query.StartIndex.HasValue)
 41            {
 042                results = results.GetRange(query.StartIndex.Value, totalRecordCount - query.StartIndex.Value);
 43            }
 44
 045            if (query.Limit.HasValue)
 46            {
 047                results = results.GetRange(0, Math.Min(query.Limit.Value, results.Count));
 48            }
 49
 050            return new QueryResult<SearchHintInfo>(
 051                query.StartIndex,
 052                totalRecordCount,
 053                results);
 54        }
 55
 56        private static void AddIfMissing(List<BaseItemKind> list, BaseItemKind value)
 57        {
 058            if (!list.Contains(value))
 59            {
 060                list.Add(value);
 61            }
 062        }
 63
 64        /// <summary>
 65        /// Gets the search hints.
 66        /// </summary>
 67        /// <param name="query">The query.</param>
 68        /// <param name="user">The user.</param>
 69        /// <returns>IEnumerable{SearchHintResult}.</returns>
 70        /// <exception cref="ArgumentException"><c>query.SearchTerm</c> is <c>null</c> or empty.</exception>
 71        private List<SearchHintInfo> GetSearchHints(SearchQuery query, User? user)
 72        {
 073            var searchTerm = query.SearchTerm;
 74
 075            ArgumentException.ThrowIfNullOrEmpty(searchTerm);
 76
 077            searchTerm = searchTerm.Trim().RemoveDiacritics();
 78
 079            var excludeItemTypes = query.ExcludeItemTypes.ToList();
 080            var includeItemTypes = query.IncludeItemTypes.ToList();
 81
 082            excludeItemTypes.Add(BaseItemKind.Year);
 083            excludeItemTypes.Add(BaseItemKind.Folder);
 84
 085            if (query.IncludeGenres && (includeItemTypes.Count == 0 || includeItemTypes.Contains(BaseItemKind.Genre)))
 86            {
 087                if (!query.IncludeMedia)
 88                {
 089                    AddIfMissing(includeItemTypes, BaseItemKind.Genre);
 090                    AddIfMissing(includeItemTypes, BaseItemKind.MusicGenre);
 91                }
 92            }
 93            else
 94            {
 095                AddIfMissing(excludeItemTypes, BaseItemKind.Genre);
 096                AddIfMissing(excludeItemTypes, BaseItemKind.MusicGenre);
 97            }
 98
 099            if (query.IncludePeople && (includeItemTypes.Count == 0 || includeItemTypes.Contains(BaseItemKind.Person)))
 100            {
 0101                if (!query.IncludeMedia)
 102                {
 0103                    AddIfMissing(includeItemTypes, BaseItemKind.Person);
 104                }
 105            }
 106            else
 107            {
 0108                AddIfMissing(excludeItemTypes, BaseItemKind.Person);
 109            }
 110
 0111            if (query.IncludeStudios && (includeItemTypes.Count == 0 || includeItemTypes.Contains(BaseItemKind.Studio)))
 112            {
 0113                if (!query.IncludeMedia)
 114                {
 0115                    AddIfMissing(includeItemTypes, BaseItemKind.Studio);
 116                }
 117            }
 118            else
 119            {
 0120                AddIfMissing(excludeItemTypes, BaseItemKind.Studio);
 121            }
 122
 0123            if (query.IncludeArtists && (includeItemTypes.Count == 0 || includeItemTypes.Contains(BaseItemKind.MusicArti
 124            {
 0125                if (!query.IncludeMedia)
 126                {
 0127                    AddIfMissing(includeItemTypes, BaseItemKind.MusicArtist);
 128                }
 129            }
 130            else
 131            {
 0132                AddIfMissing(excludeItemTypes, BaseItemKind.MusicArtist);
 133            }
 134
 0135            AddIfMissing(excludeItemTypes, BaseItemKind.CollectionFolder);
 0136            AddIfMissing(excludeItemTypes, BaseItemKind.Folder);
 0137            var mediaTypes = query.MediaTypes.ToList();
 138
 0139            if (includeItemTypes.Count > 0)
 140            {
 0141                excludeItemTypes.Clear();
 0142                mediaTypes.Clear();
 143            }
 144
 0145            var searchQuery = new InternalItemsQuery(user)
 0146            {
 0147                SearchTerm = searchTerm,
 0148                ExcludeItemTypes = excludeItemTypes.ToArray(),
 0149                IncludeItemTypes = includeItemTypes.ToArray(),
 0150                Limit = query.Limit,
 0151                IncludeItemsByName = !query.ParentId.HasValue,
 0152                ParentId = query.ParentId ?? Guid.Empty,
 0153                OrderBy = new[] { (ItemSortBy.SortName, SortOrder.Ascending) },
 0154                Recursive = true,
 0155
 0156                IsKids = query.IsKids,
 0157                IsMovie = query.IsMovie,
 0158                IsNews = query.IsNews,
 0159                IsSeries = query.IsSeries,
 0160                IsSports = query.IsSports,
 0161                MediaTypes = mediaTypes.ToArray(),
 0162
 0163                DtoOptions = new DtoOptions
 0164                {
 0165                    Fields = new ItemFields[]
 0166                    {
 0167                        ItemFields.AirTime,
 0168                        ItemFields.DateCreated,
 0169                        ItemFields.ChannelInfo,
 0170                        ItemFields.ParentId
 0171                    }
 0172                }
 0173            };
 174
 175            IReadOnlyList<BaseItem> mediaItems;
 176
 0177            if (searchQuery.IncludeItemTypes.Length == 1 && searchQuery.IncludeItemTypes[0] == BaseItemKind.MusicArtist)
 178            {
 0179                if (!searchQuery.ParentId.IsEmpty())
 180                {
 0181                    searchQuery.AncestorIds = [searchQuery.ParentId];
 0182                    searchQuery.ParentId = Guid.Empty;
 183                }
 184
 0185                searchQuery.IncludeItemsByName = true;
 0186                searchQuery.IncludeItemTypes = Array.Empty<BaseItemKind>();
 0187                mediaItems = _libraryManager.GetAllArtists(searchQuery).Items.Select(i => i.Item).ToList();
 188            }
 189            else
 190            {
 0191                mediaItems = _libraryManager.GetItemList(searchQuery);
 192            }
 193
 0194            return mediaItems.Select(i => new SearchHintInfo
 0195            {
 0196                Item = i
 0197            }).ToList();
 198        }
 199    }
 200}