private Query <SearchResult, object> BuildQuery(string searchQuery, int maxScore, int patternMatchLimit) { Query <SearchResult, object> query; if (m_QueryPool.TryGetValue(searchQuery, out query) && query.valid) { return(query); } if (m_QueryPool.Count > 50) { m_QueryPool.Clear(); } query = m_QueryEngine.Parse(searchQuery, new SearchIndexerQueryFactory(args => { if (args.op == SearchIndexOperator.None) { return(SearchIndexerQuery.EvalResult.None); } #if DEBUG_INDEXING using (var t = new DebugTimer(null)) #endif { SearchResultCollection subset = null; if (args.andSet != null) { subset = new SearchResultCollection(args.andSet); } var results = SearchTerm(args.name, args.value, args.op, args.exclude, maxScore, subset, patternMatchLimit); if (args.orSet != null) { results = results.Concat(args.orSet); } #if DEBUG_INDEXING SearchIndexerQuery.EvalResult.Print(args, results, subset, t.timeMs); #endif return(SearchIndexerQuery.EvalResult.Combined(results)); } })); if (query.valid) { m_QueryPool[searchQuery] = query; } return(query); }
private void BeginSession() { #if QUICKSEARCH_DEBUG UnityEngine.Debug.Log($"Start search session {String.Join(", ", m_SearchProviders.Select(p=>p.name.id))} -> {searchText}"); #endif foreach (var provider in m_SearchProviders) { using (var enableTimer = new DebugTimer(null)) { provider.onEnable?.Invoke(); provider.enableTime = enableTimer.timeMs; } } }
/// <summary> /// Initiate a search and return all search items matching the search context. Other items can be found later using the asynchronous searches. /// </summary> /// <param name="context">The current search context</param> /// <returns>A list of search items matching the search query.</returns> public static List <SearchItem> GetItems(SearchContext context, SearchFlags options = SearchFlags.Default) { // Stop all search sessions every time there is a new search. context.sessions.StopAllAsyncSearchSessions(); if (context.actionId == null && string.IsNullOrEmpty(context.searchText) && !context.wantsMore) { return(new List <SearchItem>(0)); } var allItems = new List <SearchItem>(3); #if QUICKSEARCH_DEBUG var debugProviderList = context.providers.ToList(); using (new DebugTimer($"Search get items {String.Join(", ", debugProviderList.Select(p=>p.name.id))} -> {context.searchQuery}")); #endif foreach (var provider in context.providers) { using (var fetchTimer = new DebugTimer(null)) { try { var iterator = provider.fetchItems(context, allItems, provider); if (iterator != null) { if (options.HasFlag(SearchFlags.Synchronous)) { var stackedEnumerator = new StackedEnumerator <SearchItem>(iterator); while (stackedEnumerator.MoveNext()) { if (stackedEnumerator.Current != null) { allItems.Add(stackedEnumerator.Current); } } } else { var session = context.sessions.GetProviderSession(provider.name.id); session.Reset(iterator, k_MaxFetchTimeMs); if (!session.FetchSome(allItems, k_MaxFetchTimeMs)) { session.Stop(); } } } provider.RecordFetchTime(fetchTimer.timeMs); } catch (Exception ex) { UnityEngine.Debug.LogException(new Exception($"Failed to get fetch {provider.name.displayName} provider items.", ex)); } } } if (!options.HasFlag(SearchFlags.Sorted)) { return(allItems); } allItems.Sort(SortItemComparer); return(allItems.GroupBy(i => i.id).Select(i => i.First()).ToList()); }