2021-05-20 12:28:18 -07:00
|
|
|
#nullable disable
|
|
|
|
|
2019-11-01 10:38:54 -07:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2014-01-07 13:12:39 -07:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2021-06-12 02:22:26 -07:00
|
|
|
using Diacritics.Extensions;
|
2020-05-20 10:07:53 -07:00
|
|
|
using Jellyfin.Data.Entities;
|
2020-06-30 18:44:41 -07:00
|
|
|
using Jellyfin.Data.Enums;
|
2017-05-21 00:25:49 -07:00
|
|
|
using MediaBrowser.Controller.Dto;
|
2019-01-13 12:21:32 -07:00
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using MediaBrowser.Model.Querying;
|
|
|
|
using MediaBrowser.Model.Search;
|
2020-05-20 10:07:53 -07:00
|
|
|
using Genre = MediaBrowser.Controller.Entities.Genre;
|
|
|
|
using Person = MediaBrowser.Controller.Entities.Person;
|
2014-01-07 13:12:39 -07:00
|
|
|
|
2016-11-02 23:37:52 -07:00
|
|
|
namespace Emby.Server.Implementations.Library
|
2014-01-07 13:12:39 -07:00
|
|
|
{
|
|
|
|
public class SearchEngine : ISearchEngine
|
|
|
|
{
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
private readonly IUserManager _userManager;
|
|
|
|
|
2020-07-20 02:01:37 -07:00
|
|
|
public SearchEngine(ILibraryManager libraryManager, IUserManager userManager)
|
2014-01-07 13:12:39 -07:00
|
|
|
{
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
_userManager = userManager;
|
|
|
|
}
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
public QueryResult<SearchHintInfo> GetSearchHints(SearchQuery query)
|
2014-01-07 13:12:39 -07:00
|
|
|
{
|
2020-05-20 10:07:53 -07:00
|
|
|
User user = null;
|
2020-07-20 02:01:37 -07:00
|
|
|
if (query.UserId != Guid.Empty)
|
2014-04-10 08:06:54 -07:00
|
|
|
{
|
2015-07-18 12:32:59 -07:00
|
|
|
user = _userManager.GetUserById(query.UserId);
|
2014-04-10 08:06:54 -07:00
|
|
|
}
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
var results = GetSearchHints(query, user);
|
|
|
|
var totalRecordCount = results.Count;
|
2014-01-07 13:12:39 -07:00
|
|
|
|
|
|
|
if (query.StartIndex.HasValue)
|
|
|
|
{
|
2020-07-20 02:01:37 -07:00
|
|
|
results = results.GetRange(query.StartIndex.Value, totalRecordCount - query.StartIndex.Value);
|
2014-01-07 13:12:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (query.Limit.HasValue)
|
|
|
|
{
|
2020-12-30 03:16:09 -07:00
|
|
|
results = results.GetRange(0, Math.Min(query.Limit.Value, results.Count));
|
2014-01-07 13:12:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return new QueryResult<SearchHintInfo>
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
TotalRecordCount = totalRecordCount,
|
2014-01-07 13:12:39 -07:00
|
|
|
|
2020-07-20 02:01:37 -07:00
|
|
|
Items = results
|
2014-01-07 13:12:39 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-06 13:50:43 -07:00
|
|
|
private static void AddIfMissing(List<string> list, string value)
|
2015-08-21 19:59:10 -07:00
|
|
|
{
|
|
|
|
if (!list.Contains(value, StringComparer.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
list.Add(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-07 13:12:39 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the search hints.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="query">The query.</param>
|
2015-07-18 12:32:59 -07:00
|
|
|
/// <param name="user">The user.</param>
|
2014-01-07 13:12:39 -07:00
|
|
|
/// <returns>IEnumerable{SearchHintResult}.</returns>
|
2019-01-13 13:37:13 -07:00
|
|
|
/// <exception cref="ArgumentNullException">searchTerm</exception>
|
2020-05-20 10:07:53 -07:00
|
|
|
private List<SearchHintInfo> GetSearchHints(SearchQuery query, User user)
|
2014-01-07 13:12:39 -07:00
|
|
|
{
|
|
|
|
var searchTerm = query.SearchTerm;
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
if (string.IsNullOrEmpty(searchTerm))
|
2014-01-07 13:12:39 -07:00
|
|
|
{
|
2020-07-20 02:01:37 -07:00
|
|
|
throw new ArgumentException("SearchTerm can't be empty.", nameof(query));
|
2014-01-07 13:12:39 -07:00
|
|
|
}
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
searchTerm = searchTerm.Trim().RemoveDiacritics();
|
2014-01-07 13:12:39 -07:00
|
|
|
|
2017-05-06 13:21:08 -07:00
|
|
|
var excludeItemTypes = query.ExcludeItemTypes.ToList();
|
2018-09-12 10:26:21 -07:00
|
|
|
var includeItemTypes = (query.IncludeItemTypes ?? Array.Empty<string>()).ToList();
|
2014-01-07 13:12:39 -07:00
|
|
|
|
2020-10-17 07:19:57 -07:00
|
|
|
excludeItemTypes.Add(nameof(Year));
|
|
|
|
excludeItemTypes.Add(nameof(Folder));
|
2014-01-07 13:12:39 -07:00
|
|
|
|
2015-08-21 19:59:10 -07:00
|
|
|
if (query.IncludeGenres && (includeItemTypes.Count == 0 || includeItemTypes.Contains("Genre", StringComparer.OrdinalIgnoreCase)))
|
2014-01-07 13:12:39 -07:00
|
|
|
{
|
2015-08-21 19:59:10 -07:00
|
|
|
if (!query.IncludeMedia)
|
2014-01-07 13:12:39 -07:00
|
|
|
{
|
2020-10-17 07:19:57 -07:00
|
|
|
AddIfMissing(includeItemTypes, nameof(Genre));
|
|
|
|
AddIfMissing(includeItemTypes, nameof(MusicGenre));
|
2014-01-07 13:12:39 -07:00
|
|
|
}
|
|
|
|
}
|
2015-08-21 19:59:10 -07:00
|
|
|
else
|
|
|
|
{
|
2020-10-17 07:19:57 -07:00
|
|
|
AddIfMissing(excludeItemTypes, nameof(Genre));
|
|
|
|
AddIfMissing(excludeItemTypes, nameof(MusicGenre));
|
2015-08-21 19:59:10 -07:00
|
|
|
}
|
2014-01-07 13:12:39 -07:00
|
|
|
|
2016-05-29 14:02:32 -07:00
|
|
|
if (query.IncludePeople && (includeItemTypes.Count == 0 || includeItemTypes.Contains("People", StringComparer.OrdinalIgnoreCase) || includeItemTypes.Contains("Person", StringComparer.OrdinalIgnoreCase)))
|
2014-01-07 13:12:39 -07:00
|
|
|
{
|
2015-08-21 19:59:10 -07:00
|
|
|
if (!query.IncludeMedia)
|
2014-01-07 13:12:39 -07:00
|
|
|
{
|
2020-10-17 07:19:57 -07:00
|
|
|
AddIfMissing(includeItemTypes, nameof(Person));
|
2014-01-07 13:12:39 -07:00
|
|
|
}
|
2015-08-21 19:59:10 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-17 07:19:57 -07:00
|
|
|
AddIfMissing(excludeItemTypes, nameof(Person));
|
2015-08-21 19:59:10 -07:00
|
|
|
}
|
2014-01-07 13:12:39 -07:00
|
|
|
|
2015-08-21 19:59:10 -07:00
|
|
|
if (query.IncludeStudios && (includeItemTypes.Count == 0 || includeItemTypes.Contains("Studio", StringComparer.OrdinalIgnoreCase)))
|
|
|
|
{
|
|
|
|
if (!query.IncludeMedia)
|
2014-01-07 13:12:39 -07:00
|
|
|
{
|
2020-10-17 07:19:57 -07:00
|
|
|
AddIfMissing(includeItemTypes, nameof(Studio));
|
2014-01-07 13:12:39 -07:00
|
|
|
}
|
2015-08-21 19:59:10 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-17 07:19:57 -07:00
|
|
|
AddIfMissing(excludeItemTypes, nameof(Studio));
|
2015-08-21 19:59:10 -07:00
|
|
|
}
|
2014-01-07 13:12:39 -07:00
|
|
|
|
2015-08-21 19:59:10 -07:00
|
|
|
if (query.IncludeArtists && (includeItemTypes.Count == 0 || includeItemTypes.Contains("MusicArtist", StringComparer.OrdinalIgnoreCase)))
|
|
|
|
{
|
|
|
|
if (!query.IncludeMedia)
|
2014-01-07 13:12:39 -07:00
|
|
|
{
|
2020-10-17 07:19:57 -07:00
|
|
|
AddIfMissing(includeItemTypes, nameof(MusicArtist));
|
2014-01-07 13:12:39 -07:00
|
|
|
}
|
|
|
|
}
|
2015-08-21 19:59:10 -07:00
|
|
|
else
|
|
|
|
{
|
2020-10-17 07:19:57 -07:00
|
|
|
AddIfMissing(excludeItemTypes, nameof(MusicArtist));
|
2015-08-21 19:59:10 -07:00
|
|
|
}
|
2014-01-07 13:12:39 -07:00
|
|
|
|
2020-10-17 07:19:57 -07:00
|
|
|
AddIfMissing(excludeItemTypes, nameof(CollectionFolder));
|
|
|
|
AddIfMissing(excludeItemTypes, nameof(Folder));
|
2017-11-01 12:50:44 -07:00
|
|
|
var mediaTypes = query.MediaTypes.ToList();
|
|
|
|
|
|
|
|
if (includeItemTypes.Count > 0)
|
|
|
|
{
|
|
|
|
excludeItemTypes.Clear();
|
|
|
|
mediaTypes.Clear();
|
|
|
|
}
|
2015-10-29 06:28:05 -07:00
|
|
|
|
2017-11-01 12:50:44 -07:00
|
|
|
var searchQuery = new InternalItemsQuery(user)
|
2014-01-07 13:12:39 -07:00
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
SearchTerm = searchTerm,
|
2018-12-28 08:48:26 -07:00
|
|
|
ExcludeItemTypes = excludeItemTypes.ToArray(),
|
|
|
|
IncludeItemTypes = includeItemTypes.ToArray(),
|
2015-11-02 10:25:01 -07:00
|
|
|
Limit = query.Limit,
|
2020-12-01 11:10:56 -07:00
|
|
|
IncludeItemsByName = !query.ParentId.HasValue,
|
2020-12-01 11:07:41 -07:00
|
|
|
ParentId = query.ParentId ?? Guid.Empty,
|
2019-10-20 07:08:40 -07:00
|
|
|
OrderBy = new[] { (ItemSortBy.SortName, SortOrder.Ascending) },
|
2017-05-06 12:45:23 -07:00
|
|
|
Recursive = true,
|
|
|
|
|
|
|
|
IsKids = query.IsKids,
|
|
|
|
IsMovie = query.IsMovie,
|
|
|
|
IsNews = query.IsNews,
|
|
|
|
IsSeries = query.IsSeries,
|
2017-05-06 13:21:08 -07:00
|
|
|
IsSports = query.IsSports,
|
2017-11-01 12:50:44 -07:00
|
|
|
MediaTypes = mediaTypes.ToArray(),
|
2017-05-21 00:25:49 -07:00
|
|
|
|
|
|
|
DtoOptions = new DtoOptions
|
|
|
|
{
|
2017-08-19 12:43:35 -07:00
|
|
|
Fields = new ItemFields[]
|
2017-05-21 00:25:49 -07:00
|
|
|
{
|
|
|
|
ItemFields.AirTime,
|
|
|
|
ItemFields.DateCreated,
|
2017-11-23 08:46:16 -07:00
|
|
|
ItemFields.ChannelInfo,
|
|
|
|
ItemFields.ParentId
|
2017-05-21 00:25:49 -07:00
|
|
|
}
|
|
|
|
}
|
2017-11-01 12:50:44 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
List<BaseItem> mediaItems;
|
|
|
|
|
|
|
|
if (searchQuery.IncludeItemTypes.Length == 1 && string.Equals(searchQuery.IncludeItemTypes[0], "MusicArtist", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
if (!searchQuery.ParentId.Equals(Guid.Empty))
|
2017-11-01 12:50:44 -07:00
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
searchQuery.AncestorIds = new[] { searchQuery.ParentId };
|
2017-11-01 12:50:44 -07:00
|
|
|
}
|
2020-06-15 14:43:52 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
searchQuery.ParentId = Guid.Empty;
|
2017-11-01 12:50:44 -07:00
|
|
|
searchQuery.IncludeItemsByName = true;
|
2018-09-12 10:26:21 -07:00
|
|
|
searchQuery.IncludeItemTypes = Array.Empty<string>();
|
|
|
|
mediaItems = _libraryManager.GetAllArtists(searchQuery).Items.Select(i => i.Item1).ToList();
|
2017-11-01 12:50:44 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mediaItems = _libraryManager.GetItemList(searchQuery);
|
|
|
|
}
|
2014-01-07 13:12:39 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
return mediaItems.Select(i => new SearchHintInfo
|
2014-01-07 13:12:39 -07:00
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
Item = i
|
|
|
|
}).ToList();
|
2014-01-07 13:12:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|