Azure AI Search hosting integration
Dette indhold er ikke tilgængeligt i dit sprog endnu.
The Aspire Azure AI Search hosting integration models the search service as the AzureSearchResource type. To access this type and APIs for expressing them within your AppHost project, install the 📦 Aspire.Hosting.Azure.Search NuGet package:
aspire add azure-searchAspire CLI er interaktiv; vælg det passende søgeresultat når du bliver spurgt:
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="*" />For an introduction to working with the Azure AI Search hosting integration, see Get started with the Azure AI Search integration.
Add an Azure AI Search resource
Section titled “Add an Azure AI Search resource”To add an Azure AI Search resource to your AppHost project, call the AddAzureSearch method providing a name:
var builder = DistributedApplication.CreateBuilder(args);
var search = builder.AddAzureSearch("search");
builder.AddProject<Projects.ExampleProject>() .WithReference(search);
// After adding all resources, run the app...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.
Connect to an existing Azure AI Search instance
Section titled “Connect to an existing Azure AI Search instance”You might have an existing Azure AI Search service that you want to connect to. You can chain a call to annotate that your AzureSearchResource is an existing resource:
var builder = DistributedApplication.CreateBuilder(args);
var existingSearchName = builder.AddParameter("existingSearchName");var existingSearchResourceGroup = builder.AddParameter("existingSearchResourceGroup");
var search = builder.AddAzureSearch("search") .AsExisting(existingSearchName, existingSearchResourceGroup);
builder.AddProject<Projects.ExampleProject>() .WithReference(search);
// After adding all resources, run the app...For more information on treating Azure AI Search resources as existing resources, see Use existing Azure resources.
Provisioning-generated Bicep
Section titled “Provisioning-generated Bicep”If you’re new to Bicep, it’s a domain-specific language for defining Azure resources. With Aspire, you don’t need to write Bicep by hand; instead, the provisioning APIs generate Bicep for you. When you publish your app, the generated Bicep is output alongside the manifest file. When you add an Azure AI Search resource, Bicep is generated to provision the search service with appropriate defaults.
@description('The location for the resource(s) to be deployed.')param location string = resourceGroup().location
resource search 'Microsoft.Search/searchServices@2023-11-01' = { name: take('search-${uniqueString(resourceGroup().id)}', 60) location: location properties: { hostingMode: 'default' disableLocalAuth: true partitionCount: 1 replicaCount: 1 } sku: { name: 'basic' } tags: { 'aspire-resource-name': 'search' }}
output connectionString string = 'Endpoint=https://${search.name}.search.windows.net'
output name string = search.nameThe preceding Bicep is a module that provisions an Azure AI Search service resource. Additionally, role assignments are created for the Azure resource in a separate module:
@description('The location for the resource(s) to be deployed.')param location string = resourceGroup().location
param search_outputs_name string
param principalType string
param principalId string
resource search 'Microsoft.Search/searchServices@2023-11-01' existing = { name: search_outputs_name}
resource search_SearchIndexDataContributor 'Microsoft.Authorization/roleAssignments@2022-04-01' = { name: guid(search.id, principalId, subscriptionResourceId('Microsoft.Authorization/roleDefinitions', '8ebe5a00-799e-43f5-93ac-243d3dce84a7')) properties: { principalId: principalId roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', '8ebe5a00-799e-43f5-93ac-243d3dce84a7') principalType: principalType } scope: search}
resource search_SearchServiceContributor 'Microsoft.Authorization/roleAssignments@2022-04-01' = { name: guid(search.id, principalId, subscriptionResourceId('Microsoft.Authorization/roleDefinitions', '7ca78c08-252a-4471-8644-bb5ff32d4ba0')) properties: { principalId: principalId roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', '7ca78c08-252a-4471-8644-bb5ff32d4ba0') principalType: principalType } scope: search}The generated Bicep is a starting point and is influenced by changes to the provisioning infrastructure in C#. Customizations to the Bicep file directly will be overwritten, so make changes through the C# provisioning APIs to ensure they are reflected in the generated files.
Customize provisioning infrastructure
Section titled “Customize provisioning infrastructure”All Aspire Azure resources are subclasses of the AzureProvisioningResource type. This type enables the customization of the generated Bicep by providing a fluent API to configure the Azure resources using the ConfigureInfrastructure API:
var builder = DistributedApplication.CreateBuilder(args);
builder.AddAzureSearch("search") .ConfigureInfrastructure(infra => { var searchService = infra.GetProvisionableResources() .OfType<SearchService>() .Single();
searchService.PartitionCount = 6; searchService.ReplicaCount = 3; searchService.SearchSkuName = SearchServiceSkuName.Standard3; searchService.Tags.Add("ExampleKey", "Example value"); });The preceding code:
- Chains a call to the
ConfigureInfrastructureAPI:- The
infraparameter is an instance of theAzureResourceInfrastructuretype. - The provisionable resources are retrieved by calling the
GetProvisionableResourcesmethod. - The single
SearchServiceresource is retrieved.- The
PartitionCountis set to6. - The
ReplicaCountis set to3. - The
SearchSkuNameis set toStandard3. - A tag is added to the Cognitive Services resource with a key of
ExampleKeyand a value ofExample value.
- The
- The
There are many more configuration options available to customize the Search resource. For more information, see Azure.Provisioning customization.
Connection properties
Section titled “Connection properties”When you reference an Azure AI Search service using WithReference, the following connection properties are made available to the consuming project:
| Property Name | Description |
|---|---|
Uri | The HTTPS endpoint of the Azure AI Search service in the format https://{name}.search.windows.net. |