Beispiel #1
0
        /// <summary>
        /// Method used to query API for results.
        /// </summary>
        /// <param name="language">The language used to query the API</param>
        /// <param name="criteria">The partial text used to query the API</param>
        /// <param name="size">The maximum number of items that the API will return</param>
        /// <param name="contains">Indicator on whether the text is to be search from the beginning of the text or anywhere in the string</param>
        /// <returns>Returns the search results</returns>
        private AutoSuggestSearchServiceCollection Search(string language, string criteria, int size, bool contains)
        {
            // Create the collection variable
            AutoSuggestSearchServiceCollection sc = new AutoSuggestSearchServiceCollection();

            // Language converted to an enum
            DisplayLanguage displayLanguage = (DisplayLanguage)Enum.Parse(typeof(DisplayLanguage), language);

            try
            {
                // Pass the given API parameters to the business layer
                AutoSuggestAPIResultCollection apiCollection = AutoSuggestSearchManager.Search(displayLanguage, criteria, size, contains);

                // Use Linq to extract the data from the business layer and create the service data objects
                // TermID is 0 always, that value is not part of the result received from the API call.
                //But can be used in the future for other purposes.
                var collection = apiCollection.Results.Select(r => new AutoSuggestSearchServiceItem(
                                                                  0,
                                                                  r.Term,
                                                                  string.Empty
                                                                  ));

                sc.AddRange(collection);
            }
            catch (Exception ex)
            {
                // Log the error that occured
                log.Error("Error in AutoSuggestSearchService", ex);
            }

            return(sc);
        }
        /// <summary>
        /// This methods filters the information passed to it in order to refine what
        /// will be called by the API client.
        /// </summary>
        /// <param name="language">Enumeration indicating language</param>
        /// <param name="searchText">The partial text to search for</param>
        /// <param name="size">The maximum number of items that the API will return</param>
        /// <param name="contains">Indicates whether the text will be searched starting from the beginning or anywhere in the string</param>
        /// <returns>Returns the AutoSuggest API search results</returns>
        public static AutoSuggestAPIResultCollection Search(DisplayLanguage language, string searchText, int size, bool contains)
        {
            AutoSuggestAPIResultCollection rtnResults = new AutoSuggestAPIResultCollection();

            // Set collection based on web.config setting
            string collection = ConfigurationManager.AppSettings["SiteWideSearchAPICollection"];

            // Set language string
            // Default to English, as only "en" and "es" are accepted by API
            string twoCharLang = "en";

            if (language == DisplayLanguage.Spanish)
            {
                twoCharLang = "es";
            }

            try
            {
                // Call API to retrieve autosuggest results
                rtnResults = Client.Autosuggest(collection, twoCharLang, searchText, size);
            }
            catch (Exception ex)
            {
                // Log error if unable to retrieve results
                log.Error("Error retrieving results from SiteWideSearch API Client in AutoSuggestSearchManager", ex);
            }

            return(rtnResults);
        }