Beispiel #1
0
        public async Task <IEnumerable <FoundItem> > Search(string query, IImmutableList <string> markets, int maxResults)
        {
            Check.NotEmpty(query, nameof(query));
            Check.NotNull(markets, nameof(markets));
            Check.InRange(ref maxResults, nameof(maxResults), MinResults, MaxResults);

            // parse and check query
            var terms = ParseQuery(query);

            if (terms.Length == 0)
            {
                throw new InvalidQueryException();
            }

            // build query
            var request = new SearchRequest
            {
                Query       = BuildQuery(terms),
                FilterQuery = BuildFilterQuery(markets),
                Size        = maxResults,
                QueryParser = QueryParser.Structured,
            };

            Logger.Info("Searching:");
            Logger.Info($"Query: {request.Query}");
            Logger.Info($"Filter: {request.FilterQuery}");
            Logger.Info($"Size: {request.Size}");

            try
            {
                using (var client = new AmazonCloudSearchDomainClient(_config.KeyId, _config.SecretKey, _config.ServiceUrl))
                {
                    // execute search
                    var response = await client.SearchAsync(request);

                    Logger.Info($"Status: {response.HttpStatusCode}");

                    // build result
                    var result = new List <FoundItem>(response.Hits.Hit.Count);
                    foreach (var hit in response.Hits.Hit)
                    {
                        var documentItem = BuildDocumentItem(hit.Fields);
                        var foundItem    = _mapper.Map <FoundItem>(documentItem);
                        (foundItem.Id, foundItem.ItemType) = DocumentId.Parse(hit.Id);
                        result.Add(foundItem);
                    }

                    return(result);
                }
            }
            catch (Exception ex)
            {
                throw new SearchErrorException(ex);
            }
        }
Beispiel #2
0
        public async Task <IEnumerable <SearchEngineItem> > Search(string SearchStr, string market, int limit)
        {
            SearchRequest searchRequest = new SearchRequest();

            searchRequest.Query       = SearchStr;
            searchRequest.Size        = limit;
            searchRequest.FilterQuery = BuildMarketFilterQuery(market);
            searchRequest.QueryParser = QueryParser.Lucene;

            try
            {
                SearchResponse searchResponse = await searchClient.SearchAsync(searchRequest);

                return(ConvertSearchResult(searchResponse));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }