SearchEngine.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Entities.Audio;
  4. using MediaBrowser.Controller.Entities.TV;
  5. using MediaBrowser.Controller.Library;
  6. using MediaBrowser.Model.Logging;
  7. using MediaBrowser.Model.Querying;
  8. using MediaBrowser.Model.Search;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Threading.Tasks;
  13. namespace MediaBrowser.Server.Implementations.Library
  14. {
  15. /// <summary>
  16. /// Class LuceneSearchEngine
  17. /// http://www.codeproject.com/Articles/320219/Lucene-Net-ultra-fast-search-for-MVC-or-WebForms
  18. /// </summary>
  19. public class SearchEngine : ISearchEngine
  20. {
  21. private readonly ILibraryManager _libraryManager;
  22. private readonly IUserManager _userManager;
  23. private readonly ILogger _logger;
  24. public SearchEngine(ILogManager logManager, ILibraryManager libraryManager, IUserManager userManager)
  25. {
  26. _libraryManager = libraryManager;
  27. _userManager = userManager;
  28. _logger = logManager.GetLogger("Lucene");
  29. }
  30. public async Task<QueryResult<SearchHintInfo>> GetSearchHints(SearchQuery query)
  31. {
  32. User user = null;
  33. if (string.IsNullOrWhiteSpace(query.UserId))
  34. {
  35. }
  36. else
  37. {
  38. user = _userManager.GetUserById(query.UserId);
  39. }
  40. var results = await GetSearchHints(query, user).ConfigureAwait(false);
  41. var searchResultArray = results.ToArray();
  42. results = searchResultArray;
  43. var count = searchResultArray.Length;
  44. if (query.StartIndex.HasValue)
  45. {
  46. results = results.Skip(query.StartIndex.Value);
  47. }
  48. if (query.Limit.HasValue)
  49. {
  50. results = results.Take(query.Limit.Value);
  51. }
  52. return new QueryResult<SearchHintInfo>
  53. {
  54. TotalRecordCount = count,
  55. Items = results.ToArray()
  56. };
  57. }
  58. private void AddIfMissing(List<string> list, string value)
  59. {
  60. if (!list.Contains(value, StringComparer.OrdinalIgnoreCase))
  61. {
  62. list.Add(value);
  63. }
  64. }
  65. /// <summary>
  66. /// Gets the search hints.
  67. /// </summary>
  68. /// <param name="query">The query.</param>
  69. /// <param name="user">The user.</param>
  70. /// <returns>IEnumerable{SearchHintResult}.</returns>
  71. /// <exception cref="System.ArgumentNullException">searchTerm</exception>
  72. private Task<IEnumerable<SearchHintInfo>> GetSearchHints(SearchQuery query, User user)
  73. {
  74. var searchTerm = query.SearchTerm;
  75. if (string.IsNullOrWhiteSpace(searchTerm))
  76. {
  77. throw new ArgumentNullException("searchTerm");
  78. }
  79. searchTerm = searchTerm.RemoveDiacritics();
  80. var terms = GetWords(searchTerm);
  81. var hints = new List<Tuple<BaseItem, string, int>>();
  82. var excludeItemTypes = new List<string>();
  83. var includeItemTypes = (query.IncludeItemTypes ?? new string[] { }).ToList();
  84. excludeItemTypes.Add(typeof(Year).Name);
  85. if (query.IncludeGenres && (includeItemTypes.Count == 0 || includeItemTypes.Contains("Genre", StringComparer.OrdinalIgnoreCase)))
  86. {
  87. if (!query.IncludeMedia)
  88. {
  89. AddIfMissing(includeItemTypes, typeof(Genre).Name);
  90. AddIfMissing(includeItemTypes, typeof(GameGenre).Name);
  91. AddIfMissing(includeItemTypes, typeof(MusicGenre).Name);
  92. }
  93. }
  94. else
  95. {
  96. AddIfMissing(excludeItemTypes, typeof(Genre).Name);
  97. AddIfMissing(excludeItemTypes, typeof(GameGenre).Name);
  98. AddIfMissing(excludeItemTypes, typeof(MusicGenre).Name);
  99. }
  100. if (query.IncludePeople && (includeItemTypes.Count == 0 || includeItemTypes.Contains("People", StringComparer.OrdinalIgnoreCase)))
  101. {
  102. if (!query.IncludeMedia)
  103. {
  104. AddIfMissing(includeItemTypes, typeof(Person).Name);
  105. }
  106. }
  107. else
  108. {
  109. AddIfMissing(excludeItemTypes, typeof(Person).Name);
  110. }
  111. if (query.IncludeStudios && (includeItemTypes.Count == 0 || includeItemTypes.Contains("Studio", StringComparer.OrdinalIgnoreCase)))
  112. {
  113. if (!query.IncludeMedia)
  114. {
  115. AddIfMissing(includeItemTypes, typeof(Studio).Name);
  116. }
  117. }
  118. else
  119. {
  120. AddIfMissing(excludeItemTypes, typeof(Studio).Name);
  121. }
  122. if (query.IncludeArtists && (includeItemTypes.Count == 0 || includeItemTypes.Contains("MusicArtist", StringComparer.OrdinalIgnoreCase)))
  123. {
  124. if (!query.IncludeMedia)
  125. {
  126. AddIfMissing(includeItemTypes, typeof(MusicArtist).Name);
  127. }
  128. }
  129. else
  130. {
  131. AddIfMissing(excludeItemTypes, typeof(MusicArtist).Name);
  132. }
  133. var mediaItems = _libraryManager.GetItems(new InternalItemsQuery
  134. {
  135. NameContains = searchTerm,
  136. ExcludeItemTypes = excludeItemTypes.ToArray(),
  137. IncludeItemTypes = includeItemTypes.ToArray(),
  138. MaxParentalRating = user == null ? null : user.Policy.MaxParentalRating,
  139. Limit = query.Limit.HasValue ? query.Limit * 3 : null
  140. }).Items;
  141. // Add search hints based on item name
  142. hints.AddRange(mediaItems.Where(i => IncludeInSearch(i) && IsVisible(i, user) && !(i is CollectionFolder)).Select(item =>
  143. {
  144. var index = GetIndex(item.Name, searchTerm, terms);
  145. return new Tuple<BaseItem, string, int>(item, index.Item1, index.Item2);
  146. }));
  147. var returnValue = hints.Where(i => i.Item3 >= 0).OrderBy(i => i.Item3).Select(i => new SearchHintInfo
  148. {
  149. Item = i.Item1,
  150. MatchedTerm = i.Item2
  151. });
  152. return Task.FromResult(returnValue);
  153. }
  154. private bool IsVisible(BaseItem item, User user)
  155. {
  156. if (user == null)
  157. {
  158. return true;
  159. }
  160. if (item is IItemByName)
  161. {
  162. var dual = item as IHasDualAccess;
  163. if (dual == null || dual.IsAccessedByName)
  164. {
  165. return true;
  166. }
  167. }
  168. return item.IsVisibleStandalone(user);
  169. }
  170. private bool IncludeInSearch(BaseItem item)
  171. {
  172. var episode = item as Episode;
  173. if (episode != null)
  174. {
  175. if (episode.IsMissingEpisode)
  176. {
  177. return false;
  178. }
  179. }
  180. return true;
  181. }
  182. /// <summary>
  183. /// Gets the index.
  184. /// </summary>
  185. /// <param name="input">The input.</param>
  186. /// <param name="searchInput">The search input.</param>
  187. /// <param name="searchWords">The search input.</param>
  188. /// <returns>System.Int32.</returns>
  189. private Tuple<string, int> GetIndex(string input, string searchInput, List<string> searchWords)
  190. {
  191. if (string.IsNullOrWhiteSpace(input))
  192. {
  193. throw new ArgumentNullException("input");
  194. }
  195. input = input.RemoveDiacritics();
  196. if (string.Equals(input, searchInput, StringComparison.OrdinalIgnoreCase))
  197. {
  198. return new Tuple<string, int>(searchInput, 0);
  199. }
  200. var index = input.IndexOf(searchInput, StringComparison.OrdinalIgnoreCase);
  201. if (index == 0)
  202. {
  203. return new Tuple<string, int>(searchInput, 1);
  204. }
  205. if (index > 0)
  206. {
  207. return new Tuple<string, int>(searchInput, 2);
  208. }
  209. var items = GetWords(input);
  210. for (var i = 0; i < searchWords.Count; i++)
  211. {
  212. var searchTerm = searchWords[i];
  213. for (var j = 0; j < items.Count; j++)
  214. {
  215. var item = items[j];
  216. if (string.Equals(item, searchTerm, StringComparison.OrdinalIgnoreCase))
  217. {
  218. return new Tuple<string, int>(searchTerm, 3 + (i + 1) * (j + 1));
  219. }
  220. index = item.IndexOf(searchTerm, StringComparison.OrdinalIgnoreCase);
  221. if (index == 0)
  222. {
  223. return new Tuple<string, int>(searchTerm, 4 + (i + 1) * (j + 1));
  224. }
  225. if (index > 0)
  226. {
  227. return new Tuple<string, int>(searchTerm, 5 + (i + 1) * (j + 1));
  228. }
  229. }
  230. }
  231. return new Tuple<string, int>(null, -1);
  232. }
  233. /// <summary>
  234. /// Gets the words.
  235. /// </summary>
  236. /// <param name="term">The term.</param>
  237. /// <returns>System.String[][].</returns>
  238. private List<string> GetWords(string term)
  239. {
  240. var stoplist = GetStopList().ToList();
  241. return term.Split()
  242. .Where(i => !string.IsNullOrWhiteSpace(i) && !stoplist.Contains(i, StringComparer.OrdinalIgnoreCase))
  243. .ToList();
  244. }
  245. private IEnumerable<string> GetStopList()
  246. {
  247. return new[]
  248. {
  249. "the",
  250. "a",
  251. "of",
  252. "an"
  253. };
  254. }
  255. }
  256. }