Ejemplo n.º 1
0
        /// <summary>
        /// Calls the search endpoint (/Search) of the sitewide search API
        /// </summary>
        /// <param name="collection">Collection to use (required)</param>
        /// <param name="language">Language to use (required)</param>
        /// <param name="searchTerm">Search term (required)</param>
        /// <param name="from">Beginning index for results (optional)</param>
        /// <param name="size"># of results to return (optional)</param>
        /// <param name="site">Filter items returned based on site (optional)</param>
        /// <returns>Collection of Sitewide Search results</returns>
        public SiteWideSearchAPIResultCollection Search(
            string collection,
            string language,
            string searchTerm,
            int from    = 0,
            int size    = 10,
            string site = "all"
            )
        {
            SiteWideSearchAPIResultCollection rtnResult = null;

            // Check fields
            if (String.IsNullOrWhiteSpace(collection))
            {
                throw new ArgumentNullException("The collection is null or an empty string");
            }
            if (String.IsNullOrWhiteSpace(language))
            {
                throw new ArgumentNullException("The language is null or an empty string");
            }

            string[] searchParams = { collection, language, searchTerm };
            string   searchParam  = string.Join("/", searchParams);

            searchParam += "?size=" + size.ToString() + "&from=" + from + "&site=" + site;

            //Get the HTTP response content from GET request
            HttpContent httpContent = ReturnGetRespContent("Search", searchParam);

            rtnResult = httpContent.ReadAsAsync <SiteWideSearchAPIResultCollection>().Result;

            return(rtnResult);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// This methods filters the information passed to it in order to refine what
        /// will be called by the API client.
        /// </summary>
        /// <param name="searchCollection">The search collection</param>
        /// <param name="searchText">The partial text to search for</param>
        /// <param name="size">The number of items that the API will return</param>
        /// <param name="from">Beginning index for results</param>
        /// <returns>Returns the SiteWide Search API search results</returns>
        public static SiteWideSearchAPIResultCollection Search(string searchCollection, string searchText, int size, int from)
        {
            SiteWideSearchAPIResultCollection rtnResults = null;

            // Get search collection config to set up collection and site
            SiteWideSearchCollectionElement config = SiteWideSearchSection.GetSearchCollectionConfig(searchCollection);

            // Set up collection parameter
            // Set to "doc" for doc sites, and "cgov" for all others
            string collection = null;

            if (config.Template == "docSearch")
            {
                collection = "doc";
            }
            else
            {
                collection = "cgov";
            }

            // Set up language based on current culture
            string twoCharLang = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName;

            // Set up site parameter
            string site = config.Site;

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

            foreach (SiteWideSearchAPIResult res in rtnResults.SearchResults)
            {
                if (string.IsNullOrWhiteSpace(res.Title))
                {
                    res.Title = "Untitled";
                }
            }

            return(rtnResults);
        }