你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

将语义排名添加到 Azure AI 搜索中的查询

如果搜索文档包含字符串字段,并且搜索文档中的矢量查询具有文本表示形式,可以将语义排序应用于文本查询、混合查询和矢量查询。

本文介绍了如何对查询调用语义排序器。

先决条件

注意

标题和答案会从搜索文档的文本中逐字提取。 语义子系统使用计算机阅读理解来识别具有标题或答案特征的内容,但不会撰写新句子或短语。 出于此原因,包含解释或定义的内容最适用于语义排名。 如果希望与生成的响应进行聊天式交互,请参阅检索增强生成 (RAG)

选择客户端

可以使用以下任何工具和 SDK 来生成使用语义排序的查询:

避免绕过相关性评分的功能

一些查询功能绕过了相关性评分,使得它们无法与语义排名兼容。 如果你的查询逻辑包含以下功能,则无法对结果进行语义排名:

  • 包含 search=* 或空搜索字符串的查询(例如纯筛选器查询)将不起作用,因为没有任何内容可供度量语义相关性,因此搜索分数为零。 查询必须提供可在处理过程中评估的词语或短语。

  • 对特定字段进行排序(orderBy 子句)会覆盖搜索分数和语义分数。 鉴于语义分数应该用于提供排名,如果你对有序结果应用语义排名,添加 orderby 子句会导致 HTTP 400 错误。

设置查询

默认情况下,查询不使用语义排名。 若要使用语义排名,可以使用两个不同的参数。 每个参数都支持一组不同的方案。

无论是通过 search 加上 queryType 还是通过 semanticQuery 指定,语义查询都必须是纯文本,且不能为空。 空查询将导致不对结果应用语义排名。

语义排序器参数 纯文本搜索 简单文本搜索语法 全文本搜索语法 矢量搜索 混合搜索 语义答案和标题
queryType-semantic 1
semanticQuery="<your plain text query>"2

1 queryType=semantic 不支持显式 simplefull 值,因为 queryType 参数用于 semantic。 有效的查询行为是简单分析器的默认值。

2 semanticQuery 参数可用于所有查询类型。 但是,它在门户搜索资源管理器中不受支持。

无论选择的参数如何,索引都应包含具有丰富语义内容和语义配置的文本字段。

搜索资源管理器包括语义排序的选项。 回想一下,你无法在 Azure 门户中设置 semanticQuery 参数。

  1. 登录到 Azure 门户

  2. 打开搜索索引并选择“搜索资源管理器”。

  3. 选择“查询选项”。 如果已定义语义配置,则默认选择该配置。 如果未定义,请为索引创建语义配置

    显示搜索资源管理器中的查询选项的屏幕截图。

  4. 输入查询,如“拥有美食的历史酒店”,然后选择“搜索”。

  5. 或者,选择 JSON 视图并将定义粘贴到查询编辑器中。 Azure 门户不支持使用 semanticQuery,因此需要将 queryType 设置为 "semantic"

    显示 Azure 门户中 JSON 查询语法的屏幕截图。

    将查询类型设置为可以粘贴到视图中的语义的 JSON 示例:

    {
      "search": "funky or interesting hotel with good food on site",
      "count": true,
      "queryType": "semantic",
      "semanticConfiguration": "my-semantic-config",
      "captions": "extractive|highlight-true",
      "answers": "extractive|count-3",
      "highlightPreTag": "<strong>",
      "highlightPostTag": "</strong>",
      "select": "HotelId,HotelName,Description,Category"
    }
    

评估响应

只有最初结果中的前 50 个匹配项可以进行语义排序。 与所有查询一样,响应由标记为可检索的所有字段组成,或仅仅由 select 参数中列出的那些字段组成。 响应包括原始相关性分数,还可能包括计数或批处理结果,具体取决于你的请求的表述方式。

在语义排名中,响应包含更多元素:一个在语义上排名的新相关性分数、一个可选标题(纯文本或带有突出显示效果),以及一个可选答案。 如果结果不包含这些额外元素,则表示查询可能配置错误。 作为解决问题的第一步,请检查语义配置,以确保在索引定义和查询中都指定了它。

在客户端应用中,可以将搜索页面构建为包含一个标题作为匹配项的说明,而不是包含特定字段的全部内容。 此方法在“搜索结果”页的各个字段过于密集时很有用。

上述示例查询(“有趣的酒店,酒店内设有餐厅和舒适的大堂或共享区域”)的响应返回三个答案 ("answers": "extractive|count-e")。 返回标题是因为设置了“captions”属性,具有纯文本和突出显示的版本。 如果无法确定答案,则从响应中省略该答案。 为了简洁起见,此示例只显示查询中的三个答案和三个评分最高的结果。

{
  "@odata.count": 29,
  "@search.answers": [
    {
      "key": "24",
      "text": "Chic hotel near the city. High-rise hotel in downtown, within walking distance to theaters, art galleries, restaurants and shops. Visit Seattle Art Museum by day, and then head over to Benaroya Hall to catch the evening's concert performance.",
      "highlights": "Chic hotel near the city. <strong>High-rise hotel in downtown, </strong>within<strong> walking distance to </strong>theaters, art<strong> galleries, restaurants and shops.</strong> Visit Seattle Art Museum by day, and then head over to Benaroya Hall to catch the evening's concert performance.",
      "score": 0.9340000152587891
    },
    {
      "key": "40",
      "text": "Only 8 miles from Downtown. On-site bar/restaurant, Free hot breakfast buffet, Free wireless internet, All non-smoking hotel. Only 15 miles from airport.",
      "highlights": "Only 8 miles from Downtown. <strong>On-site bar/restaurant, Free hot breakfast buffet, Free wireless internet, </strong>All non-smoking<strong> hotel.</strong> Only 15 miles from airport.",
      "score": 0.9210000038146973
    },
    {
      "key": "38",
      "text": "Nature is Home on the beach. Explore the shore by day, and then come home to our shared living space to relax around a stone fireplace, sip something warm, and explore the library by night. Save up to 30 percent. Valid Now through the end of the year. Restrictions and blackouts may apply.",
      "highlights": "Nature is Home on the beach. Explore the shore by day, and then come home to our<strong> shared living space </strong>to relax around a stone fireplace, sip something warm, and explore the library by night. Save up to 30 percent. Valid Now through the end of the year. Restrictions and blackouts may apply.",
      "score": 0.9200000166893005
    }
  ],
  "value": [
    {
      "@search.score": 3.2328331,
      "@search.rerankerScore": 2.575303316116333,
      "@search.captions": [
        {
          "text": "The best of old town hospitality combined with views of the river and cool breezes off the prairie. Our penthouse suites offer views for miles and the rooftop plaza is open to all guests from sunset to 10 p.m. Enjoy a complimentary continental breakfast in the lobby, and free Wi-Fi throughout the hotel.",
          "highlights": "The best of old town hospitality combined with views of the river and cool breezes off the prairie. Our<strong> penthouse </strong>suites offer views for miles and the rooftop<strong> plaza </strong>is open to all guests from sunset to 10 p.m. Enjoy a<strong> complimentary continental breakfast in the lobby, </strong>and free Wi-Fi<strong> throughout </strong>the hotel."
        }
      ],
      "HotelId": "50",
      "HotelName": "Head Wind Resort",
      "Description": "The best of old town hospitality combined with views of the river and cool breezes off the prairie. Our penthouse suites offer views for miles and the rooftop plaza is open to all guests from sunset to 10 p.m. Enjoy a complimentary continental breakfast in the lobby, and free Wi-Fi throughout the hotel.",
      "Category": "Suite"
    },
    {
      "@search.score": 0.632956,
      "@search.rerankerScore": 2.5425150394439697,
      "@search.captions": [
        {
          "text": "Every stay starts with a warm cookie. Amenities like the Counting Sheep sleep experience, our Wake-up glorious breakfast buffet and spacious workout facilities await.",
          "highlights": "Every stay starts with a warm cookie. Amenities like the<strong> Counting Sheep sleep experience, </strong>our<strong> Wake-up glorious breakfast buffet and spacious workout facilities </strong>await."
        }
      ],
      "HotelId": "34",
      "HotelName": "Lakefront Captain Inn",
      "Description": "Every stay starts with a warm cookie. Amenities like the Counting Sheep sleep experience, our Wake-up glorious breakfast buffet and spacious workout facilities await.",
      "Category": "Budget"
    },
    {
      "@search.score": 3.7076726,
      "@search.rerankerScore": 2.4554927349090576,
      "@search.captions": [
        {
          "text": "Chic hotel near the city. High-rise hotel in downtown, within walking distance to theaters, art galleries, restaurants and shops. Visit Seattle Art Museum by day, and then head over to Benaroya Hall to catch the evening's concert performance.",
          "highlights": "Chic hotel near the city. <strong>High-rise hotel in downtown, </strong>within<strong> walking distance to </strong>theaters, art<strong> galleries, restaurants and shops.</strong> Visit Seattle Art Museum by day, and then head over to Benaroya Hall to catch the evening's concert performance."
        }
      ],
      "HotelId": "24",
      "HotelName": "Uptown Chic Hotel",
      "Description": "Chic hotel near the city. High-rise hotel in downtown, within walking distance to theaters, art galleries, restaurants and shops. Visit Seattle Art Museum by day, and then head over to Benaroya Hall to catch the evening's concert performance.",
      "Category": "Suite"
    },
   . . .
  ]
}

期望的工作负荷

对于语义排序,应要求搜索服务支持每个副本最多 10 个并发查询。

如果卷过高,服务会限制语义排序请求。 包含这些短语的错误消息指示服务容量为语义排序:

Error in search query: Operation returned an invalid status 'Partial Content'`
@search.semanticPartialResponseReason`
CapacityOverloaded

如果预计一致的吞吐量要求接近、达到或高于此级别,请提交支持票证,以便我们可以为工作负荷预配。

后续步骤

语义排序可用于将关键字搜索和矢量搜索合并到单个请求和统一响应的混合查询。