public async Task<QueryResult<SearchHintInfo>> GetSearchHints(SearchQuery query) { var user = _userManager.GetUserById(new Guid(query.UserId)); var inputItems = user.RootFolder.GetRecursiveChildren(user, null); var results = await GetSearchHints(inputItems, query).ConfigureAwait(false); var searchResultArray = results.ToArray(); results = searchResultArray; var count = searchResultArray.Length; if (query.StartIndex.HasValue) { results = results.Skip(query.StartIndex.Value); } if (query.Limit.HasValue) { results = results.Take(query.Limit.Value); } return new QueryResult<SearchHintInfo> { TotalRecordCount = count, Items = results.ToArray() }; }
private async Task DoSearch() { try { Log.Info("Searching for [{0}]", SearchText); SetProgressBar(AppResources.SysTraySearching); var query = new SearchQuery { UserId = AuthenticationService.Current.LoggedInUserId, SearchTerm = SearchText }; var items = await ApiClient.GetSearchHintsAsync(query); if (items != null) { await GroupSearchResults(items.SearchHints.ToList()); } SetProgressBar(); } catch (HttpException ex) { Utils.HandleHttpException("DoSearch()", ex, NavigationService, Log); App.ShowMessage(AppResources.ErrorFailedToSearch); } }
public async Task<QueryResult<SearchHintInfo>> GetSearchHints(SearchQuery query) { IEnumerable<BaseItem> inputItems; if (string.IsNullOrEmpty(query.UserId)) { inputItems = _libraryManager.RootFolder.RecursiveChildren; } else { var user = _userManager.GetUserById(new Guid(query.UserId)); inputItems = user.RootFolder.GetRecursiveChildren(user, null); } inputItems = inputItems.Where(i => !(i is ICollectionFolder)); inputItems = _libraryManager.ReplaceVideosWithPrimaryVersions(inputItems); var results = await GetSearchHints(inputItems, query).ConfigureAwait(false); // Include item types if (query.IncludeItemTypes.Length > 0) { results = results.Where(f => query.IncludeItemTypes.Contains(f.Item.GetType().Name, StringComparer.OrdinalIgnoreCase)); } var searchResultArray = results.ToArray(); results = searchResultArray; var count = searchResultArray.Length; if (query.StartIndex.HasValue) { results = results.Skip(query.StartIndex.Value); } if (query.Limit.HasValue) { results = results.Take(query.Limit.Value); } return new QueryResult<SearchHintInfo> { TotalRecordCount = count, Items = results.ToArray() }; }
public async Task<QueryResult<SearchHintInfo>> GetSearchHints(SearchQuery query) { User user = null; if (string.IsNullOrWhiteSpace(query.UserId)) { } else { user = _userManager.GetUserById(query.UserId); } var results = await GetSearchHints(query, user).ConfigureAwait(false); var searchResultArray = results.ToArray(); results = searchResultArray; var count = searchResultArray.Length; if (query.StartIndex.HasValue) { results = results.Skip(query.StartIndex.Value); } if (query.Limit.HasValue) { results = results.Take(query.Limit.Value); } return new QueryResult<SearchHintInfo> { TotalRecordCount = count, Items = results.ToArray() }; }
/// <summary> /// Gets the search hints. /// </summary> /// <param name="inputItems">The input items.</param> /// <param name="query">The query.</param> /// <returns>IEnumerable{SearchHintResult}.</returns> /// <exception cref="System.ArgumentNullException">searchTerm</exception> private Task<IEnumerable<SearchHintInfo>> GetSearchHints(IEnumerable<BaseItem> inputItems, SearchQuery query) { var searchTerm = query.SearchTerm; if (string.IsNullOrEmpty(searchTerm)) { throw new ArgumentNullException("searchTerm"); } var terms = GetWords(searchTerm); var hints = new List<Tuple<BaseItem, string, int>>(); var items = inputItems.Where(i => !(i is MusicArtist)).ToList(); if (query.IncludeMedia) { // Add search hints based on item name hints.AddRange(items.Where(i => !string.IsNullOrEmpty(i.Name)).Select(item => { var index = GetIndex(item.Name, searchTerm, terms); return new Tuple<BaseItem, string, int>(item, index.Item1, index.Item2); })); } if (query.IncludeArtists) { // Find artists var artists = _libraryManager.GetAllArtists(items) .ToList(); foreach (var item in artists) { var index = GetIndex(item, searchTerm, terms); if (index.Item2 != -1) { try { var artist = _libraryManager.GetArtist(item); hints.Add(new Tuple<BaseItem, string, int>(artist, index.Item1, index.Item2)); } catch (Exception ex) { _logger.ErrorException("Error getting {0}", ex, item); } } } } if (query.IncludeGenres) { // Find genres, from non-audio items var genres = items.Where(i => !(i is IHasMusicGenres) && !(i is Game)) .SelectMany(i => i.Genres) .Where(i => !string.IsNullOrEmpty(i)) .Distinct(StringComparer.OrdinalIgnoreCase) .ToList(); foreach (var item in genres) { var index = GetIndex(item, searchTerm, terms); if (index.Item2 != -1) { try { var genre = _libraryManager.GetGenre(item); hints.Add(new Tuple<BaseItem, string, int>(genre, index.Item1, index.Item2)); } catch (Exception ex) { _logger.ErrorException("Error getting {0}", ex, item); } } } // Find music genres var musicGenres = items.Where(i => i is IHasMusicGenres) .SelectMany(i => i.Genres) .Where(i => !string.IsNullOrEmpty(i)) .Distinct(StringComparer.OrdinalIgnoreCase) .ToList(); foreach (var item in musicGenres) { var index = GetIndex(item, searchTerm, terms); if (index.Item2 != -1) { try { var genre = _libraryManager.GetMusicGenre(item); hints.Add(new Tuple<BaseItem, string, int>(genre, index.Item1, index.Item2)); } catch (Exception ex) { _logger.ErrorException("Error getting {0}", ex, item); } } } // Find music genres var gameGenres = items.OfType<Game>() .SelectMany(i => i.Genres) .Where(i => !string.IsNullOrEmpty(i)) .Distinct(StringComparer.OrdinalIgnoreCase) .ToList(); foreach (var item in gameGenres) { var index = GetIndex(item, searchTerm, terms); if (index.Item2 != -1) { try { var genre = _libraryManager.GetGameGenre(item); hints.Add(new Tuple<BaseItem, string, int>(genre, index.Item1, index.Item2)); } catch (Exception ex) { _logger.ErrorException("Error getting {0}", ex, item); } } } } if (query.IncludeStudios) { // Find studios var studios = items.SelectMany(i => i.Studios) .Where(i => !string.IsNullOrEmpty(i)) .Distinct(StringComparer.OrdinalIgnoreCase) .ToList(); foreach (var item in studios) { var index = GetIndex(item, searchTerm, terms); if (index.Item2 != -1) { try { var studio = _libraryManager.GetStudio(item); hints.Add(new Tuple<BaseItem, string, int>(studio, index.Item1, index.Item2)); } catch (Exception ex) { _logger.ErrorException("Error getting {0}", ex, item); } } } } if (query.IncludePeople) { // Find persons var persons = items.SelectMany(i => i.People) .Select(i => i.Name) .Where(i => !string.IsNullOrEmpty(i)) .Distinct(StringComparer.OrdinalIgnoreCase) .ToList(); foreach (var item in persons) { var index = GetIndex(item, searchTerm, terms); if (index.Item2 != -1) { try { var person = _libraryManager.GetPerson(item); hints.Add(new Tuple<BaseItem, string, int>(person, index.Item1, index.Item2)); } catch (Exception ex) { _logger.ErrorException("Error getting {0}", ex, item); } } } } var returnValue = hints.Where(i => i.Item3 >= 0).OrderBy(i => i.Item3).Select(i => new SearchHintInfo { Item = i.Item1, MatchedTerm = i.Item2 }); return Task.FromResult(returnValue); }
/// <summary> /// Gets the search hints. /// </summary> /// <param name="query">The query.</param> /// <param name="user">The user.</param> /// <returns>IEnumerable{SearchHintResult}.</returns> /// <exception cref="System.ArgumentNullException">searchTerm</exception> private Task<IEnumerable<SearchHintInfo>> GetSearchHints(SearchQuery query, User user) { var searchTerm = query.SearchTerm; if (searchTerm != null) { searchTerm = searchTerm.Trim().RemoveDiacritics(); } if (string.IsNullOrWhiteSpace(searchTerm)) { throw new ArgumentNullException("searchTerm"); } var terms = GetWords(searchTerm); var hints = new List<Tuple<BaseItem, string, int>>(); var excludeItemTypes = new List<string>(); var includeItemTypes = (query.IncludeItemTypes ?? new string[] { }).ToList(); excludeItemTypes.Add(typeof(Year).Name); if (query.IncludeGenres && (includeItemTypes.Count == 0 || includeItemTypes.Contains("Genre", StringComparer.OrdinalIgnoreCase))) { if (!query.IncludeMedia) { AddIfMissing(includeItemTypes, typeof(Genre).Name); AddIfMissing(includeItemTypes, typeof(GameGenre).Name); AddIfMissing(includeItemTypes, typeof(MusicGenre).Name); } } else { AddIfMissing(excludeItemTypes, typeof(Genre).Name); AddIfMissing(excludeItemTypes, typeof(GameGenre).Name); AddIfMissing(excludeItemTypes, typeof(MusicGenre).Name); } if (query.IncludePeople && (includeItemTypes.Count == 0 || includeItemTypes.Contains("People", StringComparer.OrdinalIgnoreCase) || includeItemTypes.Contains("Person", StringComparer.OrdinalIgnoreCase))) { if (!query.IncludeMedia) { AddIfMissing(includeItemTypes, typeof(Person).Name); } } else { AddIfMissing(excludeItemTypes, typeof(Person).Name); } if (query.IncludeStudios && (includeItemTypes.Count == 0 || includeItemTypes.Contains("Studio", StringComparer.OrdinalIgnoreCase))) { if (!query.IncludeMedia) { AddIfMissing(includeItemTypes, typeof(Studio).Name); } } else { AddIfMissing(excludeItemTypes, typeof(Studio).Name); } if (query.IncludeArtists && (includeItemTypes.Count == 0 || includeItemTypes.Contains("MusicArtist", StringComparer.OrdinalIgnoreCase))) { if (!query.IncludeMedia) { AddIfMissing(includeItemTypes, typeof(MusicArtist).Name); } } else { AddIfMissing(excludeItemTypes, typeof(MusicArtist).Name); } AddIfMissing(excludeItemTypes, typeof(CollectionFolder).Name); var mediaItems = _libraryManager.GetItemList(new InternalItemsQuery(user) { NameContains = searchTerm, ExcludeItemTypes = excludeItemTypes.ToArray(), IncludeItemTypes = includeItemTypes.ToArray(), Limit = query.Limit, IncludeItemsByName = true }); // Add search hints based on item name hints.AddRange(mediaItems.Where(IncludeInSearch).Select(item => { var index = GetIndex(item.Name, searchTerm, terms); return new Tuple<BaseItem, string, int>(item, index.Item1, index.Item2); })); var returnValue = hints.Where(i => i.Item3 >= 0).OrderBy(i => i.Item3).Select(i => new SearchHintInfo { Item = i.Item1, MatchedTerm = i.Item2 }); return Task.FromResult(returnValue); }