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