Azure Search Management Client Library
The `azure-mgmt-search` library provides a client for managing Azure AI Search (formerly Azure Cognitive Search) resources, including search services, private endpoint connections, and shared private link resources. It is part of the Azure SDK for Python, currently at version 9.2.0, and follows the Azure SDK's regular release cadence with updates typically released monthly or bi-monthly.
Warnings
- breaking The `api_version` parameter is no longer directly passed to the `SearchManagementClient` constructor in version 9.x. The SDK now internally manages the API version corresponding to the client library version. Attempting to pass `api_version` will result in a `TypeError` or unexpected behavior.
- gotcha List operations (e.g., `client.services.list_by_subscription()`) return a paged iterator (`ItemPaged`), not a Python list. You must iterate over the returned object to access all items. Directly indexing or assuming it's a complete list will lead to errors or only partial results.
- gotcha Using `DefaultAzureCredential` requires proper environment variable configuration (e.g., `AZURE_TENANT_ID`, `AZURE_CLIENT_ID`, `AZURE_CLIENT_SECRET` for service principal) or active login via Azure CLI (`az login`) or Visual Studio Code with the Azure Account extension. Failing to set this up correctly will result in authentication errors.
- breaking Between versions 8.x and 9.x, some model property names or structures within the management objects (e.g., for `SearchService`, `Sku`, `AdminKey`, `QueryKey`) have been updated or renamed to align with newer API versions or Azure naming conventions (e.g., Azure AI Search).
Install
-
pip install azure-mgmt-search azure-identity
Imports
- SearchManagementClient
from azure.mgmt.search import SearchManagementClient
- DefaultAzureCredential
from azure.identity import DefaultAzureCredential
Quickstart
import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.search import SearchManagementClient
# Ensure these environment variables are set for DefaultAzureCredential
# AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, AZURE_TENANT_ID for service principal
# Or AZURE_SUBSCRIPTION_ID and logged in via Azure CLI/VS Code
subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID", "<your-subscription-id>")
resource_group_name = os.environ.get("AZURE_RESOURCE_GROUP_NAME", "<your-resource-group-name>")
search_service_name = os.environ.get("AZURE_SEARCH_SERVICE_NAME", "<your-search-service-name>")
if subscription_id == "<your-subscription-id>":
print("Please set the AZURE_SUBSCRIPTION_ID environment variable.")
exit(1)
# Authenticate using DefaultAzureCredential
credential = DefaultAzureCredential()
# Create a SearchManagementClient
client = SearchManagementClient(credential, subscription_id)
try:
# Example 1: List all search services in the subscription
print("\nListing all search services in subscription:")
for service in client.services.list_by_subscription():
print(f" - {service.name} (Location: {service.location}, SKU: {service.sku.name})")
# Example 2: List search services in a specific resource group
if resource_group_name != "<your-resource-group-name>":
print(f"\nListing search services in resource group '{resource_group_name}':")
for service in client.services.list_by_resource_group(resource_group_name):
print(f" - {service.name} (Location: {service.location}, SKU: {service.sku.name})")
else:
print("\nTo list services by resource group, set AZURE_RESOURCE_GROUP_NAME.")
except Exception as e:
print(f"An error occurred: {e}")