SearchEngine.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 (searchTerm != null)
  76. {
  77. searchTerm = searchTerm.Trim().RemoveDiacritics();
  78. }
  79. if (string.IsNullOrWhiteSpace(searchTerm))
  80. {
  81. throw new ArgumentNullException("searchTerm");
  82. }
  83. var terms = GetWords(searchTerm);
  84. var hints = new List<Tuple<BaseItem, string, int>>();
  85. var excludeItemTypes = new List<string>();
  86. var includeItemTypes = (query.IncludeItemTypes ?? new string[] { }).ToList();
  87. excludeItemTypes.Add(typeof(Year).Name);
  88. if (query.IncludeGenres && (includeItemTypes.Count == 0 || includeItemTypes.Contains("Genre", StringComparer.OrdinalIgnoreCase)))
  89. {
  90. if (!query.IncludeMedia)
  91. {
  92. AddIfMissing(includeItemTypes, typeof(Genre).Name);
  93. AddIfMissing(includeItemTypes, typeof(GameGenre).Name);
  94. AddIfMissing(includeItemTypes, typeof(MusicGenre).Name);
  95. }
  96. }
  97. else
  98. {
  99. AddIfMissing(excludeItemTypes, typeof(Genre).Name);
  100. AddIfMissing(excludeItemTypes, typeof(GameGenre).Name);
  101. AddIfMissing(excludeItemTypes, typeof(MusicGenre).Name);
  102. }
  103. if (query.IncludePeople && (includeItemTypes.Count == 0 || includeItemTypes.Contains("People", StringComparer.OrdinalIgnoreCase) || includeItemTypes.Contains("Person", StringComparer.OrdinalIgnoreCase)))
  104. {
  105. if (!query.IncludeMedia)
  106. {
  107. AddIfMissing(includeItemTypes, typeof(Person).Name);
  108. }
  109. }
  110. else
  111. {
  112. AddIfMissing(excludeItemTypes, typeof(Person).Name);
  113. }
  114. if (query.IncludeStudios && (includeItemTypes.Count == 0 || includeItemTypes.Contains("Studio", StringComparer.OrdinalIgnoreCase)))
  115. {
  116. if (!query.IncludeMedia)
  117. {
  118. AddIfMissing(includeItemTypes, typeof(Studio).Name);
  119. }
  120. }
  121. else
  122. {
  123. AddIfMissing(excludeItemTypes, typeof(Studio).Name);
  124. }
  125. if (query.IncludeArtists && (includeItemTypes.Count == 0 || includeItemTypes.Contains("MusicArtist", StringComparer.OrdinalIgnoreCase)))
  126. {
  127. if (!query.IncludeMedia)
  128. {
  129. AddIfMissing(includeItemTypes, typeof(MusicArtist).Name);
  130. }
  131. }
  132. else
  133. {
  134. AddIfMissing(excludeItemTypes, typeof(MusicArtist).Name);
  135. }
  136. AddIfMissing(excludeItemTypes, typeof(CollectionFolder).Name);
  137. var mediaItems = _libraryManager.GetItemList(new InternalItemsQuery(user)
  138. {
  139. NameContains = searchTerm,
  140. ExcludeItemTypes = excludeItemTypes.ToArray(),
  141. IncludeItemTypes = includeItemTypes.ToArray(),
  142. Limit = query.Limit,
  143. IncludeItemsByName = true
  144. }, new string[] { });
  145. // Add search hints based on item name
  146. hints.AddRange(mediaItems.Where(IncludeInSearch).Select(item =>
  147. {
  148. var index = GetIndex(item.Name, searchTerm, terms);
  149. return new Tuple<BaseItem, string, int>(item, index.Item1, index.Item2);
  150. }));
  151. var returnValue = hints.Where(i => i.Item3 >= 0).OrderBy(i => i.Item3).Select(i => new SearchHintInfo
  152. {
  153. Item = i.Item1,
  154. MatchedTerm = i.Item2
  155. });
  156. return Task.FromResult(returnValue);
  157. }
  158. private bool IncludeInSearch(BaseItem item)
  159. {
  160. var episode = item as Episode;
  161. if (episode != null)
  162. {
  163. if (episode.IsMissingEpisode)
  164. {
  165. return false;
  166. }
  167. }
  168. return true;
  169. }
  170. /// <summary>
  171. /// Gets the index.
  172. /// </summary>
  173. /// <param name="input">The input.</param>
  174. /// <param name="searchInput">The search input.</param>
  175. /// <param name="searchWords">The search input.</param>
  176. /// <returns>System.Int32.</returns>
  177. private Tuple<string, int> GetIndex(string input, string searchInput, List<string> searchWords)
  178. {
  179. if (string.IsNullOrWhiteSpace(input))
  180. {
  181. throw new ArgumentNullException("input");
  182. }
  183. input = input.RemoveDiacritics();
  184. if (string.Equals(input, searchInput, StringComparison.OrdinalIgnoreCase))
  185. {
  186. return new Tuple<string, int>(searchInput, 0);
  187. }
  188. var index = input.IndexOf(searchInput, StringComparison.OrdinalIgnoreCase);
  189. if (index == 0)
  190. {
  191. return new Tuple<string, int>(searchInput, 1);
  192. }
  193. if (index > 0)
  194. {
  195. return new Tuple<string, int>(searchInput, 2);
  196. }
  197. var items = GetWords(input);
  198. for (var i = 0; i < searchWords.Count; i++)
  199. {
  200. var searchTerm = searchWords[i];
  201. for (var j = 0; j < items.Count; j++)
  202. {
  203. var item = items[j];
  204. if (string.Equals(item, searchTerm, StringComparison.OrdinalIgnoreCase))
  205. {
  206. return new Tuple<string, int>(searchTerm, 3 + (i + 1) * (j + 1));
  207. }
  208. index = item.IndexOf(searchTerm, StringComparison.OrdinalIgnoreCase);
  209. if (index == 0)
  210. {
  211. return new Tuple<string, int>(searchTerm, 4 + (i + 1) * (j + 1));
  212. }
  213. if (index > 0)
  214. {
  215. return new Tuple<string, int>(searchTerm, 5 + (i + 1) * (j + 1));
  216. }
  217. }
  218. }
  219. return new Tuple<string, int>(null, -1);
  220. }
  221. /// <summary>
  222. /// Gets the words.
  223. /// </summary>
  224. /// <param name="term">The term.</param>
  225. /// <returns>System.String[][].</returns>
  226. private List<string> GetWords(string term)
  227. {
  228. var stoplist = GetStopList().ToList();
  229. return term.Split()
  230. .Where(i => !string.IsNullOrWhiteSpace(i) && !stoplist.Contains(i, StringComparer.OrdinalIgnoreCase))
  231. .ToList();
  232. }
  233. private IEnumerable<string> GetStopList()
  234. {
  235. return new[]
  236. {
  237. "the",
  238. "a",
  239. "of",
  240. "an"
  241. };
  242. }
  243. }
  244. }