Microsoft Azure AI Search Client Library for Python providing comprehensive search, indexing, and AI-powered document processing capabilities.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
The SearchIndexClient provides comprehensive management of search indexes, including schema definition, field configuration, analyzers, scoring profiles, vector search settings, and synonym maps. It handles the complete lifecycle of search indexes from creation to deletion.
Create a SearchIndexClient to manage indexes across a search service.
class SearchIndexClient:
def __init__(
self,
endpoint: str,
credential: Union[AzureKeyCredential, TokenCredential],
**kwargs
) -> None:
"""
Initialize SearchIndexClient for index management.
Parameters:
- endpoint (str): The URL endpoint of an Azure search service
- credential: A credential to authorize requests
- api_version (str, optional): The Search API version to use
- audience (str, optional): AAD audience for authentication
"""
def close(self) -> None:
"""Close the session."""
def __enter__(self) -> "SearchIndexClient": ...
def __exit__(self, *args) -> None: ...Get SearchClient instances for specific indexes.
def get_search_client(self, index_name: str, **kwargs) -> SearchClient:
"""
Get a SearchClient for a specific index.
Parameters:
- index_name (str): Name of the index
Returns:
SearchClient: Client for the specified index
"""List and retrieve search indexes with optional field selection.
def list_indexes(
self,
*,
select: Optional[List[str]] = None,
**kwargs
) -> ItemPaged[SearchIndex]:
"""
List all indexes in the search service.
Parameters:
- select (List[str], optional): Fields to include in results
Returns:
ItemPaged[SearchIndex]: Paginated list of indexes
"""
def list_index_names(self, **kwargs) -> ItemPaged[str]:
"""
List all index names in the search service.
Returns:
ItemPaged[str]: Paginated list of index names
"""
def get_index(self, name: str, **kwargs) -> SearchIndex:
"""
Retrieve a specific index definition.
Parameters:
- name (str): Name of the index to retrieve
Returns:
SearchIndex: The index definition
"""Get statistics and usage information for indexes.
def get_index_statistics(self, index_name: str, **kwargs) -> Dict[str, Any]:
"""
Get statistics for a specific index.
Parameters:
- index_name (str): Name of the index
Returns:
Dict[str, Any]: Index statistics including document count and storage size
"""Create new indexes and update existing ones with full schema support.
def create_index(self, index: SearchIndex, **kwargs) -> SearchIndex:
"""
Create a new search index.
Parameters:
- index (SearchIndex): The index definition to create
Returns:
SearchIndex: The created index definition
"""
def create_or_update_index(
self,
index: SearchIndex,
*,
allow_index_downtime: Optional[bool] = None,
if_match: Optional[str] = None,
if_none_match: Optional[str] = None,
**kwargs
) -> SearchIndex:
"""
Create a new index or update an existing one.
Parameters:
- index (SearchIndex): The index definition
- allow_index_downtime (bool): Allow index to be offline during update
- if_match (str): ETag for conditional updates
- if_none_match (str): ETag for conditional creation
Returns:
SearchIndex: The created or updated index definition
"""Delete search indexes with conditional support.
def delete_index(
self,
index: Union[str, SearchIndex],
*,
if_match: Optional[str] = None,
if_none_match: Optional[str] = None,
**kwargs
) -> None:
"""
Delete a search index.
Parameters:
- index: Index name or SearchIndex object to delete
- if_match (str): ETag for conditional deletion
- if_none_match (str): ETag for conditional deletion
"""Test and analyze text processing with index analyzers.
def analyze_text(
self,
index_name: str,
analyze_request: AnalyzeTextOptions,
**kwargs
) -> AnalyzeResult:
"""
Analyze text using the specified analyzer from an index.
Parameters:
- index_name (str): Name of the index containing the analyzer
- analyze_request (AnalyzeTextOptions): Analysis configuration
Returns:
AnalyzeResult: Analysis results with tokens and positions
"""Manage synonym maps for search term expansion and query enhancement.
def get_synonym_maps(
self,
*,
select: Optional[List[str]] = None,
**kwargs
) -> List[SynonymMap]:
"""
List all synonym maps in the search service.
Parameters:
- select (List[str], optional): Fields to include in results
Returns:
List[SynonymMap]: List of synonym maps
"""
def get_synonym_map_names(self, **kwargs) -> List[str]:
"""
List all synonym map names.
Returns:
List[str]: List of synonym map names
"""
def get_synonym_map(self, name: str, **kwargs) -> SynonymMap:
"""
Retrieve a specific synonym map.
Parameters:
- name (str): Name of the synonym map
Returns:
SynonymMap: The synonym map definition
"""
def create_synonym_map(self, synonym_map: SynonymMap, **kwargs) -> SynonymMap:
"""
Create a new synonym map.
Parameters:
- synonym_map (SynonymMap): The synonym map definition
Returns:
SynonymMap: The created synonym map
"""
def create_or_update_synonym_map(
self,
synonym_map: SynonymMap,
*,
if_match: Optional[str] = None,
if_none_match: Optional[str] = None,
**kwargs
) -> SynonymMap:
"""
Create a new synonym map or update an existing one.
Parameters:
- synonym_map (SynonymMap): The synonym map definition
- if_match (str): ETag for conditional updates
- if_none_match (str): ETag for conditional creation
Returns:
SynonymMap: The created or updated synonym map
"""
def delete_synonym_map(
self,
synonym_map: Union[str, SynonymMap],
*,
if_match: Optional[str] = None,
if_none_match: Optional[str] = None,
**kwargs
) -> None:
"""
Delete a synonym map.
Parameters:
- synonym_map: Synonym map name or SynonymMap object
- if_match (str): ETag for conditional deletion
- if_none_match (str): ETag for conditional deletion
"""from azure.search.documents.indexes import SearchIndexClient
from azure.search.documents.indexes.models import SearchIndex, SearchField, SearchFieldDataType
from azure.core.credentials import AzureKeyCredential
client = SearchIndexClient(
endpoint="https://service.search.windows.net",
credential=AzureKeyCredential("admin-key")
)
# Define index schema
fields = [
SearchField(name="id", type=SearchFieldDataType.String, key=True),
SearchField(name="title", type=SearchFieldDataType.String, searchable=True),
SearchField(name="content", type=SearchFieldDataType.String, searchable=True),
SearchField(name="category", type=SearchFieldDataType.String, filterable=True, facetable=True),
SearchField(name="rating", type=SearchFieldDataType.Double, filterable=True, sortable=True)
]
# Create index
index = SearchIndex(name="hotels", fields=fields)
created_index = client.create_index(index)
print(f"Created index: {created_index.name}")from azure.search.documents.indexes.models import (
SearchIndex, SearchField, SearchFieldDataType, VectorSearch,
VectorSearchProfile, VectorSearchAlgorithmConfiguration, VectorSearchAlgorithmKind
)
# Define fields including vector field
fields = [
SearchField(name="id", type=SearchFieldDataType.String, key=True),
SearchField(name="content", type=SearchFieldDataType.String, searchable=True),
SearchField(
name="content_vector",
type=SearchFieldDataType.Collection(SearchFieldDataType.Single),
vector_search_dimensions=1536,
vector_search_profile_name="my-vector-profile"
)
]
# Configure vector search
vector_search = VectorSearch(
profiles=[
VectorSearchProfile(
name="my-vector-profile",
algorithm_configuration_name="my-hnsw-config"
)
],
algorithms=[
VectorSearchAlgorithmConfiguration(
name="my-hnsw-config",
kind=VectorSearchAlgorithmKind.HNSW
)
]
)
# Create index with vector search
index = SearchIndex(
name="vector-index",
fields=fields,
vector_search=vector_search
)
client.create_index(index)from azure.search.documents.indexes.models import (
SearchIndex, SearchField, CustomAnalyzer, StandardTokenizer,
LowercaseTokenFilter, StopwordsTokenFilter
)
# Define custom analyzer
custom_analyzer = CustomAnalyzer(
name="my_analyzer",
tokenizer_name=StandardTokenizer(),
token_filters=[
LowercaseTokenFilter(),
StopwordsTokenFilter(stopwords_list="english")
]
)
fields = [
SearchField(name="id", type=SearchFieldDataType.String, key=True),
SearchField(
name="description",
type=SearchFieldDataType.String,
searchable=True,
analyzer_name="my_analyzer"
)
]
index = SearchIndex(
name="custom-analyzer-index",
fields=fields,
analyzers=[custom_analyzer]
)
client.create_index(index)from azure.search.documents.indexes.models import SynonymMap
# Create synonym map
synonym_map = SynonymMap(
name="my-synonyms",
synonyms="USA,United States,US\nNY,New York"
)
client.create_synonym_map(synonym_map)
# Use in field definition
SearchField(
name="location",
type=SearchFieldDataType.String,
searchable=True,
synonym_map_names=["my-synonyms"]
)from azure.search.documents.indexes.models import AnalyzeTextOptions
# Analyze text with index analyzer
analyze_request = AnalyzeTextOptions(
text="The quick brown fox jumps over the lazy dog",
analyzer_name="en.microsoft"
)
result = client.analyze_text("my-index", analyze_request)
for token in result.tokens:
print(f"Token: {token.token}, Position: {token.start_offset}-{token.end_offset}")# Index definition
class SearchIndex:
name: str
fields: List[SearchField]
scoring_profiles: Optional[List[ScoringProfile]] = None
default_scoring_profile: Optional[str] = None
cors_options: Optional[CorsOptions] = None
suggesters: Optional[List[Suggester]] = None
analyzers: Optional[List[LexicalAnalyzer]] = None
tokenizers: Optional[List[LexicalTokenizer]] = None
token_filters: Optional[List[TokenFilter]] = None
char_filters: Optional[List[CharFilter]] = None
encryption_key: Optional[SearchResourceEncryptionKey] = None
similarity: Optional[SimilarityAlgorithm] = None
semantic_search: Optional[SemanticSearch] = None
vector_search: Optional[VectorSearch] = None
e_tag: Optional[str] = None
# Field definition
class SearchField:
name: str
type: SearchFieldDataType
key: bool = False
retrievable: bool = True
searchable: bool = False
filterable: bool = False
sortable: bool = False
facetable: bool = False
analyzer_name: Optional[str] = None
search_analyzer_name: Optional[str] = None
index_analyzer_name: Optional[str] = None
synonym_map_names: Optional[List[str]] = None
fields: Optional[List["SearchField"]] = None
# Synonym map
class SynonymMap:
name: str
synonyms: str
encryption_key: Optional[SearchResourceEncryptionKey] = None
e_tag: Optional[str] = None
# Analysis result
class AnalyzeResult:
tokens: List[AnalyzedTokenInfo]
class AnalyzedTokenInfo:
token: str
start_offset: int
end_offset: int
position: intInstall with Tessl CLI
npx tessl i tessl/pypi-azure-search-documents