SearchService.cs 7.5 KB

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