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