Share via


步驟 2 - 使用 .NET 建立及載入搜尋索引

請遵循下列步驟以繼續建置支援搜尋的網站:

  • 建立搜尋資源
  • 建立新的索引
  • 使用範例指令碼和 Azure SDK Azure.Search.Documents,透過 .NET 匯入資料。

建立 Azure AI 搜尋服務資源

使用 Azure CLI 或 Azure PowerShell,透過命令列建立新的搜尋資源。 您也需要擷取用於索引讀取權限的查詢金鑰,並取得用於新增物件的內建系統管理金鑰。

您必須在裝置上安裝 Azure CLIAzure PowerShell。 如果您不是裝置上的本機系統管理員,請選擇 [Azure PowerShell],並使用 Scope 參數以目前使用者身分執行。

注意

此工作不需要適用於 Azure CLI 和 Azure PowerShell 的 Visual Studio Code 擴充功能。 Visual Studio Code 會辨識沒有擴充功能的命令列工具。

  1. 在 Visual Studio Code 的 [終端機] 底下,選取 [新增終端]

  2. 連線至 Azure:

    az login
    
  3. 建立新的搜尋服務之前,請先列出您訂用帳戶的現有服務:

    az resource list --resource-type Microsoft.Search/searchServices --output table
    

    如果您有想要使用的服務,請記下名稱,然後跳到下一節。

  4. 建立新的搜尋服務。 使用下列命令做為範本,將資源群組、服務名稱、服務層級、區域、分割區和複本替換為有效值。 下列陳述式會使用上一個步驟建立的「cognitive-search-demo-rg」資源群組,並指定「免費」層。 如果您的 Azure 訂用帳戶已經有免費的搜尋服務,請改為指定計費層,例如「基本」。

    az search service create --name my-cog-search-demo-svc --resource-group cognitive-search-demo-rg --sku free --partition-count 1 --replica-count 1
    
  5. 取得授與搜尋服務讀取權限的查詢金鑰。 搜尋服務會佈建兩個系統管理金鑰以及一個查詢金鑰。 將資源群組與搜尋服務替換為有效名稱。 將查詢金鑰複製到記事本,以便在稍後的步驟中貼到用戶端程式碼中:

    az search query-key list --resource-group cognitive-search-demo-rg --service-name my-cog-search-demo-svc
    
  6. 取得搜尋服務管理員 API 金鑰。 管理 API 金鑰會提供搜尋服務的寫入權限。 將其中一個系統管理金鑰複製到記事本,以便在建立及載入索引的大量匯入步驟中使用:

    az search admin-key show --resource-group cognitive-search-demo-rg --service-name my-cog-search-demo-svc
    

此指令碼會針對 Azure AI 搜尋服務使用 Azure SDK:

  1. 在 Visual Studio Code 中,開啟子目錄 (search-website-functions-v4/bulk-insert) 中的 Program.cs 檔案,用您自己的值取代下列變數,以向 Azure 搜尋服務 SDK 進行驗證:

    • YOUR-SEARCH-RESOURCE-NAME
    • YOUR-SEARCH-ADMIN-KEY
    
    using Azure;
    using Azure.Search.Documents;
    using Azure.Search.Documents.Indexes;
    using Azure.Search.Documents.Indexes.Models;
    using AzureSearch.BulkInsert;
    using ServiceStack;
    
    const string BOOKS_URL = "https://raw.githubusercontent.com/Azure-Samples/azure-search-sample-data/main/good-books/books.csv";
    const string SEARCH_ENDPOINT = "https://YOUR-SEARCH-RESOURCE-NAME.search.windows.net";
    const string SEARCH_KEY = "YOUR-SEARCH-ADMIN-KEY";
    const string SEARCH_INDEX_NAME = "good-books";
    
    Uri searchEndpointUri = new(SEARCH_ENDPOINT);
    
    SearchClient client = new(
        searchEndpointUri,
        SEARCH_INDEX_NAME,
        new AzureKeyCredential(SEARCH_KEY));
    
    SearchIndexClient clientIndex = new(
        searchEndpointUri,
        new AzureKeyCredential(SEARCH_KEY));
    
    await CreateIndexAsync(clientIndex);
    await BulkInsertAsync(client);
    
    static async Task CreateIndexAsync(SearchIndexClient clientIndex)
    {
        Console.WriteLine("Creating (or updating) search index");
        SearchIndex index = new BookSearchIndex(SEARCH_INDEX_NAME);
        var result = await clientIndex.CreateOrUpdateIndexAsync(index);
    
        Console.WriteLine(result);
    }
    
    static async Task BulkInsertAsync(SearchClient client)
    {
        Console.WriteLine("Download data file");
        using HttpClient httpClient = new();
    
        var csv = await httpClient.GetStringAsync(BOOKS_URL);
    
        Console.WriteLine("Reading and parsing raw CSV data");
        var books =
            csv.ReplaceFirst("book_id", "id").FromCsv<List<BookModel>>();
    
        Console.WriteLine("Uploading bulk book data");
        _ = await client.UploadDocumentsAsync(books);
    
        Console.WriteLine("Finished bulk inserting book data");
    }
    
  2. 在 Visual Studio Code 中開啟專案目錄子目錄 (search-website-functions-v4/bulk-insert) 的整合式終端,然後執行下列命令以安裝相依項目。

    dotnet restore
    
  1. 針對專案目錄的子目錄 (search-website-functions-v4/bulk-insert),繼續使用 Visual Studio 中的整合式終端執行下列 Bash 命令以執行 Program.cs 指令碼:

    dotnet run
    
  2. 程式碼執行時,主控台會顯示進度。

  3. 上傳完成時,主控台最後列印的陳述式為「Finished bulk inserting book data (已完成大量插入書籍資料)」。

檢閱新的搜尋索引

上傳完成之後,即可使用搜尋索引。 在 Azure 入口網站中檢閱您的新索引。

  1. 在 Azure 入口網站中,尋找您在上一個步驟中建立的搜尋服務

  2. 在左側選取 [索引],然後選取好書索引。

    顯示索引之 Azure 入口網站的可展開螢幕快照。

  3. 根據預設,索引會在 [搜尋總管] 索引標籤中開啟。選取 [搜尋] 以從索引傳回文件。

    顯示搜尋結果 Azure 入口網站的可展開螢幕快照

復原大量匯入檔案變更

bulk-insert 目錄的 Visual Studio Code 整合式終端機中,使用下列 git 命令可復原變更。 本教學課程後續不會用到這些檔案,您不應將這些檔案秘密儲存或推送至您的存放庫。

git checkout .

複製您的 Azure 搜尋服務資源名稱

記下您的搜尋資源名稱。 您需要使用此名稱才能將 Azure Function 應用程式連線到您的搜尋資源。

警告

雖然您可能會想要在 Azure Function 中使用搜尋系統管理員密鑰,但這不符合最低權限原則。 Azure Function 會使用查詢金鑰以遵守最低權限。

下一步

部署您的靜態 Web 應用程式