分享方式:


快速入門:使用 REST API 進行向量搜尋

了解如何使用搜尋服務 REST API 在 Azure AI 搜尋服務中建立、載入及查詢向量。

在 Azure AI 搜尋服務中,「向量存放區」具有定義向量和非向量欄位的索引結構描述、建立內嵌空間的演算法向量設定,以及向量欄位定義上用於查詢要求的設定。 建立索引 API 會建立向量存放區。

如尚未擁有 Azure 訂用帳戶,請在開始之前先建立免費帳戶

注意

穩定的 2023-11-01 REST API 版本取決於用於資料區塊化和內嵌的外部解決方案。 如果您想要評估內建的資料區塊化和向量化功能 (公開預覽) 功能,請嘗試匯入和向量化資料精靈,以取得端對端逐步解說。

必要條件

下載檔案

從 GitHub 下載 REST 範例,以在此快速入門中傳送要求。 如需相關資訊,請參閱從 GitHub 下載檔案

您也可以在本機系統上啟動新的檔案,並使用本文中的指示手動建立要求。

取得搜尋服務端點

您可以在 Azure 入口網站中找到搜尋服務端點。

  1. 登入 Azure 入口網站,然後尋找您的搜尋服務

  2. 在 [概觀] 首頁上,尋找 URL。 範例端點看起來會像是 https://mydemo.search.windows.net

    概觀頁面上 URL 屬性的螢幕擷取畫面。

在稍後的步驟中,您會將此端點貼到 .rest.http 檔案中。

設定存取權

對搜尋端點發出的要求必須經過驗證和授權。 您可以針對這項工作使用 API 金鑰或角色。 金鑰較容易上手,但角色更安全。

針對角色型連線,下列指示可讓您連線到您身分識別 (不是用戶端應用程式的身分識別) 下的 Azure AI 搜尋服務。

選項 1:使用金鑰

選取 [設定]>[金鑰],然後複製系統管理金鑰。 系統管理金鑰可用來新增、修改和刪除物件。 有兩個可交換的系統管理密鑰。 複製任一個。 如需詳細資訊,請參閱使用金鑰驗證連線到 Azure AI 搜尋服務

顯示 Azure 入口網站中 API 金鑰的螢幕擷取畫面。

在稍後的步驟中,您會將此金鑰貼到 .rest.http 檔案中。

選項 2:使用角色

請確定您的搜尋服務已設定角色型存取。 您必須具備預先設定的開發人員存取角色指派。 您的角色指派必須授與建立、載入及查詢搜尋索引的權限。

在本節中,使用 Azure CLI、Azure PowerShell 或 Azure 入口網站取得您的個人身分識別權杖。

  1. 登入 Azure CLI。

    az login
    
  2. 取得您的個人身分識別權杖。

    az account get-access-token --scope https://search.azure.com/.default
    

您會在稍後的步驟中,將個人身分識別權杖貼到 .rest.http 檔案中。

注意

本節假設您使用的是代表您連線到 Azure AI 搜尋服務的本機用戶端。 替代方法是取得用戶端應用程式的權杖,並假設您的應用程式已註冊 Microsoft Entra ID。

建立向量索引

建立索引 (REST) 會建立向量索引,並在您的搜尋服務上設定實體資料結構。

索引架構會針對旅館內容進行組織。 範例資料是由七家虛構旅館的向量和非向量名稱和描述所組成。 此架構包含向量索引編製和查詢的設定,以及語意排名的設定。

  1. 在 Visual Studio Code 中開啟新的文字檔。

  2. 將變數設定為您稍早收集的值。 此範例會使用個人身分識別權杖。

    @baseUrl = PUT-YOUR-SEARCH-SERVICE-URL-HERE
    @token = PUT-YOUR-PERSONAL-IDENTITY-TOKEN-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
        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": "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"
                    }
                },
                {
                    "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": "Tags" }
                        ]
                    }
                }
            ]
        }
    }
    
  5. 選取 [傳送要求]。 回想一下,您需要 REST 用戶端來傳送要求。 您應該會有 HTTP/1.1 201 Created 回應。 回應主體應該包含索引架構的 JSON 表示法。

    重點︰

    • fields 集合包含文字和向量搜尋所需的索引鍵欄位、文字和向量欄位(例如 DescriptionDescriptionVector)。 在同一個索引中共置向量和非向量欄位可啟用混合式查詢。 例如,您可以將篩選、文字搜尋與語意排名和向量合併成單一查詢作業。
    • 向量欄位必須是有 dimensionsvectorSearchProfile 屬性的 type: Collection(Edm.Single)
    • vectorSearch 區段是近似最近鄰演算法設定和設定檔的陣列。 支援的演算法包括階層式可導覽小世界 (navigable small world) 和詳盡的 K 最近鄰 (k-nearest neighbor)。 如需詳細資訊,請參閱向量搜尋中的相關性評分
    • [選用]:semantic 設定可重新排名搜尋結果。 針對設定中所指定的字串欄位,您可以進行類型 semantic 查詢結果的重新排名。 若要深入了解,請參閱語意排名概觀

上傳文件

建立和載入索引是單獨的步驟。 在 Azure AI 搜尋服務中,索引包含所有可搜尋資料以及對搜尋服務執行的查詢。 針對 REST 呼叫,數據會以 JSON 文件的形式提供。 針對這項工作使用 Documents- Index REST API

URI 會擴充以包含 docs 集合和 index 作業。

重要

下列範例不是可執行的程式碼。 為了可讀性,我們排除了向量值,因為每個值都包含 1,536 個內嵌,這對於本文而言太長。 如果您想要嘗試此步驟,請從 GitHub 上的範例中複製可執行的程式碼。

### 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": "Secret Point Motel",
            "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": "Twin Dome 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": "Triple 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 Cliff Hotel",
            "HotelNameVector": [VECTOR ARRAY OMITTED],
            "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.",
            "DescriptionVector": [VECTOR ARRAY OMITTED],
            "Category": "Boutique",
            "Tags": [
                "concierge",
                "view",
                "24-hour front desk service"
            ]
        },
        {
            "@search.action": "mergeOrUpload",
            "HotelId": "13",
            "HotelName": "Historic 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": "Nordicks Hotel",
            "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": "Old Carrabelle 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"
            ]
        }
    ]
}

重點︰

  • 承載中的文件是由索引架構中定義的欄位所組成。
  • 向量欄位包含浮點值。 維度屬性至少有 2 個,而且每個值最多 3,072 個浮點數。 本快速入門會將維度屬性設定為 1,536,因為這是 Open AI text-embedding-ada-002 模型所產生的內嵌大小。

執行查詢

現在已載入文件,您可以使用文件 - 搜尋貼文 (REST) 來對其發出向量查詢。

有一些查詢可示範各種模式:

本節中的向量查詢是以兩個字串為基礎:

  • 搜尋字串historic hotel walk to restaurants and shopping
  • 向量查詢字串 (向量化為數學表示法):classic lodging near running trails, eateries, retail

向量查詢字串在語意上類似於搜尋字串,但包含搜尋索引中不存在的字詞。 如果您對 classic lodging near running trails, eateries, retail 執行關鍵詞搜尋,則結果為零。 我們使用此範例示範如何取得相關結果,即使沒有相符的字詞也一樣。

重要

下列範例是無法執行的程式碼。 為了可讀性,我們排除了向量值,因為每個陣列都包含 1,536 個內嵌,這對於本文而言太長。 如果您想要嘗試這些步驟,請從 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
        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,其會向量化為此查詢的 1,536 個內嵌。

  2. 檢閱回應。 classic lodging near running trails, eateries, retail 對等向量的回應包含七個結果。 每個結果都會提供搜尋分數和列在 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 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": "Old Carrabelle 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": "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"
            },
            {
                "@search.score": 0.8254346,
                "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."
            },
            {
                "@search.score": 0.82380056,
                "HotelName": "Secret Point 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": "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."
            },
            {
                "@search.score": 0.8133763,
                "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’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
        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
            },
        ]
    }
    
  2. 檢閱回應。 此查詢與前一個範例相同,但包含後置處理的排除篩選,並只傳回具有免費 Wi-Fi 的三家旅館。

    {
    
        "@odata.count": 3,
        "value": [
            {
                "@search.score": 0.857736,
                "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.",
                "Tags": [
                    "continental breakfast",
                    "air conditioning",
                    "free wifi"
                ]
            },
            {
                "@search.score": 0.8383954,
                "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",
                "Tags": [
                    "view",
                    "free wifi",
                    "pool"
                ]
            },
            {
                "@search.score": 0.81514084,
                "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.",
                "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
        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
            }
        ]
    }
    

    因為這是混合式查詢,因此結果會依倒數排名融合 (RRF) 進行排名。 RRF 會評估多個搜尋結果的搜尋分數、採用反向排列,然後合併並排序合併的結果。 傳回的結果數目 top

  2. 檢閱回應。

    {
        "@odata.count": 7,
        "value": [
            {
                "@search.score": 0.03279569745063782,
                "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"
            },
            {
                "@search.score": 0.03226646035909653,
                "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."
            },
            {
                "@search.score": 0.03226646035909653,
                "HotelName": "Old Carrabelle 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 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": "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’s restaurant services."
            },
            {
                "@search.score": 0.03100961446762085,
                "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."
            },
            {
                "@search.score": 0.03077651560306549,
                "HotelName": "Secret Point Hotel",
                "Description": "The hotel is ideally located on the main commercial artery of the city in the heart of New York."
            }
        ]
    }
    

    因為 RRF 會合併結果,所以有助於檢閱輸入。 下列結果僅來自全文檢索搜尋查詢。 前兩名的結果是 Sublime Cliff Hotel 和 History Lion Resort。 Sublime Cliff Hotel 的 BM25 相關性分數較高。

            {
                "@search.score": 2.2626662,
                "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."
            },
            {
                "@search.score": 0.86421645,
                "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"
                },
    

    在僅限向量的查詢中 (其使用 HNSW 尋找相符項目),Sublime Cliff Hotel 會下降到第四個位置。 Historic Lion 在全文檢索搜尋中排名第二,在向量搜尋中排名第三,並未經歷相同的波動範圍,因此成為同質化結果集中的最高相符項目。

        "value": [
            {
                "@search.score": 0.857736,
                "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.8399129,
                "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.8383954,
                "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.8254346,
                "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.82380056,
                "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.81514084,
                "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.8133763,
                "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’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
        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
            }
        ]
    }
    
  2. 檢閱回應。 回應中有三家旅館,依位置篩選,並根據 StateProvince 進行 Facet 篩選,然後經過語意排名以提升最接近搜尋字串查詢的結果 (historic hotel walk to restaurants and shopping)。

    Old Carabelle Hotel 現在成為最佳位置。 沒有語意排名,北歐的旅館是首選。 使用語意排名,機器理解模型認識到,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": "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.0370211601257324,
                "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
                }
            },
            {
                "@search.score": 0.032258063554763794,
                "@search.rerankerScore": 1.6706111431121826,
                "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",
                "Address": {
                    "City": "New York",
                    "StateProvince": "NY"
                }
            }
        ]
    }
    

    重點︰

    • 向量搜尋是透過 vectors.value 屬性來指定。 關鍵字搜尋是透過 search 屬性來指定。
    • 在混合式搜尋中,您可以將向量搜尋與關鍵字的全文檢索搜尋整合。 篩選、拼字檢查和語意排名僅適用於文字內容,而非向量。 在此最終查詢中,沒有語意 answer,因為系統不會產生足夠強的語意。
    • 實際結果包含更多詳細資料,包括語意標題和醒目提示。 結果已針對可讀性進行修改。 若要取得回應的完整結構,請在 REST 用戶端中執行要求。

清理

如果您是在自己的訂用帳戶中進行,建議您在專案結束時判斷自己是否仍需要先前所建立的資源。 資源若繼續執行,將需付費。 您可以個別刪除資源,或刪除資源群組以刪除整組資源。

您可以使用入口網站最左邊窗格中的 [所有資源] 或 [資源群組] 連結,來尋找和管理資源。

您也可以嘗試此 DELETE 命令:

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

下一步

做為下一個步驟,建議您檢閱 PythonC#JavaScript 的示範程式碼。