Azure AI Search Client Library for Python
The `azure-search-documents` library is the Microsoft Azure AI Search client library for Python. Azure AI Search (formerly known as "Azure Cognitive Search") is an AI-powered information retrieval platform that enables developers to build rich search experiences and generative AI applications that combine large language models with enterprise data. The current stable version is 11.6.0, and the library follows a regular release cadence as part of the Azure SDK for Python.
Warnings
- breaking Version 11 (`azure-search-documents`) is a complete redesign of the client library from previous versions (e.g., `Microsoft.Azure.Search` v10). It introduces new client classes (`SearchClient`, `SearchIndexClient`, `SearchIndexerClient`) and significant API and naming differences, consolidating functionality from four packages into one.
- breaking Azure AI Search API versions are updated regularly, and older preview versions (e.g., `2023-07-01-preview` for vector search) are deprecated and no longer supported. Migrating to newer API versions may require changes to vector search configurations and other features.
- gotcha Always use a query API key for client-side applications to restrict access and operations to read-only queries. Admin keys grant full read-write access to the search service and should be protected and only used for administrative tasks.
- gotcha Modifying or removing an existing field in an Azure AI Search index's schema is not allowed directly. To change an index schema (e.g., changing field types, adding/removing fields, making a field filterable), you must create an entirely new index with the desired schema, re-populate it with all documents, and then update your application to point to the new index.
- gotcha Filtering in Azure AI Search is case-sensitive. A filter for `Make eq 'toyota'` will not match a document where `Make` is 'Toyota' or 'TOYOTA'.
- gotcha In hybrid search, if you intend to apply a strict filter (e.g., using regular expressions in `search_text`), unexpected results might occur because vector search always returns a `k` number of matches, which are then combined. For hard filters where non-matching documents must be excluded, use the `filter` argument with OData syntax.
Install
-
pip install azure-search-documents
Imports
- SearchClient
from azure.search.documents import SearchClient
- SearchIndexClient
from azure.search.documents.indexes import SearchIndexClient
- SearchIndexerClient
from azure.search.documents.indexers import SearchIndexerClient
- AzureKeyCredential
from azure.core.credentials import AzureKeyCredential
- DefaultAzureCredential
from azure.identity import DefaultAzureCredential
Quickstart
import os
from azure.core.credentials import AzureKeyCredential
from azure.search.documents import SearchClient
# Set environment variables for your Azure AI Search service endpoint, API key, and index name
service_endpoint = os.environ.get("AZURE_SEARCH_SERVICE_ENDPOINT", "")
index_name = os.environ.get("AZURE_SEARCH_INDEX_NAME", "")
api_key = os.environ.get("AZURE_SEARCH_API_KEY", "")
if not service_endpoint or not index_name or not api_key:
raise ValueError("Please set AZURE_SEARCH_SERVICE_ENDPOINT, AZURE_SEARCH_INDEX_NAME, and AZURE_SEARCH_API_KEY environment variables.")
# Create a SearchClient
search_client = SearchClient(service_endpoint, index_name, AzureKeyCredential(api_key))
# Example: Search for documents
results = search_client.search(search_text="office")
print("Search Results:")
for result in results:
print(f"ID: {result['id']}, Name: {result['hotelName']}") # Adjust field names based on your index schema
# Example: Get a single document by key
document_key = "23"
try:
document = search_client.get_document(key=document_key)
print(f"\nDetails for document '{document_key}':")
print(f"Name: {document['hotelName']}") # Adjust field name based on your index schema
print(f"Rating: {document['rating']}") # Adjust field name based on your index schema
except Exception as e:
print(f"\nError retrieving document '{document_key}': {e}")