你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
快速入门:使用 REST 进行矢量搜索
了解如何使用搜索 REST API 在 Azure AI 搜索中创建、加载和查询矢量。
在 Azure AI 搜索中,矢量存储具有定义矢量和非矢量字段的索引架构、用于创建嵌入空间的算法的矢量配置,以及查询请求中使用的矢量字段定义的设置。 创建索引 REST API 将创建矢量存储。
如果没有 Azure 订阅,请在开始之前创建一个免费帐户。
注意
本快速入门省略了矢量化步骤,并在示例文档中提供了嵌入内容。 如果要添加基于你自己的内容的内置数据分块和矢量化功能,请尝试使用“导入和矢量化数据”向导进行端到端演练。
先决条件
任何区域和任何层中的 Azure AI 搜索。 创建或找到当前订阅下的现有 Azure AI 搜索资源。
检索资源信息
搜索终结点的请求必须经过身份验证和授权。 你可以使用 API 密钥或角色来完成此任务。 建议通过 Microsoft Entra ID 使用无键连接。
选择与首选身份验证方法对应的选项卡。 对于本快速入门中的所有请求,请使用相同的方法。
创建或下载代码文件
使用一个 .rest
或 .http
文件运行本快速入门中的所有请求。 可以下载包含本快速入门代码的 REST 文件,也可以在 Visual Studio Code 中创建新文件,并将代码复制到其中。
在 Visual Studio Code 中,创建包含
.rest
或.http
文件扩展的新文件。 例如,az-search-vector-quickstart.rest
。 将 Azure-Samples/azure-search-rest-samples/blob/main/Quickstart-vectors/az-search-vector-quickstart.rest 文件的原始内容复制并粘贴到此新文件中。在文件的顶部,将
@baseUrl
的占位符值替换为搜索服务 URL。 有关如何查找搜索服务 URL 的说明,请参阅检索资源信息部分。@baseUrl = PUT-YOUR-SEARCH-SERVICE-URL-HERE
在文件的顶部,将占位符值替换为身份验证。 有关如何获取 Microsoft Entra 令牌或 API 密钥的说明,请参阅检索资源信息部分。
若要使用 Microsoft Entra ID 进行推荐的无密钥身份验证,你需要将
@apiKey
替换为@token
变量。@token = PUT-YOUR-MICROSOFT-ENTRA-TOKEN-HERE
如果想要使用 API 密钥,请将
@apiKey
替换为从 Azure 门户复制的密钥。@apiKey = PUT-YOUR-ADMIN-KEY-HERE
若要使用 Microsoft Entra ID 进行推荐的无密钥身份验证,你需要将
api-key: {{apiKey}}
替换为请求标头中的Authorization: Bearer {{token}}
。 替换文件中找到的所有api-key: {{apiKey}}
实例。
创建矢量索引
使用创建索引 REST API 创建矢量索引,并在搜索服务上设置物理数据结构。
此示例中的索引架构围绕酒店内容进行组织。 示例数据由矢量和非矢量名称以及虚构酒店的描述组成。 该架构包括矢量索引和查询以及语义排名的配置。
在 Visual Studio Code 中,打开先前创建的
az-search-vector-quickstart.rest
文件。在文件中查找
### Create a new index
代码块。 此块包含在搜索服务上创建hotels-vector-quickstart
索引的请求。### Create a new index POST {{baseUrl}}/indexes?api-version=2023-11-01 HTTP/1.1 Content-Type: application/json Authorization: Bearer {{token}} { "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": "Description_fr", "type": "Edm.String", "searchable": true, "filterable": false, "retrievable": true, "sortable": false, "facetable": false, "analyzer": "en.microsoft" }, { "name": "Description_frvector", "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": "ParkingIncluded", "type": "Edm.Boolean", "searchable": false, "filterable": true, "retrievable": true, "sortable": true, "facetable": true }, { "name": "LastRenovationDate", "type": "Edm.DateTimeOffset", "searchable": false, "filterable": true, "retrievable": true, "sortable": true, "facetable": true }, { "name": "Rating", "type": "Edm.Double", "searchable": false, "filterable": true, "retrievable": true, "sortable": true, "facetable": true }, { "name": "Address", "type": "Edm.ComplexType", "fields": [ { "name": "StreetAddress", "type": "Edm.String", "searchable": true, "filterable": false, "retrievable": true, "sortable": false, "facetable": false }, { "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": "PostalCode", "type": "Edm.String", "searchable": true, "filterable": true, "retrievable": true, "sortable": true, "facetable": true }, { "name": "Country", "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" } }, { "name": "my-hnsw-vector-config-2", "kind": "hnsw", "hnswParameters": { "m": 4, "metric": "euclidean" } }, { "name": "my-eknn-vector-config", "kind": "exhaustiveKnn", "exhaustiveKnnParameters": { "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": "Category" } ] } } ] } }
选择“发送请求”。 应具有
HTTP/1.1 201 Created
响应。
响应正文应包含索引架构的 JSON 表示形式。
{
"@odata.context": "https://my-demo-search.search.windows.net/$metadata#indexes/$entity",
"@odata.etag": "\"0x8DD2E70E6C36D8E\"",
"name": "hotels-vector-quickstart",
"defaultScoringProfile": null,
"fields": [
{
"name": "HotelId",
"type": "Edm.String",
"searchable": false,
"filterable": true,
"retrievable": true,
"sortable": false,
"facetable": false,
"key": true,
"indexAnalyzer": null,
"searchAnalyzer": null,
"analyzer": null,
"dimensions": null,
"vectorSearchProfile": null,
"synonymMaps": []
},
[MORE FIELD DEFINITIONS OMITTED FOR BREVITY]
],
"scoringProfiles": [],
"corsOptions": null,
"suggesters": [],
"analyzers": [],
"tokenizers": [],
"tokenFilters": [],
"charFilters": [],
"encryptionKey": null,
"similarity": {
"@odata.type": "#Microsoft.Azure.Search.BM25Similarity",
"k1": null,
"b": null
},
"vectorSearch": {
"algorithms": [
{
"name": "my-hnsw-vector-config-1",
"kind": "hnsw",
"hnswParameters": {
"metric": "cosine",
"m": 4,
"efConstruction": 400,
"efSearch": 500
},
"exhaustiveKnnParameters": null
},
{
"name": "my-hnsw-vector-config-2",
"kind": "hnsw",
"hnswParameters": {
"metric": "euclidean",
"m": 4,
"efConstruction": 400,
"efSearch": 500
},
"exhaustiveKnnParameters": null
},
{
"name": "my-eknn-vector-config",
"kind": "exhaustiveKnn",
"hnswParameters": null,
"exhaustiveKnnParameters": {
"metric": "cosine"
}
}
],
"profiles": [
{
"name": "my-vector-profile",
"algorithm": "my-hnsw-vector-config-1"
}
]
},
"semantic": {
"defaultConfiguration": null,
"configurations": [
{
"name": "my-semantic-config",
"prioritizedFields": {
"titleField": {
"fieldName": "HotelName"
},
"prioritizedContentFields": [
{
"fieldName": "Description"
}
],
"prioritizedKeywordsFields": [
{
"fieldName": "Category"
}
]
}
}
]
}
}
有关创建索引 REST API 的要点:
fields
集合包含文本和矢量搜索所需的关键字段、文本字段和矢量字段(例如Description
和DescriptionVector
)。 将矢量字段和非矢量字段并置在同一索引中可实现混合查询。 例如,可以将筛选器、带语义排名的文本搜索和矢量合并到单个查询操作中。矢量字段必须是带有
dimensions
和vectorSearchProfile
的type: Collection(Edm.Single)
。vectorSearch
部分是一组最近的邻域算法配置和配置文件的数组。 支持的算法包括分层可导航小世界和穷举 k 最近邻域。 有关详细信息,请参阅矢量搜索中的相关性评分。(可选)
semantic
配置允许对搜索结果重新排名。 可以对配置中指定字符串字段的semantic
类型查询的结果进行重新排名。 若要了解详细信息,请参阅语义排名概述。
上传文档
创建和加载索引是两个独立的步骤。 在上一步中创建了索引架构。 现在需要将文档加载到索引中。
在 Azure AI 搜索中,索引包含所有可搜索的数据,并且查询在搜索服务上运行。 对于 REST 调用,数据以 JSON 文档的形式提供。 为此任务使用文档 - 索引 REST API。 会扩展 URI 以包含 docs
集合和 index
操作。
在 Visual Studio Code 中,打开先前创建的
az-search-vector-quickstart.rest
文件。在文件中查找
### Upload documents
代码块。 此块包含将文档上传到搜索服务上的hotels-vector-quickstart
索引的请求。### Upload documents POST {{baseUrl}}/indexes/hotels-quickstart-vectors/docs/index?api-version=2023-11-01 HTTP/1.1 Content-Type: application/json Authorization: Bearer {{token}} { "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" ] } ] }
重要
此示例中的代码不可运行。 为了简洁起见,删除了多个字符或行。 使用
az-search-vector-quickstart.rest
文件中的代码运行请求。选择“发送请求”。 应具有
HTTP/1.1 200 OK
响应。 响应正文应包含搜索文档的 JSON 表示形式。
有关文档 - 索引 REST API 请求的关键要点:
有效负载中的文档由索引架构中定义的字段组成。
矢量字段包含浮点值。 dimensions 属性的最小值为 2,每个字段不超过 3072 个浮点值。 本快速入门将 dimensions 属性设置为 1536,因为这是 Azure OpenAI text-embedding-ada-002 模型生成的嵌入项的大小。
运行查询
加载文档后,可以使用文档 - 搜索 POST (REST) 对其发出矢量查询。
在接下来的部分中,我们将针对 hotels-vector-quickstart
索引运行查询。 查询包含:
示例矢量查询基于两个字符串:
- 搜索字符串:
historic hotel walk to restaurants and shopping
- 矢量查询字符串(矢量化为数学表示形式):
classic lodging near running trails, eateries, retail
矢量查询字符串在语义上类似于搜索字符串,但包含搜索索引中不存在的术语。 如果对 classic lodging near running trails, eateries, retail
执行关键字搜索,则不返回任何结果。 以上示例演示了即使没有匹配的字词,你如何获取相关结果。
单矢量搜索
在 Visual Studio Code 中,打开先前创建的
az-search-vector-quickstart.rest
文件。在文件中查找
### Run a single vector query
代码块。 此块包含查询搜索索引的请求。### Run a single vector query POST {{baseUrl}}/indexes/hotels-vector-quickstart/docs/search?api-version=2023-11-01 HTTP/1.1 Content-Type: application/json Authorization: Bearer {{token}} { "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 个嵌入项。重要
此示例中的代码不可运行。 为了简洁起见,删除了多个字符或行。 使用
az-search-vector-quickstart.rest
文件中的代码运行请求。选择“发送请求”。 应具有
HTTP/1.1 200 OK
响应。 响应正文应包含搜索结果的 JSON 表示形式。
等效于 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.85773647,
"HotelId": "48",
"HotelName": "Nordick's 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.8399132,
"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"
},
{
"@search.score": 0.83839583,
"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": "Resort and Spa"
},
{
"@search.score": 0.82543474,
"HotelId": "4",
"HotelName": "Sublime Cliff Hotel",
"Description": "Sublime Cliff 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 Cliff is part of a lovingly restored 1800 palace.",
"Category": "Boutique"
},
{
"@search.score": 0.82380104,
"HotelId": "1",
"HotelName": "Secret Point 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.8151413,
"HotelId": "2",
"HotelName": "Twin Dome 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.8133767,
"HotelId": "3",
"HotelName": "Triple 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\u2019s restaurant services.",
"Category": "Resort and Spa"
}
]
}
使用筛选器的单矢量搜索
可以添加筛选器,但筛选器将应用于索引中的非矢量内容。 在此示例中,筛选器适用于 Tags
字段,可筛选出任何不提供免费 Wi-Fi 的酒店。
在 Visual Studio Code 中,打开先前创建的
az-search-vector-quickstart.rest
文件。在文件中查找
### Run a vector query with a filter
代码块。 此块包含查询搜索索引的请求。### 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 Authorization: Bearer {{token}} { "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 }, ] }
重要
此示例中的代码不可运行。 为了简洁起见,删除了多个字符或行。 使用
az-search-vector-quickstart.rest
文件中的代码运行请求。选择“发送请求”。 应具有
HTTP/1.1 200 OK
响应。 响应正文应包含搜索结果的 JSON 表示形式。
查询与前面的单向量搜索示例相同,但包含后处理排除筛选器,仅返回具有免费 Wi-Fi 的三家酒店。
{
"@odata.context": "https://my-demo-search.search.windows.net/indexes('hotels-vector-quickstart')/$metadata#docs(*)",
"@odata.count": 3,
"value": [
{
"@search.score": 0.85773647,
"HotelId": "48",
"HotelName": "Nordick's 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",
"Tags": [
"continental breakfast",
"air conditioning",
"free wifi"
]
},
{
"@search.score": 0.83839583,
"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": "Resort and Spa",
"Tags": [
"view",
"free wifi",
"pool"
]
},
{
"@search.score": 0.8151413,
"HotelId": "2",
"HotelName": "Twin Dome 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",
"Tags": [
"pool",
"free wifi",
"air conditioning",
"concierge"
]
}
]
}
混合搜索
混合搜索由单个搜索请求中的关键字查询和矢量查询组成。 此示例并发运行矢量查询和全文搜索:
- 搜索字符串:
historic hotel walk to restaurants and shopping
- 矢量查询字符串(矢量化为数学表示形式):
classic lodging near running trails, eateries, retail
在 Visual Studio Code 中,打开先前创建的
az-search-vector-quickstart.rest
文件。在文件中查找
### Run a hybrid query
代码块。 此块包含查询搜索索引的请求。### Run a hybrid query POST {{baseUrl}}/indexes/hotels-vector-quickstart/docs/search?api-version=2023-11-01 HTTP/1.1 Content-Type: application/json Authorization: Bearer {{token}} { "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 } ] }
重要
此示例中的代码不可运行。 为了简洁起见,删除了多个字符或行。 使用
az-search-vector-quickstart.rest
文件中的代码运行请求。选择“发送请求”。 应具有
HTTP/1.1 200 OK
响应。 响应正文应包含搜索结果的 JSON 表示形式。
由于这是混合查询,因此结果按倒数排名融合 (RRF) 进行排名。 RRF 评估多个搜索结果的搜索分数,采用反函数,然后合并和排序组合的结果。 返回 top
结果数。
检查响应:
{
"@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
)。
在 Visual Studio Code 中,打开先前创建的
az-search-vector-quickstart.rest
文件。在文件中查找
### Run a hybrid query with semantic reranking
代码块。 此块包含查询搜索索引的请求。### Run a hybrid query with semantic reranking POST {{baseUrl}}/indexes/hotels-vector-quickstart/docs/search?api-version=2023-11-01 HTTP/1.1 Content-Type: application/json Authorization: Bearer {{token}} { "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 } ] }
重要
此示例中的代码不可运行。 为了简洁起见,删除了多个字符或行。 使用
az-search-vector-quickstart.rest
文件中的代码运行请求。选择“发送请求”。 应具有
HTTP/1.1 200 OK
响应。 响应正文应包含搜索结果的 JSON 表示形式。
查看回应。 响应返回三家酒店,它们按位置筛选,按 StateProvince
划分,按语义重新排名以提升结果从而显示最接近搜索字符串查询的结果 (historic hotel walk to restaurants and shopping
)。
现在,Swirling Currents Hotel 跃居榜首。 如果没有语义排名,Nordick's Valley Motel 就是第一名。 借助语义排名,机器理解模型理解 historic
适用于酒店,并且步行可达餐饮(餐馆)和购物场所。
{
"@odata.context": "https://my-demo-search.search.windows.net/indexes('hotels-vector-quickstart')/$metadata#docs(*)",
"@odata.count": 2,
"@search.facets": {
"Address/StateProvince": [
{
"count": 1,
"value": "VA"
}
]
},
"@search.answers": [],
"value": [
{
"@search.score": 0.03306011110544205,
"@search.rerankerScore": 2.8773112297058105,
"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",
"Address": {
"City": "Arlington",
"StateProvince": "VA"
}
},
{
"@search.score": 0.03306011110544205,
"@search.rerankerScore": 2.1703834533691406,
"HotelId": "48",
"HotelName": "Nordick's 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
}
}
]
}
有关文档 - 搜索帖子 REST API 的关键要点:
矢量搜索通过
vectors.value
属性指定。 关键字搜索通过search
属性指定。在混合搜索中,可以将矢量搜索与关键字的全文搜索相集成。 筛选器、拼写检查和语义排名仅适用于文本内容,不适用于矢量。 在此最后查询中,没有语义
answer
,因为系统没有产生一个足够强大的答案。实际结果包含更多详细信息,包括语义说明和突出显示。 为便于阅读,对结果进行了修改。 若要获取响应的完整结构,请在 REST 客户端中运行请求。
清理
在自己的订阅中操作时,最好在项目结束时确定是否仍需要已创建的资源。 持续运行资源可能会产生费用。 可以逐个删除资源,也可以删除资源组以删除整个资源集。
可以在 Azure 门户中使用最左侧窗格中的“所有资源”或“资源组”链接来查找和管理资源。
如果要保留搜索服务,但删除索引和文档,则可以在 REST 客户端中使用 DELETE
命令。 此命令(在 az-search-vector-quickstart.rest
文件末尾)删除 hotels-vector-quickstart
索引:
### Delete an index
DELETE {{baseUrl}}/indexes/hotels-vector-quickstart?api-version=2023-11-01 HTTP/1.1
Content-Type: application/json
Authorization: Bearer {{token}}
后续步骤
接下来,我们建议学习如何在不使用 API 密钥的情况下调用 REST API。
你还可以查看 Python、C# 或 JavaScript 的演示代码。