我有一个域,其中有 2 个可搜索字段(文字和文本字段),基本上是类别和内容。
如何指定要在字段中使用的特定字段?据我所知,它将默认搜索所有可搜索的字段,但这不是我想要的(即,不想仅仅因为它在内容。)
我正在使用 .Net SDK,并且我一直在浏览他们的文档,但找不到该部分。
谢谢!
我有一个域,其中有 2 个可搜索字段(文字和文本字段),基本上是类别和内容。
如何指定要在字段中使用的特定字段?据我所知,它将默认搜索所有可搜索的字段,但这不是我想要的(即,不想仅仅因为它在内容。)
我正在使用 .Net SDK,并且我一直在浏览他们的文档,但找不到该部分。
谢谢!
在深入挖掘之后,指定字段的方法是使用fields
数组字段。
使用时.Net SDK
,可以这样做:
new SearchRequest {
Query = query,
QueryOptions = @"{""fields"":[""field_name""]}"
};
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;
}