2015-01-09 03:34:09 +00:00
|
|
|
|
using MediaBrowser.Common.Extensions;
|
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2014-01-07 20:12:39 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
2014-12-20 18:59:10 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
2014-01-07 20:12:39 +00:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Server.Implementations.Library
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class LuceneSearchEngine
|
|
|
|
|
/// http://www.codeproject.com/Articles/320219/Lucene-Net-ultra-fast-search-for-MVC-or-WebForms
|
|
|
|
|
/// </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)
|
|
|
|
|
{
|
2014-04-10 15:06:54 +00:00
|
|
|
|
IEnumerable<BaseItem> inputItems;
|
2014-01-07 20:12:39 +00:00
|
|
|
|
|
2015-01-25 06:34:50 +00:00
|
|
|
|
Func<BaseItem, bool> filter = i => !(i is ICollectionFolder);
|
|
|
|
|
|
2014-10-03 03:48:59 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(query.UserId))
|
2014-04-10 15:06:54 +00:00
|
|
|
|
{
|
2015-01-25 06:34:50 +00:00
|
|
|
|
inputItems = _libraryManager.RootFolder.GetRecursiveChildren(filter);
|
2014-04-10 15:06:54 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-09-14 15:10:51 +00:00
|
|
|
|
var user = _userManager.GetUserById(query.UserId);
|
2014-04-10 15:06:54 +00:00
|
|
|
|
|
2015-01-25 06:34:50 +00:00
|
|
|
|
inputItems = user.RootFolder.GetRecursiveChildren(user, filter);
|
2014-04-10 15:06:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-19 17:44:55 +00:00
|
|
|
|
inputItems = _libraryManager.ReplaceVideosWithPrimaryVersions(inputItems);
|
|
|
|
|
|
2014-01-07 20:12:39 +00:00
|
|
|
|
var results = await GetSearchHints(inputItems, query).ConfigureAwait(false);
|
|
|
|
|
|
2014-03-07 15:53:23 +00:00
|
|
|
|
// Include item types
|
|
|
|
|
if (query.IncludeItemTypes.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
results = results.Where(f => query.IncludeItemTypes.Contains(f.Item.GetType().Name, StringComparer.OrdinalIgnoreCase));
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-07 20:12:39 +00: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()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the search hints.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="inputItems">The input items.</param>
|
|
|
|
|
/// <param name="query">The query.</param>
|
|
|
|
|
/// <returns>IEnumerable{SearchHintResult}.</returns>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">searchTerm</exception>
|
|
|
|
|
private Task<IEnumerable<SearchHintInfo>> GetSearchHints(IEnumerable<BaseItem> inputItems, SearchQuery query)
|
|
|
|
|
{
|
|
|
|
|
var searchTerm = query.SearchTerm;
|
|
|
|
|
|
2014-10-03 03:48:59 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(searchTerm))
|
2014-01-07 20:12:39 +00:00
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("searchTerm");
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-09 03:34:09 +00:00
|
|
|
|
searchTerm = searchTerm.RemoveDiacritics();
|
|
|
|
|
|
2014-01-07 20:12:39 +00:00
|
|
|
|
var terms = GetWords(searchTerm);
|
|
|
|
|
|
|
|
|
|
var hints = new List<Tuple<BaseItem, string, int>>();
|
|
|
|
|
|
|
|
|
|
var items = inputItems.Where(i => !(i is MusicArtist)).ToList();
|
|
|
|
|
|
|
|
|
|
if (query.IncludeMedia)
|
|
|
|
|
{
|
|
|
|
|
// Add search hints based on item name
|
2014-12-20 18:59:10 +00:00
|
|
|
|
hints.AddRange(items.Where(i => !string.IsNullOrWhiteSpace(i.Name) && IncludeInSearch(i)).Select(item =>
|
2014-01-07 20:12:39 +00:00
|
|
|
|
{
|
|
|
|
|
var index = GetIndex(item.Name, searchTerm, terms);
|
|
|
|
|
|
|
|
|
|
return new Tuple<BaseItem, string, int>(item, index.Item1, index.Item2);
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (query.IncludeArtists)
|
|
|
|
|
{
|
|
|
|
|
// Find artists
|
2014-03-09 22:14:44 +00:00
|
|
|
|
var artists = items.OfType<Audio>()
|
|
|
|
|
.SelectMany(i => i.AllArtists)
|
2014-10-03 03:48:59 +00:00
|
|
|
|
.Where(i => !string.IsNullOrWhiteSpace(i))
|
2015-04-09 21:11:57 +00:00
|
|
|
|
.DistinctNames()
|
2014-01-07 20:12:39 +00:00
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
foreach (var item in artists)
|
|
|
|
|
{
|
|
|
|
|
var index = GetIndex(item, searchTerm, terms);
|
|
|
|
|
|
|
|
|
|
if (index.Item2 != -1)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var artist = _libraryManager.GetArtist(item);
|
|
|
|
|
|
|
|
|
|
hints.Add(new Tuple<BaseItem, string, int>(artist, index.Item1, index.Item2));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error getting {0}", ex, item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (query.IncludeGenres)
|
|
|
|
|
{
|
|
|
|
|
// Find genres, from non-audio items
|
|
|
|
|
var genres = items.Where(i => !(i is IHasMusicGenres) && !(i is Game))
|
|
|
|
|
.SelectMany(i => i.Genres)
|
2014-10-03 03:48:59 +00:00
|
|
|
|
.Where(i => !string.IsNullOrWhiteSpace(i))
|
2014-01-07 20:12:39 +00:00
|
|
|
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
foreach (var item in genres)
|
|
|
|
|
{
|
|
|
|
|
var index = GetIndex(item, searchTerm, terms);
|
|
|
|
|
|
|
|
|
|
if (index.Item2 != -1)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var genre = _libraryManager.GetGenre(item);
|
|
|
|
|
|
|
|
|
|
hints.Add(new Tuple<BaseItem, string, int>(genre, index.Item1, index.Item2));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error getting {0}", ex, item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find music genres
|
|
|
|
|
var musicGenres = items.Where(i => i is IHasMusicGenres)
|
|
|
|
|
.SelectMany(i => i.Genres)
|
2014-10-03 03:48:59 +00:00
|
|
|
|
.Where(i => !string.IsNullOrWhiteSpace(i))
|
2014-01-07 20:12:39 +00:00
|
|
|
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
foreach (var item in musicGenres)
|
|
|
|
|
{
|
|
|
|
|
var index = GetIndex(item, searchTerm, terms);
|
|
|
|
|
|
|
|
|
|
if (index.Item2 != -1)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var genre = _libraryManager.GetMusicGenre(item);
|
|
|
|
|
|
|
|
|
|
hints.Add(new Tuple<BaseItem, string, int>(genre, index.Item1, index.Item2));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error getting {0}", ex, item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find music genres
|
|
|
|
|
var gameGenres = items.OfType<Game>()
|
|
|
|
|
.SelectMany(i => i.Genres)
|
2014-10-03 03:48:59 +00:00
|
|
|
|
.Where(i => !string.IsNullOrWhiteSpace(i))
|
2014-01-07 20:12:39 +00:00
|
|
|
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
foreach (var item in gameGenres)
|
|
|
|
|
{
|
|
|
|
|
var index = GetIndex(item, searchTerm, terms);
|
|
|
|
|
|
|
|
|
|
if (index.Item2 != -1)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var genre = _libraryManager.GetGameGenre(item);
|
|
|
|
|
|
|
|
|
|
hints.Add(new Tuple<BaseItem, string, int>(genre, index.Item1, index.Item2));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error getting {0}", ex, item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (query.IncludeStudios)
|
|
|
|
|
{
|
|
|
|
|
// Find studios
|
|
|
|
|
var studios = items.SelectMany(i => i.Studios)
|
2014-10-03 03:48:59 +00:00
|
|
|
|
.Where(i => !string.IsNullOrWhiteSpace(i))
|
2014-01-07 20:12:39 +00:00
|
|
|
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
foreach (var item in studios)
|
|
|
|
|
{
|
|
|
|
|
var index = GetIndex(item, searchTerm, terms);
|
|
|
|
|
|
|
|
|
|
if (index.Item2 != -1)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var studio = _libraryManager.GetStudio(item);
|
|
|
|
|
|
|
|
|
|
hints.Add(new Tuple<BaseItem, string, int>(studio, index.Item1, index.Item2));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error getting {0}", ex, item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (query.IncludePeople)
|
|
|
|
|
{
|
|
|
|
|
// Find persons
|
|
|
|
|
var persons = items.SelectMany(i => i.People)
|
|
|
|
|
.Select(i => i.Name)
|
2014-10-03 03:48:59 +00:00
|
|
|
|
.Where(i => !string.IsNullOrWhiteSpace(i))
|
2014-01-07 20:12:39 +00:00
|
|
|
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
foreach (var item in persons)
|
|
|
|
|
{
|
|
|
|
|
var index = GetIndex(item, searchTerm, terms);
|
|
|
|
|
|
|
|
|
|
if (index.Item2 != -1)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var person = _libraryManager.GetPerson(item);
|
|
|
|
|
|
|
|
|
|
hints.Add(new Tuple<BaseItem, string, int>(person, index.Item1, index.Item2));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error getting {0}", ex, item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var returnValue = hints.Where(i => i.Item3 >= 0).OrderBy(i => i.Item3).Select(i => new SearchHintInfo
|
|
|
|
|
{
|
|
|
|
|
Item = i.Item1,
|
|
|
|
|
MatchedTerm = i.Item2
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return Task.FromResult(returnValue);
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-20 18:59:10 +00:00
|
|
|
|
private bool IncludeInSearch(BaseItem item)
|
|
|
|
|
{
|
|
|
|
|
var episode = item as Episode;
|
|
|
|
|
|
|
|
|
|
if (episode != null)
|
|
|
|
|
{
|
|
|
|
|
if (episode.IsVirtualUnaired || episode.IsMissingEpisode)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-07 20:12:39 +00:00
|
|
|
|
/// <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-03 03:48:59 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(input))
|
2014-01-07 20:12:39 +00:00
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("input");
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-09 03:34:09 +00:00
|
|
|
|
input = input.RemoveDiacritics();
|
|
|
|
|
|
2014-01-07 20:12:39 +00: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-24 01:44:05 +00: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 20:12:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|