Class SearchClientBuilder
- All Implemented Interfaces:
com.azure.core.client.traits.AzureKeyCredentialTrait<SearchClientBuilder>,com.azure.core.client.traits.ConfigurationTrait<SearchClientBuilder>,com.azure.core.client.traits.EndpointTrait<SearchClientBuilder>,com.azure.core.client.traits.HttpTrait<SearchClientBuilder>,com.azure.core.client.traits.TokenCredentialTrait<SearchClientBuilder>
SearchClients and SearchAsyncClients.
Overview
This client allows you to create instances of SearchClient and SearchAsyncClient to
utilize synchronous and asynchronous APIs respectively to interact with Azure AI Search.
Getting Started
Authentication
Azure AI Search supports Microsoft Entra ID (role-based) authentication and API keys for authentication.
For more information about the scopes of authorization, see the Azure AI Search Security Overview documentation.
Building and Authenticating a SearchClient or SearchAsyncClient using API keys
To build an instance of SearchClient or SearchAsyncClient using API keys, call
buildClient and buildAsyncClient respectively from the
SearchClientBuilder.
The following must be provided to construct a client instance.
- The Azure AI Search service URL.
-
An
API Keythat grants access to the Azure AI Search service.
Instantiating a synchronous Search Client
SearchClient searchClient = new SearchClientBuilder()
.credential(new AzureKeyCredential("{key}"))
.endpoint("{endpoint}")
.indexName("{indexName}")
.buildClient();
Instantiating an asynchronous Search Client
SearchAsyncClient searchAsyncClient = new SearchClientBuilder()
.credential(new AzureKeyCredential("{key}"))
.endpoint("{endpoint}")
.indexName("{indexName}")
.buildAsyncClient();
Building and Authenticating a SearchClient or SearchAsyncClient using Microsoft Entra ID
You can also create a SearchClient or SearchAsyncClient using Microsoft Entra ID
authentication. Your user or service principal must be assigned the "Search Index Data Reader" role. Using the
DefaultAzureCredential 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 the documentation for
instructions on how to connect to Azure AI Search using Azure role-based access control (Azure RBAC).
Before you can use the `DefaultAzureCredential`, or any credential type from Azure.Identity, you'll first need to install the Azure.Identity package.
To use DefaultAzureCredential with a client ID and secret, you'll need to set the `AZURE_TENANT_ID`, `AZURE_CLIENT_ID`, and `AZURE_CLIENT_SECRET` environment variables; alternatively, you can pass those values to the `ClientSecretCredential` also in azure-identity.
Make sure you use the right namespace for DefaultAzureCredential at the top of your source file:
import com.azure.identity.DefaultAzureCredential; import com.azure.identity.DefaultAzureCredentialBuilder;
Then you can create an instance of DefaultAzureCredential and pass it to a new instance of your client:
The following sample builds a SearchClient using DefaultAzureCredential.
Instantiating a synchronous Search Client
DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().build();
SearchClient searchClient = new SearchClientBuilder()
.credential(credential)
.endpoint("{endpoint}")
.indexName("{indexName}")
.buildClient();
Instantiating an asynchronous Search Client
DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().build();
SearchAsyncClient searchAsyncClient = new SearchClientBuilder()
.credential(credential)
.endpoint("{endpoint}")
.indexName("{indexName}")
.buildAsyncClient();
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionfinal classThis class provides a fluent builder API to help aid the configuration and instantiation ofSearchIndexingBufferedSendersandSearchIndexingBufferedAsyncSenders. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a builder instance that is able to configure and constructSearchClientsandSearchAsyncClients. -
Method Summary
Modifier and TypeMethodDescriptionaddPolicy(com.azure.core.http.policy.HttpPipelinePolicy policy) Adds apipeline policyto apply on each request sent.audience(SearchAudience audience) Sets the Audience to use for authentication with Microsoft Entra ID.bufferedSender(com.azure.core.util.serializer.TypeReference<T> documentType) Create a new instance ofSearchClientBuilder.SearchIndexingBufferedSenderBuilderused to configureSearchIndexingBufferedSendersandSearchIndexingBufferedAsyncSenders.Creates aSearchAsyncClientbased on options set in the builder.Creates aSearchClientbased on options set in the builder.clientOptions(com.azure.core.util.ClientOptions clientOptions) Allows for setting common properties such as application ID, headers, proxy configuration, etc.configuration(com.azure.core.util.Configuration configuration) Sets the configuration store that is used during construction of the service client.credential(com.azure.core.credential.AzureKeyCredential credential) Sets theAzureKeyCredentialused to authenticate HTTP requests.credential(com.azure.core.credential.TokenCredential credential) Sets theTokenCredentialused to authorize requests sent to the service.Sets the service endpoint for the Azure AI Search instance.static com.azure.core.http.policy.HttpLogOptionsGets the default Azure Search headers and query parameters allow list.httpClient(com.azure.core.http.HttpClient client) Sets theHttpClientto use for sending and receiving requests to and from the service.httpLogOptions(com.azure.core.http.policy.HttpLogOptions logOptions) Sets thelogging configurationto use when sending and receiving requests to and from the service.Sets the name of the index.pipeline(com.azure.core.http.HttpPipeline httpPipeline) Sets theHttpPipelineto use for the service client.retryOptions(com.azure.core.http.policy.RetryOptions retryOptions) Sets theRetryOptionsfor all the requests made through the client.retryPolicy(com.azure.core.http.policy.RetryPolicy retryPolicy) Sets theHttpPipelinePolicythat will attempt to retry requests when needed.serializer(com.azure.core.util.serializer.JsonSerializer jsonSerializer) Custom JSON serializer that is used to handle model types that are not contained in the Azure Search Documents library.serviceVersion(SearchServiceVersion serviceVersion) Sets theSearchServiceVersionthat is used when making API requests.
-
Constructor Details
-
SearchClientBuilder
public SearchClientBuilder()Creates a builder instance that is able to configure and constructSearchClientsandSearchAsyncClients.
-
-
Method Details
-
buildClient
Creates aSearchClientbased on options set in the builder. Every timebuildClient()is called a new instance ofSearchClientis created.If
pipelineis set, then only thepipeline,endpoint, andindexNameare used to create theclient. All other builder settings are ignored.- Returns:
- A SearchClient with the options set from the builder.
- Throws:
NullPointerException- IfindexNameorendpointare null.IllegalStateException- If bothretryOptions(RetryOptions)andretryPolicy(RetryPolicy)have been set.
-
buildAsyncClient
Creates aSearchAsyncClientbased on options set in the builder. Every timebuildAsyncClient()is called a new instance ofSearchAsyncClientis created.If
pipelineis set, then only thepipeline,endpoint, andindexNameare used to create theclient. All other builder settings are ignored.- Returns:
- A SearchClient with the options set from the builder.
- Throws:
NullPointerException- IfindexNameorendpointare null.IllegalStateException- If bothretryOptions(RetryOptions)andretryPolicy(RetryPolicy)have been set.
-
bufferedSender
public <T> SearchClientBuilder.SearchIndexingBufferedSenderBuilder<T> bufferedSender(com.azure.core.util.serializer.TypeReference<T> documentType) Create a new instance ofSearchClientBuilder.SearchIndexingBufferedSenderBuilderused to configureSearchIndexingBufferedSendersandSearchIndexingBufferedAsyncSenders.- Type Parameters:
T- The type of the document that the buffered sender will use.- Parameters:
documentType- TheTypeReferencerepresenting the document type associated with the sender.- Returns:
- A new instance of
SearchClientBuilder.SearchIndexingBufferedSenderBuilder.
-
endpoint
Sets the service endpoint for the Azure AI Search instance.- Specified by:
endpointin interfacecom.azure.core.client.traits.EndpointTrait<SearchClientBuilder>- Parameters:
endpoint- The URL of the Azure AI Search instance.- Returns:
- The updated SearchClientBuilder object.0ed into a valid URL.
-
credential
Sets theAzureKeyCredentialused to authenticate HTTP requests.- Specified by:
credentialin interfacecom.azure.core.client.traits.AzureKeyCredentialTrait<SearchClientBuilder>- Parameters:
credential- TheAzureKeyCredentialused to authenticate HTTP requests.- Returns:
- The updated SearchClientBuilder object.
-
credential
Sets theTokenCredentialused to authorize requests sent to the service. Refer to the Azure SDK for Java identity and authentication documentation for more details on proper usage of theTokenCredentialtype.- Specified by:
credentialin interfacecom.azure.core.client.traits.TokenCredentialTrait<SearchClientBuilder>- Parameters:
credential-TokenCredentialused to authorize requests sent to the service.- Returns:
- The updated SearchClientBuilder object.
-
audience
Sets the Audience to use for authentication with Microsoft Entra ID.The audience is not considered when using a
shared key.If
audienceis null the public cloud audience will be assumed.- Parameters:
audience- The Audience to use for authentication with Microsoft Entra ID.- Returns:
- The updated SearchClientBuilder object.
-
indexName
Sets the name of the index.- Parameters:
indexName- Name of the index.- Returns:
- The updated SearchClientBuilder object.
- Throws:
IllegalArgumentException- IfindexNameis null or empty.
-
httpLogOptions
Sets thelogging configurationto use when sending and receiving requests to and from the service. If alogLevelis not provided, default value ofHttpLogDetailLevel.NONEis set.Note: It is important to understand the precedence order of the HttpTrait APIs. In particular, if a
HttpPipelineis specified, this takes precedence over all other APIs in the trait, and they will be ignored. If noHttpPipelineis specified, a HTTP pipeline will be constructed internally based on the settings provided to this trait. Additionally, there may be other APIs in types that implement this trait that are also ignored if anHttpPipelineis specified, so please be sure to refer to the documentation of types that implement this trait to understand the full set of implications.- Specified by:
httpLogOptionsin interfacecom.azure.core.client.traits.HttpTrait<SearchClientBuilder>- Parameters:
logOptions- Thelogging configurationto use when sending and receiving requests to and from the service.- Returns:
- The updated SearchClientBuilder object.
-
getDefaultLogOptions
public static com.azure.core.http.policy.HttpLogOptions getDefaultLogOptions()Gets the default Azure Search headers and query parameters allow list.- Returns:
- The default
HttpLogOptionsallow list.
-
clientOptions
Allows for setting common properties such as application ID, headers, proxy configuration, etc. Note that it is recommended that this method be called with an instance of theHttpClientOptionsclass (a subclass of theClientOptionsbase class). The HttpClientOptions subclass provides more configuration options suitable for HTTP clients, which is applicable for any class that implements this HttpTrait interface.Note: It is important to understand the precedence order of the HttpTrait APIs. In particular, if a
HttpPipelineis specified, this takes precedence over all other APIs in the trait, and they will be ignored. If noHttpPipelineis specified, a HTTP pipeline will be constructed internally based on the settings provided to this trait. Additionally, there may be other APIs in types that implement this trait that are also ignored if anHttpPipelineis specified, so please be sure to refer to the documentation of types that implement this trait to understand the full set of implications.- Specified by:
clientOptionsin interfacecom.azure.core.client.traits.HttpTrait<SearchClientBuilder>- Parameters:
clientOptions- A configured instance ofHttpClientOptions.- Returns:
- The updated SearchClientBuilder object.
- See Also:
-
addPolicy
Adds apipeline policyto apply on each request sent.Note: It is important to understand the precedence order of the HttpTrait APIs. In particular, if a
HttpPipelineis specified, this takes precedence over all other APIs in the trait, and they will be ignored. If noHttpPipelineis specified, a HTTP pipeline will be constructed internally based on the settings provided to this trait. Additionally, there may be other APIs in types that implement this trait that are also ignored if anHttpPipelineis specified, so please be sure to refer to the documentation of types that implement this trait to understand the full set of implications.- Specified by:
addPolicyin interfacecom.azure.core.client.traits.HttpTrait<SearchClientBuilder>- Parameters:
policy- Apipeline policy.- Returns:
- The updated SearchClientBuilder object.
- Throws:
NullPointerException- Ifpolicyis null.
-
serializer
Custom JSON serializer that is used to handle model types that are not contained in the Azure Search Documents library.- Parameters:
jsonSerializer- The serializer to serialize user defined models.- Returns:
- The updated SearchClientBuilder object.
-
httpClient
Sets theHttpClientto use for sending and receiving requests to and from the service.Note: It is important to understand the precedence order of the HttpTrait APIs. In particular, if a
HttpPipelineis specified, this takes precedence over all other APIs in the trait, and they will be ignored. If noHttpPipelineis specified, a HTTP pipeline will be constructed internally based on the settings provided to this trait. Additionally, there may be other APIs in types that implement this trait that are also ignored if anHttpPipelineis specified, so please be sure to refer to the documentation of types that implement this trait to understand the full set of implications.- Specified by:
httpClientin interfacecom.azure.core.client.traits.HttpTrait<SearchClientBuilder>- Parameters:
client- TheHttpClientto use for requests.- Returns:
- The updated SearchClientBuilder object.
-
pipeline
Sets theHttpPipelineto use for the service client.Note: It is important to understand the precedence order of the HttpTrait APIs. In particular, if a
HttpPipelineis specified, this takes precedence over all other APIs in the trait, and they will be ignored. If noHttpPipelineis specified, a HTTP pipeline will be constructed internally based on the settings provided to this trait. Additionally, there may be other APIs in types that implement this trait that are also ignored if anHttpPipelineis specified, so please be sure to refer to the documentation of types that implement this trait to understand the full set of implications.If
pipelineis set, all other settings are ignored, aside fromendpointandindexwhen building aSearchClientorSearchAsyncClient.- Specified by:
pipelinein interfacecom.azure.core.client.traits.HttpTrait<SearchClientBuilder>- Parameters:
httpPipeline-HttpPipelineto use for sending service requests and receiving responses.- Returns:
- The updated SearchClientBuilder object.
-
configuration
Sets the configuration store that is used during construction of the service client.The default configuration store is a clone of the
global configuration store, useConfiguration.NONEto bypass using configuration settings during construction.- Specified by:
configurationin interfacecom.azure.core.client.traits.ConfigurationTrait<SearchClientBuilder>- Parameters:
configuration- The configuration store that will be used.- Returns:
- The updated SearchClientBuilder object.
-
retryPolicy
Sets theHttpPipelinePolicythat will attempt to retry requests when needed.A default retry policy will be supplied if one isn't provided.
Setting this is mutually exclusive with using
retryOptions(RetryOptions).- Parameters:
retryPolicy- TheRetryPolicythat will attempt to retry requests when needed.- Returns:
- The updated SearchClientBuilder object.
-
retryOptions
Sets theRetryOptionsfor all the requests made through the client.Note: It is important to understand the precedence order of the HttpTrait APIs. In particular, if a
HttpPipelineis specified, this takes precedence over all other APIs in the trait, and they will be ignored. If noHttpPipelineis specified, a HTTP pipeline will be constructed internally based on the settings provided to this trait. Additionally, there may be other APIs in types that implement this trait that are also ignored if anHttpPipelineis specified, so please be sure to refer to the documentation of types that implement this trait to understand the full set of implications.Setting this is mutually exclusive with using
retryPolicy(RetryPolicy).- Specified by:
retryOptionsin interfacecom.azure.core.client.traits.HttpTrait<SearchClientBuilder>- Parameters:
retryOptions- TheRetryOptionsto use for all the requests made through the client.- Returns:
- The updated SearchClientBuilder object.
-
serviceVersion
Sets theSearchServiceVersionthat is used when making API requests.If a service version is not provided,
SearchServiceVersion.getLatest()will be used as a default. When the default is used, updating to a newer client library may implicitly use a newer version of the service.- Parameters:
serviceVersion- The version of the service to be used when making requests.- Returns:
- The updated SearchClientBuilder object.
-