This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
Azure AI Search (formerly known as "Azure Cognitive Search") is an AI-powered information retrieval platform that helps developers build rich search experiences and generative AI apps that combine large language models with enterprise data.
Azure AI Search is well suited for the following application scenarios:
Use the Azure.Search.Documents client library to:
Source code|Package (PyPI)|Package (Conda)|API reference documentation|Product documentation|Samples
Install the Azure AI Search client library for Python withpip:
pip install azure-search-documents
To create a new search service, you can use theAzure portal,Azure PowerShell, or theAzure CLI.
az search service create --name <mysearch> --resource-group <mysearch-rg> --sku free --location westus
Seechoosing a pricing tierfor more information about available options.
To interact with the search service, you'll need to create an instance of the appropriate client class:SearchClient
for searching indexed documents,SearchIndexClient
for managing indexes, orSearchIndexerClient
for crawling data sources and loading search documents into an index. To instantiate a client object, you'll need anendpoint andAzure roles or anAPI key. You can refer to the documentation for more information onsupported authenticating approaches with the search service.
An API key can be an easier approach to start with because it doesn't require pre-existing role assignments.
You can get theendpoint and anAPI key from the Search service in theAzure portal. Please refer thedocumentation for instructions on how to get an API key.
Alternatively, you can use the followingAzure CLI command to retrieve the API key from the Search service:
az search admin-key show --service-name <mysearch> --resource-group <mysearch-rg>
There are two types of keys used to access your search service:admin(read-write) andquery(read-only) keys. Restricting access andoperations in client apps is essential to safeguarding the search assets on yourservice. Always use a query key rather than an admin key for any queryoriginating from a client app.
Note: The example Azure CLI snippet above retrieves an admin key so it's easierto get started exploring APIs, but it should be managed carefully.
To instantiate theSearchClient
, you'll need theendpoint,API key andindex name:
from azure.core.credentials import AzureKeyCredentialfrom azure.search.documents import SearchClientservice_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))
You can also create aSearchClient
,SearchIndexClient
, orSearchIndexerClient
using Microsoft Entra ID authentication. Your user or service principal must be assigned the "Search Index Data Reader" role.Using theDefaultAzureCredential you can authenticate a service using Managed Identity or a service principal, authenticate as a developer working on an application, and more all without changing code. Please refer thedocumentation for instructions on how to connect to Azure AI Search using Azure role-based access control (Azure RBAC).
Before you can use theDefaultAzureCredential
, or any credential type fromAzure.Identity, you'll first need toinstall the Azure.Identity package.
To useDefaultAzureCredential
with a client ID and secret, you'll need to set theAZURE_TENANT_ID
,AZURE_CLIENT_ID
, andAZURE_CLIENT_SECRET
environment variables; alternatively, you can pass those valuesto theClientSecretCredential
also in Azure.Identity.
Make sure you use the right namespace forDefaultAzureCredential
at the top of your source file:
from azure.identity import DefaultAzureCredentialfrom azure.search.documents import SearchClientservice_endpoint = os.getenv("AZURE_SEARCH_SERVICE_ENDPOINT")index_name = os.getenv("AZURE_SEARCH_INDEX_NAME")credential = DefaultAzureCredential()search_client = SearchClient(service_endpoint, index_name, credential)
An Azure AI Search service contains one or more indexes that providepersistent storage of searchable data in the form of JSON documents.(Ifyou're brand new to search, you can make a very rough analogy betweenindexes and database tables.) The Azure.Search.Documents client libraryexposes operations on these resources through three main client types.
SearchClient
helps with:
SearchIndexClient
allows you to:
SearchIndexerClient
allows you to:Azure AI Search provides two powerful features:semantic ranking andvector search.
Semantic ranking enhances the quality of search results for text-based queries. By enabling semantic ranking on your search service, you can improve the relevance of search results in two ways:
To learn more about semantic ranking, you can refer to thedocumentation.
Vector search is an information retrieval technique that uses numeric representations of searchable documents and query strings. By searching for numeric representations of content that are most similar to the numeric query, vector search can find relevant matches, even if the exact terms of the query are not present in the index. Moreover, vector search can be applied to various types of content, including images and videos and translated text, not just same-language text.
To learn how to index vector fields and perform vector search, you can refer to thesample. This sample provides detailed guidance on indexing vector fields and demonstrates how to perform vector search.
Additionally, for more comprehensive information about vector search, including its concepts and usage, you can refer to thedocumentation. The documentation provides in-depth explanations and guidance on leveraging the power of vector search in Azure AI Search.
_TheAzure.Search.Documents
client library (v1) provides APIs for data plane operations. ThepreviousMicrosoft.Azure.Search
client library (v10) is now retired. It has many similar looking APIs, so please be careful to avoid confusion when exploring online resources. A good rule of thumb is to check for the namespaceAzure.Search.Documents;
when you're looking for API reference.
The following examples all use a simpleHotel data setthat you canimport into your own index from the Azure portal.These are just a few of the basics - pleasecheck out our Samples formuch more.
Let's start by importing our namespaces.
import osfrom azure.core.credentials import AzureKeyCredentialfrom azure.search.documents import SearchClient
We'll then create aSearchClient
to access our hotels search index.
index_name = "hotels"# Get the service endpoint and API key from the environmentendpoint = os.environ["SEARCH_ENDPOINT"]key = os.environ["SEARCH_API_KEY"]# Create a clientcredential = AzureKeyCredential(key)client = SearchClient(endpoint=endpoint, index_name=index_name, credential=credential)
Let's search for a "luxury" hotel.
results = client.search(search_text="luxury")for result in results: print("{}: {})".format(result["hotelId"], result["hotelName"]))
You can use theSearchIndexClient
to create a search index. Fields can bedefined using convenientSimpleField
,SearchableField
, orComplexField
models. Indexes can also define suggesters, lexical analyzers, and more.
client = SearchIndexClient(service_endpoint, AzureKeyCredential(key))name = "hotels"fields = [ SimpleField(name="hotelId", type=SearchFieldDataType.String, key=True), SimpleField(name="baseRate", type=SearchFieldDataType.Double), SearchableField(name="description", type=SearchFieldDataType.String, collection=True), ComplexField( name="address", fields=[ SimpleField(name="streetAddress", type=SearchFieldDataType.String), SimpleField(name="city", type=SearchFieldDataType.String), ], collection=True, ),]cors_options = CorsOptions(allowed_origins=["*"], max_age_in_seconds=60)scoring_profiles: List[ScoringProfile] = []index = SearchIndex(name=name, fields=fields, scoring_profiles=scoring_profiles, cors_options=cors_options)result = client.create_index(index)
You canUpload
,Merge
,MergeOrUpload
, andDelete
multiple documents froman index in a single batched request. There area few special rules for mergingto be aware of.
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))
To authenticate in aNational Cloud, you will need to make the following additions to your client configuration:
AuthorityHost
in the credential options or via theAZURE_AUTHORITY_HOST
environment variableaudience
inSearchClient
,SearchIndexClient
, orSearchIndexerClient
# Create a SearchClient that will authenticate through AAD in the China national cloud.import osfrom azure.identity import DefaultAzureCredential, AzureAuthorityHostsfrom azure.search.documents import SearchClientindex_name = "hotels"endpoint = os.environ["SEARCH_ENDPOINT"]key = os.environ["SEARCH_API_KEY"]credential = DefaultAzureCredential(authority=AzureAuthorityHosts.AZURE_CHINA)search_client = SearchClient(endpoint, index_name, credential=credential, audience="https://search.azure.cn")
In addition to querying for documents using keywords and optional filters,you can retrieve a specific document from your index if you already know thekey. You could get the key from a query, for example, and want to show moreinformation about it or navigate your customer to that document.
from azure.core.credentials import AzureKeyCredentialfrom azure.search.documents import SearchClientsearch_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"]))
This library includes a complete async API. To use it, you mustfirst install an async transport, such asaiohttp.Seeazure-core documentationfor more information.
from azure.core.credentials import AzureKeyCredentialfrom azure.search.documents.aio import SearchClientsearch_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"]))
The Azure AI Search client will raise exceptions defined inAzure Core.
This library uses the standardlogging library for logging.Basic information about HTTP sessions (URLs, headers, etc.) is logged at INFOlevel.
Detailed DEBUG level logging, including request/response bodies and unredactedheaders, can be enabled on a client with thelogging_enable
keyword argument:
import sysimport loggingfrom azure.core.credentials import AzureKeyCredentialfrom azure.search.documents import SearchClient# Create a logger for the 'azure' SDKlogger = logging.getLogger('azure')logger.setLevel(logging.DEBUG)# Configure a console outputhandler = logging.StreamHandler(stream=sys.stdout)logger.addHandler(handler)# This client will log detailed information about its HTTP sessions, at DEBUG levelclient = SearchClient("<service endpoint>", "<index_name>", AzureKeyCredential("<api key>"), logging_enable=True)
Similarly,logging_enable
can enable detailed logging for a single operation,even when it isn't enabled for the client:
result = client.search(search_text="spa", logging_enable=True)
See ourSearch CONTRIBUTING.md for details on building,testing, and contributing to this library.
This project welcomes contributions and suggestions. Most contributions requireyou to agree to a Contributor License Agreement (CLA) declaring that you havethe right to, and actually do, grant us the rights to use your contribution. Fordetails, visitcla.microsoft.com.
This project has adopted theMicrosoft Open Source Code of Conduct.For more information, see theCode of Conduct FAQor contactopencode@microsoft.com with anyadditional questions or comments.
Was this page helpful?
Was this page helpful?