Ejemplo n.º 1
0
        public Task <SearchResponse <T> > SearchAsync <T>(SearchRequest request)
        {
            var queryString = HttpUtility.ParseQueryString(String.Empty);
            var info        = new SearchInfo();

            if (request.Query != null)
            {
                queryString["q"]        = info.Query = request.Query.Definition;
                queryString["q.parser"] = info.Parser = request.Query.Parser;
            }

            if (request.Filter != null)
            {
                queryString["fq"] = info.Filter = request.Filter.Definition;
            }

            if (request.Start.HasValue)
            {
                queryString["start"] = info.Start = request.Start.ToString();
            }

            if (request.Size.HasValue)
            {
                queryString["size"] = info.Size = request.Size.ToString();
            }

            if (request.Sort.Any())
            {
                queryString["sort"] = info.Sort = string.Join(",", request.Sort);
            }

            if (request.Return.Any())
            {
                queryString["return"] = info.Return = string.Join(",", request.Return);
            }

            info.Facets = request.Facets.Select(facet =>
            {
                var queryParamName          = string.Format("facet.{0}", facet.Field.Name);
                var queryParamValue         = facet.Definition;
                queryString[queryParamName] = queryParamValue;
                return(new KeyValuePair <string, string>(queryParamName, queryParamValue));
            }).ToArray();

            info.Expressions = request.Expressions.Select(expression =>
            {
                var queryParamName          = string.Format("expr.{0}", expression.Name);
                var queryParamValue         = expression.Definition;
                queryString[queryParamName] = queryParamValue;
                return(new KeyValuePair <string, string>(queryParamName, queryParamValue));
            }).ToArray();

            return(RunSearch <T>(_searchClient, "search", queryString, info));
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Initializes a new instance of <see cref="SearchException" /> with the given info, status
 /// code, and message.
 /// </summary>
 public SearchException(SearchInfo request, HttpStatusCode httpStatusCode, string message)
     : base(httpStatusCode, message)
 {
     Request = request;
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Initializes a new instance of <see cref="SearchException" /> with the given info, status
 /// code, and message.
 /// </summary>
 public SearchException(SearchInfo request, HttpStatusCode httpStatusCode, string message)
     : base(httpStatusCode, message)
 {
     Request = request;
 }
Ejemplo n.º 4
0
        Exception HandleSearchError(HttpResponseMessage httpResponse, JsonTextReader jsonReader, SearchInfo info)
        {
            // TODO: 500 level errors should be marked Retry
            // TODO: 400 level errors should be marked as bad input.
            // TODO: Backoffs etc
            // http://docs.aws.amazon.com/cloudsearch/latest/developerguide/error-handling.html
            // http://docs.aws.amazon.com/general/latest/gr/api-retries.html

            var response = _responseDeserializer.Deserialize <SearchErrorResponse>(jsonReader);

            return(new SearchException(info, httpResponse.StatusCode, response.Message));
        }
Ejemplo n.º 5
0
        async Task <SearchResponse <T> > RunSearch <T>(HttpClient httpClient, string url, NameValueCollection queryString, SearchInfo info)
        {
            if (queryString != null)
            {
                url = info.Url = string.Format("{0}?{1}", url, queryString);
            }

            using (var httpResponse = await httpClient.GetAsync(url).ConfigureAwait(false))
            {
                using (var content = await httpResponse.Content.ReadAsStreamAsync().ConfigureAwait(false))
                    using (var streamReader = new StreamReader(content, Encoding.UTF8))
                        using (var jsonReader = new JsonTextReader(streamReader))
                        {
                            if (!httpResponse.IsSuccessStatusCode)
                            {
                                throw HandleSearchError(httpResponse, jsonReader, info);
                            }

                            var response = _jsonSerializer.Deserialize <SearchResponse <T> >(jsonReader);
                            response.Request = info;
                            return(response);
                        }
            }
        }