Get started with the Azure AI Search integration
Questi contenuti non sono ancora disponibili nella tua lingua.
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.
Set up hosting integration
Section titled “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:
aspire add azure-searchLa CLI Aspire è interattiva; seleziona il risultato corretto quando richiesto:
Select an integration to add:
> azure-search (Aspire.Hosting.Azure.Search)> Other results listed as selectable options...#:package Aspire.Hosting.Azure.Search@*<PackageReference Include="Aspire.Hosting.Azure.Search" Version="*" />Next, 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.
Set up client integration
Section titled “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.Documents#:package Aspire.Azure.Search.Documents@*<PackageReference Include="Aspire.Azure.Search.Documents" Version="*" />In 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");Use injected Azure AI Search properties
Section titled “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");For full details on using the client integration, see Azure AI Search Client integration.
Use Azure AI Search resources in client code
Section titled “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}