You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求C#从Azure Cognitive Search语义搜索获取AnswerResult的示例

C#从Azure Cognitive Search语义搜索获取Answer的示例

要获取语义搜索的AnswerResult,你需要在SearchOptions中开启答案提取配置,并从返回结果的SemanticSearchResults属性中读取答案,以下是修改后的完整代码:

SearchOptions options;
SearchResults<Hotel> response;

options = new SearchOptions()
{
    QueryType = Azure.Search.Documents.Models.SearchQueryType.Semantic,
    QueryLanguage = QueryLanguage.EnUs,
    SemanticConfigurationName = "my-semantic-config",
    QueryCaption = QueryCaptionType.Extractive,
    QueryCaptionHighlightEnabled = true,
    // 开启提取式语义答案
    QueryAnswer = QueryAnswerType.Extractive
};
options.Select.Add("HotelName");
options.Select.Add("Category");
options.Select.Add("Description");

response = srchclient.Search<Hotel>("Who is the manager of Triple Landscape Hotel?", options);

// 读取语义答案
if (response.SemanticSearchResults != null && response.SemanticSearchResults.Answers != null)
{
    foreach (var answer in response.SemanticSearchResults.Answers)
    {
        // 输出答案文本
        Console.WriteLine($"答案内容: {answer.AnswerText}");
        // 输出高亮的关键词(如果有)
        if (!string.IsNullOrEmpty(answer.Highlights))
        {
            Console.WriteLine("高亮部分: " + answer.Highlights);
        }
        // 输出答案来源的文档ID和字段
        Console.WriteLine($"来源文档ID: {answer.DocumentId},来源字段: {answer.Field}");
    }
}

关键说明

  • 必须设置QueryAnswer = QueryAnswerType.Extractive才能触发语义答案提取,这是你原有代码缺失的核心配置
  • 语义答案不在返回的文档集合里,而是存储在SearchResults<T>.SemanticSearchResults.Answers集合中
  • 确保你的语义配置my-semantic-config已经在Azure门户中正确启用了答案提取功能,并且指定了合适的字段作为答案来源

内容的提问来源于stack exchange,提问作者Joe Campbell

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 09:43:18