如何用.net代码执行亚马逊云搜索

本文关键字:亚马逊 搜索 执行 代码 何用 net | 更新日期: 2023-09-27 18:09:55

我正在学习亚马逊云搜索,但我在c#或Java中找不到任何代码(虽然我在c#中创建,但如果我能在Java中获得代码,那么我可以尝试在c#中转换)。

这只是我在c#中发现的1个代码:https://github.com/Sitefinity-SDK/amazon-cloud-search-sample/tree/master/SitefinityWebApp.

这是我在这段代码中找到的一个方法:

 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);
        }

我已经使用AWS控制台在Amazon Cloud Search中创建了域,并使用Amazon预定义配置选项上传了文档,该选项是Amazon提供的用于演示的movie Imdb json文件。

但是在这个方法中,我不知道如何使用这个方法,就像如果我想搜索Director name那么我如何传递这个方法,因为这个方法参数的类型是ISearchQuery?

如何用.net代码执行亚马逊云搜索

我建议使用官方AWS CloudSearch . net SDK。你正在看的库看起来不错(尽管我没有看到任何细节),但官方版本更有可能在发布后立即暴露新的CloudSearch功能,如果你需要与AWS支持人员交谈,将会得到支持,等等。

具体来说,看一下SearchRequest类——它的所有参数都是字符串,所以我认为这就消除了你关于ISearchQuery的问题。

我在。net中找不到一个查询的例子,但这显示有人使用AWS。net SDK上传文档。它本质上与查询过程相同:创建和配置Request对象并将其传递给客户端。

编辑:既然你还是觉得困难,我给你举个例子。请记住,我不熟悉c#,也没有尝试运行或甚至编译这个,但我认为它至少应该接近工作。它基于在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);