public SuggestReturn SearchSuggest(String searchText, SearchType searchType, DictionaryType dictionary, Language language)
        {
            // This should possibly be made a parameter
            int MaxResultsAllowed = 10;

            log.DebugFormat("Enter ValidateSearchSuggest( {0}, {1}, {2}, {3}).", searchText, searchType, dictionary, language);

            SuggestReturn ret;

            try
            {
                InputValidator.ValidateSearch(searchType, dictionary, language);

                DictionaryManager mgr = new DictionaryManager();
                ret = mgr.SearchSuggest(searchText, searchType, MaxResultsAllowed, dictionary, language, API_VERSION);

                log.DebugFormat("Returning {0} results.", ret.Result.Count());
            }
            // If there was a problem with the inputs for this request, fail with
            // an HTTP status message and an explanation.
            catch (DictionaryValidationException ex)
            {
                WebOperationContext ctx = WebOperationContext.Current;
                ctx.OutgoingResponse.SetStatusAsNotFound(ex.Message);
                ret = new SuggestReturn()
                {
                    Meta = new SuggestReturnMeta()
                    {
                        Messages = new string[] { ex.Message }
                    }
                };
            }

            return(ret);
        }
Esempio n. 2
0
        /// <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);
        }