简体   繁体   English

如何在 AWS CloudSearch 中指定搜索字段(使用 .net SDK)

[英]How to specify search field in AWS CloudSearch (using .net SDK)

I have a domain where I have 2 searchable fields (a literal and a text field), basically Category and Content.我有一个域,其中有 2 个可搜索字段(一个文字和一个文本字段),基本上是类别和内容。

How can I specify a specific field to use in a field?如何指定要在字段中使用的特定字段? From what I can tell, it will default to search all searchable fields, but that is not what I want (ie, don't want to look for category Cats and get a result from another category just because it includes the word Cat in the content.)据我所知,它将默认搜索所有可搜索的字段,但这不是我想要的(即,不想查找 Cats 类别并从另一个类别中获取结果,因为它在内容。)

I am using .Net SDK, and I've been going through their documentation, but can't find that section.我正在使用 .Net SDK,并且我一直在浏览他们的文档,但找不到该部分。

Thanks!谢谢!

After digging a bit more, and that the way to specify a field is using the fields array field.进一步挖掘之后,指定字段的方法是使用fields数组字段。

When using the .Net SDK , it can be done this way:使用.Net SDK ,可以通过以下方式完成:

new SearchRequest { 
   Query = query, 
   QueryOptions = @"{""fields"":[""field_name""]}" 
};

source: https://docs.aws.amazon.com/cloudsearch/latest/developerguide/search-api.html#structured-search-syntax来源: https : //docs.aws.amazon.com/cloudsearch/latest/developerguide/search-api.html#structured-search-syntax

Sample codes for Cloudsearch in C# aren't common however you can try this one i wrote C# 中 Cloudsearch 的示例代码并不常见,但是你可以试试我写的这个

public List<AdditionalFields> Search(string query)
    {
        var batch = new List<AdditionalFields>();
        query = query.Trim();
        try
        {
            // Configure the Client that you'll use to make search requests
            string queryUrl = @"http://search-********.cloudsearch.amazonaws.com";
            var awsCredentials = new Amazon.Runtime.BasicAWSCredentials();
            AmazonCloudSearchDomainClient searchClient = new AmazonCloudSearchDomainClient(queryUrl, awsCredentials);

            SearchRequest searchRequest = new SearchRequest();
            searchRequest.Query = query;
            //searchRequest.Facet =
            //searchRequest.QueryOptions.
            searchRequest.Return = "_all_fields";

            SearchResponse searchResponse = searchClient.SearchAsync(searchRequest).Result;

            foreach (var hit in searchResponse.Hits.Hit)
            {
                var fields = new AdditionalFields();
                var searchfields = new Fields();
                searchfields.content = hit.Fields["content"][0].ToString();
                searchfields.directory = hit.Fields["directory"][0].ToString();
                searchfields.resourcename = hit.Fields["resourcename"][0].ToString();
                searchfields.title = hit.Fields["title"][0].ToString();
                fields.abridgedContent = searchfields.content.Replace(searchfields.title, string.Empty);
                searchfields.version = hit.Fields["version"][0].ToString();
                fields.abridgedContent = Abridged(fields.abridgedContent, query);
                searchfields.description = hit.Fields["description"][0].ToString();
                searchfields.resourcename = hit.Fields["resourcename"][0].ToString();
                searchfields.resourcetype = hit.Fields["resourcetype"][0].ToString();
                fields.url = "/bookviewer?bookValue=" + searchfields.resourcename + "&version=" + searchfields.version + "&chapterFolder=" + searchfields.directory + "&firstChapterNo=" + searchfields.description + ".htm";
                fields.fields = searchfields;
                batch.Add(fields);
            }

            // return searchResponse.Status.ToString();
        }
        catch (AmazonCloudSearchDomainException ex)
        {
            var fields = new AdditionalFields();
            fields.error = "ERROR : " + ex.Message;

            batch.Add(fields);

        }
        catch (Exception ex)
        {
            var fields = new AdditionalFields();
            fields.error = "ERROR : " + ex.Message;
            batch.Add(fields);
        }

        return batch;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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