public void CreateIndex(string name, IEnumerable<IFieldDefinition> fieldDefinitions) { //You must add here your accessKey and SecretAccessKey. See here how to get them: http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSGettingStartedGuide/AWSCredentials.html using (IAmazonCloudSearch cloudSearchClient = AWSClientFactory.CreateAmazonCloudSearchClient(AmazonSearchService.AwsAccessKey, AmazonSearchService.AwsSecretAccessKey, RegionEndpoint.EUWest1)) { try { CreateDomainRequest domainRequest = new CreateDomainRequest(); domainRequest.DomainName = name; cloudSearchClient.CreateDomain(domainRequest); if (fieldDefinitions == null) throw new ArgumentNullException("fieldDefinitions"); foreach (var fieldDefinition in fieldDefinitions) { DefineIndexFieldRequest request = new DefineIndexFieldRequest(); request.DomainName = name; request.IndexField = new IndexField(); request.IndexField.IndexFieldName = fieldDefinition.Name.ToUpperInvariant(); if (fieldDefinition.Type == null || fieldDefinition.Type == typeof(string)) request.IndexField.IndexFieldType = IndexFieldType.Text; if (fieldDefinition.Type == typeof(string[])) request.IndexField.IndexFieldType = IndexFieldType.TextArray; if (fieldDefinition.Type == typeof(int)) request.IndexField.IndexFieldType = IndexFieldType.Int; if (fieldDefinition.Type == typeof(DateTime)) request.IndexField.IndexFieldType = IndexFieldType.Date; cloudSearchClient.DefineIndexField(request); } SearchResults searchResults = new SearchResults(); foreach (var field in searchResults.HighlightedFields) { Suggester suggester = new Suggester(); DocumentSuggesterOptions suggesterOptions = new DocumentSuggesterOptions(); suggesterOptions.FuzzyMatching = SuggesterFuzzyMatching.None; suggesterOptions.SourceField = field.ToUpperInvariant(); suggester.DocumentSuggesterOptions = suggesterOptions; suggester.SuggesterName = field.ToUpperInvariant() + "_suggester"; DefineSuggesterRequest defineRequest = new DefineSuggesterRequest(); defineRequest.DomainName = name; defineRequest.Suggester = suggester; cloudSearchClient.DefineSuggester(defineRequest); } searchResults.Dispose(); IndexDocumentsRequest documentRequest = new IndexDocumentsRequest(); documentRequest.DomainName = name; cloudSearchClient.IndexDocuments(documentRequest); } catch (BaseException ex) { Log.Write(ex.InnerException.Message); } catch (LimitExceededException ex) { Log.Write(ex.InnerException.Message); } catch (InternalException ex) { Log.Write(ex.InnerException.Message); } } }
public IResultSet Search(ISearchQuery query) { AmazonCloudSearchDomainConfig config = new AmazonCloudSearchDomainConfig(); config.ServiceURL = "http://search-index2-cdduimbipgk3rpnfgny6posyzy.eu-west-1.cloudsearch.amazonaws.com/"; AmazonCloudSearchDomainClient domainClient = new AmazonCloudSearchDomainClient("AKIAJ6MPIX37TLIXW7HQ", "DnrFrw9ZEr7g4Svh0rh6z+s3PxMaypl607eEUehQ", config); SearchRequest searchRequest = new SearchRequest(); 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.ToUpperInvariant()); highlights.Append("\':{} "); SuggestRequest suggestRequest = new SuggestRequest(); Suggester suggester = new Suggester(); suggester.SuggesterName = field.ToUpperInvariant() + "_suggester"; suggestRequest.Suggester = suggester.SuggesterName; 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("}"); 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); }