コード例 #1
0
        /// <summary>
        /// Run a query
        /// </summary>
        /// <param name="parameters">The query parameters</param>
        /// <returns>matching results</returns>
        public CloudSearchResults ExecuteQuery(NameValueCollection parameters)
        {
            CloudSearchResults result = new CloudSearchResults();

            try
            {
                AmazonCloudSearchDomainClient client = GetCloudSearchClient();
                SearchRequest  request  = BuildSearchRequest(parameters);
                SearchResponse response = client.Search(request);

                result.Items  = response.Hits.Hit.Select(hit => CreateSearchResult(hit)).ToList();
                result.Facets = (
                    from f in response.Facets
                    select new Facet
                {
                    Name = f.Key,
                    Buckets = f.Value.Buckets.Select(b => new SI4T.Query.CloudSearch.Models.Bucket(b.Value, b.Count)).ToList()
                }).ToList();

                result.PageSize  = Convert.ToInt32(request.Size);
                result.Total     = Convert.ToInt32(response.Hits.Found);
                result.Start     = Convert.ToInt32(request.Start);
                result.QueryText = request.Query;
            }
            catch (Exception ex)
            {
                result.HasError    = true;
                result.ErrorDetail = ex.Message + " : " + ex.StackTrace;
            }

            return(result);
        }
コード例 #2
0
        public SearchResponse SearchConnectAwsCloudsearch(string querystring, string returnFields, int returnSize = 10000, GeoCoordinate originCoords = null, AwsBoundingBox boundingBox = null)
        {
            var cloudSearch   = new AmazonCloudSearchDomainClient(AwsAccessKeyId, AwsSecretAccessKey, AmazonSearchUrl);
            var searchRequest = new SearchRequest
            {
                Query       = querystring,
                QueryParser = QueryParser.Structured,
                Size        = returnSize,
                Return      = returnFields + ",_score"
            };

            if (boundingBox != null)
            {
                searchRequest.FilterQuery = $"latlong:['{boundingBox.UpperLeftCoordinates.Lat},{boundingBox.UpperLeftCoordinates.Lng}','{boundingBox.BottomRightCoordinates.Lat},{boundingBox.BottomRightCoordinates.Lng}']";
            }

            if (originCoords != null)
            {
                searchRequest.Expr    = $"{{'distance':'haversin({originCoords.Latitude},{originCoords.Longitude},latlong.latitude,latlong.longitude)'}}"; // use to sort by proximity
                searchRequest.Sort    = "distance asc";
                searchRequest.Return += ",distance";
            }

            var response = cloudSearch.Search(searchRequest);

            return(response);
        }
コード例 #3
0
        public SearchResponse SearchByGroupId(string groupId)
        {
            var cloudSearch   = new AmazonCloudSearchDomainClient(AwsAccessKeyId, AwsSecretAccessKey, AmazonSearchUrl);
            var searchRequest = new SearchRequest
            {
                Query        = "groupid: " + groupId,
                QueryParser  = QueryParser.Structured,
                QueryOptions = "{'fields': ['groupid']}",
                Size         = 1,
                Return       = "_all_fields"
            };

            var response = cloudSearch.Search(searchRequest);

            return(response);
        }
コード例 #4
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));
        }
コード例 #5
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);
        }