LuceneSearchEngine.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. using MediaBrowser.Controller;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Entities.Audio;
  4. using MediaBrowser.Controller.Library;
  5. using MediaBrowser.Model.Logging;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Threading.Tasks;
  10. namespace MediaBrowser.Server.Implementations.Library
  11. {
  12. /// <summary>
  13. /// Class LuceneSearchEngine
  14. /// http://www.codeproject.com/Articles/320219/Lucene-Net-ultra-fast-search-for-MVC-or-WebForms
  15. /// </summary>
  16. public class LuceneSearchEngine : ILibrarySearchEngine, IDisposable
  17. {
  18. private readonly ILibraryManager _libraryManager;
  19. private readonly ILogger _logger;
  20. public LuceneSearchEngine(IServerApplicationPaths serverPaths, ILogManager logManager, ILibraryManager libraryManager)
  21. {
  22. _libraryManager = libraryManager;
  23. _logger = logManager.GetLogger("Lucene");
  24. }
  25. /// <summary>
  26. /// Searches items and returns them in order of relevance.
  27. /// </summary>
  28. /// <param name="items">The items.</param>
  29. /// <param name="searchTerm">The search term.</param>
  30. /// <returns>IEnumerable{BaseItem}.</returns>
  31. /// <exception cref="System.ArgumentNullException">searchTerm</exception>
  32. public IEnumerable<BaseItem> Search(IEnumerable<BaseItem> items, string searchTerm)
  33. {
  34. return items;
  35. }
  36. public void Dispose()
  37. {
  38. //BaseItem.LibraryManager.LibraryChanged -= LibraryChanged;
  39. //LuceneSearch.CloseAll();
  40. }
  41. /// <summary>
  42. /// Gets the search hints.
  43. /// </summary>
  44. /// <param name="inputItems">The input items.</param>
  45. /// <param name="searchTerm">The search term.</param>
  46. /// <returns>IEnumerable{SearchHintResult}.</returns>
  47. /// <exception cref="System.ArgumentNullException">searchTerm</exception>
  48. public Task<IEnumerable<SearchHintInfo>> GetSearchHints(IEnumerable<BaseItem> inputItems, string searchTerm)
  49. {
  50. if (string.IsNullOrEmpty(searchTerm))
  51. {
  52. throw new ArgumentNullException("searchTerm");
  53. }
  54. var terms = GetWords(searchTerm);
  55. var hints = new List<Tuple<BaseItem, string, int>>();
  56. var items = inputItems.Where(i => !(i is MusicArtist)).ToList();
  57. // Add search hints based on item name
  58. hints.AddRange(items.Where(i => !string.IsNullOrEmpty(i.Name)).Select(item =>
  59. {
  60. var index = GetIndex(item.Name, searchTerm, terms);
  61. return new Tuple<BaseItem, string, int>(item, index.Item1, index.Item2);
  62. }));
  63. // Find artists
  64. var artists = _libraryManager.GetAllArtists(items)
  65. .ToList();
  66. foreach (var item in artists)
  67. {
  68. var index = GetIndex(item, searchTerm, terms);
  69. if (index.Item2 != -1)
  70. {
  71. try
  72. {
  73. var artist = _libraryManager.GetArtist(item);
  74. hints.Add(new Tuple<BaseItem, string, int>(artist, index.Item1, index.Item2));
  75. }
  76. catch (Exception ex)
  77. {
  78. _logger.ErrorException("Error getting {0}", ex, item);
  79. }
  80. }
  81. }
  82. // Find genres, from non-audio items
  83. var genres = items.Where(i => !(i is IHasMusicGenres) && !(i is Game))
  84. .SelectMany(i => i.Genres)
  85. .Where(i => !string.IsNullOrEmpty(i))
  86. .Distinct(StringComparer.OrdinalIgnoreCase)
  87. .ToList();
  88. foreach (var item in genres)
  89. {
  90. var index = GetIndex(item, searchTerm, terms);
  91. if (index.Item2 != -1)
  92. {
  93. try
  94. {
  95. var genre = _libraryManager.GetGenre(item);
  96. hints.Add(new Tuple<BaseItem, string, int>(genre, index.Item1, index.Item2));
  97. }
  98. catch (Exception ex)
  99. {
  100. _logger.ErrorException("Error getting {0}", ex, item);
  101. }
  102. }
  103. }
  104. // Find music genres
  105. var musicGenres = items.Where(i => i is IHasMusicGenres)
  106. .SelectMany(i => i.Genres)
  107. .Where(i => !string.IsNullOrEmpty(i))
  108. .Distinct(StringComparer.OrdinalIgnoreCase)
  109. .ToList();
  110. foreach (var item in musicGenres)
  111. {
  112. var index = GetIndex(item, searchTerm, terms);
  113. if (index.Item2 != -1)
  114. {
  115. try
  116. {
  117. var genre = _libraryManager.GetMusicGenre(item);
  118. hints.Add(new Tuple<BaseItem, string, int>(genre, index.Item1, index.Item2));
  119. }
  120. catch (Exception ex)
  121. {
  122. _logger.ErrorException("Error getting {0}", ex, item);
  123. }
  124. }
  125. }
  126. // Find music genres
  127. var gameGenres = items.OfType<Game>()
  128. .SelectMany(i => i.Genres)
  129. .Where(i => !string.IsNullOrEmpty(i))
  130. .Distinct(StringComparer.OrdinalIgnoreCase)
  131. .ToList();
  132. foreach (var item in gameGenres)
  133. {
  134. var index = GetIndex(item, searchTerm, terms);
  135. if (index.Item2 != -1)
  136. {
  137. try
  138. {
  139. var genre = _libraryManager.GetGameGenre(item);
  140. hints.Add(new Tuple<BaseItem, string, int>(genre, index.Item1, index.Item2));
  141. }
  142. catch (Exception ex)
  143. {
  144. _logger.ErrorException("Error getting {0}", ex, item);
  145. }
  146. }
  147. }
  148. // Find studios
  149. var studios = items.SelectMany(i => i.Studios)
  150. .Where(i => !string.IsNullOrEmpty(i))
  151. .Distinct(StringComparer.OrdinalIgnoreCase)
  152. .ToList();
  153. foreach (var item in studios)
  154. {
  155. var index = GetIndex(item, searchTerm, terms);
  156. if (index.Item2 != -1)
  157. {
  158. try
  159. {
  160. var studio = _libraryManager.GetStudio(item);
  161. hints.Add(new Tuple<BaseItem, string, int>(studio, index.Item1, index.Item2));
  162. }
  163. catch (Exception ex)
  164. {
  165. _logger.ErrorException("Error getting {0}", ex, item);
  166. }
  167. }
  168. }
  169. // Find persons
  170. var persons = items.SelectMany(i => i.People)
  171. .Select(i => i.Name)
  172. .Where(i => !string.IsNullOrEmpty(i))
  173. .Distinct(StringComparer.OrdinalIgnoreCase)
  174. .ToList();
  175. foreach (var item in persons)
  176. {
  177. var index = GetIndex(item, searchTerm, terms);
  178. if (index.Item2 != -1)
  179. {
  180. try
  181. {
  182. var person = _libraryManager.GetPerson(item);
  183. hints.Add(new Tuple<BaseItem, string, int>(person, index.Item1, index.Item2));
  184. }
  185. catch (Exception ex)
  186. {
  187. _logger.ErrorException("Error getting {0}", ex, item);
  188. }
  189. }
  190. }
  191. var returnValue = hints.Where(i => i.Item3 >= 0).OrderBy(i => i.Item3).Select(i => new SearchHintInfo
  192. {
  193. Item = i.Item1,
  194. MatchedTerm = i.Item2
  195. });
  196. return Task.FromResult(returnValue);
  197. }
  198. /// <summary>
  199. /// Gets the index.
  200. /// </summary>
  201. /// <param name="input">The input.</param>
  202. /// <param name="searchInput">The search input.</param>
  203. /// <param name="searchWords">The search input.</param>
  204. /// <returns>System.Int32.</returns>
  205. private Tuple<string, int> GetIndex(string input, string searchInput, List<string> searchWords)
  206. {
  207. if (string.IsNullOrEmpty(input))
  208. {
  209. throw new ArgumentNullException("input");
  210. }
  211. if (string.Equals(input, searchInput, StringComparison.OrdinalIgnoreCase))
  212. {
  213. return new Tuple<string, int>(searchInput, 0);
  214. }
  215. var index = input.IndexOf(searchInput, StringComparison.OrdinalIgnoreCase);
  216. if (index == 0)
  217. {
  218. return new Tuple<string, int>(searchInput, 1);
  219. }
  220. if (index > 0)
  221. {
  222. return new Tuple<string, int>(searchInput, 2);
  223. }
  224. var items = GetWords(input);
  225. for (var i = 0; i < searchWords.Count; i++)
  226. {
  227. var searchTerm = searchWords[i];
  228. for (var j = 0; j < items.Count; j++)
  229. {
  230. var item = items[j];
  231. if (string.Equals(item, searchTerm, StringComparison.OrdinalIgnoreCase))
  232. {
  233. return new Tuple<string, int>(searchTerm, 3 + (i + 1) * (j + 1));
  234. }
  235. index = item.IndexOf(searchTerm, StringComparison.OrdinalIgnoreCase);
  236. if (index == 0)
  237. {
  238. return new Tuple<string, int>(searchTerm, 4 + (i + 1) * (j + 1));
  239. }
  240. if (index > 0)
  241. {
  242. return new Tuple<string, int>(searchTerm, 5 + (i + 1) * (j + 1));
  243. }
  244. }
  245. }
  246. return new Tuple<string, int>(null, -1);
  247. }
  248. /// <summary>
  249. /// Gets the words.
  250. /// </summary>
  251. /// <param name="term">The term.</param>
  252. /// <returns>System.String[][].</returns>
  253. private List<string> GetWords(string term)
  254. {
  255. return term.Split().Where(i => !string.IsNullOrWhiteSpace(i)).ToList();
  256. }
  257. }
  258. }