ai-agents-for-beginners

Azure AI Search Setup Guide

Dis guide go help you set up Azure AI Search using the Azure portal. Follow di steps wey dey below to create and configure your Azure AI Search service.

Prerequisites

Before you start, make sure say you get dis one:

Step 1: Create an Azure Storage Account

  1. Follow dis instruction, Create an Azure storage account, to create new Azure Storage Account. NOTE: Make sure say di type of Storage Account na Standard General Purpose V2.

Step 2: Create an Azure AI Search Service

  1. Sign in to the Azure portal.
  2. For di left-hand navigation pane, click Create a resource.
  3. For di search box, type “Azure AI Search” and select Azure AI Search from di list of results.
  4. Click di Create button.
  5. For di Basics tab, put di following information:
    • Subscription: Select your Azure subscription.
    • Resource group: Create new resource group or select one wey dey.
    • Resource name: Enter unique name for your search service.
    • Region: Select di region wey near your users.
    • Pricing tier: Choose pricing tier wey suit your needs. You fit start with Free tier for testing.
  6. Click Review + create.
  7. Review di settings then click Create to create di search service.
  1. When deployment finish, waka go your search service for Azure portal.
  2. For di search service overview pane, copy di URL. E go be like https://<service-name>.search.windows.net.
  3. (Recommended) Enable keyless access with Microsoft Entra ID (RBAC) like e show for Step 4 below—no key dey needed. Di samples for dis guide go create/update indexes and upload documents, wey need Search Service Contributor and Search Index Data Contributor roles (or, if na key-based auth, di primary admin key — no be di query key). If you no fit use RBAC, open Settings > Keys pane and copy primary admin key.
  4. Follow di steps for the Quickstart guide page to create index, upload data, and do search.

Step 4: Use Azure AI Search Tools

Azure AI Search dey work with different tools to make your search beta. You fit use Azure CLI, Python SDK, .NET SDK and other tools for advanced setup and actions.

Using Azure CLI

  1. Install Azure CLI by following instructions for Install Azure CLI.
  2. Sign in to Azure CLI with this 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"
    

    When you enable RBAC, Python and .NET SDK samples for below go authenticate with DefaultAzureCredential, wey dey use your az login session during local development—no admin key required. See Connect to Azure AI Search using roles.

  4. (Fallback) Key-based auth — na only if you no fit use RBAC, store admin key also:

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

Using Python SDK

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

    pip install azure-search-documents azure-identity
    
  2. Use dis Python code to create 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 dis command to create index and upload documents:

     dotnet run ./AzureSearch.cs
    

    .NET sample for below dey use DefaultAzureCredential, wey fit use your Azure CLI sign-in from az login during local development.

  2. Here na di .NET code for 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, check di following documentation:

Conclusion

You don successfully set up Azure AI Search using Azure portal and other integrated tools. Now you fit explore more advanced features and things wey Azure AI Search get to make your search better.

If you need more help, visit Azure Cognitive Search documentation.


Disclaimer: Dis document don translate wit AI translation service Co-op Translator. Even tho we dey try make am correct, abeg make you know say automated translation fit get errors or mistakes. Di original document for dia own language na im be di correct source. For important info, make person wey sabi human translation do am. We no go responsible for any misunderstanding or wrong understanding wey fit happen because of dis translation.