Azure 認知搜尋 (已淘汰) 的 Visual Studio Code擴充功能

先前處於預覽狀態的 Azure 認知搜尋 Visual Studio Code 延伸模組並未繼續進行正式運作,現在已于 2022 年 11 月 1 日淘汰。

雖然擴充功能已無法在 Azure Marketplace中使用,但程式碼會在 開啟原始碼 https://github.com/microsoft/vscode-azurecognitivesearch 。 您可以複製和修改工具以供自己使用。

如果您使用擴充功能,本文說明如何使用Azure 認知搜尋 REST API以互動方式制定 REST API 要求。

必要條件

使用擴充功能需要下列服務和工具

安裝延伸模組

請參閱 Github 上的讀我檔案

連線至您的訂用帳戶

啟動 Visual Studio COde。

選取 [登入 Azure...],並登入您的 Azure 帳戶。

您應該會看到您的訂用帳戶。 在下列螢幕擷取畫面中,訂用帳戶名稱為 「Visual Studio Enterprise」,其中包含一個名為 「azsearch-service」 的搜尋服務。

VS Code 中的 VS Code Azure 訂帳戶

若要限制顯示的訂用帳戶,請開啟命令選擇區 (Ctrl+Shift+P 或 Cmd+Shift+P),然後搜尋 Azure選取訂用帳戶。 您也可以使用命令來登入和登出您的 Azure 帳戶。

當您展開搜尋服務時,您會看到每個認知搜尋專案的樹狀結構專案:索引、資料來源、索引子、技能集、同義字對應和別名。

VS Code Azure 搜尋樹

您可以展開這些樹狀結構專案,以顯示您在搜尋服務中擁有的任何資源。

1 - 建立索引

若要建立索引,請使用 建立索引 REST API

透過 VS Code 擴充功能,您只需處理要求本文即可。 在本快速入門中,我們將提供範例索引定義和對應的文件。

索引定義

以下索引定義是虛構飯店的範例結構描述。

fields 集合會定義搜尋索引中的文件結構。 每個欄位都有一個資料類型和一些額外的屬性,以決定欄位的使用方式。

{
    "name": "hotels-quickstart",
    "fields": [
        {
            "name": "HotelId",
            "type": "Edm.String",
            "key": true,
            "filterable": true
        },
        {
            "name": "HotelName",
            "type": "Edm.String",
            "searchable": true,
            "filterable": false,
            "sortable": true,
            "facetable": false
        },
        {
            "name": "Description",
            "type": "Edm.String",
            "searchable": true,
            "filterable": false,
            "sortable": false,
            "facetable": false,
            "analyzer": "en.lucene"
        },
        {
            "name": "Description_fr",
            "type": "Edm.String",
            "searchable": true,
            "filterable": false,
            "sortable": false,
            "facetable": false,
            "analyzer": "fr.lucene"
        },
        {
            "name": "Category",
            "type": "Edm.String",
            "searchable": true,
            "filterable": true,
            "sortable": true,
            "facetable": true
        },
        {
            "name": "Tags",
            "type": "Collection(Edm.String)",
            "searchable": true,
            "filterable": true,
            "sortable": false,
            "facetable": true
        },
        {
            "name": "ParkingIncluded",
            "type": "Edm.Boolean",
            "filterable": true,
            "sortable": true,
            "facetable": true
        },
        {
            "name": "LastRenovationDate",
            "type": "Edm.DateTimeOffset",
            "filterable": true,
            "sortable": true,
            "facetable": true
        },
        {
            "name": "Rating",
            "type": "Edm.Double",
            "filterable": true,
            "sortable": true,
            "facetable": true
        },
        {
            "name": "Address",
            "type": "Edm.ComplexType",
            "fields": [
                {
                    "name": "StreetAddress",
                    "type": "Edm.String",
                    "filterable": false,
                    "sortable": false,
                    "facetable": false,
                    "searchable": true
                },
                {
                    "name": "City",
                    "type": "Edm.String",
                    "searchable": true,
                    "filterable": true,
                    "sortable": true,
                    "facetable": true
                },
                {
                    "name": "StateProvince",
                    "type": "Edm.String",
                    "searchable": true,
                    "filterable": true,
                    "sortable": true,
                    "facetable": true
                },
                {
                    "name": "PostalCode",
                    "type": "Edm.String",
                    "searchable": true,
                    "filterable": true,
                    "sortable": true,
                    "facetable": true
                },
                {
                    "name": "Country",
                    "type": "Edm.String",
                    "searchable": true,
                    "filterable": true,
                    "sortable": true,
                    "facetable": true
                }
            ]
        }
    ],
    "suggesters": [
        {
            "name": "sg",
            "searchMode": "analyzingInfixMatching",
            "sourceFields": [
                "HotelName"
            ]
        }
    ]
}

若要建立新的索引,請以滑鼠右鍵按一下 [索引],然後選取 [建立新的索引]。 此時會顯示名稱類似於 indexes-new-28c972f661.azsindex 的編輯器。

將上述索引定義貼到視窗中。 儲存檔案,並在系統提示您是否要更新索引時,選取 [上傳]。 此步驟會建立索引,並將它新增至左側的樹狀檢視。

建立索引的 Gif

如果您的索引定義發生問題,您應該會看到類似下面的錯誤訊息。

建立索引錯誤訊息

如果發生錯誤,請修正問題並重新儲存檔案。

2 - 載入文件

在 REST API 中,建立索引並填入索引是個別的步驟。 在 Azure 認知搜尋中,索引會包含所有可搜尋的資料。 在本快速入門中,資料會以 JSON 檔的形式提供。 這項工作會使用新增、更新或刪除文件 REST API \(英文\)。

若要將新檔新增至索引:

  1. 展開您所建立的 hotels-quickstart 索引。 以滑鼠右鍵按一下 [文件],然後選取 [建立新的文件]。

    建立文件

  2. 您應該會看到已推斷索引架構的 JSON 編輯器。

    建立文件 json

  3. 貼上下列 JSON,然後儲存檔案。 提示會要求您確認您的變更。 請選取 [上傳] 以儲存變更。

    {
        "HotelId": "1",
        "HotelName": "Secret Point Motel",
        "Description": "The hotel is ideally located on the main commercial artery of the city in the heart of New York. A few minutes away is Time's Square and the historic centre of the city, as well as other places of interest that make New York one of America's most attractive and cosmopolitan cities.",
        "Category": "Boutique",
        "Tags": [ "pool", "air conditioning", "concierge" ],
        "ParkingIncluded": false,
        "LastRenovationDate": "1970-01-18T00:00:00Z",
        "Rating": 3.60,
        "Address": {
            "StreetAddress": "677 5th Ave",
            "City": "New York",
            "StateProvince": "NY",
            "PostalCode": "10022",
            "Country": "USA"
        } 
    }
    
  4. 針對其餘三份檔重複此程式:

    文件 2:

    {
        "HotelId": "2",
        "HotelName": "Twin Dome Motel",
        "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", "concierge" ],
        "ParkingIncluded": false,
        "LastRenovationDate": "1979-02-18T00:00:00Z",
        "Rating": 3.60,
        "Address": {
            "StreetAddress": "140 University Town Center Dr",
            "City": "Sarasota",
            "StateProvince": "FL",
            "PostalCode": "34243",
            "Country": "USA"
        } 
    }
    

    文件 3:

    {
        "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",
        "Tags": [ "air conditioning", "bar", "continental breakfast" ],
        "ParkingIncluded": true,
        "LastRenovationDate": "2015-09-20T00:00:00Z",
        "Rating": 4.80,
        "Address": {
            "StreetAddress": "3393 Peachtree Rd",
            "City": "Atlanta",
            "StateProvince": "GA",
            "PostalCode": "30326",
            "Country": "USA"
        } 
    }
    

    文件 4:

    {
        "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",
        "Tags": [ "concierge", "view", "24-hour front desk service" ],
        "ParkingIncluded": true,
        "LastRenovationDate": "1960-02-06T00:00:00Z",
        "Rating": 4.60,
        "Address": {
            "StreetAddress": "7400 San Pedro Ave",
            "City": "San Antonio",
            "StateProvince": "TX",
            "PostalCode": "78216",
            "Country": "USA"
        }
    }
    

此時,您應該會在 [文件] 區段中看到四份文件全數顯示。

上傳所有文件後的狀態

3 - 搜尋索引

現在索引包含內容,您可以使用 搜尋檔 REST API發出查詢:

  1. 以滑鼠右鍵按一下您要搜尋的索引,然後選取 [ 搜尋]。 此步驟會開啟名稱類似于 sandbox-b946dcda48.azs 的編輯器。

    擴充功能的搜尋檢視

  2. 系統會自動填入簡單的查詢。 請按 Ctrl+Alt+RCmd+Alt+R 以提交查詢。 您會在左側的視窗中看到顯示的結果。

    擴充功能中的搜尋結果

查詢範例

嘗試其他幾個查詢範例來了解語法的風格。 此外還有以下四個查詢可供您試用。 您可以在相同的編輯器中新增多個查詢。 當您按 Ctrl+Alt+RCmd+Alt+R 時,游標所在的行會決定要提交的查詢。

並排的查詢和結果

在第一個查詢中,我們僅搜尋 boutiqueselect 特定欄位。 最佳做法是只針對所需的欄位執行 select,因為提取不必要的資料可能會讓查詢延遲。 此查詢也會設定 $count=true,以傳回搜尋結果的結果總數。

// Query example 1 - Search `boutique` with select and return count
search=boutique&$count=true&$select=HotelId,HotelName,Rating,Category

在下一個查詢中,我們會指定搜尋詞彙 wifi,同時包含一個篩選準則,只會傳回狀態等於 'FL' 的結果。 結果也會依飯店的 Rating 排序。

// Query example 2 - Search with filter, orderBy, select, and count
search=wifi&$filter=Address/StateProvince eq 'FL'&$select=HotelId,HotelName,Rating,Address/StateProvince&$orderby=Rating desc

接下來,使用 searchFields 參數,將搜尋限制為單一可搜尋的欄位。 如果您只想要知道在特定欄位中的相符項目,這是很好的選項,會讓您的查詢更有效率。

// Query example 3 - Limit searchFields
search=sublime cliff&$select=HotelId,HotelName,Rating&searchFields=HotelName

另一個要包含在查詢中的常見選項是 facets。 Facet 可讓您在應用程式上建置篩選,讓使用者輕鬆瞭解他們可以篩選的值。

// Query example 4 - Take the top two results, and show only HotelName and Category in the results
search=*&$select=HotelId,HotelName,Rating&searchFields=HotelName&facet=Category

在入口網站中開啟索引

如果您想要在入口網站中檢視搜尋服務,請以滑鼠右鍵按一下搜尋服務的名稱,然後選取 [在入口網站中開啟]。

清除資源

使用您自己的訂用帳戶時,在專案結束後確認您是否還需要您建立的資源,是很好的做法。 資源若繼續執行,您將必須付費。 您可以個別刪除資源,或刪除資源群組以刪除整組資源。

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

如果您使用免費服務,請記住您會有三個索引、索引子和資料來源的限制。 您可以在入口網站中刪除個別項目,以避免超出限制。

後續步驟

既然您已瞭解如何執行核心工作,您可以繼續進行其他 REST API 呼叫,以取得更進階的功能,例如索引子或設定 擴充管線 ,以將內容轉換新增至索引。 對於您的下一個步驟,建議您參閱下列連結: