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

步骤 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 SDK 进行 Azure AI 搜索:

  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. 继续使用 Visual Studio 中的集成终端作为项目目录的子目录 search-website-functions-v4/bulk-insert ,运行以下 bash 命令来运行 Program.cs 脚本:

    dotnet run
    
  2. 当代码运行时,控制台将显示进度。

  3. 上传完成后,打印到控制台的最后一个语句将为“已完成批量插入书籍数据”。

查看新的搜索索引

上传完成后,便可以使用搜索索引。 在 Azure 门户中查看新索引。

  1. 在 Azure 门户中,找到在上一步中创建的搜索服务

  2. 在左侧,选择“索引”,然后选择好书索引。

    可展开的 Azure 门户屏幕截图,其中显示了索引。

  3. 默认情况下,索引将在“搜索资源管理器”选项卡中打开。选择“搜索”以从索引返回文档。

    可展开的 Azure 门户屏幕截图,其中显示了搜索结果

回滚批量导入文件更改

bulk-insert 目录中的 Visual Studio Code 集成终端使用以下 git 命令来回滚更改。 它们不是继续学习教程所必需的,不应将这些机密保存或推送到你的存储库。

git checkout .

复制搜索资源名称

记下搜索资源名称。 将 Azure Function 应用连接到搜索资源时需要用到它。

注意

尽管你可能想要在 Azure 函数中使用搜索管理密钥,但这并不符合最低特权原则。 Azure Function 将使用查询密钥以符合最小特权。

后续步骤

部署静态 Web 应用