2016-11-02 23:37:52 -07:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2014-01-07 13:12:39 -07:00
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using MediaBrowser.Model.Querying;
|
|
|
|
|
using MediaBrowser.Model.Search;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-05-21 00:25:49 -07:00
|
|
|
|
using MediaBrowser.Controller.Dto;
|
2016-10-23 12:14:57 -07:00
|
|
|
|
using MediaBrowser.Controller.Extensions;
|
2017-08-09 12:56:38 -07:00
|
|
|
|
using MediaBrowser.Model.Extensions;
|
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
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class SearchEngine : ISearchEngine
|
|
|
|
|
{
|
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
private readonly IUserManager _userManager;
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
|
|
public SearchEngine(ILogManager logManager, ILibraryManager libraryManager, IUserManager userManager)
|
|
|
|
|
{
|
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
|
_userManager = userManager;
|
|
|
|
|
|
|
|
|
|
_logger = logManager.GetLogger("Lucene");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<QueryResult<SearchHintInfo>> GetSearchHints(SearchQuery query)
|
|
|
|
|
{
|
2015-07-18 12:32:59 -07:00
|
|
|
|
User user = null;
|
|
|
|
|
|
2014-10-02 20:48:59 -07:00
|
|
|
|
if (string.IsNullOrWhiteSpace(query.UserId))
|
2014-04-10 08:06:54 -07:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2015-07-18 12:32:59 -07:00
|
|
|
|
user = _userManager.GetUserById(query.UserId);
|
2014-04-10 08:06:54 -07:00
|
|
|
|
}
|
|
|
|
|
|
2015-08-21 19:59:10 -07:00
|
|
|
|
var results = await GetSearchHints(query, user).ConfigureAwait(false);
|
2014-03-07 08:53:23 -07:00
|
|
|
|
|
2014-01-07 13:12:39 -07:00
|
|
|
|
var searchResultArray = results.ToArray();
|
|
|
|
|
results = searchResultArray;
|
|
|
|
|
|
|
|
|
|
var count = searchResultArray.Length;
|
|
|
|
|
|
|
|
|
|
if (query.StartIndex.HasValue)
|
|
|
|
|
{
|
|
|
|
|
results = results.Skip(query.StartIndex.Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (query.Limit.HasValue)
|
|
|
|
|
{
|
|
|
|
|
results = results.Take(query.Limit.Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new QueryResult<SearchHintInfo>
|
|
|
|
|
{
|
|
|
|
|
TotalRecordCount = count,
|
|
|
|
|
|
|
|
|
|
Items = results.ToArray()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-21 19:59:10 -07:00
|
|
|
|
private void AddIfMissing(List<string> list, string value)
|
|
|
|
|
{
|
|
|
|
|
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>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">searchTerm</exception>
|
2015-08-21 19:59:10 -07:00
|
|
|
|
private Task<IEnumerable<SearchHintInfo>> GetSearchHints(SearchQuery query, User user)
|
2014-01-07 13:12:39 -07:00
|
|
|
|
{
|
|
|
|
|
var searchTerm = query.SearchTerm;
|
|
|
|
|
|
2016-04-27 10:43:09 -07:00
|
|
|
|
if (searchTerm != null)
|
|
|
|
|
{
|
|
|
|
|
searchTerm = searchTerm.Trim().RemoveDiacritics();
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-02 20:48:59 -07:00
|
|
|
|
if (string.IsNullOrWhiteSpace(searchTerm))
|
2014-01-07 13:12:39 -07:00
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("searchTerm");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var terms = GetWords(searchTerm);
|
|
|
|
|
|
|
|
|
|
var hints = new List<Tuple<BaseItem, string, int>>();
|
|
|
|
|
|
2017-05-06 13:21:08 -07:00
|
|
|
|
var excludeItemTypes = query.ExcludeItemTypes.ToList();
|
2015-08-21 19:59:10 -07:00
|
|
|
|
var includeItemTypes = (query.IncludeItemTypes ?? new string[] { }).ToList();
|
2014-01-07 13:12:39 -07:00
|
|
|
|
|
2015-08-21 19:59:10 -07:00
|
|
|
|
excludeItemTypes.Add(typeof(Year).Name);
|
2016-10-10 11:18:28 -07:00
|
|
|
|
excludeItemTypes.Add(typeof(Folder).Name);
|
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
|
|
|
|
{
|
2015-08-21 19:59:10 -07:00
|
|
|
|
AddIfMissing(includeItemTypes, typeof(Genre).Name);
|
|
|
|
|
AddIfMissing(includeItemTypes, typeof(GameGenre).Name);
|
|
|
|
|
AddIfMissing(includeItemTypes, typeof(MusicGenre).Name);
|
2014-01-07 13:12:39 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-21 19:59:10 -07:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
AddIfMissing(excludeItemTypes, typeof(Genre).Name);
|
|
|
|
|
AddIfMissing(excludeItemTypes, typeof(GameGenre).Name);
|
|
|
|
|
AddIfMissing(excludeItemTypes, typeof(MusicGenre).Name);
|
|
|
|
|
}
|
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
|
|
|
|
{
|
2015-08-21 19:59:10 -07:00
|
|
|
|
AddIfMissing(includeItemTypes, typeof(Person).Name);
|
2014-01-07 13:12:39 -07:00
|
|
|
|
}
|
2015-08-21 19:59:10 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
AddIfMissing(excludeItemTypes, typeof(Person).Name);
|
|
|
|
|
}
|
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
|
|
|
|
{
|
2015-08-21 19:59:10 -07:00
|
|
|
|
AddIfMissing(includeItemTypes, typeof(Studio).Name);
|
2014-01-07 13:12:39 -07:00
|
|
|
|
}
|
2015-08-21 19:59:10 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
AddIfMissing(excludeItemTypes, typeof(Studio).Name);
|
|
|
|
|
}
|
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
|
|
|
|
{
|
2015-08-21 19:59:10 -07:00
|
|
|
|
AddIfMissing(includeItemTypes, typeof(MusicArtist).Name);
|
2014-01-07 13:12:39 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-21 19:59:10 -07:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
AddIfMissing(excludeItemTypes, typeof(MusicArtist).Name);
|
|
|
|
|
}
|
2014-01-07 13:12:39 -07:00
|
|
|
|
|
2015-10-07 18:49:40 -07:00
|
|
|
|
AddIfMissing(excludeItemTypes, typeof(CollectionFolder).Name);
|
2017-01-09 10:05:34 -07:00
|
|
|
|
AddIfMissing(excludeItemTypes, typeof(Folder).Name);
|
2015-10-29 06:28:05 -07:00
|
|
|
|
|
2016-03-19 23:46:51 -07:00
|
|
|
|
var mediaItems = _libraryManager.GetItemList(new InternalItemsQuery(user)
|
2014-01-07 13:12:39 -07:00
|
|
|
|
{
|
2015-08-21 19:59:10 -07:00
|
|
|
|
NameContains = searchTerm,
|
2017-08-09 12:56:38 -07:00
|
|
|
|
ExcludeItemTypes = excludeItemTypes.ToArray(excludeItemTypes.Count),
|
|
|
|
|
IncludeItemTypes = includeItemTypes.ToArray(includeItemTypes.Count),
|
2015-11-02 10:25:01 -07:00
|
|
|
|
Limit = query.Limit,
|
2017-05-01 13:03:27 -07:00
|
|
|
|
IncludeItemsByName = string.IsNullOrWhiteSpace(query.ParentId),
|
|
|
|
|
ParentId = string.IsNullOrWhiteSpace(query.ParentId) ? (Guid?)null : new Guid(query.ParentId),
|
|
|
|
|
SortBy = new[] { ItemSortBy.SortName },
|
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-05-21 00:25:49 -07:00
|
|
|
|
MediaTypes = query.MediaTypes,
|
|
|
|
|
|
|
|
|
|
DtoOptions = new DtoOptions
|
|
|
|
|
{
|
|
|
|
|
Fields = new List<ItemFields>
|
|
|
|
|
{
|
|
|
|
|
ItemFields.AirTime,
|
|
|
|
|
ItemFields.DateCreated,
|
|
|
|
|
ItemFields.ChannelInfo
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-30 12:01:48 -07:00
|
|
|
|
});
|
2014-01-07 13:12:39 -07:00
|
|
|
|
|
2015-08-21 19:59:10 -07:00
|
|
|
|
// Add search hints based on item name
|
2016-08-17 22:56:10 -07:00
|
|
|
|
hints.AddRange(mediaItems.Select(item =>
|
2014-01-07 13:12:39 -07:00
|
|
|
|
{
|
2015-08-21 19:59:10 -07:00
|
|
|
|
var index = GetIndex(item.Name, searchTerm, terms);
|
2015-07-09 20:00:03 -07:00
|
|
|
|
|
2015-08-21 19:59:10 -07:00
|
|
|
|
return new Tuple<BaseItem, string, int>(item, index.Item1, index.Item2);
|
|
|
|
|
}));
|
2014-01-07 13:12:39 -07:00
|
|
|
|
|
2017-02-01 13:56:14 -07:00
|
|
|
|
var returnValue = hints.Where(i => i.Item3 >= 0).OrderBy(i => i.Item3).ThenBy(i => i.Item1.SortName).Select(i => new SearchHintInfo
|
2014-01-07 13:12:39 -07:00
|
|
|
|
{
|
|
|
|
|
Item = i.Item1,
|
|
|
|
|
MatchedTerm = i.Item2
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return Task.FromResult(returnValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the index.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="input">The input.</param>
|
|
|
|
|
/// <param name="searchInput">The search input.</param>
|
|
|
|
|
/// <param name="searchWords">The search input.</param>
|
|
|
|
|
/// <returns>System.Int32.</returns>
|
|
|
|
|
private Tuple<string, int> GetIndex(string input, string searchInput, List<string> searchWords)
|
|
|
|
|
{
|
2014-10-02 20:48:59 -07:00
|
|
|
|
if (string.IsNullOrWhiteSpace(input))
|
2014-01-07 13:12:39 -07:00
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("input");
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-08 20:34:09 -07:00
|
|
|
|
input = input.RemoveDiacritics();
|
|
|
|
|
|
2014-01-07 13:12:39 -07:00
|
|
|
|
if (string.Equals(input, searchInput, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return new Tuple<string, int>(searchInput, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var index = input.IndexOf(searchInput, StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
|
|
if (index == 0)
|
|
|
|
|
{
|
|
|
|
|
return new Tuple<string, int>(searchInput, 1);
|
|
|
|
|
}
|
|
|
|
|
if (index > 0)
|
|
|
|
|
{
|
|
|
|
|
return new Tuple<string, int>(searchInput, 2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var items = GetWords(input);
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < searchWords.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
var searchTerm = searchWords[i];
|
|
|
|
|
|
|
|
|
|
for (var j = 0; j < items.Count; j++)
|
|
|
|
|
{
|
|
|
|
|
var item = items[j];
|
|
|
|
|
|
|
|
|
|
if (string.Equals(item, searchTerm, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return new Tuple<string, int>(searchTerm, 3 + (i + 1) * (j + 1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
index = item.IndexOf(searchTerm, StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
|
|
if (index == 0)
|
|
|
|
|
{
|
|
|
|
|
return new Tuple<string, int>(searchTerm, 4 + (i + 1) * (j + 1));
|
|
|
|
|
}
|
|
|
|
|
if (index > 0)
|
|
|
|
|
{
|
|
|
|
|
return new Tuple<string, int>(searchTerm, 5 + (i + 1) * (j + 1));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return new Tuple<string, int>(null, -1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the words.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="term">The term.</param>
|
|
|
|
|
/// <returns>System.String[][].</returns>
|
|
|
|
|
private List<string> GetWords(string term)
|
|
|
|
|
{
|
2014-09-23 18:44:05 -07:00
|
|
|
|
var stoplist = GetStopList().ToList();
|
|
|
|
|
|
|
|
|
|
return term.Split()
|
|
|
|
|
.Where(i => !string.IsNullOrWhiteSpace(i) && !stoplist.Contains(i, StringComparer.OrdinalIgnoreCase))
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerable<string> GetStopList()
|
|
|
|
|
{
|
|
|
|
|
return new[]
|
|
|
|
|
{
|
|
|
|
|
"the",
|
|
|
|
|
"a",
|
|
|
|
|
"of",
|
|
|
|
|
"an"
|
|
|
|
|
};
|
2014-01-07 13:12:39 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|