SearchService.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using MediaBrowser.Controller;
  2. using MediaBrowser.Controller.Dto;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.Entities.Audio;
  5. using MediaBrowser.Controller.Entities.TV;
  6. using MediaBrowser.Controller.Library;
  7. using MediaBrowser.Model.Entities;
  8. using MediaBrowser.Model.Search;
  9. using ServiceStack.ServiceHost;
  10. using System;
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Threading.Tasks;
  15. namespace MediaBrowser.Api
  16. {
  17. /// <summary>
  18. /// Class GetSearchHints
  19. /// </summary>
  20. [Route("/Search/Hints", "GET")]
  21. [Api(Description = "Gets search hints based on a search term")]
  22. public class GetSearchHints : IReturn<SearchHintResult>
  23. {
  24. /// <summary>
  25. /// Skips over a given number of items within the results. Use for paging.
  26. /// </summary>
  27. /// <value>The start index.</value>
  28. [ApiMember(Name = "StartIndex", Description = "Optional. The record index to start at. All items with a lower index will be dropped from the results.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  29. public int? StartIndex { get; set; }
  30. /// <summary>
  31. /// The maximum number of items to return
  32. /// </summary>
  33. /// <value>The limit.</value>
  34. [ApiMember(Name = "Limit", Description = "Optional. The maximum number of records to return", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  35. public int? Limit { get; set; }
  36. /// <summary>
  37. /// Gets or sets the user id.
  38. /// </summary>
  39. /// <value>The user id.</value>
  40. [ApiMember(Name = "UserId", Description = "Optional. Supply a user id to search within a user's library or omit to search all.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  41. public Guid? UserId { get; set; }
  42. /// <summary>
  43. /// Search characters used to find items
  44. /// </summary>
  45. /// <value>The index by.</value>
  46. [ApiMember(Name = "SearchTerm", Description = "The search term to filter on", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
  47. public string SearchTerm { get; set; }
  48. }
  49. /// <summary>
  50. /// Class SearchService
  51. /// </summary>
  52. public class SearchService : BaseApiService
  53. {
  54. /// <summary>
  55. /// The _user manager
  56. /// </summary>
  57. private readonly IUserManager _userManager;
  58. /// <summary>
  59. /// The _search engine
  60. /// </summary>
  61. private readonly ILibrarySearchEngine _searchEngine;
  62. private readonly ILibraryManager _libraryManager;
  63. private readonly IDtoService _dtoService;
  64. /// <summary>
  65. /// Initializes a new instance of the <see cref="SearchService" /> class.
  66. /// </summary>
  67. /// <param name="userManager">The user manager.</param>
  68. /// <param name="searchEngine">The search engine.</param>
  69. /// <param name="libraryManager">The library manager.</param>
  70. public SearchService(IUserManager userManager, ILibrarySearchEngine searchEngine, ILibraryManager libraryManager, IDtoService dtoService)
  71. {
  72. _userManager = userManager;
  73. _searchEngine = searchEngine;
  74. _libraryManager = libraryManager;
  75. _dtoService = dtoService;
  76. }
  77. /// <summary>
  78. /// Gets the specified request.
  79. /// </summary>
  80. /// <param name="request">The request.</param>
  81. /// <returns>System.Object.</returns>
  82. public object Get(GetSearchHints request)
  83. {
  84. var result = GetSearchHintsAsync(request).Result;
  85. return ToOptimizedResult(result);
  86. }
  87. /// <summary>
  88. /// Gets the search hints async.
  89. /// </summary>
  90. /// <param name="request">The request.</param>
  91. /// <returns>Task{IEnumerable{SearchHintResult}}.</returns>
  92. private async Task<SearchHintResult> GetSearchHintsAsync(GetSearchHints request)
  93. {
  94. IEnumerable<BaseItem> inputItems;
  95. if (request.UserId.HasValue)
  96. {
  97. var user = _userManager.GetUserById(request.UserId.Value);
  98. inputItems = user.RootFolder.GetRecursiveChildren(user);
  99. }
  100. else
  101. {
  102. inputItems = _libraryManager.RootFolder.RecursiveChildren;
  103. }
  104. var results = await _searchEngine.GetSearchHints(inputItems, request.SearchTerm).ConfigureAwait(false);
  105. var searchResultArray = results.ToArray();
  106. IEnumerable<SearchHintInfo> returnResults = searchResultArray;
  107. if (request.StartIndex.HasValue)
  108. {
  109. returnResults = returnResults.Skip(request.StartIndex.Value);
  110. }
  111. if (request.Limit.HasValue)
  112. {
  113. returnResults = returnResults.Take(request.Limit.Value);
  114. }
  115. return new SearchHintResult
  116. {
  117. TotalRecordCount = searchResultArray.Length,
  118. SearchHints = returnResults.Select(GetSearchHintResult).ToArray()
  119. };
  120. }
  121. /// <summary>
  122. /// Gets the search hint result.
  123. /// </summary>
  124. /// <param name="hintInfo">The hint info.</param>
  125. /// <returns>SearchHintResult.</returns>
  126. private SearchHint GetSearchHintResult(SearchHintInfo hintInfo)
  127. {
  128. var item = hintInfo.Item;
  129. var result = new SearchHint
  130. {
  131. Name = item.Name,
  132. IndexNumber = item.IndexNumber,
  133. ParentIndexNumber = item.ParentIndexNumber,
  134. ItemId = _dtoService.GetDtoId(item),
  135. Type = item.GetType().Name,
  136. MediaType = item.MediaType,
  137. MatchedTerm = hintInfo.MatchedTerm,
  138. DisplayMediaType = item.DisplayMediaType,
  139. RunTimeTicks = item.RunTimeTicks
  140. };
  141. if (item.HasImage(ImageType.Primary))
  142. {
  143. result.PrimaryImageTag = Kernel.Instance.ImageManager.GetImageCacheTag(item, ImageType.Primary, item.GetImage(ImageType.Primary));
  144. }
  145. var episode = item as Episode;
  146. if (episode != null)
  147. {
  148. result.Series = episode.Series.Name;
  149. }
  150. var season = item as Season;
  151. if (season != null)
  152. {
  153. result.Series = season.Series.Name;
  154. result.EpisodeCount = season.RecursiveChildren.OfType<Episode>().Count();
  155. }
  156. var series = item as Series;
  157. if (series != null)
  158. {
  159. result.EpisodeCount = series.RecursiveChildren.OfType<Episode>().Count();
  160. }
  161. var album = item as MusicAlbum;
  162. if (album != null)
  163. {
  164. var songs = album.RecursiveChildren.OfType<Audio>().ToList();
  165. result.SongCount = songs.Count;
  166. result.Artists = songs
  167. .SelectMany(i => i.Artists)
  168. .Distinct(StringComparer.OrdinalIgnoreCase)
  169. .ToArray();
  170. result.AlbumArtist = songs.Select(i => i.AlbumArtist).FirstOrDefault(i => !string.IsNullOrEmpty(i));
  171. }
  172. var song = item as Audio;
  173. if (song != null)
  174. {
  175. result.Album = song.Album;
  176. result.AlbumArtist = song.AlbumArtist;
  177. result.Artists = song.Artists.ToArray();
  178. }
  179. return result;
  180. }
  181. }
  182. }