6.9 KiB
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:
- An Azure subscription. If you don't have an Azure subscription, you can create a free account at Azure Free Account.
Step 1: Create an Azure Storage Account
- 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
- Sign in to the Azure portal.
- In the left-hand navigation pane, click on Create a resource.
- In the search box, type "Azure AI Search" and select Azure AI Search from the list of results.
- Click the Create button.
- 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.
- Click Review + create.
- Review the settings and click Create to create the search service.
Step 3: Get Started with Azure AI Search
- Once the deployment is complete, navigate to your search service in the Azure portal.
- In the search service overview pane, copy the URL. It should look like
https://<service-name>.search.windows.net. - In the Settings > Keys pane, copy the query key.
- Follow the steps in the Quickstart guide page to create an index, upload data, and perform a search query.
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
-
Install the Azure CLI by following the instructions at Install Azure CLI.
-
Sign in to Azure CLI using the command:
az login -
Store both endpoint and API key for Azure AI Search instance to environment variables.
# zsh/bash export AZURE_SEARCH_SERVICE_ENDPOINT=$(az search service show -g <resource-group> -n <service-name> --query "endpoint" -o tsv) export AZURE_SEARCH_API_KEY=$(az search service admin-key list -g <resource-group> --search-service-name <service-name> --query "primaryKey" -o tsv)# PowerShell $env:AZURE_SEARCH_SERVICE_ENDPOINT = az search service show -g <resource-group> -n <service-name> --query "endpoint" -o tsv $env:AZURE_SEARCH_API_KEY = $(az search service admin-key list -g <resource-group> --search-service-name <service-name> --query "primaryKey" -o tsv)
Using Python SDK
-
Install the Azure Cognitive Search client library for Python:
pip install azure-search-documents -
Use the following Python code to create an index and upload documents:
import os from azure.core.credentials import AzureKeyCredential 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") api_key = os.getenv("AZURE_SEARCH_API_KEY") index_name = "sample-index" credential = AzureKeyCredential(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
-
Run the following command to create an index and upload documents:
dotnet run ./AzureSearch.cs -
Here's the .NET code of
AzureSearch.cs:#:package Azure.Search.Documents@11.* #:property PublishAot=false using Azure; 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 apiKey = Environment.GetEnvironmentVariable("AZURE_SEARCH_API_KEY")!; var indexName = "sample-index"; var credential = new AzureKeyCredential(apiKey); 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:
- Create an Azure Cognitive Search service
- Get started with Azure Cognitive Search
- Azure AI Search Tools
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.