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

快速入门:使用 REST 进行矢量搜索

了解如何使用搜索 REST API 在 Azure AI 搜索中创建、加载和查询矢量。

在 Azure AI 搜索中,矢量存储具有定义矢量和非矢量字段的索引架构、用于创建嵌入空间的算法的矢量配置,以及查询请求中使用的矢量字段定义的设置。 创建索引 REST API 将创建矢量存储。

如果没有 Azure 订阅,请在开始之前创建一个免费帐户

注意

本快速入门省略了矢量化步骤,并在示例文档中提供了嵌入内容。 如果要添加基于你自己的内容的内置数据分块和矢量化功能,请尝试使用“导入和矢量化数据”向导进行端到端演练。

先决条件

下载文件

从 GitHub 下载 REST 示例以发送本快速入门中的请求。 有关详细信息,请参阅从 GitHub 下载文件

你还可以在本地系统上启动一个新文件,并根据本文中的说明手动创建请求。

获取搜索终结点和 API 密钥

你可以在 Azure 门户中找到搜索服务终结点和 API 密钥。 在下一步中,你要将这些值粘贴到 .rest.http 文件中。

搜索终结点的请求必须经过身份验证和授权。 你可以使用 API 密钥或角色来完成此任务。 密钥更容易上手,但角色更安全。 尽管我们在本快速入门中使用 API 密钥,但我们建议切换到无密钥连接

  1. 登录到 Azure 门户查找你的搜索服务

  2. 在“概述”主页上,找到相应 URL。 示例终结点可能类似于 https://mydemo.search.windows.net

    概述页面上 URL 属性的屏幕截图。

  3. 选择“设置”>“密钥”。 必须启用“API 密钥”或“两者”管理 API 密钥用于添加、修改和删除对象。 有两个可互换的管理密钥。 复制其中任意一个。

    显示 Azure 门户中的 API 密钥的屏幕截图。

创建矢量索引

创建索引 (REST) 创建矢量索引并在搜索服务上设置物理数据结构。

索引架构围绕酒店内容进行组织。 示例数据由矢量和非矢量名称以及七家虚构酒店的描述组成。 该架构包括矢量索引和查询以及语义排名的配置。

  1. 在 Visual Studio Code 中创建新的文本文件。

  2. 在文件顶部,为之前收集的值添加变量。

    @baseUrl = PUT-YOUR-SEARCH-SERVICE-URL-HERE
    @apiKey = PUT-YOUR-ADMIN-KEY-HERE
    
  3. 使用 .rest.http 文件扩展名保存文件。

  4. 粘贴以下示例,以在搜索服务上创建 hotels-vector-quickstart 索引。

    ### Create a new index
    POST {{baseUrl}}/indexes?api-version=2023-11-01  HTTP/1.1
        Content-Type: application/json
        api-key: {{apiKey}}
    
    {
        "name": "hotels-vector-quickstart",
        "fields": [
            {
                "name": "HotelId", 
                "type": "Edm.String",
                "searchable": false, 
                "filterable": true, 
                "retrievable": true, 
                "sortable": false, 
                "facetable": false,
                "key": true
            },
            {
                "name": "HotelName", 
                "type": "Edm.String",
                "searchable": true, 
                "filterable": false, 
                "retrievable": true, 
                "sortable": true, 
                "facetable": false
            },
            {
                "name": "HotelNameVector",
                "type": "Collection(Edm.Single)",
                "searchable": true,
                "retrievable": true,
                "dimensions": 1536,
                "vectorSearchProfile": "my-vector-profile"
            },
            {
                "name": "Description", 
                "type": "Edm.String",
                "searchable": true, 
                "filterable": false, 
                "retrievable": true, 
                "sortable": false, 
                "facetable": false
            },
            {
                "name": "DescriptionVector",
                "type": "Collection(Edm.Single)",
                "searchable": true,
                "retrievable": true,
                "dimensions": 1536,
                "vectorSearchProfile": "my-vector-profile"
            },
            {
                "name": "Category", 
                "type": "Edm.String",
                "searchable": true, 
                "filterable": true, 
                "retrievable": true, 
                "sortable": true, 
                "facetable": true
            },
            {
                "name": "Tags",
                "type": "Collection(Edm.String)",
                "searchable": true,
                "filterable": true,
                "retrievable": true,
                "sortable": false,
                "facetable": true
            },
            {
                "name": "Address", 
                "type": "Edm.ComplexType",
                "fields": [
                    {
                        "name": "City", "type": "Edm.String",
                        "searchable": true, "filterable": true, "retrievable": true, "sortable": true, "facetable": true
                    },
                    {
                        "name": "StateProvince", "type": "Edm.String",
                        "searchable": true, "filterable": true, "retrievable": true, "sortable": true, "facetable": true
                    }
                ]
            },
            {
                "name": "Location",
                "type": "Edm.GeographyPoint",
                "searchable": false, 
                "filterable": true, 
                "retrievable": true, 
                "sortable": true, 
                "facetable": false
            }
        ],
        "vectorSearch": {
            "algorithms": [
                {
                    "name": "my-hnsw-vector-config-1",
                    "kind": "hnsw",
                    "hnswParameters": 
                    {
                        "m": 4,
                        "efConstruction": 400,
                        "efSearch": 500,
                        "metric": "cosine"
                    }
                }
            ],
            "profiles": [      
                {
                    "name": "my-vector-profile",
                    "algorithm": "my-hnsw-vector-config-1"
                }
          ]
        },
        "semantic": {
            "configurations": [
                {
                    "name": "my-semantic-config",
                    "prioritizedFields": {
                        "titleField": {
                            "fieldName": "HotelName"
                        },
                        "prioritizedContentFields": [
                            { "fieldName": "Description" }
                        ],
                        "prioritizedKeywordsFields": [
                            { "fieldName": "Tags" }
                        ]
                    }
                }
            ]
        }
    }
    
  5. 再次保存文件,然后选择“发送请求”。 应具有 HTTP/1.1 201 Created 响应。 响应正文应包含索引架构的 JSON 表示形式。

    有关此 REST API 的要点:

    • fields 集合包含文本和矢量搜索所需的关键字段、文本字段和矢量字段(例如 DescriptionDescriptionVector)。 将矢量字段和非矢量字段并置在同一索引中可实现混合查询。 例如,可以将筛选器、带语义排名的文本搜索和矢量合并到单个查询操作中。

    • 矢量字段必须是带有 dimensionsvectorSearchProfiletype: Collection(Edm.Single)

    • vectorSearch 部分是一组最近的邻域算法配置和配置文件的数组。 支持的算法包括分层可导航小世界和穷举 k 最近邻域。 有关详细信息,请参阅矢量搜索中的相关性评分

    • [可选]:semantic 配置允许对搜索结果重新排名。 可以对配置中指定字符串字段的 semantic 类型查询的结果进行重新排名。 若要了解详细信息,请参阅语义排名概述

上传文档

创建和加载索引是两个独立的步骤。 在 Azure AI 搜索中,索引包含所有可搜索的数据,并且查询在搜索服务上运行。 对于 REST 调用,数据以 JSON 文档的形式提供。 为此任务使用文档 - 索引 REST API

会扩展 URI 以包含 docs 集合和 index 操作。

重要

以下示例不是可运行的代码。 为了便于阅读,我们排除了矢量值,因为每个矢量值包含 1536 个嵌入,这对于本文而言太长了。 如果你要尝试执行此步骤,请从 GitHub 上的示例中复制可运行的代码。

  1. 粘贴上传文档的有效请求,如以下示例所示。

  2. 保存该文件,然后选择“发送请求”。 应具有 HTTP/1.1 201 Created 响应。 响应正文应包含搜索文档的 JSON 表示形式。

### Upload documents
POST {{baseUrl}}/indexes/hotels-quickstart-vectors/docs/index?api-version=2023-11-01  HTTP/1.1
Content-Type: application/json
api-key: {{apiKey}}

{
    "value": [
        {
            "@search.action": "mergeOrUpload",
            "HotelId": "1",
            "HotelName": "Stay-Kay City Hotel",
            "HotelNameVector": [VECTOR ARRAY OMITTED],
            "Description": 
                "The hotel is ideally located on the main commercial artery of the city 
                in the heart of New York.",
            "DescriptionVector": [VECTOR ARRAY OMITTED],
            "Category": "Boutique",
            "Tags": [
                "pool",
                "air conditioning",
                "concierge"
            ],
        },
        {
            "@search.action": "mergeOrUpload",
            "HotelId": "2",
            "HotelName": "Old Century Hotel",
            "HotelNameVector": [VECTOR ARRAY OMITTED],
            "Description": 
                "The hotel is situated in a  nineteenth century plaza, which has been 
                expanded and renovated to the highest architectural standards to create a modern, 
                functional and first-class hotel in which art and unique historical elements 
                coexist with the most modern comforts.",
            "DescriptionVector": [VECTOR ARRAY OMITTED],
            "Category": "Boutique",
            "Tags": [
                "pool",
                "air conditioning",
                "free wifi",
                "concierge"
            ]
        },
        {
            "@search.action": "mergeOrUpload",
            "HotelId": "3",
            "HotelName": "Gastronomic Landscape Hotel",
            "HotelNameVector": [VECTOR ARRAY OMITTED],
            "Description": 
                "The Hotel stands out for its gastronomic excellence under the management of 
                William Dough, who advises on and oversees all of the Hotel’s restaurant services.",
            "DescriptionVector": [VECTOR ARRAY OMITTED],
            "Category": "Resort and Spa",
            "Tags": [
                "air conditioning",
                "bar",
                "continental breakfast"
            ]
        }
        {
            "@search.action": "mergeOrUpload",
            "HotelId": "4",
            "HotelName": "Sublime Palace Hotel",
            "HotelNameVector": [VECTOR ARRAY OMITTED],
            "Description": 
                "Sublime Palace Hotel is located in the heart of the historic center of 
                Sublime in an extremely vibrant and lively area within short walking distance to 
                the sites and landmarks of the city and is surrounded by the extraordinary beauty 
                of churches, buildings, shops and monuments. 
                Sublime Palace is part of a lovingly restored 1800 palace.",
            "DescriptionVector": [VECTOR ARRAY OMITTED],
            "Category": "Boutique",
            "Tags": [
                "concierge",
                "view",
                "24-hour front desk service"
            ]
        },
        {
            "@search.action": "mergeOrUpload",
            "HotelId": "13",
            "HotelName": "Luxury Lion Resort",
            "HotelNameVector": [VECTOR ARRAY OMITTED],
            "Description": 
                "Unmatched Luxury.  Visit our downtown hotel to indulge in luxury 
                accommodations. Moments from the stadium, we feature the best in comfort",
            "DescriptionVector": [VECTOR ARRAY OMITTED],
            "Category": "Resort and Spa",
            "Tags": [
                "view",
                "free wifi",
                "pool"
            ]
        },
        {
            "@search.action": "mergeOrUpload",
            "HotelId": "48",
            "HotelName": "Nordick's Valley Motel",
            "HotelNameVector": [VECTOR ARRAY OMITTED],
            "Description": 
                "Only 90 miles (about 2 hours) from the nation's capital and nearby 
                most everything the historic valley has to offer.  Hiking? Wine Tasting? Exploring 
                the caverns?  It's all nearby and we have specially priced packages to help make 
                our B&B your home base for fun while visiting the valley.",
            "DescriptionVector": [VECTOR ARRAY OMITTED],
            "Category": "Boutique",
            "Tags": [
                "continental breakfast",
                "air conditioning",
                "free wifi"
            ],
        },
        {
            "@search.action": "mergeOrUpload",
            "HotelId": "49",
            "HotelName": "Swirling Currents Hotel",
            "HotelNameVector": [VECTOR ARRAY OMITTED],
            "Description": 
                "Spacious rooms, glamorous suites and residences, rooftop pool, walking 
                access to shopping, dining, entertainment and the city center.",
            "DescriptionVector": [VECTOR ARRAY OMITTED],
            "Category": "Luxury",
            "Tags": [
                "air conditioning",
                "laundry service",
                "24-hour front desk service"
            ]
        }
    ]
}

有关此 REST API 的要点:

  • 有效负载中的文档由索引架构中定义的字段组成。

  • 矢量字段包含浮点值。 dimensions 属性的最小值为 2,每个字段不超过 3072 个浮点值。 本快速入门将 dimensions 属性设置为 1536,因为这是 Azure OpenAI text-embedding-ada-002 模型生成的嵌入项的大小。

运行查询

加载文档后,可以使用文档 - 搜索 POST (REST) 对其发出矢量查询。

有几个查询可用于演示各种模式:

本部分中的矢量查询基于两个字符串:

  • 搜索字符串historic hotel walk to restaurants and shopping
  • 矢量查询字符串(矢量化为数学表示形式):classic lodging near running trails, eateries, retail

矢量查询字符串在语义上类似于搜索字符串,但包含搜索索引中不存在的术语。 如果对 classic lodging near running trails, eateries, retail 执行关键字搜索,则不返回任何结果。 以上示例演示了即使没有匹配的字词,你如何获取相关结果。

重要

以下示例是不可运行的代码。 为了便于阅读,我们排除了矢量值,因为每个数组值包含 1536 个嵌入,这对于本文而言太长了。 如果你要尝试这些查询,请从 GitHub 上的示例中复制可运行的代码。

  1. 粘贴 POST 请求以查询搜索索引。 保存文件。 然后,选择“发送请求”。 URI 扩展为包含 /docs/search 运算符。

    ### Run a query
    POST {{baseUrl}}/indexes/hotels-vector-quickstart/docs/search?api-version=2023-11-01  HTTP/1.1
        Content-Type: application/json
        api-key: {{apiKey}}
    
        {
            "count": true,
            "select": "HotelId, HotelName, Description, Category",
            "vectorQueries": [
                {
                    "vector"": [0.01944167, 0.0040178085
                        . . .  TRIMMED FOR BREVITY
                        010858015, -0.017496133],
                    "k": 7,
                    "fields": "DescriptionVector",
                    "kind": "vector",
                    "exhaustive": true
                }
            ]
        }
    

    为简洁起见,缩短了此矢量查询。 vectorQueries.vector 包含查询输入的矢量化文本,fields 确定要搜索的矢量字段,k 指定要返回的最近邻域的数目。

    矢量查询字符串为 classic lodging near running trails, eateries, retail,该字符串将矢量化为此查询的 1536 个嵌入项。

  2. 查看回应。 等效于 classic lodging near running trails, eateries, retail 的矢量的响应包括 7 个结果。 每个结果都提供一个搜索分数和 select 中列出的字段。 在相似性搜索中,响应总是包含按相似性分数值排序的 k 个结果。

    {
        "@odata.context": "https://my-demo-search.search.windows.net/indexes('hotels-vector-quickstart')/$metadata#docs(*)",
        "@odata.count": 7,
        "value": [
            {
                "@search.score": 0.857736,
                "HotelName": "Nordick's Valley Motel",
                "Description": "Only 90 miles (about 2 hours) from the nation's capital and nearby most everything the historic valley has to offer.  Hiking? Wine Tasting? Exploring the caverns?  It's all nearby and we have specially priced packages to help make our B&B your home base for fun while visiting the valley."
            },
            {
                "@search.score": 0.8399129,
                "HotelName": "Swirling Currents Hotel",
                "Description": "Spacious rooms, glamorous suites and residences, rooftop pool, walking access to shopping, dining, entertainment and the city center."
            },
            {
                "@search.score": 0.8383954,
                "HotelName": "Luxury Lion Resort",
                "Description": "Unmatched Luxury.  Visit our downtown hotel to indulge in luxury accommodations. Moments from the stadium, we feature the best in comfort"
            },
            {
                "@search.score": 0.8254346,
                "HotelName": "Sublime Palace Hotel",
                "Description": "Sublime Palace Hotel is located in the heart of the historic center of Sublime in an extremely vibrant and lively area within short walking distance to the sites and landmarks of the city and is surrounded by the extraordinary beauty of churches, buildings, shops and monuments. Sublime Palace is part of a lovingly restored 1800 palace."
            },
            {
                "@search.score": 0.82380056,
                "HotelName": "Stay-Kay City Hotel",
                "Description": "The hotel is ideally located on the main commercial artery of the city in the heart of New York."
            },
            {
                "@search.score": 0.81514084,
                "HotelName": "Old Century Hotel",
                "Description": "The hotel is situated in a  nineteenth century plaza, which has been expanded and renovated to the highest architectural standards to create a modern, functional and first-class hotel in which art and unique historical elements coexist with the most modern comforts."
            },
            {
                "@search.score": 0.8133763,
                "HotelName": "Gastronomic Landscape Hotel",
                "Description": "The Hotel stands out for its gastronomic excellence under the management of William Dough, who advises on and oversees all of the Hotel’s restaurant services."
            }
        ]
    }
    

使用筛选器的单矢量搜索

可以添加筛选器,但筛选器将应用于索引中的非矢量内容。 在此示例中,筛选器适用于 Tags 字段,可筛选出任何不提供免费 Wi-Fi 的酒店。

  1. 粘贴 POST 请求以查询搜索索引。

    ### Run a vector query with a filter
    POST {{baseUrl}}/indexes/hotels-vector-quickstart/docs/search?api-version=2023-11-01  HTTP/1.1
        Content-Type: application/json
        api-key: {{apiKey}}
    
        {
            "count": true,
            "select": "HotelId, HotelName, Category, Tags, Description",
            "filter": "Tags/any(tag: tag eq 'free wifi')",
            "vectorFilterMode": "postFilter",
            "vectorQueries": [
            {
                "vector": [ VECTOR OMITTED ],
                "k": 7,
                "fields": "DescriptionVector",
                "kind": "vector",
                "exhaustive": true
            },
        ]
    }
    
  2. 查看回应。 查询与前面的示例相同,但包含后处理排除筛选器,仅返回具有免费 Wi-Fi 的三家酒店。

    {
    
        "@odata.count": 3,
        "value": [
            {
                "@search.score": 0.857736,
                "HotelName": "Nordick's Valley Motel",
                "Description": "Only 90 miles (about 2 hours) from the nation's capital and nearby most everything the historic valley has to offer.  Hiking? Wine Tasting? Exploring the caverns?  It's all nearby and we have specially priced packages to help make our B&B your home base for fun while visiting the valley.",
                "Tags": [
                    "continental breakfast",
                    "air conditioning",
                    "free wifi"
                ]
            },
            {
                "@search.score": 0.8383954,
                "HotelName": "Luxury Lion Resort",
                "Description": "Unmatched Luxury.  Visit our downtown hotel to indulge in luxury accommodations. Moments from the stadium, we feature the best in comfort",
                "Tags": [
                    "view",
                    "free wifi",
                    "pool"
                ]
            },
            {
                "@search.score": 0.81514084,
                "HotelName": "Old Century Hotel",
                "Description": "The hotel is situated in a  nineteenth century plaza, which has been expanded and renovated to the highest architectural standards to create a modern, functional and first-class hotel in which art and unique historical elements coexist with the most modern comforts.",
                "Tags": [
                    "pool",
                    "free wifi",
                    "concierge"
                ]
            }
        ]
    }
    

混合搜索由单个搜索请求中的关键字查询和矢量查询组成。 此示例并发运行矢量查询和全文搜索:

  • 搜索字符串historic hotel walk to restaurants and shopping
  • 矢量查询字符串(矢量化为数学表示形式):classic lodging near running trails, eateries, retail
  1. 粘贴 POST 请求以查询搜索索引。 然后,选择“发送请求”。

    ### Run a hybrid query
    POST {{baseUrl}}/indexes/hotels-vector-quickstart/docs/search?api-version=2023-11-01  HTTP/1.1
        Content-Type: application/json
        api-key: {{apiKey}}
    
    {
        "count": true,
        "search": "historic hotel walk to restaurants and shopping",
        "select": "HotelName, Description",
        "top": 7,
        "vectorQueries": [
            {
                "vector": [ VECTOR OMITTED],
                "k": 7,
                "fields": "DescriptionVector",
                "kind": "vector",
                "exhaustive": true
            }
        ]
    }
    

    由于这是混合查询,因此结果按倒数排名融合 (RRF) 进行排名。 RRF 评估多个搜索结果的搜索分数,采用反函数,然后合并和排序组合的结果。 返回 top 结果数。

  2. 查看回应。

    {
        "@odata.count": 7,
        "value": [
            {
                "@search.score": 0.03279569745063782,
                "HotelName": "Luxury Lion Resort",
                "Description": "Unmatched Luxury.  Visit our downtown hotel to indulge in luxury accommodations. Moments from the stadium, we feature the best in comfort"
            },
            {
                "@search.score": 0.03226646035909653,
                "HotelName": "Sublime Palace Hotel",
                "Description": "Sublime Palace Hotel is located in the heart of the historic center of Sublime in an extremely vibrant and lively area within short walking distance to the sites and landmarks of the city and is surrounded by the extraordinary beauty of churches, buildings, shops and monuments. Sublime Palace is part of a lovingly restored 1800 palace."
            },
            {
                "@search.score": 0.03226646035909653,
                "HotelName": "Swirling Currents Hotel",
                "Description": "Spacious rooms, glamorous suites and residences, rooftop pool, walking access to shopping, dining, entertainment and the city center."
            },
            {
                "@search.score": 0.03205128386616707,
                "HotelName": "Nordick's Valley Motel",
                "Description": "Only 90 miles (about 2 hours) from the nation's capital and nearby most everything the historic valley has to offer.  Hiking? Wine Tasting? Exploring the caverns?  It's all nearby and we have specially priced packages to help make our B&B your home base for fun while visiting the valley."
            },
            {
                "@search.score": 0.03128054738044739,
                "HotelName": "Gastronomic Landscape Hotel",
                "Description": "The Hotel stands out for its gastronomic excellence under the management of William Dough, who advises on and oversees all of the Hotel’s restaurant services."
            },
            {
                "@search.score": 0.03100961446762085,
                "HotelName": "Old Century Hotel",
                "Description": "The hotel is situated in a  nineteenth century plaza, which has been expanded and renovated to the highest architectural standards to create a modern, functional and first-class hotel in which art and unique historical elements coexist with the most modern comforts."
            },
            {
                "@search.score": 0.03077651560306549,
                "HotelName": "Stay-Kay City Hotel",
                "Description": "The hotel is ideally located on the main commercial artery of the city in the heart of New York."
            }
        ]
    }
    

    由于 RRF 合并了结果,因此这有助于查看输入。 以下结果仅来自全文查询。 前两个结果是 Sublime Palace Hotel 和 History Lion Resort。 Sublime Palace Hotel 具有更强的 BM25 相关性分数。

            {
                "@search.score": 2.2626662,
                "HotelName": "Sublime Palace Hotel",
                "Description": "Sublime Palace Hotel is located in the heart of the historic center of Sublime in an extremely vibrant and lively area within short walking distance to the sites and landmarks of the city and is surrounded by the extraordinary beauty of churches, buildings, shops and monuments. Sublime Palace is part of a lovingly restored 1800 palace."
            },
            {
                "@search.score": 0.86421645,
                "HotelName": "Luxury Lion Resort",
                "Description": "Unmatched Luxury.  Visit our downtown hotel to indulge in luxury accommodations. Moments from the stadium, we feature the best in comfort"
                },
    

    在使用 HNSW 查找匹配项的纯向量查询中,Sublime Palace Hotel 跌至第四位。 History Lion Resort 是全文搜索的第二位和矢量搜索的第三位,并没有经历相同的波动范围,因此在均匀化结果集中显示为顶级匹配。

        "value": [
            {
                "@search.score": 0.857736,
                "HotelId": "48",
                "HotelName": "Nordick's Valley Motel",
                "Description": "Only 90 miles (about 2 hours) from the nation's capital and nearby most everything the historic valley has to offer.  Hiking? Wine Tasting? Exploring the caverns?  It's all nearby and we have specially priced packages to help make our B&B your home base for fun while visiting the valley.",
                "Category": "Boutique"
            },
            {
                "@search.score": 0.8399129,
                "HotelId": "49",
                "HotelName": "Swirling Currents Hotel",
                "Description": "Spacious rooms, glamorous suites and residences, rooftop pool, walking access to shopping, dining, entertainment and the city center.",
                "Category": "Luxury"
            },
            {
                "@search.score": 0.8383954,
                "HotelId": "13",
                "HotelName": "Luxury 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": "Resort and Spa"
            },
            {
                "@search.score": 0.8254346,
                "HotelId": "4",
                "HotelName": "Sublime Palace Hotel",
                "Description": "Sublime Palace Hotel is located in the heart of the historic center of Sublime in an extremely vibrant and lively area within short walking distance to the sites and landmarks of the city and is surrounded by the extraordinary beauty of churches, buildings, shops and monuments. Sublime Palace is part of a lovingly restored 1800 palace.",
                "Category": "Boutique"
            },
            {
                "@search.score": 0.82380056,
                "HotelId": "1",
                "HotelName": "Stay-Kay City Hotel",
                "Description": "The hotel is ideally located on the main commercial artery of the city in the heart of New York.",
                "Category": "Boutique"
            },
            {
                "@search.score": 0.81514084,
                "HotelId": "2",
                "HotelName": "Old Century Hotel",
                "Description": "The hotel is situated in a  nineteenth century plaza, which has been expanded and renovated to the highest architectural standards to create a modern, functional and first-class hotel in which art and unique historical elements coexist with the most modern comforts.",
                "Category": "Boutique"
            },
            {
                "@search.score": 0.8133763,
                "HotelId": "3",
                "HotelName": "Gastronomic Landscape Hotel",
                "Description": "The Hotel stands out for its gastronomic excellence under the management of William Dough, who advises on and oversees all of the Hotel’s restaurant services.",
                "Category": "Resort and Spa"
            }
        ]
    

使用筛选器的语义混合搜索

下面是集合中的最后一个查询。 具有语义排名的此混合查询经过筛选,仅显示华盛顿特区半径 500 公里内的酒店。可以将 vectorFilterMode 设置为 null,这相当于默认值(较新的索引为 preFilter,较旧的索引为 postFilter)。

  1. 粘贴 POST 请求以查询搜索索引。 然后,选择“发送请求”。

    ### Run a hybrid query
    POST {{baseUrl}}/indexes/hotels-vector-quickstart/docs/search?api-version=2023-11-01  HTTP/1.1
        Content-Type: application/json
        api-key: {{apiKey}}
    
    {
        "count": true,
        "search": "historic hotel walk to restaurants and shopping",
        "select": "HotelId, HotelName, Category, Description,Address/City, Address/StateProvince",
        "filter": "geo.distance(Location, geography'POINT(-77.03241 38.90166)') le 500",
        "vectorFilterMode": null,
        "facets": [ "Address/StateProvince"],
        "top": 7,
        "queryType": "semantic",
        "answers": "extractive|count-3",
        "captions": "extractive|highlight-true",
        "semanticConfiguration": "my-semantic-config",
        "vectorQueries": [
            {
                "vector": [ VECTOR OMITTED ],
                "k": 7,
                "fields": "DescriptionVector",
                "kind": "vector",
                "exhaustive": true
            }
        ]
    }
    
  2. 查看回应。 响应返回三家酒店,它们按位置筛选,按 StateProvince 划分,按语义重新排名以提升结果从而显示最接近搜索字符串查询的结果 (historic hotel walk to restaurants and shopping)。

    现在,Swirling Currents Hotel 跃居榜首。 如果没有语义排名,Nordick's Valley Motel 就是第一名。 借助语义排名,机器理解模型理解 historic 适用于酒店,并且步行可达餐饮(餐馆)和购物场所。

    {
        "@odata.count": 3,
        "@search.facets": {
            "Address/StateProvince": [
                {
                    "count": 1,
                    "value": "NY"
                },
                {
                    "count": 1,
                    "value": "VA"
                }
            ]
        },
        "@search.answers": [],
        "value": [
            {
                "@search.score": 0.03306011110544205,
                "@search.rerankerScore": 2.5094974040985107,
                "HotelId": "49",
                "HotelName": "Swirling Currents Hotel",
                "Description": "Spacious rooms, glamorous suites and residences, rooftop pool, walking access to shopping, dining, entertainment and the city center.",
                "Category": "Luxury",
                "Address": {
                    "City": "Arlington",
                    "StateProvince": "VA"
                }
            },
            {
                "@search.score": 0.03306011110544205,
                "@search.rerankerScore": 2.0370211601257324,
                "HotelId": "48",
                "HotelName": "Nordick's Valley Motel",
                "Description": "Only 90 miles (about 2 hours) from the nation's capital and nearby most everything the historic valley has to offer.  Hiking? Wine Tasting? Exploring the caverns?  It's all nearby and we have specially priced packages to help make our B&B your home base for fun while visiting the valley.",
                "Category": "Boutique",
                "Address": {
                    "City": "Washington D.C.",
                    "StateProvince": null
                }
            },
            {
                "@search.score": 0.032258063554763794,
                "@search.rerankerScore": 1.6706111431121826,
                "HotelId": "1",
                "HotelName": "Stay-Kay City Hotel",
                "Description": "The hotel is ideally located on the main commercial artery of the city in the heart of New York.",
                "Category": "Boutique",
                "Address": {
                    "City": "New York",
                    "StateProvince": "NY"
                }
            }
        ]
    }
    

    有关此 REST API 的要点:

    • 矢量搜索通过 vectors.value 属性指定。 关键字搜索通过 search 属性指定。

    • 在混合搜索中,可以将矢量搜索与关键字的全文搜索相集成。 筛选器、拼写检查和语义排名仅适用于文本内容,不适用于矢量。 在此最后查询中,没有语义 answer,因为系统没有产生一个足够强大的答案。

    • 实际结果包含更多详细信息,包括语义说明和突出显示。 为便于阅读,对结果进行了修改。 若要获取响应的完整结构,请在 REST 客户端中运行请求。

清理

在自己的订阅中操作时,最好在项目结束时确定是否仍需要已创建的资源。 持续运行资源可能会产生费用。 可以逐个删除资源,也可以删除资源组以删除整个资源集。

可以在 Azure 门户中使用最左侧窗格中的“所有资源”或“资源组”链接来查找和管理资源。

还可以尝试以下 DELETE 命令:

### Delete an index
DELETE  {{baseUrl}}/indexes/hotels-vector-quickstart?api-version=2023-11-01 HTTP/1.1
    Content-Type: application/json
    api-key: {{apiKey}}

后续步骤

接下来,我们建议学习如何在不使用 API 密钥的情况下调用 REST API。

你还可以查看 PythonC#JavaScript 的演示代码。