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.
pip install azure-search-documentsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to instantiate a `SearchClient` using an API key and perform a basic search query and retrieve a document. Ensure `AZURE_SEARCH_SERVICE_ENDPOINT`, `AZURE_SEARCH_INDEX_NAME`, and `AZURE_SEARCH_API_KEY` environment variables are set. Replace 'hotelName', 'id', and 'rating' with actual field names from your index schema.
Rewrite code to use the new `azure-search-documents` client classes and API surface. Refer to migration guides for detailed changes.
Regularly check the Azure AI Search documentation for the latest recommended API versions and update your client library and code accordingly, especially for new features like vector search.
When creating an `AzureKeyCredential`, ensure you are using a query key retrieved from the Azure portal for client applications. Admin keys should only be used in secure backend services for index management and document ingestion.
Plan schema changes carefully. Use index aliases to manage seamless transitions between old and new indexes in production environments.
If case-insensitive filtering is required, consider storing a normalized (e.g., lowercase) version of the field in your index specifically for filtering, alongside the original field for display. Alternatively, use fuzzy search or other query types if exact filtering isn't strictly necessary.
Differentiate between `search_text` for relevance-based matching and the `filter` argument for strict, Boolean-logic filtering. Use `filter` for hard constraints.
Ensure the necessary environment variables (`AZURE_SEARCH_SERVICE_ENDPOINT`, `AZURE_SEARCH_API_KEY`, `AZURE_SEARCH_INDEX_NAME`) are set in your execution environment, or pass these values directly as parameters when creating Azure AI Search client objects (e.g., `SearchClient`, `SearchIndexClient`, `SearchIndexerClient`).
Ensure `AZURE_SEARCH_SERVICE_ENDPOINT`, `AZURE_SEARCH_INDEX_NAME`, and `AZURE_SEARCH_API_KEY` environment variables are correctly set, or pass these values directly when initializing client objects (e.g., `SearchClient`, `SearchIndexClient`).
Ensure you are using an instance of `SearchClient` for document operations. You can obtain a `SearchClient` from a `SearchIndexClient` using `get_search_client()` or instantiate it directly.
```python
from azure.search.documents import SearchClient
from azure.core.credentials import AzureKeyCredential
service_endpoint = "YOUR_SEARCH_SERVICE_ENDPOINT"
index_name = "YOUR_INDEX_NAME"
key = "YOUR_ADMIN_API_KEY"
# Instantiate SearchClient directly for document operations
search_client = SearchClient(service_endpoint, index_name, AzureKeyCredential(key))
documents = [
{"id": "1", "text": "This is a sample document."}
]
result = search_client.upload_documents(documents=documents)
# or for more granular control, use index_documents
# from azure.search.documents.models import IndexAction, IndexActionType
# actions = [IndexAction(document=doc, action_type=IndexActionType.UPLOAD) for doc in documents]
# result = search_client.index_documents(actions=actions)
```Install the specific client library directly using pip. If you see this error, it means the required `azure-search-documents` (or its dependencies like `azure-core`) is not correctly installed or recognized in your environment. ```bash pip install azure-search-documents # If you also need core components or other Azure SDKs, install them explicitly: pip install azure-core azure-identity ```
Verify that your API key is correct and has the necessary permissions (admin key for index management/document write, query key for document read). If using Azure AD, ensure the Managed Identity or Service Principal has been assigned appropriate roles (e.g., 'Search Service Contributor' for index creation, 'Search Index Data Contributor' for document write, 'Search Index Data Reader' for document read) on the Azure AI Search service. ```python from azure.core.credentials import AzureKeyCredential from azure.search.documents.indexes import SearchIndexClient service_endpoint = "https://<your-search-service-name>.search.windows.net" admin_key = "YOUR_ADMIN_API_KEY_FROM_PORTAL" # Ensure this is an admin key for index creation credential = AzureKeyCredential(admin_key) index_client = SearchIndexClient(endpoint=service_endpoint, credential=credential) # Example: Attempting to create an index requires appropriate permissions # index_client.create_index(SearchIndex(...)) ```
Replace `SearchFieldDataType.Single` with `SearchFieldDataType.Double` when defining fields in your search index schema. ```python from azure.search.documents.indexes.models import SearchField, SearchFieldDataType # Incorrect # SearchField(name="price", type=SearchFieldDataType.Single) # Correct SearchField(name="price", type=SearchFieldDataType.Double, searchable=True, filterable=True) ```
Specify the `content_key` parameter in the `AzureCognitiveSearchRetriever` constructor to match the actual name of the field in your Azure AI Search index that holds the main document content.
```python
import os
from langchain_community.retrievers import AzureCognitiveSearchRetriever
# Assume these are set as environment variables or directly defined
cognitive_search_name = os.environ.get("AZURE_COGNITIVE_SEARCH_SERVICE_NAME")
index_name = os.environ.get("AZURE_COGNITIVE_SEARCH_INDEX_NAME")
vector_store_address = f"https://{cognitive_search_name}.search.windows.net/"
vector_store_password = os.environ.get("AZURE_COGNITIVE_SEARCH_API_KEY")
# If your index field for content is 'text' instead of 'content'
retriever = AzureCognitiveSearchRetriever(
service_name=cognitive_search_name,
index_name=index_name,
api_key=vector_store_password,
content_key="text" # Change 'text' to whatever your actual content field name is
)
# Example usage:
# documents = retriever.invoke("your query")
```