2 - Create and load Search Index with .NET

Continue to build your Search-enabled website by:

  • Creating a Search resource with the VS Code extension
  • Creating a new index and importing data with .NET using the sample script and Azure SDK Azure.Search.Documents.

Create an Azure Search resource

Create a new Search resource with the Azure Cognitive Search extension for Visual Studio Code.

  1. In Visual Studio Code, open the Activity bar, and select the Azure icon.

  2. In the Side bar, right-click on your Azure subscription under the Azure: Cognitive Search area and select Create new search service.

    In the Side bar, right-click on your Azure subscription under the **Azure: Cognitive Search** area and select **Create new search service**.

  3. Follow the prompts to provide the following information:

    Prompt Enter
    Enter a globally unique name for the new Search Service. Remember this name. This resource name becomes part of your resource endpoint.
    Select a resource group for new resources Use the resource group you created for this tutorial.
    Select the SKU for your Search service. Select Free for this tutorial. You can't change a SKU pricing tier after the service is created.
    Select a location for new resources. Select a region close to you.
  4. After you complete the prompts, your new Search resource is created.

Get your Search resource admin key

Get your Search resource admin key with the Visual Studio Code extension.

  1. In Visual Studio Code, in the Side bar, right-click on your Search resource and select Copy Admin Key.

    In the Side bar, right-click on your Search resource and select **Copy Admin Key**.

  2. Keep this admin key, you will need to use it in a later section.

The script uses the Azure SDK for Cognitive Search:

  1. In Visual Studio Code, open the Program.cs file in the subdirectory, search-website/bulk-insert, replace the following variables with your own values to authenticate with the Azure Search SDK:

    • YOUR-SEARCH-RESOURCE-NAME
    • YOUR-SEARCH-ADMIN-KEY
    using System;
    using Azure;
    using Azure.Search.Documents;
    using Azure.Search.Documents.Indexes;
    using Azure.Search.Documents.Indexes.Models;
    using AzureSearch.BulkInsert;
    using System.Net.Http;
    using System.Threading.Tasks;
    using ServiceStack;
    using System.Collections.Generic;
    
    namespace ConsoleApp1
    {
        class Program
        {
            const string BOOKS_URL = "https://raw.githubusercontent.com/zygmuntz/goodbooks-10k/master/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";
    
            static void Main(string[] args)
            {
                Uri searchEndpointUri = new Uri(SEARCH_ENDPOINT);
    
                SearchClient client = new SearchClient(
                    searchEndpointUri,
                    SEARCH_INDEX_NAME,
                    new AzureKeyCredential(SEARCH_KEY));
    
                SearchIndexClient clientIndex = new SearchIndexClient(
                    searchEndpointUri,
                    new AzureKeyCredential(SEARCH_KEY));
    
                CreateIndexAsync(clientIndex).Wait();
                BulkInsertAsync(client).Wait();
            }
            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 HttpClient();
                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. Open an integrated terminal in Visual Studio Code for the project directory's subdirectory, search-website/bulk-insert, then run the following command to install the dependencies.

    dotnet restore
    
  1. Continue using the integrated terminal in Visual Studio for the project directory's subdirectory, search-website/bulk-insert, to run the following bash command to run the Program.cs script:

    dotnet run
    
  2. As the code runs, the console displays progress.

  3. When the upload is complete, the last statement printed to the console is "Finished bulk inserting book data".

Review the new Search Index

Once the upload completes, the Search Index is ready to use. Review your new Index.

  1. In Visual Studio Code, open the Azure Cognitive Search extension and select your Search resource.

    In Visual Studio Code, open the Azure Cognitive Search extension and open your Search resource.

  2. Expand Indexes, then Documents, then good-books, then select a doc to see all the document-specific data.

    Expand Indexes, then `good-books`, then select a doc.

Copy your Search resource name

Note your Search resource name. You will need this to connect the Azure Function app to your Search resource.

Caution

While you may be tempted to use your Search admin key in the Azure Function, that isn't following the principle of least privilege. The Azure Function will use the query key to conform to least privilege.

Rollback bulk import file changes

Use the following git command in the VS Code integrated terminal at the bulk-insert directory, to rollback the changes. They are not needed to continue the tutorial and you shouldn't save or push these secrets to your repo.

git checkout .

Next steps

Deploy your Static Web App