Перейти до вмісту

Azure AI Search client integration

Цей контент ще не доступний вашою мовою.

Azure AI Search logo

The Aspire Azure AI Search client integration is used to connect to an Azure AI Search service using the SearchIndexClient. To get started with the Aspire Azure AI Search client integration, install the 📦 Aspire.Azure.Search.Documents NuGet package.

.NET CLI — Add Aspire.Azure.Search.Documents package
dotnet add package Aspire.Azure.Search.Documents

For an introduction to working with the Azure AI Search client integration, see Get started with the Azure AI Search integration.

In the Program.cs file of your client-consuming project, call the AddAzureSearchClient extension method on any IHostApplicationBuilder to register a SearchIndexClient class for use via the dependency injection container. The method takes a connection name parameter.

builder.AddAzureSearchClient(connectionName: "search");

After adding the SearchIndexClient, you can retrieve the client instance using dependency injection. For example, to retrieve the client instance from an example service:

public class ExampleService(SearchIndexClient indexClient)
{
// Use indexClient
}

You can also retrieve a SearchClient which can be used for querying, by calling the GetSearchClient method:

public class ExampleService(SearchIndexClient indexClient)
{
public async Task<long> GetDocumentCountAsync(
string indexName,
CancellationToken cancellationToken)
{
var searchClient = indexClient.GetSearchClient(indexName);
var documentCountResponse = await searchClient.GetDocumentCountAsync(
cancellationToken);
return documentCountResponse.Value;
}
}

For more information, see:

Properties of the Azure AI Search resources

Section titled “Properties of the Azure AI Search resources”

When you use the WithReference method to pass an Azure AI Search resource from the AppHost project to a consuming client project, several properties are available to use in the consuming project.

Aspire exposes each property as an environment variable named [RESOURCE]_[PROPERTY]. For instance, the Uri property of a resource called search becomes SEARCH_URI.

The Azure AI Search resource exposes the following connection property:

Property NameDescription
UriThe service endpoint URI (e.g., https://{name}.search.windows.net)

For example, if you reference a search resource named search in your AppHost project, the following environment variables will be available in the consuming project:

  • SEARCH_URI

There might be situations where you want to register multiple SearchIndexClient instances with different connection names. To register keyed Azure AI Search clients, call the AddKeyedAzureSearchClient method:

builder.AddKeyedAzureSearchClient(name: "images");
builder.AddKeyedAzureSearchClient(name: "documents");

Then you can retrieve the client instances using dependency injection. For example, to retrieve the clients from a service:

public class ExampleService(
[KeyedService("images")] SearchIndexClient imagesClient,
[KeyedService("documents")] SearchIndexClient documentsClient)
{
// Use clients...
}

For more information, see Keyed services in .NET.

The Azure AI Search Documents library provides multiple options to configure the Azure AI Search connection based on the requirements and conventions of your project. Either an Endpoint or a ConnectionString is required to be supplied.

A connection can be constructed from the Keys and Endpoint tab with the format Endpoint={endpoint};Key={key};. You can provide the name of the connection string when calling builder.AddAzureSearchClient():

builder.AddAzureSearchClient("searchConnectionName");

The connection string is retrieved from the ConnectionStrings configuration section. Two connection formats are supported:

The recommended approach is to use an Endpoint, which works with the AzureSearchSettings.Credential property to establish a connection. If no credential is configured, the DefaultAzureCredential is used.

{
"ConnectionStrings": {
"search": "https://{search_service}.search.windows.net/"
}
}

Alternatively, a connection string with key can be used, however; it’s not the recommended approach:

{
"ConnectionStrings": {
"search": "Endpoint=https://{search_service}.search.windows.net/;Key={account_key};"
}
}

The Azure AI Search library supports Microsoft.Extensions.Configuration. It loads the AzureSearchSettings and SearchClientOptions from configuration by using the Aspire:Azure:Search:Documents key. Example appsettings.json that configures some of the options:

{
"Aspire": {
"Azure": {
"Search": {
"Documents": {
"DisableTracing": false
}
}
}
}
}

For the complete Azure AI Search Documents client integration JSON schema, see Aspire.Azure.Search.Documents/ConfigurationSchema.json.

The Azure AI Search library supports named configuration, which allows you to configure multiple instances of the same resource type with different settings. The named configuration uses the connection name as a key under the main configuration section.

{
"Aspire": {
"Azure": {
"Search": {
"Documents": {
"search1": {
"Endpoint": "https://mysearch1.search.windows.net/",
"DisableTracing": false
},
"search2": {
"Endpoint": "https://mysearch2.search.windows.net/",
"DisableTracing": true
}
}
}
}
}
}

In this example, the search1 and search2 connection names can be used when calling AddAzureSearchClient:

builder.AddAzureSearchClient("search1");
builder.AddAzureSearchClient("search2");

Named configuration takes precedence over the top-level configuration. If both are provided, the settings from the named configuration override the top-level settings.

You can also pass the Action<AzureSearchSettings> configureSettings delegate to set up some or all the options inline, for example to disable tracing from code:

builder.AddAzureSearchClient(
"searchConnectionName",
static settings => settings.DisableTracing = true);

You can also set up the SearchClientOptions using the optional Action<IAzureClientBuilder<SearchIndexClient, SearchClientOptions>> configureClientBuilder parameter of the AddAzureSearchClient method. For example, to set the client ID for this client:

builder.AddAzureSearchClient(
"searchConnectionName",
configureClientBuilder: builder => builder.ConfigureOptions(
static options => options.Diagnostics.ApplicationId = "CLIENT_ID"));

The Azure AI Search Documents integration implements a single health check that calls the GetServiceStatisticsAsync method on the SearchIndexClient to verify that the service is available.

Aspire integrations automatically set up Logging, Tracing, and Metrics configurations, which are sometimes known as the pillars of observability. For more information about integration observability and telemetry, see Aspire integrations overview. Depending on the backing service, some integrations may only support some of these features. For example, some integrations support logging and tracing, but not metrics. Telemetry features can also be disabled using the techniques presented in the Configuration section.

The Azure AI Search Documents integration uses the following log categories:

  • Azure
  • Azure.Core
  • Azure.Identity

The Azure AI Search Documents integration emits tracing activities using OpenTelemetry when interacting with the search service.