Orchard Core Azure AI Search - Prompt Templates
Configure Azure AI Search
You are an Orchard Core expert. Generate code and configuration for Azure AI Search integration in Orchard Core.
Guidelines
- Enable
OrchardCore.AzureAIto manage Azure AI Search indices (OrchardCore.Search.AzureAIis an obsolete feature id kept for compatibility). - Configure the Azure AI Search connection in
appsettings.jsonunderOrchardCore:Search:AzureAISearch(Orchard Core 4.0;OrchardCore_AzureAISearchis deprecated but still read) or through the admin UI at Settings > Search > Azure AI Search. - Read the effective connection settings with
IOptionsMonitor<AzureAISearchDefaultOptions>.CurrentValue. Admin settings changes refresh the monitor without a shell release, so a cachedIOptions<AzureAISearchDefaultOptions>.Valuegoes stale. - Use the
CreateOrUpdateIndexProfilerecipe step to create indexes (preferred over the obsoleteazureai-index-createstep). - Use the
ResetIndexrecipe step to restart indexing without deleting entries (preferred over the obsoleteazureai-index-resetstep). - Use the
RebuildIndexrecipe step to delete and recreate indexes (preferred over the obsoleteazureai-index-rebuildstep). - Supported authentication types:
ApiKey,Default,ManagedIdentity. - Use
IndexesPrefixto prevent naming conflicts when sharing an Azure AI Search instance across environments. - Enable
OrchardCore.Searchalongside Azure AI Search for frontend search functionality. - Azure AI Search uses a different field naming convention with double underscores (e.g.,
Content__ContentItem__FullText). - All recipe JSON must be wrapped in
{ "steps": [...] }. - All C# classes must use the
sealedmodifier.
Enabling Azure AI Search Features
{
"steps": [
{
"name": "Feature",
"enable": [
"OrchardCore.Search",
"OrchardCore.AzureAI",
"OrchardCore.Indexing"
],
"disable": []
}
]
}
Azure AI Search Connection Configuration
Configure in appsettings.json (for all tenants, or per tenant through any configuration source):
{
"OrchardCore": {
"Search": {
"AzureAISearch": {
"Endpoint": "https://[search-service-name].search.windows.net",
"IndexesPrefix": "",
"AuthenticationType": "ApiKey",
"IdentityClientId": null,
"DisableUIConfiguration": false,
"Credential": {
"Key": "your-api-key-here"
}
}
}
}
}
Environment variable example: OrchardCore__Search__AzureAISearch__Credential__Key.
| Property | Description |
|----------|-------------|
| Endpoint | The Azure AI Search service endpoint URL. |
| IndexesPrefix | Prefix for all index names. Use environment names (e.g., "staging-") to prevent conflicts when sharing a single Azure AI Search instance. |
| AuthenticationType | "ApiKey" for key-based, "Default" for default Azure credentials, or "ManagedIdentity" for managed identity. |
| IdentityClientId | Optional client ID for user-assigned managed identity. Omit for system-assigned identity. |
| DisableUIConfiguration | When true, disables per-tenant UI configuration and forces all tenants to use appsettings values. |
| Credential.Key | The API key for the Azure AI Search service. Required for ApiKey authentication. |
In 4.0 a configured Credential:Key logs a LegacySecretConfiguration
warning (event 8100); it still works. To keep the key in the Secrets module,
use the admin settings (with DisableUIConfiguration left false), check
Use custom Azure Search AI settings (UseCustomConfiguration) and choose
ApiKey authentication, then Use a secret
in the API key editor (suggested name AzureAISearch.ApiKey). It sets
AzureAISearchDefaultSettings.ApiKeySecretName, which takes precedence over
the protected ApiKey. A saved key can be moved without retyping it from
Tools → Security → Migrate Secrets. Configuration-only credentials have no
secret reference. See orchardcore-secrets.
Reading the Connection Options in Code
using Microsoft.Extensions.Options;
using OrchardCore.AzureAI.Models;
public sealed class AzureSearchStatusService
{
private readonly IOptionsMonitor<AzureAISearchDefaultOptions> _options;
public AzureSearchStatusService(IOptionsMonitor<AzureAISearchDefaultOptions> options)
{
_options = options;
}
// Always read CurrentValue: admin settings changes update it after the session commits.
public bool IsConfigured()
=> _options.CurrentValue.ConfigurationExists();
public string GetIndexesPrefix()
=> _options.CurrentValue.IndexesPrefix;
}
AzureAIClientFactory (namespace OrchardCore.AzureAI.Services) also reads IOptionsMonitor<AzureAISearchDefaultOptions> and recreates its clients when the endpoint or credentials change. Use CreateSearchClient(indexFullName) and CreateSearchIndexClient() instead of building SDK clients yourself.
Creating an Azure AI Search Index Profile (Recommended)
{
"steps": [
{
"name": "CreateOrUpdateIndexProfile",
"indexes": [
{
"Name": "BlogPostsAI",
"IndexName": "blogposts",
"ProviderName": "AzureAISearch",
"Type": "Content",
"Properties": {
"ContentIndexMetadata": {
"IndexLatest": false,
"IndexedContentTypes": ["BlogPost"],
"Culture": "any"
},
"AzureAISearchIndexMetadata": {
"AnalyzerName": "standard"
},
"AzureAISearchDefaultQueryMetadata": {
"QueryAnalyzerName": "standard.lucene",
"DefaultSearchFields": [
"Content__ContentItem__FullText"
]
}
}
}
]
}
]
}
| Property | Description |
|----------|-------------|
| Name | Unique display name for the index profile. |
| IndexName | Physical index name in Azure AI Search. |
| ProviderName | Must be "AzureAISearch". |
| Type | Source category, typically "Content" for content items. |
| IndexLatest | When true, indexes both published and draft items. |
| IndexedContentTypes | Array of content type names to include. |
| Culture | "any" for all cultures or a specific culture code. |
| AnalyzerName | Azure AI Search analyzer (e.g., "standard", "standard.lucene"). |
| QueryAnalyzerName | Analyzer used at query time. |
| DefaultSearchFields | Fields to search across. Uses double underscores as separators (e.g., Content__ContentItem__FullText). |
Legacy Index Creation (Obsolete)
{
"steps": [
{
"name": "azureai-index-create",
"Indices": [
{
"Source": "Contents",
"IndexName": "articles",
"IndexLatest": false,
"IndexedContentTypes": ["Article"],
"AnalyzerName": "standard.lucene",
"Culture": "any"
}
]
}
]
}
Reset Azure AI Search Index
Restarts indexing from the beginning without deleting existing entries:
{
"steps": [
{
"name": "ResetIndex",
"indexNames": [
"BlogPostsAI"
]
}
]
}
To reset all indices:
{
"steps": [
{
"name": "ResetIndex",
"IncludeAll": true
}
]
}
Rebuild Azure AI Search Index
Deletes and recreates the full index content:
{
"steps": [
{
"name": "RebuildIndex",
"indexNames": [
"BlogPostsAI"
]
}
]
}
To rebuild all indices:
{
"steps": [
{
"name": "RebuildIndex",
"IncludeAll": true
}
]
}
Indexing Custom Data
Register a custom indexing source in Startup.cs:
using Microsoft.Extensions.Localization;
using OrchardCore.Modules;
namespace MyModule;
[Feature("MyModule.CustomAzureAIIndex")]
public sealed class Startup : StartupBase
{
private readonly IStringLocalizer S;
public Startup(IStringLocalizer<Startup> localizer)
{
S = localizer;
}
public override void ConfigureServices(IServiceCollection services)
{
services.AddAzureAISearchIndexingSource("CustomSource", o =>
{
o.DisplayName = S["Custom Source"];
o.Description = S["Create an Azure AI Search index based on a custom data source."];
});
}
}
For custom index field mapping, implement IIndexProfileHandler and populate AzureAISearchIndexMetadata with IndexMappings in the CreatingAsync and UpdatingAsync methods. Use IndexProfileHandlerBase to simplify the implementation, and register it with services.AddIndexProfileHandler<THandler>().
To capture custom UI data related to your source, implement DisplayDriver<IndexProfile>.
Indexing Vector Fields
If a custom source emits embeddings, add a vector field to AzureAISearchIndexMetadata.IndexMappings and define the vector search profile in AzureAISearchIndexMetadata.VectorSearchMappings:
using OrchardCore.AzureAI;
using OrchardCore.AzureAI.Models;
using OrchardCore.Entities;
using OrchardCore.Indexing;
using OrchardCore.Indexing.Core.Handlers;
using OrchardCore.Indexing.Models;
using static OrchardCore.Indexing.DocumentIndex;
public sealed class ProductIndexProfileHandler : IndexProfileHandlerBase
{
public override Task CreatingAsync(CreatingContext<IndexProfile> context)
{
Configure(context.Model);
return Task.CompletedTask;
}
public override Task UpdatingAsync(UpdatingContext<IndexProfile> context)
{
Configure(context.Model);
return Task.CompletedTask;
}
private static void Configure(IndexProfile index)
{
var metadata = index.GetOrCreate<AzureAISearchIndexMetadata>();
metadata.IndexMappings.Clear();
metadata.IndexMappings.Add(new AzureAISearchIndexMap("documentId", Types.Text)
{
IndexingKey = "DocumentId",
IsKey = true,
IsFilterable = true,
IsSortable = true,
});
metadata.IndexMappings.Add(new AzureAISearchIndexMap("title", Types.Text)
{
IndexingKey = "Title",
IsSearchable = true,
});
metadata.IndexMappings.Add(new AzureAISearchIndexMap("embedding", Types.Vector)
{
AzureFieldKey = "Embedding",
IsSearchable = true,
VectorInfo = new AzureAISearchIndexMapVectorInfo
{
Dimensions = 1536,
VectorSearchConfiguration = "products-vector",
},
});
metadata.VectorSearchMappings = new VectorSearchMappings
{
Profiles =
[
new VectorSearchProfileMap { Name = "default", AlgorithmConfigurationName = "products-vector" },
],
Algorithms =
[
new VectorSearchAlgorithmMap { Name = "products-vector", Kind = VectorSearchAlgorithmMap.HnswKind },
],
};
index.Put(metadata);
}
}
Write each embedding with documentIndex.Set("embedding", embedding, embedding.Length, DocumentIndexOptions.Store). Pass new Dictionary<string, object> { ["VectorSearchConfiguration"] = "products-vector" } as a fifth argument to choose a profile per field; without it, the first configured profile (or the built-in default) is used.
Querying Vector Fields
ISearchService and the Site Search UI run full-text search only. For vector or hybrid queries, use the SDK through AzureAIClientFactory:
using Azure.Search.Documents;
using Azure.Search.Documents.Models;
using OrchardCore.AzureAI;
using OrchardCore.AzureAI.Services;
using OrchardCore.Indexing;
public sealed class ProductVectorSearchService
{
private readonly AzureAIClientFactory _clientFactory;
private readonly IIndexProfileStore _indexProfileStore;
public ProductVectorSearchService(AzureAIClientFactory clientFactory, IIndexProfileStore indexProfileStore)
{
_clientFactory = clientFactory;
_indexProfileStore = indexProfileStore;
}
public async Task<IReadOnlyList<SearchDocument>> SearchAsync(float[] embedding, CancellationToken cancellationToken)
{
var index = await _indexProfileStore.FindByIndexNameAndProviderAsync("products", AzureAISearchConstants.ProviderName)
?? throw new InvalidOperationException("The 'products' Azure AI Search index profile doesn't exist.");
var client = _clientFactory.CreateSearchClient(index.IndexFullName);
var options = new SearchOptions
{
Size = 5,
Select = { "documentId", "title" },
VectorSearch = new VectorSearchOptions
{
Queries =
{
new VectorizedQuery(embedding) { Fields = { "embedding" }, KNearestNeighborsCount = 5 },
},
},
};
// Replace "*" with a text query for hybrid search.
var response = await client.SearchAsync<SearchDocument>("*", options, cancellationToken);
var documents = new List<SearchDocument>();
await foreach (var result in response.Value.GetResultsAsync())
{
documents.Add(result.Document);
}
return documents;
}
}
Frontend Search Integration
When OrchardCore.Search is enabled alongside Azure AI Search:
- Navigate to Settings > Search > Site Search in the admin dashboard.
- Select the Azure AI Search index as the default search provider.
- The
/searchroute becomes available for frontend search. - Configure search permissions per role to control which indexes are queryable by anonymous or authenticated users.
Permissions
| Permission | Member | Notes |
|------------|--------|-------|
| ManageAzureAISearchSettings | OrchardCore.AzureAI.AzureAISearchPermissions.ManageAzureAISearchSettings | Edit the Azure AI Search settings and the per-field index settings. |
| ManageIndexes | OrchardCore.Indexing.Core.IndexingPermissions.ManageIndexes | Manage index profiles under Search > Indexes. |
| QuerySearchIndex / QueryIndex_{ProfileName} | IndexingPermissions.QuerySearchIndex / IndexingPermissions.CreateDynamicPermission(indexProfile) | Front-end search on any index or on one index. |
Upgrading from 3.x
- Rename the
OrchardCore_AzureAISearchsection toOrchardCore:Search:AzureAISearch(the old name is still read; the new one wins when both exist). - Replace
IOptions<AzureAISearchDefaultOptions>withIOptionsMonitor<AzureAISearchDefaultOptions>and readCurrentValue; settings changes no longer release the shell. - The 3.x permission
AzureAISearchPermissions.ManageAzureAISearchISettings(stored nameManageAzureAISearchISettings) is nowManageAzureAISearchSettings, for both the member and the stored name. Re-grant Manage Azure AI Search Settings to non-administrator roles that had it. OrchardCore.Search.Permissions.QuerySearchIndexwas removed; useIndexingPermissions.QuerySearchIndex.- Saved API keys keep working.
AzureAISearchDefaultSettingsgainsApiKeySecretName; code readingApiKeydirectly must honor it, or readIOptionsMonitor<AzureAISearchDefaultOptions>.