/// <summary>
        /// Retrieving suggestions using the AWS CloudSearch Suggester. The AWS CloudSearch Suggester
        /// returns results from the entire index and does not suport query filtering.
        /// </summary>
        /// <param name="parameters">Suggest request parameters</param>
        /// <returns>SuggestResults</returns>
        public SuggestResults RetieveSuggestions(NameValueCollection parameters)
        {
            SuggestResults suggestResults = new SuggestResults();

            try
            {
                AmazonCloudSearchDomainClient client = GetCloudSearchClient();

                SuggestRequest request = new SuggestRequest();
                if (parameters["suggestername"] != null)
                {
                    request.Suggester = parameters["suggestername"];
                }

                if (parameters["q"] != null)
                {
                    request.Query = parameters["q"];
                }

                request.Size = parameters["size"] != null?Convert.ToInt32(parameters["size"]) : this.DefaultPageSize;

                SuggestResponse results = client.Suggest(request);
                suggestResults.Matches = results.Suggest.Suggestions.Select(c => c.Suggestion).ToList();
            }
            catch (Exception ex)
            {
                suggestResults.HasError    = true;
                suggestResults.ErrorDetail = ex.Message + " : " + ex.StackTrace;
            }

            return(suggestResults);
        }
Example #2
0
        public IResultSet Search(ISearchQuery query)
        {
            var amazonSearchParameters           = this.GetAmazonParams();
            AmazonCloudSearchDomainConfig config = new AmazonCloudSearchDomainConfig();

            config.ServiceURL = amazonSearchParameters[SearchEndPoint];

            AmazonCloudSearchDomainClient domainClient = new AmazonCloudSearchDomainClient(amazonSearchParameters[AccessKey], amazonSearchParameters[SecretAccessKey], config);
            List <string> suggestions = new List <string>();
            StringBuilder highlights  = new StringBuilder();

            highlights.Append("{\'");

            if (query == null)
            {
                throw new ArgumentNullException("query");
            }

            foreach (var field in query.HighlightedFields)
            {
                if (highlights.Length > 2)
                {
                    highlights.Append(", \'");
                }

                highlights.Append(field.ToLowerInvariant());
                highlights.Append("\':{} ");

                SuggestRequest suggestRequest = new SuggestRequest();
                suggestRequest.Suggester = this.GetSuggesterName(field);
                suggestRequest.Size      = query.Take;
                suggestRequest.Query     = query.Text;
                SuggestResponse suggestion = domainClient.Suggest(suggestRequest);
                foreach (var suggest in suggestion.Suggest.Suggestions)
                {
                    suggestions.Add(suggest.Suggestion);
                }
            }

            highlights.Append("}");

            SearchRequest searchRequest = new SearchRequest();

            if (query.Filter != null)
            {
                searchRequest.FilterQuery = this.BuildQueryFilter(query.Filter);
            }

            if (query.OrderBy != null)
            {
                searchRequest.Sort = string.Join(",", query.OrderBy);
            }

            if (query.Take > 0)
            {
                searchRequest.Size = query.Take;
            }

            if (query.Skip > 0)
            {
                searchRequest.Start = query.Skip;
            }

            searchRequest.Highlight   = highlights.ToString();
            searchRequest.Query       = query.Text;
            searchRequest.QueryParser = QueryParser.Simple;
            var result = domainClient.Search(searchRequest).SearchResult;

            return(new AmazonResultSet(result, suggestions));
        }