通过术语提升提高文档的排名

已完成

最先显示最相关的结果时,搜索效果最佳。 所有搜索引擎都尝试返回与搜索查询最相关的结果。 Azure AI 搜索实现了增强版本的 Apache Lucene 以进行全文搜索。

在此,你将了解如何编写更复杂的 Lucene 查询。 然后,你将通过提升搜索查询中的特定术语来提高结果的相关性。

搜索索引

通过 Azure AI 搜索,可以使用 REST 终结点或在 Azure 门户内使用搜索资源管理器工具来查询索引。 若要快速回顾查询处理的各个阶段,请参阅“创建 Azure AI 搜索解决方案”中的“搜索索引”单元

A diagram showing the four stages of query processing.

本单元重点介绍查询分析。

你将使用搜索资源管理器了解使用简单查询类型和完整查询类型更改搜索结果之间的差异。

注意

若要自行运行查询,则需要 Azure 订阅。 创建 Azure AI 搜索服务,并将酒店的示例数据导入索引中。

编写简单查询

酒店示例数据包含 50 家酒店的描述、客房详细信息及其位置。 假设你经营一项酒店预订业务,并拥有一款用户可用于预订酒店的应用。 用户可以进行搜索,并且必须最先显示最相关的酒店。

你有一个用例,其中客户试图寻找一家豪华型酒店。 首先查看这个简单查询中的搜索结果:

search=luxury&$select=HotelId, HotelName, Category, Tags, Description&$count=true

查询分析将在索引中文档的所有字段中搜索 luxury 一词。

查询字符串还会通过添加 select 选项来限制文档中返回的字段。

&$select=HotelId, HotelName, Category, Tags, Description

查询的最后一个参数要求索引对总结果进行计数。

$count=true

无需执行词法分析,因此文档检索将返回 14 个文档。 前三个文档为:

{
  "@odata.context": "https://advanced-cognitive-search.search.windows.net/indexes('hotels-sample-index')/$metadata#docs(*)",
  "@odata.count": 14,
  "value": [
    {
      "@search.score": 2.633778,
      "HotelId": "13",
      "HotelName": "Historic Lion Resort",
      "Description": "Unmatched Luxury.  Visit our downtown hotel to indulge in luxury accommodations. Moments from the stadium, we feature the best in comfort",
      "Category": "Budget",
      "Tags": [
        "view",
        "free wifi",
        "free wifi"
      ]
    },
    {
      "@search.score": 2.1104424,
      "HotelId": "18",
      "HotelName": "Oceanside Resort",
      "Description": "New Luxury Hotel.  Be the first to stay. Bay views from every room, location near the piper, rooftop pool, waterfront dining & more.",
      "Category": "Budget",
      "Tags": [
        "view",
        "laundry service",
        "air conditioning"
      ]
    },
    {
      "@search.score": 1.966516,
      "HotelId": "40",
      "HotelName": "Trails End Motel",
      "Description": "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.",
      "Category": "Luxury",
      "Tags": [
        "continental breakfast",
        "view",
        "view"
      ]
    },
    ...
  ]
}

客户可能会感到惊讶,你拥有的排名靠前的酒店应该是“豪华型”酒店,但实际上是经济型酒店,且未配备空调。 如果客户在其搜索中输入多个字词,则应用假定所有字词都应出现在结果中,因此它会在查询的字词之间添加 + 符号。 它发送到 API 的此查询为:

search=luxury + air con&$select=HotelId, HotelName, Category, Tags, Description&$count=true

搜索服务现在返回五个文档,但排名靠前的结果仍是经济型酒店。

启用 Lucene 查询分析器

可以通过将 &queryType=full 添加到查询字符串,指示搜索资源管理器使用 Lucene 查询分析器。

search=luxury AND air con&$select=HotelId, HotelName, Category, Tags, Description&$count=true&queryType=full

使用 Lucene 语法,可以编写更精确的查询。 下面是可用功能的摘要:

  • 布尔运算符ANDORNOT,例如 luxury AND 'air con'
  • 字段搜索fieldName:search term,例如 Description:luxury AND Tags:air con
  • 模糊搜索~,例如,Description:luxury~ 返回 luxury 的拼写错误的版本
  • 术语邻近搜索"term1 term2"~n,例如,"indoor swimming pool"~3 返回多个文档,每个文档都包含三个以内的字词 - indoor swimming pool
  • 正则表达式搜索/regular expression/ 使用 / 之间的正则表达式,例如 /[mh]otel/ 会返回包含 hotel 和 motel 的文档
  • 通配符搜索*?,其中 * 将匹配多个字符,而 ? 匹配单个字符,例如 'air con'* 会查找 air con 和 air conditioning
  • 优先级分组(term AND (term OR term)),例如 (Description:luxury OR Category:luxury) AND Tags:air?con*
  • 术语提升^,例如 Description:luxury OR Category:luxury^3 打给类型为 luxury 的 hotel 的分数比描述中有 luxury 的 hotel 的分数更高

若要详细了解这些功能,请参阅文档中的 Azure AI 搜索中的 Lucene 查询语法

提升搜索词

使用上述内容可以改进结果。 分析器应优先考虑豪华型酒店。 还可以更精确地在“标记”字段中查找空调。

(Description:luxury OR Category:luxury^3) AND Tags:'air con'*

添加其他查询字符串参数可获得此查询字符串:

search=(Description:luxury OR Category:luxury^3) AND Tags:'air con'*&$select=HotelId, HotelName, Category, Tags, Description&$count=true&queryType=full

前三家酒店现在包括:

{
  "@odata.context": "https://advanced-cognitive-search.search.windows.net/indexes('hotels-sample-index')/$metadata#docs(*)",
  "@odata.count": 5,
  "value": [
    {
      "@search.score": 5.3537707,
      "HotelId": "8",
      "HotelName": "Sapphire Resort",
      "Description": "Downtown, close to everything, steps to the park, shopping, and restaurants.",
      "Category": "Luxury",
      "Tags": [
        "free wifi",
        "continental breakfast",
        "air conditioning"
      ]
    },
    {
      "@search.score": 5.3522806,
      "HotelId": "49",
      "HotelName": "Old Carrabelle Hotel",
      "Description": "Spacious rooms, glamorous suites and residences, rooftop pool, walking access to shopping, dining, entertainment and the city center.",
      "Category": "Luxury",
      "Tags": [
        "air conditioning",
        "laundry service",
        "24-hour front desk service"
      ]
    },
    {
      "@search.score": 4.1448884,
      "HotelId": "18",
      "HotelName": "Oceanside Resort",
      "Description": "New Luxury Hotel.  Be the first to stay. Bay views from every room, location near the piper, rooftop pool, waterfront dining & more.",
      "Category": "Budget",
      "Tags": [
        "view",
        "laundry service",
        "air conditioning"
      ]
    },
    ...
  ]
}

蓝宝石度假酒店的搜索分数已从 2.3321536 增加到 5.3537707,现在是客户将看到的第一家酒店。 欧申赛德度假酒店现在位居第三。