azure.search.documents.aio package

class azure.search.documents.aio.AsyncSearchItemPaged(*args, **kwargs)[source]

Return an async iterator of items.

args and kwargs will be passed to the AsyncPageIterator constructor directly, except page_iterator_class

by_page(continuation_token: str | None = None) AsyncIterator[AsyncIterator[ReturnType]]

Get an async iterator of pages of objects, instead of an async iterator of objects.

Parameters:

continuation_token (str) – An opaque continuation token. This value can be retrieved from the continuation_token field of a previous generator object. If specified, this generator will begin returning results from this point.

Returns:

An async iterator of pages (themselves async iterator of objects)

Return type:

AsyncIterator[AsyncIterator[ReturnType]]

async get_answers() List[QueryAnswerResult] | None[source]

Return semantic answers. Only included if the semantic ranker is used and answers are requested in the search query via the query_answer parameter.

Returns:

Answers.

Return type:

list[QueryAnswerResult]

async get_count() int[source]

Return the count of results if include_total_count was set for the query.

Returns:

Count of results.

Return type:

int

async get_coverage() float[source]

Return the coverage percentage, if minimum_coverage was specificied for the query.

Returns:

Coverage percentage.

Return type:

float

async get_facets() Dict | None[source]

Return any facet results if faceting was requested.

Returns:

Facet results.

Return type:

dict

class azure.search.documents.aio.SearchClient(endpoint: str, index_name: str, credential: AzureKeyCredential | AsyncTokenCredential, **kwargs: Any)[source]

A client to interact with an existing Azure search index.

Parameters:
  • endpoint (str) – The URL endpoint of an Azure search service

  • index_name (str) – The name of the index to connect to

  • credential (AzureKeyCredential or AsyncTokenCredential) – A credential to authorize search client requests

Keyword Arguments:
  • api_version (str) – The Search API version to use for requests.

  • audience (str) – sets the Audience to use for authentication with Azure Active Directory (AAD). The audience is not considered when using a shared key. If audience is not provided, the public cloud audience will be assumed.

Example:

Creating the SearchClient with an API key.
from azure.core.credentials import AzureKeyCredential
from azure.search.documents.aio import SearchClient

service_endpoint = os.environ["AZURE_SEARCH_SERVICE_ENDPOINT"]
index_name = os.environ["AZURE_SEARCH_INDEX_NAME"]
key = os.environ["AZURE_SEARCH_API_KEY"]

search_client = SearchClient(service_endpoint, index_name, AzureKeyCredential(key))
async autocomplete(search_text: str, suggester_name: str, *, mode: str | AutocompleteMode | None = None, filter: str | None = None, use_fuzzy_matching: bool | None = None, highlight_post_tag: str | None = None, highlight_pre_tag: str | None = None, minimum_coverage: float | None = None, search_fields: List[str] | None = None, top: int | None = None, **kwargs) List[Dict][source]

Get search auto-completion results from the Azure search index.

Parameters:
  • search_text (str) – The search text on which to base autocomplete results.

  • suggester_name (str) – The name of the suggester as specified in the suggesters collection that’s part of the index definition.

Keyword Arguments:
  • mode (str or AutocompleteMode) – Specifies the mode for Autocomplete. The default is ‘oneTerm’. Use ‘twoTerms’ to get shingles and ‘oneTermWithContext’ to use the current context while producing auto-completed terms. Possible values include: ‘oneTerm’, ‘twoTerms’, ‘oneTermWithContext’.

  • filter (str) – An OData expression that filters the documents used to produce completed terms for the Autocomplete result.

  • use_fuzzy_matching (bool) – A value indicating whether to use fuzzy matching for the autocomplete query. Default is false. When set to true, the query will find terms even if there’s a substituted or missing character in the search text. While this provides a better experience in some scenarios, it comes at a performance cost as fuzzy autocomplete queries are slower and consume more resources.

  • highlight_post_tag (str) – A string tag that is appended to hit highlights. Must be set with highlightPreTag. If omitted, hit highlighting is disabled.

  • highlight_pre_tag (str) – A string tag that is prepended to hit highlights. Must be set with highlightPostTag. If omitted, hit highlighting is disabled.

  • minimum_coverage (float) – A number between 0 and 100 indicating the percentage of the index that must be covered by an autocomplete query in order for the query to be reported as a success. This parameter can be useful for ensuring search availability even for services with only one replica. The default is 80.

  • search_fields (list[str]) – The list of field names to consider when querying for auto-completed terms. Target fields must be included in the specified suggester.

  • top (int) – The number of auto-completed terms to retrieve. This must be a value between 1 and 100. The default is 5.

Returns:

List of auto-completion results.

Return type:

list[Dict]

Example:

Get a auto-completions.
from azure.core.credentials import AzureKeyCredential
from azure.search.documents.aio import SearchClient

search_client = SearchClient(service_endpoint, index_name, AzureKeyCredential(key))

async with search_client:
    results = await search_client.autocomplete(search_text="bo", suggester_name="sg")

    print("Autocomplete suggestions for 'bo'")
    for result in results:
        print("    Completion: {}".format(result["text"]))
async close() None[source]

Close the session. :return: None :rtype: None

async delete_documents(documents: List[Dict], **kwargs: Any) List[IndexingResult][source]

Delete documents from the Azure search index

Delete removes the specified document from the index. Any field you specify in a delete operation, other than the key field, will be ignored. If you want to remove an individual field from a document, use merge_documents instead and set the field explicitly to None.

Delete operations are idempotent. That is, even if a document key does not exist in the index, attempting a delete operation with that key will result in a 200 status code.

Parameters:

documents (list[dict]) – A list of documents to delete.

Returns:

List of IndexingResult

Return type:

list[IndexingResult]

Example:

Delete existing documents to an index
result = await search_client.upload_documents(documents=[{"hotelId": "1000"}])

print("Delete new document succeeded: {}".format(result[0].succeeded))
async get_document(key: str, selected_fields: List[str] | None = None, **kwargs: Any) Dict[source]

Retrieve a document from the Azure search index by its key.

Parameters:
  • key (str) – The primary key value for the document to retrieve

  • selected_fields (list[str]) – an allow-list of fields to include in the results

Returns:

The document that matches the specified key

Return type:

dict

Example:

Get a specific document from the search index.
from azure.core.credentials import AzureKeyCredential
from azure.search.documents.aio import SearchClient

search_client = SearchClient(service_endpoint, index_name, AzureKeyCredential(key))

async with search_client:
    result = await search_client.get_document(key="23")

    print("Details for hotel '23' are:")
    print("        Name: {}".format(result["hotelName"]))
    print("      Rating: {}".format(result["rating"]))
    print("    Category: {}".format(result["category"]))
async get_document_count(**kwargs: Any) int[source]

Return the number of documents in the Azure search index.

Returns:

The count of documents in the index

Return type:

int

async index_documents(batch: IndexDocumentsBatch, **kwargs: Any) List[IndexingResult][source]

Specify a document operations to perform as a batch.

Parameters:

batch (IndexDocumentsBatch) – A batch of document operations to perform.

Returns:

List of IndexingResult

Return type:

list[IndexingResult]

:raises ~azure.search.documents.RequestEntityTooLargeError

async merge_documents(documents: List[Dict], **kwargs: Any) List[IndexingResult][source]

Merge documents in to existing documents in the Azure search index.

Merge updates an existing document with the specified fields. If the document doesn’t exist, the merge will fail. Any field you specify in a merge will replace the existing field in the document. This also applies to collections of primitive and complex types.

Parameters:

documents (list[dict]) – A list of documents to merge.

Returns:

List of IndexingResult

Return type:

list[IndexingResult]

Example:

Merge fields into existing documents to an index
result = await search_client.upload_documents(documents=[{"hotelId": "1000", "rating": 4.5}])

print("Merge into new document succeeded: {}".format(result[0].succeeded))
async merge_or_upload_documents(documents: List[Dict], **kwargs: Any) List[IndexingResult][source]

Merge documents in to existing documents in the Azure search index, or upload them if they do not yet exist.

This action behaves like merge_documents if a document with the given key already exists in the index. If the document does not exist, it behaves like upload_documents with a new document.

Parameters:

documents (list[dict]) – A list of documents to merge or upload.

Returns:

List of IndexingResult

Return type:

list[IndexingResult]

async search(search_text: str | None = None, *, include_total_count: bool | None = None, facets: List[str] | None = None, filter: str | None = None, highlight_fields: str | None = None, highlight_post_tag: str | None = None, highlight_pre_tag: str | None = None, minimum_coverage: float | None = None, order_by: List[str] | None = None, query_type: str | QueryType | None = None, scoring_parameters: List[str] | None = None, scoring_profile: str | None = None, semantic_query: str | None = None, search_fields: List[str] | None = None, search_mode: str | SearchMode | None = None, query_answer: str | QueryAnswerType | None = None, query_answer_count: int | None = None, query_answer_threshold: float | None = None, query_caption: str | QueryCaptionType | None = None, query_caption_highlight_enabled: bool | None = None, semantic_configuration_name: str | None = None, select: List[str] | None = None, skip: int | None = None, top: int | None = None, scoring_statistics: str | ScoringStatistics | None = None, session_id: str | None = None, vector_queries: List[VectorQuery] | None = None, vector_filter_mode: str | VectorFilterMode | None = None, semantic_error_mode: str | SemanticErrorMode | None = None, semantic_max_wait_in_milliseconds: int | None = None, **kwargs) AsyncSearchItemPaged[Dict][source]

Search the Azure search index for documents.

Parameters:

search_text (str) – A full-text search query expression; Use “*” or omit this parameter to match all documents.

Keyword Arguments:
  • include_total_count (bool) – A value that specifies whether to fetch the total count of results. Default is false. Setting this value to true may have a performance impact. Note that the count returned is an approximation.

  • facets (list[str]) – The list of facet expressions to apply to the search query. Each facet expression contains a field name, optionally followed by a comma-separated list of name:value pairs.

  • filter (str) – The OData $filter expression to apply to the search query.

  • highlight_fields (str) – The comma-separated list of field names to use for hit highlights. Only searchable fields can be used for hit highlighting.

  • highlight_post_tag (str) – A string tag that is appended to hit highlights. Must be set with highlightPreTag. Default is </em>.

  • highlight_pre_tag (str) – A string tag that is prepended to hit highlights. Must be set with highlightPostTag. Default is <em>.

  • minimum_coverage (float) – A number between 0 and 100 indicating the percentage of the index that must be covered by a search query in order for the query to be reported as a success. This parameter can be useful for ensuring search availability even for services with only one replica. The default is 100.

  • order_by (list[str]) – The list of OData $orderby expressions by which to sort the results. Each expression can be either a field name or a call to either the geo.distance() or the search.score() functions. Each expression can be followed by asc to indicate ascending, and desc to indicate descending. The default is ascending order. Ties will be broken by the match scores of documents. If no OrderBy is specified, the default sort order is descending by document match score. There can be at most 32 $orderby clauses.

  • query_type (str or QueryType) – A value that specifies the syntax of the search query. The default is ‘simple’. Use ‘full’ if your query uses the Lucene query syntax. Possible values include: ‘simple’, ‘full’, “semantic”.

  • scoring_parameters (list[str]) – The list of parameter values to be used in scoring functions (for example, referencePointParameter) using the format name-values. For example, if the scoring profile defines a function with a parameter called ‘mylocation’ the parameter string would be “mylocation–122.2,44.8” (without the quotes).

  • scoring_profile (str) – The name of a scoring profile to evaluate match scores for matching documents in order to sort the results.

  • semantic_query (str) – Allows setting a separate search query that will be solely used for semantic reranking, semantic captions and semantic answers. Is useful for scenarios where there is a need to use different queries between the base retrieval and ranking phase, and the L2 semantic phase.

  • search_fields (list[str]) – The list of field names to which to scope the full-text search. When using fielded search (fieldName:searchExpression) in a full Lucene query, the field names of each fielded search expression take precedence over any field names listed in this parameter.

  • search_mode (str or SearchMode) – A value that specifies whether any or all of the search terms must be matched in order to count the document as a match. Possible values include: ‘any’, ‘all’.

  • query_answer (str or QueryAnswerType) – This parameter is only valid if the query type is ‘semantic’. If set, the query returns answers extracted from key passages in the highest ranked documents. Possible values include: “none”, “extractive”.

  • query_answer_count (int) – This parameter is only valid if the query type is ‘semantic’ and query answer is ‘extractive’. Configures the number of answers returned. Default count is 1.

  • query_answer_threshold (float) – This parameter is only valid if the query type is ‘semantic’ and query answer is ‘extractive’. Configures the number of confidence threshold. Default count is 0.7.

  • query_caption (str or QueryCaptionType) – This parameter is only valid if the query type is ‘semantic’. If set, the query returns captions extracted from key passages in the highest ranked documents. Defaults to ‘None’. Possible values include: “none”, “extractive”.

  • query_caption_highlight_enabled (bool) – This parameter is only valid if the query type is ‘semantic’ when query caption is set to ‘extractive’. Determines whether highlighting is enabled. Defaults to ‘true’.

  • semantic_configuration_name (str) – The name of the semantic configuration that will be used when processing documents for queries of type semantic.

  • select (list[str]) – The list of fields to retrieve. If unspecified, all fields marked as retrievable in the schema are included.

  • skip (int) – The number of search results to skip. This value cannot be greater than 100,000. If you need to scan documents in sequence, but cannot use $skip due to this limitation, consider using $orderby on a totally-ordered key and $filter with a range query instead.

  • top (int) – The number of search results to retrieve. This can be used in conjunction with $skip to implement client-side paging of search results. If results are truncated due to server-side paging, the response will include a continuation token that can be used to issue another Search request for the next page of results.

  • scoring_statistics (str or ScoringStatistics) – A value that specifies whether we want to calculate scoring statistics (such as document frequency) globally for more consistent scoring, or locally, for lower latency. The default is ‘local’. Use ‘global’ to aggregate scoring statistics globally before scoring. Using global scoring statistics can increase latency of search queries. Possible values include: “local”, “global”.

  • session_id (str) – A value to be used to create a sticky session, which can help getting more consistent results. As long as the same sessionId is used, a best-effort attempt will be made to target the same replica set. Be wary that reusing the same sessionID values repeatedly can interfere with the load balancing of the requests across replicas and adversely affect the performance of the search service. The value used as sessionId cannot start with a ‘_’ character.

  • semantic_error_mode (str or SemanticErrorMode) – Allows the user to choose whether a semantic call should fail completely (default / current behavior), or to return partial results. Known values are: “partial” and “fail”.

  • semantic_max_wait_in_milliseconds (int) – Allows the user to set an upper bound on the amount of time it takes for semantic enrichment to finish processing before the request fails.

  • vector_queries (list[VectorQuery]) – The query parameters for vector and hybrid search queries.

  • vector_filter_mode (str or VectorFilterMode) – Determines whether or not filters are applied before or after the vector search is performed. Default is ‘preFilter’. Known values are: “postFilter” and “preFilter”.

Returns:

A list of documents (dicts) matching the specified search criteria.

Returns:

List of search results.

Return type:

AsyncSearchItemPaged[dict]

Example:

Search on a simple text term.
from azure.core.credentials import AzureKeyCredential
from azure.search.documents.aio import SearchClient

search_client = SearchClient(service_endpoint, index_name, AzureKeyCredential(key))

async with search_client:
    results = await search_client.search(search_text="spa")

    print("Hotels containing 'spa' in the name (or other fields):")
    async for result in results:
        print("    Name: {} (rating {})".format(result["hotelName"], result["rating"]))

Example:

Filter and sort search results.
from azure.core.credentials import AzureKeyCredential
from azure.search.documents.aio import SearchClient

search_client = SearchClient(service_endpoint, index_name, AzureKeyCredential(key))

async with search_client:
    results = await search_client.search(
        search_text="WiFi",
        filter="Address/StateProvince eq 'FL' and Address/Country eq 'USA'",
        select=["hotelName", "rating"],
        order_by=["rating desc"],
    )

    print("Florida hotels containing 'WiFi', sorted by Rating:")
    async for result in results:
        print("    Name: {} (rating {})".format(result["hotelName"], result["rating"]))

Example:

Get search result facets.
from azure.core.credentials import AzureKeyCredential
from azure.search.documents.aio import SearchClient

search_client = SearchClient(service_endpoint, index_name, AzureKeyCredential(key))

async with search_client:
    results = await search_client.search(search_text="WiFi", facets=["category,count:3", "parkingIncluded"])

    facets: Dict[str, List[str]] = cast(Dict[str, List[str]], await results.get_facets())

    print("Catgory facet counts for hotels:")
    for facet in facets["category"]:
        print("    {}".format(facet))
async send_request(request: HttpRequest, *, stream: bool = False, **kwargs) AsyncHttpResponse[source]

Runs a network request using the client’s existing pipeline.

Parameters:

request (HttpRequest) – The network request you want to make.

Keyword Arguments:

stream (bool) – Whether the response payload will be streamed. Defaults to False.

Returns:

The response of your network call. Does not do error handling on your response.

Return type:

AsyncHttpResponse

async suggest(search_text: str, suggester_name: str, *, filter: str | None = None, use_fuzzy_matching: bool | None = None, highlight_post_tag: str | None = None, highlight_pre_tag: str | None = None, minimum_coverage: float | None = None, order_by: List[str] | None = None, search_fields: List[str] | None = None, select: List[str] | None = None, top: int | None = None, **kwargs) List[Dict][source]

Get search suggestion results from the Azure search index.

Parameters:
  • search_text (str) – Required. The search text to use to suggest documents. Must be at least 1 character, and no more than 100 characters.

  • suggester_name (str) – Required. The name of the suggester as specified in the suggesters collection that’s part of the index definition.

Keyword Arguments:
  • filter (str) – An OData expression that filters the documents considered for suggestions.

  • use_fuzzy_matching (bool) – A value indicating whether to use fuzzy matching for the suggestions query. Default is false. When set to true, the query will find terms even if there’s a substituted or missing character in the search text. While this provides a better experience in some scenarios, it comes at a performance cost as fuzzy suggestions queries are slower and consume more resources.

  • highlight_post_tag (str) – A string tag that is appended to hit highlights. Must be set with highlightPreTag. If omitted, hit highlighting of suggestions is disabled.

  • highlight_pre_tag (str) – A string tag that is prepended to hit highlights. Must be set with highlightPostTag. If omitted, hit highlighting of suggestions is disabled.

  • minimum_coverage (float) – A number between 0 and 100 indicating the percentage of the index that must be covered by a suggestions query in order for the query to be reported as a success. This parameter can be useful for ensuring search availability even for services with only one replica. The default is 80.

  • order_by (list[str]) – The list of OData $orderby expressions by which to sort the results. Each expression can be either a field name or a call to either the geo.distance() or the search.score() functions. Each expression can be followed by asc to indicate ascending, or desc to indicate descending. The default is ascending order. Ties will be broken by the match scores of documents. If no $orderby is specified, the default sort order is descending by document match score. There can be at most 32 $orderby clauses.

  • search_fields (list[str]) – The list of field names to search for the specified search text. Target fields must be included in the specified suggester.

  • select (list[str]) – The list of fields to retrieve. If unspecified, only the key field will be included in the results.

  • top (int) – The number of suggestions to retrieve. The value must be a number between 1 and 100. The default is 5.

Returns:

List of suggestion results.

Return type:

list[dict]

Example:

Get search suggestions.
from azure.core.credentials import AzureKeyCredential
from azure.search.documents.aio import SearchClient

search_client = SearchClient(service_endpoint, index_name, AzureKeyCredential(key))

async with search_client:
    results = await search_client.suggest(search_text="coffee", suggester_name="sg")

    print("Search suggestions for 'coffee'")
    for result in results:
        hotel = await search_client.get_document(key=result["hotelId"])
        print("    Text: {} for Hotel: {}".format(repr(result["text"]), hotel["hotelName"]))
async upload_documents(documents: List[Dict], **kwargs: Any) List[IndexingResult][source]

Upload documents to the Azure search index.

An upload action is similar to an “upsert” where the document will be inserted if it is new and updated/replaced if it exists. All fields are replaced in the update case.

Parameters:

documents (list[dict]) – A list of documents to upload.

Returns:

List of IndexingResult

Return type:

list[IndexingResult]

Example:

Upload new documents to an index
DOCUMENT = {
    "category": "Hotel",
    "hotelId": "1000",
    "rating": 4.0,
    "rooms": [],
    "hotelName": "Azure Inn",
}

result = await search_client.upload_documents(documents=[DOCUMENT])

print("Upload of new document succeeded: {}".format(result[0].succeeded))
class azure.search.documents.aio.SearchIndexingBufferedSender(endpoint: str, index_name: str, credential: AzureKeyCredential | AsyncTokenCredential, **kwargs: Any)[source]

A buffered sender for document indexing actions.

Parameters:
  • endpoint (str) – The URL endpoint of an Azure search service

  • index_name (str) – The name of the index to connect to

  • credential (AzureKeyCredential or AsyncTokenCredential) – A credential to authorize search client requests

Keyword Arguments:
  • auto_flush_interval (int) – how many max seconds if between 2 flushes. This only takes effect when auto_flush is on. Default to 60 seconds.

  • initial_batch_action_count (int) – The initial number of actions to group into a batch when tuning the behavior of the sender. The default value is 512.

  • max_retries_per_action (int) – The number of times to retry a failed document. The default value is 3.

  • on_new (callable) – If it is set, the client will call corresponding methods when there is a new IndexAction added.

  • on_progress (callable) – If it is set, the client will call corresponding methods when there is a IndexAction succeeds.

  • on_error (callable) – If it is set, the client will call corresponding methods when there is a IndexAction fails.

  • on_remove (callable) – If it is set, the client will call corresponding methods when there is a IndexAction removed from the queue (succeeds or fails).

  • api_version (str) – The Search API version to use for requests.

  • audience (str) – sets the Audience to use for authentication with Azure Active Directory (AAD). The audience is not considered when using a shared key. If audience is not provided, the public cloud audience will be assumed.

async close(**kwargs: Any) None[source]

Close the session. :return: None :rtype: None

async delete_documents(documents: List[Dict], **kwargs: Any) None[source]

Queue delete documents actions :param documents: A list of documents to delete. :type documents: list[Dict]

async flush(timeout: int = 86400, **kwargs) bool[source]

Flush the batch. :param int timeout: time out setting. Default is 86400s (one day) :return: True if there are errors. Else False :rtype: bool :raises ~azure.core.exceptions.ServiceResponseTimeoutError:

async index_documents(batch: IndexDocumentsBatch, **kwargs: Any) List[IndexingResult][source]

Specify a document operations to perform as a batch.

Parameters:

batch (IndexDocumentsBatch) – A batch of document operations to perform.

Returns:

Indexing result for each action in the batch.

Return type:

list[IndexingResult]

:raises ~azure.search.documents.RequestEntityTooLargeError

async merge_documents(documents: List[Dict], **kwargs: Any) None[source]

Queue merge documents actions :param documents: A list of documents to merge. :type documents: list[dict]

async merge_or_upload_documents(documents: List[Dict], **kwargs: Any) None[source]

Queue merge documents or upload documents actions :param documents: A list of documents to merge or upload. :type documents: list[dict]

async upload_documents(documents: List[Dict], **kwargs: Any) None[source]

Queue upload documents actions. :param documents: A list of documents to upload. :type documents: list[dict]

property actions: List[IndexAction]

The list of currently index actions in queue to index. :return: The list of currently index actions in queue to index. :rtype: list[IndexAction]