/// <summary> /// Lightweight method to search for terms matching searchText. This method is intended for use with autosuggest /// and returns a maximum of 10 results /// </summary> /// <param name="searchText">text to search for.</param> /// <param name="searchType">The type of search to perform. /// Valid values are: /// Begins - Search for terms beginning with searchText. /// Contains - Search for terms containing searchText. /// Magic - Search for terms beginning with searchText, followed by those containing searchText. /// </param> /// <param name="numResults">Maximum number of results to return.</param> /// <param name="dictionary">The dictionary to retreive the Term from. /// Valid values are /// Term - Dictionary of Cancer Terms /// drug - Drug Dictionary /// genetic - Dictionary of Genetics Terms /// </param> /// <param name="language">The Term's desired language. /// Supported values are: /// en - English /// es - Spanish /// </param> /// <param name="version">String identifying which vereion of the JSON structure to retrieve.</param> /// <returns></returns> public SuggestReturn SearchSuggest(String searchText, SearchType searchType, int numResults, DictionaryType dictionary, Language language, String version) { log.DebugFormat("Enter ValidateSearchSuggest( {0}, {1}, {2}, {3}, {4}, {5}).", searchText, searchType, numResults, dictionary, language, version); // Sanity check for numResults if (numResults < 10) { numResults = 10; } // In the initial implementation, the audience is implied by the particular dictionary being used. AudienceType audience = GetDefaultAudienceFromDictionaryType(dictionary); DictionaryQuery query = new DictionaryQuery(); SuggestionResults results = query.SearchSuggest(searchText, searchType, numResults, dictionary, language, audience, version); List <String> messages = new List <string>(); int resultCount = results.MatchCount; // Report the count in a human-readable format. String message = String.Format("Found {0} results.", resultCount); log.Debug(message); messages.Add(message); // Retrieve results. We already know the number of results, so let's preset the // list to the size we know we're going to need. List <DictionarySuggestion> foundTerms = new List <DictionarySuggestion>(resultCount); foreach (DataRow row in results.Data.Rows) { int ID = row.Field <int>("TermID"); string term = row.Field <String>("TermName"); DictionarySuggestion suggestion = new DictionarySuggestion(ID, term); foundTerms.Add(suggestion); } // Populate return metadata structure SuggestReturnMeta meta = new SuggestReturnMeta() { ResultCount = resultCount, Messages = messages.ToArray() }; // Combine meta and results to create the final return object. SuggestReturn suggestReturn = new SuggestReturn() { Result = foundTerms.ToArray(), Meta = meta }; return(suggestReturn); }
/// <summary> /// Term suggestions from what is being typed into the search box. Used for autosuggest /// </summary> /// <param name="searchText">the string being typed</param> /// <param name="searchType">Type of search being done (contains, starts with, etc.)</param> /// <param name="dictionary">Which dictionary is being searched</param> /// <param name="language">Language</param> /// <returns>returns list of suggestions</returns> public DictionarySuggestionCollection SearchSuggest(String searchText, SearchType searchType, DictionaryType dictionary, String language) { // Translate from types the AppManager exposes to types the Dictionary Service exposes. svcDictionaryType svcDictionary = TypeTranslator.Translate(dictionary); svcSearchType svcSearchType = TypeTranslator.Translate(searchType); svcLanguage svcLanguage = TypeTranslator.TranslateLocaleString(language); //Set up variables we will use List <DictionarySuggestion> list = new List <DictionarySuggestion>(); DictionaryService service = new DictionaryService(); SuggestReturnMeta meta = new SuggestReturnMeta(); NCI.Services.Dictionary.BusinessObjects.SuggestReturn suggestRet = null; try { service.SearchSuggest(searchText, svcSearchType, svcDictionary, svcLanguage); } catch (Exception ex) { log.Error("Error in search suggest method in Dictionary Web Service.", ex); } //sets up the suggest so the list of suggestions DictionarySuggestion suggest = new DictionarySuggestion(); foreach (NCI.Services.Dictionary.BusinessObjects.DictionarySuggestion m in suggestRet.Result) { //get properties and set them then add to list suggest.ID = m.ID; suggest.Term = m.Term; list.Add(suggest); } //create return variable based on list DictionarySuggestionCollection result = new DictionarySuggestionCollection(list.AsEnumerable()); result.ResultsCount = suggestRet.Meta.ResultCount; return(result); }