SearchClient Class
A client to interact with an existing Azure search index.
- Inheritance
-
azure.search.documents._headers_mixin.HeadersMixinSearchClient
Constructor
SearchClient(endpoint: str, index_name: str, credential: AzureKeyCredential, **kwargs: Any)
Parameters
- api_version
- str
The Search API version to use for requests.
Examples
Creating the SearchClient with an API key.
from azure.core.credentials import AzureKeyCredential
from azure.search.documents import SearchClient
service_endpoint = os.getenv("AZURE_SEARCH_SERVICE_ENDPOINT")
index_name = os.getenv("AZURE_SEARCH_INDEX_NAME")
key = os.getenv("AZURE_SEARCH_API_KEY")
search_client = SearchClient(service_endpoint, index_name, AzureKeyCredential(key))
Methods
| autocomplete |
Get search auto-completion results from the Azure search index. collection that's part of the index definition. :keyword mode: 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'. |
| close |
Close the SearchClient session. |
| delete_documents |
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. |
| get_document |
Retrieve a document from the Azure search index by its key. |
| get_document_count |
Return the number of documents in the Azure search index. |
| index_documents |
Specify a document operations to perform as a batch. :raises RequestEntityTooLargeError |
| merge_documents |
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. |
| merge_or_upload_documents |
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. |
| search |
Search the Azure search index for documents. |
| suggest |
Get search suggestion results from the Azure search index. character, and no more than 100 characters. :param str suggester_name: Required. The name of the suggester as specified in the suggesters collection that's part of the index definition. :keyword str filter: An OData expression that filters the documents considered for suggestions. :keyword bool use_fuzzy_matching: 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. |
| upload_documents |
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. |
autocomplete
Get search auto-completion results from the Azure search index.
collection that's part of the index definition. :keyword mode: 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'.
autocomplete(search_text: str, suggester_name: str, **kwargs: Any) -> List[dict]
Parameters
- 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.
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.
Return type
Examples
Get a auto-completions.
from azure.core.credentials import AzureKeyCredential
from azure.search.documents import SearchClient
search_client = SearchClient(service_endpoint, index_name, AzureKeyCredential(key))
results = search_client.autocomplete(search_text="bo", suggester_name="sg")
print("Autocomplete suggestions for 'bo'")
for result in results:
print(" Completion: {}".format(result["text"]))
close
Close the SearchClient session.
close() -> None
delete_documents
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.
delete_documents(documents: List[dict], **kwargs: Any) -> List[IndexingResult]
Parameters
Return type
Examples
Delete existing documents to an index
result = search_client.delete_documents(documents=[{"HotelId": "1000"}])
print("Delete new document succeeded: {}".format(result[0].succeeded))
get_document
Retrieve a document from the Azure search index by its key.
get_document(key: str, selected_fields: List[str] = None, **kwargs: Any) -> dict
Parameters
Return type
Examples
Get a specific document from the search index.
from azure.core.credentials import AzureKeyCredential
from azure.search.documents import SearchClient
search_client = SearchClient(service_endpoint, index_name, AzureKeyCredential(key))
result = 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"]))
get_document_count
Return the number of documents in the Azure search index.
get_document_count(**kwargs: Any) -> int
Return type
index_documents
Specify a document operations to perform as a batch.
:raises RequestEntityTooLargeError
index_documents(batch: IndexDocumentsBatch, **kwargs: Any) -> List[IndexingResult]
Parameters
Return type
merge_documents
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.
merge_documents(documents: List[dict], **kwargs: Any) -> List[IndexingResult]
Parameters
Return type
Examples
Merge fields into existing documents to an index
result = search_client.merge_documents(documents=[{"HotelId": "1000", "Rating": 4.5}])
print("Merge into new document succeeded: {}".format(result[0].succeeded))
merge_or_upload_documents
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.
merge_or_upload_documents(documents: List[dict], **kwargs: Any) -> List[IndexingResult]
Parameters
Return type
search
Search the Azure search index for documents.
search(search_text: str, **kwargs: Any) -> SearchItemPaged[dict]
Parameters
- search_text
- str
A full-text search query expression; Use "*" or omit this parameter to match all documents.
- 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.
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.
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 .
- highlight_pre_tag
- str
A string tag that is prepended to hit highlights. Must be set with highlightPostTag. Default is .
- 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.
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.
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".
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.
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'.
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.
Return type
Examples
Get search result facets.
from azure.core.credentials import AzureKeyCredential
from azure.search.documents import SearchClient
search_client = SearchClient(service_endpoint, index_name, AzureKeyCredential(key))
results = search_client.search(search_text="WiFi", facets=["Category,count:3", "ParkingIncluded"])
facets = results.get_facets()
print("Catgory facet counts for hotels:")
for facet in facets["Category"]:
print(" {}".format(facet))
suggest
Get search suggestion results from the Azure search index.
character, and no more than 100 characters. :param str suggester_name: Required. The name of the suggester as specified in the suggesters collection that's part of the index definition. :keyword str filter: An OData expression that filters the documents considered for suggestions. :keyword bool use_fuzzy_matching: 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.
suggest(search_text: str, suggester_name: str, **kwargs: Any) -> List[dict]
Parameters
- 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.
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.
The list of field names to search for the specified search text. Target fields must be included in the specified suggester.
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.
Return type
Examples
Get search suggestions.
from azure.core.credentials import AzureKeyCredential
from azure.search.documents import SearchClient
search_client = SearchClient(service_endpoint, index_name, AzureKeyCredential(key))
results = search_client.suggest(search_text="coffee", suggester_name="sg")
print("Search suggestions for 'coffee'")
for result in results:
hotel = search_client.get_document(key=result["HotelId"])
print(" Text: {} for Hotel: {}".format(repr(result["text"]), hotel["HotelName"]))
upload_documents
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.
upload_documents(documents: List[dict], **kwargs: Any) -> List[IndexingResult]
Parameters
Return type
Examples
Upload new documents to an index
DOCUMENT = {
'Category': 'Hotel',
'HotelId': '1000',
'Rating': 4.0,
'Rooms': [],
'HotelName': 'Azure Inn',
}
result = search_client.upload_documents(documents=[DOCUMENT])
print("Upload of new document succeeded: {}".format(result[0].succeeded))
Feedback
Submit and view feedback for