SearchEngine.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. AddIfMissing(excludeItemTypes, typeof(CollectionFolder).Name);
  134. var mediaItems = _libraryManager.GetItemList(new InternalItemsQuery(user)
  135. {
  136. NameContains = searchTerm,
  137. ExcludeItemTypes = excludeItemTypes.ToArray(),
  138. IncludeItemTypes = includeItemTypes.ToArray(),
  139. Limit = query.Limit,
  140. IncludeItemsByName = true
  141. }, new string[] { });
  142. // Add search hints based on item name
  143. hints.AddRange(mediaItems.Where(IncludeInSearch).Select(item =>
  144. {
  145. var index = GetIndex(item.Name, searchTerm, terms);
  146. return new Tuple<BaseItem, string, int>(item, index.Item1, index.Item2);
  147. }));
  148. var returnValue = hints.Where(i => i.Item3 >= 0).OrderBy(i => i.Item3).Select(i => new SearchHintInfo
  149. {
  150. Item = i.Item1,
  151. MatchedTerm = i.Item2
  152. });
  153. return Task.FromResult(returnValue);
  154. }
  155. private bool IncludeInSearch(BaseItem item)
  156. {
  157. var episode = item as Episode;
  158. if (episode != null)
  159. {
  160. if (episode.IsMissingEpisode)
  161. {
  162. return false;
  163. }
  164. }
  165. return true;
  166. }
  167. /// <summary>
  168. /// Gets the index.
  169. /// </summary>
  170. /// <param name="input">The input.</param>
  171. /// <param name="searchInput">The search input.</param>
  172. /// <param name="searchWords">The search input.</param>
  173. /// <returns>System.Int32.</returns>
  174. private Tuple<string, int> GetIndex(string input, string searchInput, List<string> searchWords)
  175. {
  176. if (string.IsNullOrWhiteSpace(input))
  177. {
  178. throw new ArgumentNullException("input");
  179. }
  180. input = input.RemoveDiacritics();
  181. if (string.Equals(input, searchInput, StringComparison.OrdinalIgnoreCase))
  182. {
  183. return new Tuple<string, int>(searchInput, 0);
  184. }
  185. var index = input.IndexOf(searchInput, StringComparison.OrdinalIgnoreCase);
  186. if (index == 0)
  187. {
  188. return new Tuple<string, int>(searchInput, 1);
  189. }
  190. if (index > 0)
  191. {
  192. return new Tuple<string, int>(searchInput, 2);
  193. }
  194. var items = GetWords(input);
  195. for (var i = 0; i < searchWords.Count; i++)
  196. {
  197. var searchTerm = searchWords[i];
  198. for (var j = 0; j < items.Count; j++)
  199. {
  200. var item = items[j];
  201. if (string.Equals(item, searchTerm, StringComparison.OrdinalIgnoreCase))
  202. {
  203. return new Tuple<string, int>(searchTerm, 3 + (i + 1) * (j + 1));
  204. }
  205. index = item.IndexOf(searchTerm, StringComparison.OrdinalIgnoreCase);
  206. if (index == 0)
  207. {
  208. return new Tuple<string, int>(searchTerm, 4 + (i + 1) * (j + 1));
  209. }
  210. if (index > 0)
  211. {
  212. return new Tuple<string, int>(searchTerm, 5 + (i + 1) * (j + 1));
  213. }
  214. }
  215. }
  216. return new Tuple<string, int>(null, -1);
  217. }
  218. /// <summary>
  219. /// Gets the words.
  220. /// </summary>
  221. /// <param name="term">The term.</param>
  222. /// <returns>System.String[][].</returns>
  223. private List<string> GetWords(string term)
  224. {
  225. var stoplist = GetStopList().ToList();
  226. return term.Split()
  227. .Where(i => !string.IsNullOrWhiteSpace(i) && !stoplist.Contains(i, StringComparer.OrdinalIgnoreCase))
  228. .ToList();
  229. }
  230. private IEnumerable<string> GetStopList()
  231. {
  232. return new[]
  233. {
  234. "the",
  235. "a",
  236. "of",
  237. "an"
  238. };
  239. }
  240. }
  241. }