ai-agents-for-beginners

Azure AI Search Setup Guide

This guide will help you set up Azure AI Search using the Azure portal. Follow the steps below to create and configure your Azure AI Search service.

Prerequisites

Before you begin, ensure you have the following:

Step 1: Create an Azure Storage Account

  1. Follow this instruction, Create an Azure storage account, to create a new Azure Storage Account. NOTE: Make sure that the type of Storage Account is Standard General Purpose V2.

Step 2: Create an Azure AI Search Service

  1. Sign in to the Azure portal.
  2. In the left-hand navigation pane, click on Create a resource.
  3. In the search box, type “Azure AI Search” and select Azure AI Search from the list of results.
  4. Click the Create button.
  5. In the Basics tab, provide the following information:
    • Subscription: Select your Azure subscription.
    • Resource group: Create a new resource group or select an existing one.
    • Resource name: Enter a unique name for your search service.
    • Region: Select the region closest to your users.
    • Pricing tier: Choose a pricing tier that suits your requirements. You can start with the Free tier for testing.
  6. Click Review + create.
  7. Review the settings and click Create to create the search service.
  1. Once the deployment is complete, navigate to your search service in the Azure portal.
  2. In the search service overview pane, copy the URL. It should look like https://<service-name>.search.windows.net.
  3. (Recommended) Enable keyless access with Microsoft Entra ID (RBAC) as shown in Step 4 below — no key needed. The samples in this guide create/update indexes and upload documents, which require the Search Service Contributor and Search Index Data Contributor roles (or, for key-based auth, the primary admin key — not the query key). Only if you cannot use RBAC, open the Settings > Keys pane and copy the primary admin key.
  4. Follow the steps in the Quickstart guide page to create an index, upload data, and perform a search.

Step 4: Use Azure AI Search Tools

Azure AI Search integrates with various tools to enhance your search capabilities. You can use Azure CLI, Python SDK, .NET SDK and other tools for advanced configurations and operations.

Using Azure CLI

  1. Install the Azure CLI by following the instructions at Install Azure CLI.
  2. Sign in to Azure CLI using the command:

    az login
    
  3. (Recommended) Enable keyless access with Microsoft Entra ID (RBAC):

     az search service update --name <service-name> --resource-group <resource-group> --auth-options aadOrApiKey
     az role assignment create --assignee <your-user-or-principal-id> --role "Search Service Contributor" --scope $(az search service show -g <resource-group> -n <service-name> --query id -o tsv)
     az role assignment create --assignee <your-user-or-principal-id> --role "Search Index Data Contributor" --scope $(az search service show -g <resource-group> -n <service-name> --query id -o tsv)
     # az search service show has no "endpoint" field; build the URL from the service name.
     export AZURE_SEARCH_SERVICE_ENDPOINT="https://<service-name>.search.windows.net"
    

    With RBAC enabled, the Python and .NET SDK samples below authenticate with DefaultAzureCredential, which uses your az login session during local development — no admin key needed. See Connect to Azure AI Search using roles.

  4. (Fallback) Key-based auth — only if you cannot use RBAC, store the admin key as well:

Store both endpoint and API key for Azure AI Search instance to environment variables.

```bash
# zsh/bash
# az search service show has no "endpoint" field; build the URL from the service name.
export AZURE_SEARCH_SERVICE_ENDPOINT="https://<service-name>.search.windows.net"
export AZURE_SEARCH_API_KEY=$(az search admin-key show -g <resource-group> --service-name <service-name> --query "primaryKey" -o tsv)
```

```powershell
# PowerShell
# az search service show has no "endpoint" field; build the URL from the service name.
$env:AZURE_SEARCH_SERVICE_ENDPOINT = "https://<service-name>.search.windows.net"
$env:AZURE_SEARCH_API_KEY = $(az search admin-key show -g <resource-group> --service-name <service-name> --query "primaryKey" -o tsv)
```

Using Python SDK

  1. Install the Azure Cognitive Search client library and Azure Identity for Python:

    pip install azure-search-documents azure-identity
    
  2. Use the following Python code to create an index and upload documents:

     import os
     from azure.identity import DefaultAzureCredential
     from azure.search.documents import SearchClient
     from azure.search.documents.indexes import SearchIndexClient
     from azure.search.documents.indexes.models import SearchIndex, SimpleField, edm
    
     service_endpoint = os.getenv("AZURE_SEARCH_SERVICE_ENDPOINT")
     index_name = "sample-index"
    
     # Keyless (recommended): uses your `az login` identity via Entra ID RBAC.
     # Requires the "Search Service Contributor" and "Search Index Data Contributor" roles.
     credential = DefaultAzureCredential()
     # Fallback (key-based auth):
     # from azure.core.credentials import AzureKeyCredential
     # credential = AzureKeyCredential(os.getenv("AZURE_SEARCH_API_KEY"))
     index_client = SearchIndexClient(service_endpoint, credential)
    
     fields = [
         SimpleField(name="id", type=edm.String, key=True),
         SimpleField(name="content", type=edm.String, searchable=True),
     ]
    
     index = SearchIndex(name=index_name, fields=fields)
    
     index_client.create_index(index)
    
     search_client = SearchClient(service_endpoint, index_name, credential)
    
     documents = [
         {"id": "1", "content": "Hello world"},
         {"id": "2", "content": "Azure Cognitive Search"}
     ]
    
     search_client.upload_documents(documents)
    

Using .NET SDK

  1. Run the following command to create an index and upload documents:

     dotnet run ./AzureSearch.cs
    

    The .NET sample below uses DefaultAzureCredential, which can use your Azure CLI sign-in from az login during local development.

  2. Here’s the .NET code of AzureSearch.cs:

     #:package Azure.Search.Documents@11.*
     #:package Azure.Identity@1.21.0
     #:property PublishAot=false
    
     using Azure;
     using Azure.Identity;
     using Azure.Search.Documents;
     using Azure.Search.Documents.Indexes;
     using Azure.Search.Documents.Indexes.Models;
    
     var serviceEndpoint = new Uri(Environment.GetEnvironmentVariable("AZURE_SEARCH_SERVICE_ENDPOINT")!);
     var indexName = "sample-index";
    
     // Keyless (recommended): uses your `az login` identity via Entra ID RBAC.
     // Requires the "Search Service Contributor" and "Search Index Data Contributor" roles.
     var credential = new DefaultAzureCredential();
     // Fallback (key-based auth): the `using Azure;` directive above already imports
     // AzureKeyCredential; replace the credential line above with:
     // var credential = new AzureKeyCredential(Environment.GetEnvironmentVariable("AZURE_SEARCH_API_KEY")!);
     var indexClient = new SearchIndexClient(serviceEndpoint, credential);
    
     var fields = new List<SearchField>()
     {
         new SimpleField("id", SearchFieldDataType.String) { IsKey = true },
         new SearchableField("content")
     };
    
     var index = new SearchIndex(name: indexName, fields: fields);
    
     var response = await indexClient.CreateOrUpdateIndexAsync(index);
     Console.WriteLine($"Index '{response.Value.Name}' ready.");
    
     var searchClient = new SearchClient(serviceEndpoint, indexName, credential);
    
     var documents = new[]
     {
         new { id = "1", content = "Hello world" },
         new { id = "2", content = "Azure Cognitive Search" }
     };
    
     var result = await searchClient.UploadDocumentsAsync(documents);
     Console.WriteLine($"Uploaded {result.Value.Results.Count} documents to index '{response.Value.Name}'.");
    

For more detailed information, refer to the following documentation:

Conclusion

You have successfully set up Azure AI Search using the Azure portal and integrated tools. You can now explore more advanced features and capabilities of Azure AI Search to enhance your search solutions.

For further assistance, visit the Azure Cognitive Search documentation.