SearchEngine.cs 9.2 KB

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