SearchEngine.cs 9.3 KB

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