from azure.search.documents.models import QueryType
results = client.search(
search_text="what is azure search",
query_type=QueryType.SEMANTIC,
semantic_configuration_name="my-semantic-config",
select=["id", "title", "content"],
top=10
)
for result in results:
print(f"{result['title']}")
if result.get("@search.captions"):
print(f" Caption: {result['@search.captions'][0].text}")
results = client.search(
search_text="*",
facets=["category,count:10", "rating"],
top=0 # Only get facets, no documents
)
for facet_name, facet_values in results.get_facets().items():
print(f"{facet_name}:")
for facet in facet_values:
print(f" {facet['value']}: {facet['count']}")
Write clean, idiomatic Python code for Azure AI Search using azure-search-documents.
Installation for Additional Patterns
pip install azure-search-documents azure-identity
Environment Variables for Additional Patterns
AZURE_SEARCH_ENDPOINT=https://<search-service>.search.windows.net
AZURE_SEARCH_INDEX_NAME=<index-name>
# For API key auth (not recommended for production)
AZURE_SEARCH_API_KEY=<api-key>
Authentication for Additional Patterns
DefaultAzureCredential (preferred):
from azure.identity import DefaultAzureCredential
from azure.search.documents import SearchClient
credential = DefaultAzureCredential()
client = SearchClient(endpoint, index_name, credential)
API Key:
from azure.core.credentials import AzureKeyCredential
from azure.search.documents import SearchClient
client = SearchClient(endpoint, index_name, AzureKeyCredential(api_key))
For LLM-powered Q&A with answer synthesis, see references/agentic-retrieval.md.
Key concepts:
Knowledge Source: Points to a search index
Knowledge Base: Wraps knowledge sources + LLM for query planning and synthesis
Output modes: EXTRACTIVE_DATA (raw chunks) or ANSWER_SYNTHESIS (LLM-generated answers)
Async Pattern
from azure.search.documents.aio import SearchClient
async with SearchClient(endpoint, index_name, credential) as client:
results = await client.search(search_text="query")
async for result in results:
print(result["title"])
Best Practices for Additional Patterns
Use environment variables for endpoints, keys, and deployment names
Prefer DefaultAzureCredential over API keys for production
Use SearchIndexingBufferedSender for batch uploads (handles batching/retries)
Always define semantic configuration for agentic retrieval indexes
Use create_or_update_index for idempotent index creation
Close clients with context managers or explicit close()
Field Types Reference
EDM Type
Python
Notes
Edm.String
str
Searchable text
Edm.Int32
int
Integer
Edm.Int64
int
Long integer
Edm.Double
float
Floating point
Edm.Boolean
bool
True/False
Edm.DateTimeOffset
datetime
ISO 8601
Collection(Edm.Single)
List[float]
Vector embeddings
Collection(Edm.String)
List[str]
String arrays
Error Handling
from azure.core.exceptions import (
HttpResponseError,
ResourceNotFoundError,
ResourceExistsError
)
try:
result = search_client.get_document(key="123")
except ResourceNotFoundError:
print("Document not found")
except HttpResponseError as e:
print(f"Search error: {e.message}")
When to Use
This skill is applicable to execute the workflow or actions described in the overview.
Limitations
Use this skill only when the task clearly matches the scope described above.
Do not treat the output as a substitute for environment-specific validation, testing, or expert review.
Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.