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

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

注意

Azure AI 搜索可通过Azure门户、REST API 和Azure SDK获取。 它也是 Foundry IQ 的基础;Foundry IQ 是一个托管式知识层,可将企业内容转化为供 Microsoft Foundry 门户中的智能体使用的、可复用且具备权限感知能力的知识库。

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

本文介绍如何对查询调用语义排名器。 它假定你使用的是最新的稳定 API 或预览 API。 有关较旧版本的帮助,请参阅 Migrate 语义排名代码。

先决条件

注意

标题和答案是从搜索文档中的文本逐字提取的。 语义子系统使用机器阅读理解来识别具有题注或答案特征的内容,但除 查询重写(预览) 外,不会生成新的句子或短语。 因此,包含解释或定义的内容最适合语义排名。 如果要与生成的响应进行聊天式交互,请参阅代理检索或检索扩充生成(RAG)。

选择客户端

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

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

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

  • 具有 search=* 或空搜索字符串(如纯筛选查询)的查询不起作用,因为没有任何可度量语义相关性,因此搜索分数为零。 查询必须提供可在处理过程中评估的术语或短语,并生成具有关联性评分的搜索文档。 评分结果作为语义排名器的输入。

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

设置查询

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

所有语义查询(无论是通过 search 加号 queryType还是通过 semanticQuery指定)都必须是纯文本,它们不能为空。 如下表所示,参数 queryType-semantic 支持查询格式的子集。

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

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

2 此参数 semanticQuery 可用于所有查询类型。 但是,Azure门户中不支持Search Explorer。

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

搜索资源管理器 包括语义排名的选项。 回想一下,无法在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

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

后续步骤

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