2013-09-25 18:58:03 +00:00
|
|
|
|
using MediaBrowser.Controller;
|
2013-04-23 21:14:07 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2013-04-26 19:20:53 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
2013-04-23 21:14:07 +00:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
2013-04-26 19:20:53 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2013-03-28 22:00:58 +00:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Server.Implementations.Library
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class LuceneSearchEngine
|
|
|
|
|
/// http://www.codeproject.com/Articles/320219/Lucene-Net-ultra-fast-search-for-MVC-or-WebForms
|
|
|
|
|
/// </summary>
|
2013-04-23 21:14:07 +00:00
|
|
|
|
public class LuceneSearchEngine : ILibrarySearchEngine, IDisposable
|
2013-03-28 22:00:58 +00:00
|
|
|
|
{
|
2013-04-26 19:20:53 +00:00
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
|
|
public LuceneSearchEngine(IServerApplicationPaths serverPaths, ILogManager logManager, ILibraryManager libraryManager)
|
2013-04-23 21:14:07 +00:00
|
|
|
|
{
|
2013-04-26 19:20:53 +00:00
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
|
|
|
|
|
|
_logger = logManager.GetLogger("Lucene");
|
2013-04-23 21:14:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-28 22:00:58 +00:00
|
|
|
|
/// <summary>
|
2013-03-28 22:08:04 +00:00
|
|
|
|
/// Searches items and returns them in order of relevance.
|
2013-03-28 22:00:58 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="items">The items.</param>
|
|
|
|
|
/// <param name="searchTerm">The search term.</param>
|
|
|
|
|
/// <returns>IEnumerable{BaseItem}.</returns>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">searchTerm</exception>
|
|
|
|
|
public IEnumerable<BaseItem> Search(IEnumerable<BaseItem> items, string searchTerm)
|
|
|
|
|
{
|
2013-09-25 18:58:03 +00:00
|
|
|
|
return items;
|
2013-04-23 21:14:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2013-04-24 00:25:49 +00:00
|
|
|
|
//BaseItem.LibraryManager.LibraryChanged -= LibraryChanged;
|
2013-04-23 21:14:07 +00:00
|
|
|
|
|
2013-04-24 00:25:49 +00:00
|
|
|
|
//LuceneSearch.CloseAll();
|
2013-04-23 21:14:07 +00:00
|
|
|
|
}
|
2013-04-26 19:20:53 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the search hints.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="inputItems">The input items.</param>
|
|
|
|
|
/// <param name="searchTerm">The search term.</param>
|
|
|
|
|
/// <returns>IEnumerable{SearchHintResult}.</returns>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">searchTerm</exception>
|
2013-09-17 02:08:18 +00:00
|
|
|
|
public Task<IEnumerable<SearchHintInfo>> GetSearchHints(IEnumerable<BaseItem> inputItems, string searchTerm)
|
2013-04-26 19:20:53 +00:00
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(searchTerm))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("searchTerm");
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-27 13:05:33 +00:00
|
|
|
|
var terms = GetWords(searchTerm);
|
|
|
|
|
|
|
|
|
|
var hints = new List<Tuple<BaseItem, string, int>>();
|
2013-04-26 19:20:53 +00:00
|
|
|
|
|
|
|
|
|
var items = inputItems.Where(i => !(i is MusicArtist)).ToList();
|
|
|
|
|
|
2013-06-11 03:31:00 +00:00
|
|
|
|
// Add search hints based on item name
|
2013-06-14 12:19:09 +00:00
|
|
|
|
hints.AddRange(items.Where(i => !string.IsNullOrEmpty(i.Name)).Select(item =>
|
2013-04-26 19:20:53 +00:00
|
|
|
|
{
|
2013-04-27 13:05:33 +00:00
|
|
|
|
var index = GetIndex(item.Name, searchTerm, terms);
|
2013-04-26 19:20:53 +00:00
|
|
|
|
|
2013-04-27 22:04:14 +00:00
|
|
|
|
return new Tuple<BaseItem, string, int>(item, index.Item1, index.Item2);
|
|
|
|
|
}));
|
2013-04-26 19:20:53 +00:00
|
|
|
|
|
2013-04-26 20:53:54 +00:00
|
|
|
|
// Find artists
|
2013-04-26 19:20:53 +00:00
|
|
|
|
var artists = items.OfType<Audio>()
|
2013-09-05 19:00:50 +00:00
|
|
|
|
.SelectMany(i =>
|
|
|
|
|
{
|
|
|
|
|
var list = new List<string>();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(i.AlbumArtist))
|
|
|
|
|
{
|
|
|
|
|
list.Add(i.AlbumArtist);
|
|
|
|
|
}
|
|
|
|
|
list.AddRange(i.Artists);
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
})
|
2013-04-26 20:53:54 +00:00
|
|
|
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
2013-04-26 19:20:53 +00:00
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
foreach (var item in artists)
|
|
|
|
|
{
|
2013-04-27 13:05:33 +00:00
|
|
|
|
var index = GetIndex(item, searchTerm, terms);
|
2013-04-26 19:20:53 +00:00
|
|
|
|
|
2013-04-27 13:05:33 +00:00
|
|
|
|
if (index.Item2 != -1)
|
2013-04-26 19:20:53 +00:00
|
|
|
|
{
|
2013-04-27 13:05:33 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2013-09-17 02:08:18 +00:00
|
|
|
|
var artist = _libraryManager.GetArtist(item);
|
2013-04-27 13:05:33 +00:00
|
|
|
|
|
|
|
|
|
hints.Add(new Tuple<BaseItem, string, int>(artist, index.Item1, index.Item2));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error getting {0}", ex, item);
|
|
|
|
|
}
|
2013-04-26 19:20:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-04-27 13:05:33 +00:00
|
|
|
|
|
2013-06-11 03:31:00 +00:00
|
|
|
|
// Find genres, from non-audio items
|
2013-09-10 18:56:00 +00:00
|
|
|
|
var genres = items.Where(i => !(i is IHasMusicGenres) && !(i is Game))
|
2013-06-11 03:31:00 +00:00
|
|
|
|
.SelectMany(i => i.Genres)
|
2013-04-26 19:20:53 +00:00
|
|
|
|
.Where(i => !string.IsNullOrEmpty(i))
|
|
|
|
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
foreach (var item in genres)
|
|
|
|
|
{
|
2013-04-27 13:05:33 +00:00
|
|
|
|
var index = GetIndex(item, searchTerm, terms);
|
2013-04-26 19:20:53 +00:00
|
|
|
|
|
2013-04-27 13:05:33 +00:00
|
|
|
|
if (index.Item2 != -1)
|
2013-04-26 19:20:53 +00:00
|
|
|
|
{
|
2013-04-27 13:05:33 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2013-09-17 02:08:18 +00:00
|
|
|
|
var genre = _libraryManager.GetGenre(item);
|
2013-04-27 13:05:33 +00:00
|
|
|
|
|
|
|
|
|
hints.Add(new Tuple<BaseItem, string, int>(genre, index.Item1, index.Item2));
|
|
|
|
|
}
|
2013-06-11 03:31:00 +00:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error getting {0}", ex, item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find music genres
|
2013-09-10 18:56:00 +00:00
|
|
|
|
var musicGenres = items.Where(i => i is IHasMusicGenres)
|
2013-06-11 03:31:00 +00:00
|
|
|
|
.SelectMany(i => i.Genres)
|
|
|
|
|
.Where(i => !string.IsNullOrEmpty(i))
|
|
|
|
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
foreach (var item in musicGenres)
|
|
|
|
|
{
|
|
|
|
|
var index = GetIndex(item, searchTerm, terms);
|
|
|
|
|
|
|
|
|
|
if (index.Item2 != -1)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2013-09-17 02:08:18 +00:00
|
|
|
|
var genre = _libraryManager.GetMusicGenre(item);
|
2013-06-11 03:31:00 +00:00
|
|
|
|
|
|
|
|
|
hints.Add(new Tuple<BaseItem, string, int>(genre, index.Item1, index.Item2));
|
|
|
|
|
}
|
2013-04-27 13:05:33 +00:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error getting {0}", ex, item);
|
2013-07-01 18:17:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find music genres
|
|
|
|
|
var gameGenres = items.OfType<Game>()
|
|
|
|
|
.SelectMany(i => i.Genres)
|
|
|
|
|
.Where(i => !string.IsNullOrEmpty(i))
|
|
|
|
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
foreach (var item in gameGenres)
|
|
|
|
|
{
|
|
|
|
|
var index = GetIndex(item, searchTerm, terms);
|
|
|
|
|
|
|
|
|
|
if (index.Item2 != -1)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2013-09-17 02:08:18 +00:00
|
|
|
|
var genre = _libraryManager.GetGameGenre(item);
|
2013-07-01 18:17:55 +00:00
|
|
|
|
|
|
|
|
|
hints.Add(new Tuple<BaseItem, string, int>(genre, index.Item1, index.Item2));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error getting {0}", ex, item);
|
2013-04-27 13:05:33 +00:00
|
|
|
|
}
|
2013-04-26 19:20:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find studios
|
|
|
|
|
var studios = items.SelectMany(i => i.Studios)
|
|
|
|
|
.Where(i => !string.IsNullOrEmpty(i))
|
|
|
|
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
foreach (var item in studios)
|
|
|
|
|
{
|
2013-04-27 13:05:33 +00:00
|
|
|
|
var index = GetIndex(item, searchTerm, terms);
|
2013-04-26 19:20:53 +00:00
|
|
|
|
|
2013-04-27 13:05:33 +00:00
|
|
|
|
if (index.Item2 != -1)
|
2013-04-26 19:20:53 +00:00
|
|
|
|
{
|
2013-04-27 13:05:33 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2013-09-17 02:08:18 +00:00
|
|
|
|
var studio = _libraryManager.GetStudio(item);
|
2013-04-27 13:05:33 +00:00
|
|
|
|
|
|
|
|
|
hints.Add(new Tuple<BaseItem, string, int>(studio, index.Item1, index.Item2));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error getting {0}", ex, item);
|
|
|
|
|
}
|
2013-04-26 19:20:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find persons
|
|
|
|
|
var persons = items.SelectMany(i => i.People)
|
|
|
|
|
.Select(i => i.Name)
|
|
|
|
|
.Where(i => !string.IsNullOrEmpty(i))
|
|
|
|
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
foreach (var item in persons)
|
|
|
|
|
{
|
2013-04-27 13:05:33 +00:00
|
|
|
|
var index = GetIndex(item, searchTerm, terms);
|
2013-04-26 19:20:53 +00:00
|
|
|
|
|
2013-04-27 13:05:33 +00:00
|
|
|
|
if (index.Item2 != -1)
|
2013-04-26 19:20:53 +00:00
|
|
|
|
{
|
2013-04-27 13:05:33 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2013-09-17 02:08:18 +00:00
|
|
|
|
var person = _libraryManager.GetPerson(item);
|
2013-04-27 13:05:33 +00:00
|
|
|
|
|
|
|
|
|
hints.Add(new Tuple<BaseItem, string, int>(person, index.Item1, index.Item2));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error getting {0}", ex, item);
|
|
|
|
|
}
|
2013-04-26 19:20:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-17 02:08:18 +00:00
|
|
|
|
var returnValue = hints.Where(i => i.Item3 >= 0).OrderBy(i => i.Item3).Select(i => new SearchHintInfo
|
2013-04-27 13:05:33 +00:00
|
|
|
|
{
|
|
|
|
|
Item = i.Item1,
|
|
|
|
|
MatchedTerm = i.Item2
|
|
|
|
|
});
|
2013-09-17 02:08:18 +00:00
|
|
|
|
|
|
|
|
|
return Task.FromResult(returnValue);
|
2013-04-26 19:20:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2013-04-27 13:05:33 +00:00
|
|
|
|
/// Gets the index.
|
2013-04-26 19:20:53 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="input">The input.</param>
|
2013-04-27 13:05:33 +00:00
|
|
|
|
/// <param name="searchInput">The search input.</param>
|
|
|
|
|
/// <param name="searchWords">The search input.</param>
|
2013-04-26 19:20:53 +00:00
|
|
|
|
/// <returns>System.Int32.</returns>
|
2013-09-20 00:53:18 +00:00
|
|
|
|
private Tuple<string, int> GetIndex(string input, string searchInput, List<string> searchWords)
|
2013-04-26 19:20:53 +00:00
|
|
|
|
{
|
2013-06-14 12:19:09 +00:00
|
|
|
|
if (string.IsNullOrEmpty(input))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("input");
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-27 13:05:33 +00:00
|
|
|
|
if (string.Equals(input, searchInput, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return new Tuple<string, int>(searchInput, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-30 01:29:04 +00:00
|
|
|
|
var index = input.IndexOf(searchInput, StringComparison.OrdinalIgnoreCase);
|
2013-04-27 13:05:33 +00:00
|
|
|
|
|
2013-04-30 01:29:04 +00:00
|
|
|
|
if (index == 0)
|
2013-04-27 13:05:33 +00:00
|
|
|
|
{
|
2013-04-30 01:29:04 +00:00
|
|
|
|
return new Tuple<string, int>(searchInput, 1);
|
|
|
|
|
}
|
|
|
|
|
if (index > 0)
|
|
|
|
|
{
|
|
|
|
|
return new Tuple<string, int>(searchInput, 2);
|
2013-04-27 13:05:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var items = GetWords(input);
|
2013-04-26 19:20:53 +00:00
|
|
|
|
|
2013-09-20 00:53:18 +00:00
|
|
|
|
for (var i = 0; i < searchWords.Count; i++)
|
2013-04-26 19:20:53 +00:00
|
|
|
|
{
|
2013-04-27 13:05:33 +00:00
|
|
|
|
var searchTerm = searchWords[i];
|
|
|
|
|
|
2013-09-20 00:53:18 +00:00
|
|
|
|
for (var j = 0; j < items.Count; j++)
|
2013-04-26 19:20:53 +00:00
|
|
|
|
{
|
2013-04-27 13:05:33 +00:00
|
|
|
|
var item = items[j];
|
|
|
|
|
|
|
|
|
|
if (string.Equals(item, searchTerm, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return new Tuple<string, int>(searchTerm, 3 + (i + 1) * (j + 1));
|
|
|
|
|
}
|
2013-04-27 22:04:14 +00:00
|
|
|
|
|
2013-04-30 01:29:04 +00:00
|
|
|
|
index = item.IndexOf(searchTerm, StringComparison.OrdinalIgnoreCase);
|
2013-04-27 13:05:33 +00:00
|
|
|
|
|
2013-04-30 01:29:04 +00:00
|
|
|
|
if (index == 0)
|
|
|
|
|
{
|
|
|
|
|
return new Tuple<string, int>(searchTerm, 4 + (i + 1) * (j + 1));
|
|
|
|
|
}
|
|
|
|
|
if (index > 0)
|
2013-04-27 13:05:33 +00:00
|
|
|
|
{
|
2013-04-30 01:29:04 +00:00
|
|
|
|
return new Tuple<string, int>(searchTerm, 5 + (i + 1) * (j + 1));
|
2013-04-27 13:05:33 +00:00
|
|
|
|
}
|
2013-04-26 19:20:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-04-27 13:05:33 +00:00
|
|
|
|
return new Tuple<string, int>(null, -1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the words.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="term">The term.</param>
|
|
|
|
|
/// <returns>System.String[][].</returns>
|
2013-09-20 00:53:18 +00:00
|
|
|
|
private List<string> GetWords(string term)
|
2013-04-27 13:05:33 +00:00
|
|
|
|
{
|
2013-09-20 00:53:18 +00:00
|
|
|
|
return term.Split().Where(i => !string.IsNullOrWhiteSpace(i)).ToList();
|
2013-04-26 19:20:53 +00:00
|
|
|
|
}
|
2013-04-23 21:14:07 +00:00
|
|
|
|
}
|
2013-03-28 22:00:58 +00:00
|
|
|
|
}
|