简体   繁体   中英

How to perform Amazon Cloud Search with .net code?

I am learning Amazon Cloud Search but I couldn't find any code in either C# or Java (though I am creating in C# but if I can get code in Java then I can try converting in C#).

This is just 1 code I found in C#: https://github.com/Sitefinity-SDK/amazon-cloud-search-sample/tree/master/SitefinityWebApp .

This is 1 method i found in this code:

 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;

            //var result = domainClient.Search(searchRequest).SearchResult;

            return new AmazonResultSet(result, suggestions);
        }

I have already created domain in Amazon Cloud Search using AWS console and uploaded document using Amazon predefine configuration option that is movie Imdb json file provided by Amazon for demo.

But in this method I am not getting how to use this method, like if I want to search Director name then how do I pass in this method as because this method parameter is of type ISearchQuery ?

I'd suggest using the official AWS CloudSearch .NET SDK . The library you were looking at seems fine (although I haven't look at it any detail) but the official version is more likely to expose new CloudSearch features as soon as they're released, will be supported if you need to talk to AWS support, etc, etc.

Specifically, take a look at the SearchRequest class -- all its params are strings so I think that obviates your question about ISearchQuery.

I wasn't able to find an example of a query in .NET but this shows someone uploading docs using the AWS .NET SDK. It's essentially the same procedure as querying: creating and configuring a Request object and passing it to the client.

EDIT : Since you're still having a hard time, here's an example. Bear in mind that I am unfamiliar with C# and have not attempted to run or even compile this but I think it should at least be close to working. It's based off looking at the docs at http://docs.aws.amazon.com/sdkfornet/v3/apidocs/

// Configure the Client that you'll use to make search requests
string queryUrl = @"http://search-<domainname>-xxxxxxxxxxxxxxxxxxxxxxxxxx.us-east-1.cloudsearch.amazonaws.com";
AmazonCloudSearchDomainClient searchClient = new AmazonCloudSearchDomainClient(queryUrl);

// Configure a search request with your query
SearchRequest searchRequest = new SearchRequest();
searchRequest.Query = "potato";
// TODO Set your other params like parser, suggester, etc

// Submit your request via the client and get back a response containing search results
SearchResponse searchResponse = searchClient.Search(searchRequest);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM