Beispiel #1
0
        /// <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;
        }
Beispiel #2
0
        /// <summary>
        /// Retrieving suggestions using a custom implementation. This implementation assumes a 'literal'
        /// field type to be used as source for suggestions. This field must have lowercase values.
        /// Optionally an alternate display field can be provided which can be used populate the result.
        /// This implementation returns similar results as the AWS CloudSearch Suggester when populate the
        /// suggest field with the same value field as display field (but in lowercase).
        /// </summary>
        /// <param name="parameters">Suggest request parameters</param>
        /// <param name="suggestFieldName">Name of the suggest field</param>
        /// <param name="displayFieldName">Name of the display field</param>
        /// <returns>SuggestResults</returns>
        public SuggestResults RetieveFilteredSuggestions(NameValueCollection parameters, string suggestFieldName, string displayFieldName = null)
        {
            SuggestResults suggestResults = new SuggestResults();

            try
            {
                AmazonCloudSearchDomainClient client = GetCloudSearchClient();

                SearchRequest request = new SearchRequest();

                if (parameters["q"] != null)
                {
                    request.QueryParser = QueryParser.Structured;
                    request.Query = String.Format("(prefix field={0} '{1}')", suggestFieldName, parameters["q"].ToLower());
                }

                if (displayFieldName == null)
                {
                    request.Facet = String.Format("{{'{0}':{{'sort':'bucket'}}}}", suggestFieldName);
                    request.Return = "_no_fields";
                }
                else
                {
                    request.Return = displayFieldName;
                    request.Sort = String.Format("{0} asc", displayFieldName);
                }

                if (!String.IsNullOrEmpty(parameters["fq"]))
                {
                    string filters = string.Empty;
                    foreach (string filterString in parameters["fq"].Split(','))
                    {
                        if (filterString.Contains(":"))
                        {
                            filters += (String.Format(" {0}:'{1}'", filterString.Split(':')[0], filterString.Split(':')[1]));
                        }
                    }
                    request.FilterQuery = String.Format("(and{0})", filters);
                }

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

                SearchResponse response = client.Search(request);
                if (displayFieldName == null)
                {
                    if (response.Facets.Count > 0)
                    {
                        suggestResults.Matches = response.Facets[suggestFieldName].Buckets.Select(b => b.Value).ToList();
                    }
                }
                else
                {
                    if (response.Hits.Hit.Count > 0)
                    {
                        suggestResults.Matches = response.Hits.Hit.Select(h => h.Fields[displayFieldName].FirstOrDefault()).ToList();
                    }
                }
            }
            catch (Exception ex)
            {
                suggestResults.HasError = true;
                suggestResults.ErrorDetail = ex.Message + " : " + ex.StackTrace;
            }

            return suggestResults;
        }