Get started with the Azure AI Search integration
Azure AI Search is an enterprise-ready information retrieval system for your heterogeneous content that you ingest into a search index, and surface to users through queries and apps. It comes with a comprehensive set of advanced search technologies, built for high-performance applications at any scale. The Aspire Azure AI Search integration enables you to connect to Azure AI Search services from your applications.
In this introduction, you'll see how to install and use the Aspire Azure AI Search integrations in a simple configuration. If you already have this knowledge, see Azure AI Search Hosting integration for full reference details.
Note
To follow this guide, you must have created an Aspire solution to work with. To learn how to do that, see Build your first Aspire app.
Set up hosting integration
To begin, install the Aspire Azure AI Search Hosting integration in your Aspire AppHost project. This integration allows you to create and manage Azure AI Search resources from your Aspire hosting projects:
dotnet add package Aspire.Hosting.Azure.SearchNext, in the AppHost project, create an Azure AI Search resource and pass it to the consuming client projects:
var builder = DistributedApplication.CreateBuilder(args);
var search = builder.AddAzureSearch("search");
builder.AddProject<Projects.ExampleProject>()
.WithReference(search);
// After adding all resources, run the app...
builder.Build().Run();
The preceding code adds an Azure AI Search resource named search to the AppHost project. The WithReference method passes the connection information to the ExampleProject project.
Caution
When you call AddAzureSearch, it implicitly calls
AddAzureProvisioning—which adds support for generating Azure resources
dynamically during app startup. The app must configure the appropriate
subscription and location. For more information, see Local provisioning:
Configuration.
Tip
This is the simplest implementation of Azure AI Search resources in the AppHost. There are many more options you can choose from to address your requirements. For full details, see Azure AI Search Hosting integration.
Set up client integration
To use Azure AI Search from your client applications, install the Aspire Azure AI Search client integration in your client project:
dotnet add package Aspire.Azure.Search.DocumentsIn the Program.cs file of your client-consuming project, call the AddAzureSearchClient extension method to register a SearchIndexClient for use via the dependency injection container:
builder.AddAzureSearchClient(connectionName: "search");
Tip
The connectionName parameter must match the name used when adding the Azure AI Search resource in the AppHost project.
Use injected Azure AI Search properties
In the AppHost, when you used the WithReference method to pass an Azure AI Search resource to a consuming client project, Aspire injects several configuration properties that you can 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.
Use the GetValue() method to obtain these environment variables in consuming projects:
string searchUri = builder.Configuration.GetValue<string>("SEARCH_URI");
Tip
The full set of properties that Aspire injects is available in the client integration documentation. For more information, see Properties of the Azure AI Search resources.
For full details on using the client integration, see Azure AI Search Client integration.
Use Azure AI Search resources in client code
After adding the SearchIndexClient, you can retrieve the client instance using dependency injection:
public class ExampleService(SearchIndexClient indexClient)
{
// Use indexClient
}