Gå til indhold

Get started with the Azure AI Search integration

Dette indhold er ikke tilgængeligt i dit sprog endnu.

Azure AI Search logo

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.

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 CLI — Tilføj Aspire.Hosting.Azure.Search-pakke
aspire add azure-search

Aspire CLI er interaktiv; vælg det passende søgeresultat når du bliver spurgt:

Aspire CLI — Eksempel output
Select an integration to add:
> azure-search (Aspire.Hosting.Azure.Search)
> Other results listed as selectable options...

Next, in the AppHost project, create an Azure AI Search resource and pass it to the consuming client projects:

C# — AppHost.cs
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.

To use Azure AI Search from your client applications, install the Aspire Azure AI Search client integration in your client project:

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

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");

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:

C# — Obtain configuration properties
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
}