Skip to content

Commit

Permalink
Simplify AzureAI Search events (#17577)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek authored Mar 8, 2025
1 parent 0695f34 commit 6d2faf3
Show file tree
Hide file tree
Showing 15 changed files with 106 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public async Task<IActionResult> CreatePost(string source)
{
try
{
settings.IndexMappings = await _azureAIIndexDocumentManager.GetMappingsAsync(settings);
await _indexSettingsService.SetMappingsAsync(settings);

if (await _indexManager.CreateAsync(settings))
{
Expand Down Expand Up @@ -328,7 +328,7 @@ public async Task<IActionResult> EditPost(string id)
{
try
{
settings.IndexMappings = await _azureAIIndexDocumentManager.GetMappingsAsync(settings);
await _indexSettingsService.SetMappingsAsync(settings);

if (!await _indexManager.CreateAsync(settings))
{
Expand Down Expand Up @@ -420,7 +420,7 @@ public async Task<IActionResult> Rebuild(string id)
return NotFound();
}

settings.IndexMappings = await _azureAIIndexDocumentManager.GetMappingsAsync(settings);
await _indexSettingsService.SetMappingsAsync(settings);
await _indexSettingsService.ResetAsync(settings);
await _indexSettingsService.UpdateAsync(settings);
await _indexManager.RebuildAsync(settings);
Expand Down Expand Up @@ -458,7 +458,8 @@ public async Task<IActionResult> Reset(string id)
return RedirectToAction(nameof(Index));
}

settings.IndexMappings = await _azureAIIndexDocumentManager.GetMappingsAsync(settings);
await _indexSettingsService.SetMappingsAsync(settings);
await _indexSettingsService.ResetAsync(settings);
await _indexSettingsService.UpdateAsync(settings);
await _indexSettingsService.SynchronizeAsync(settings);
await _notifier.SuccessAsync(H["Index <em>{0}</em> reset successfully.", settings.IndexName]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ public ContentsStartup(IStringLocalizer<ContentsStartup> stringLocalizer)

public override void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IAzureAISearchEvents, ContentAzureAISearchEvents>();
services.AddDisplayDriver<AzureAISearchIndexSettings, ContentAzureAISearchIndexSettingsDisplayDriver>();
services.AddScoped<IAzureAISearchIndexSettingsHandler, ContentAzureAISearchIndexHandler>();
services.Configure<AzureAISearchOptions>(options =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,39 @@
using System.Text.Json.Nodes;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using OrchardCore.BackgroundJobs;
using OrchardCore.ContentManagement;
using OrchardCore.Contents.Indexing;
using OrchardCore.Entities;
using OrchardCore.Indexing;
using OrchardCore.Modules;
using OrchardCore.Search.AzureAI.Models;
using OrchardCore.Search.AzureAI.Services;
using static OrchardCore.Indexing.DocumentIndexBase;

namespace OrchardCore.Search.AzureAI.Handlers;

public sealed class ContentAzureAISearchIndexHandler : AzureAISearchIndexSettingsHandlerBase
{
private readonly IContentManager _contentManager;
private readonly IEnumerable<IContentItemIndexHandler> _contentItemIndexHandlers;
private readonly IEnumerable<IAzureAISearchFieldIndexEvents> _fieldIndexEvents;
private readonly ILogger _logger;

private readonly IStringLocalizer S;

public ContentAzureAISearchIndexHandler(IStringLocalizer<ContentAzureAISearchIndexHandler> stringLocalizer)
public ContentAzureAISearchIndexHandler(
IContentManager contentManager,
IEnumerable<IContentItemIndexHandler> contentItemIndexHandlers,
IEnumerable<IAzureAISearchFieldIndexEvents> fieldIndexEvents,
ILogger<ContentAzureAISearchIndexHandler> logger,
IStringLocalizer<ContentAzureAISearchIndexHandler> stringLocalizer)
{
_contentManager = contentManager;
_contentItemIndexHandlers = contentItemIndexHandlers;
_fieldIndexEvents = fieldIndexEvents;
_logger = logger;
S = stringLocalizer;
}

Expand Down Expand Up @@ -87,6 +107,11 @@ public override Task ValidatingAsync(AzureAISearchIndexSettingsValidatingContext

public override Task ResetAsync(AzureAISearchIndexSettingsResetContext context)
{
if (!string.Equals(AzureAISearchConstants.ContentsIndexSource, context.Settings.Source, StringComparison.OrdinalIgnoreCase))
{
return Task.CompletedTask;
}

context.Settings.SetLastTaskId(0);

return Task.CompletedTask;
Expand Down Expand Up @@ -129,4 +154,50 @@ public override Task ExportingAsync(AzureAISearchIndexSettingsExportingContext c

return Task.CompletedTask;
}

public override async Task MappingAsync(AzureAISearchMappingContext context)
{
if (!string.Equals(AzureAISearchConstants.ContentsIndexSource, context.Settings.Source, StringComparison.OrdinalIgnoreCase))
{
return;
}
var metadata = context.Settings.As<ContentIndexMetadata>();

foreach (var contentType in metadata.IndexedContentTypes ?? [])
{
var contentItem = await _contentManager.NewAsync(contentType);
var index = new DocumentIndex(contentItem.ContentItemId, contentItem.ContentItemVersionId);
var buildIndexContext = new BuildIndexContext(index, contentItem, [contentType], new AzureAISearchContentIndexSettings());
await _contentItemIndexHandlers.InvokeAsync(x => x.BuildIndexAsync(buildIndexContext), _logger);

await AddIndexMappingAsync(context.Settings.IndexMappings, IndexingConstants.ContentItemIdKey, new DocumentIndexEntry(IndexingConstants.ContentItemIdKey, contentItem.ContentItemId, Types.Text, DocumentIndexOptions.Keyword), context.Settings);
await AddIndexMappingAsync(context.Settings.IndexMappings, IndexingConstants.ContentItemVersionIdKey, new DocumentIndexEntry(IndexingConstants.ContentItemVersionIdKey, contentItem.ContentItemId, Types.Text, DocumentIndexOptions.Keyword), context.Settings);

foreach (var entry in index.Entries)
{
if (!AzureAISearchIndexNamingHelper.TryGetSafeFieldName(entry.Name, out var safeFieldName))
{
continue;
}

await AddIndexMappingAsync(context.Settings.IndexMappings, safeFieldName, entry, context.Settings);
}
}
}

private async Task AddIndexMappingAsync(IList<AzureAISearchIndexMap> indexMappings, string safeFieldName, DocumentIndexEntry entry, AzureAISearchIndexSettings settings)
{
var indexMap = new AzureAISearchIndexMap(safeFieldName, entry.Type, entry.Options)
{
IndexingKey = entry.Name,
};

var context = new SearchIndexDefinition(indexMap, entry, settings);

await _fieldIndexEvents.InvokeAsync((handler, ctx) => handler.MappingAsync(ctx), context, _logger);

await _fieldIndexEvents.InvokeAsync((handler, ctx) => handler.MappedAsync(ctx), context, _logger);

indexMappings.Add(indexMap);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public interface IAzureAISearchIndexSettingsHandler

Task ValidatingAsync(AzureAISearchIndexSettingsValidatingContext context);

Task MappingAsync(AzureAISearchMappingContext context);

Task ResetAsync(AzureAISearchIndexSettingsResetContext context);

Task SynchronizedAsync(AzureAISearchIndexSettingsSynchronizedContext context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ public class AzureAISearchMappingContext
{
public readonly AzureAISearchIndexSettings Settings;

public readonly List<AzureAISearchIndexMap> Mappings = [];

public AzureAISearchMappingContext(AzureAISearchIndexSettings settings)
{
ArgumentNullException.ThrowIfNull(settings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ protected override async Task HandleAsync(RecipeExecutionContext context)

await HttpBackgroundJob.ExecuteAfterEndOfRequestAsync(AzureAISearchIndexRebuildDeploymentSource.Name, async scope =>
{
var searchIndexingService = scope.ServiceProvider.GetService<AzureAISearchIndexingService>();
var indexSettingsService = scope.ServiceProvider.GetService<AzureAISearchIndexSettingsService>();
var indexDocumentManager = scope.ServiceProvider.GetRequiredService<AzureAIIndexDocumentManager>();
var indexManager = scope.ServiceProvider.GetRequiredService<AzureAISearchIndexManager>();
Expand All @@ -42,14 +41,12 @@ await HttpBackgroundJob.ExecuteAfterEndOfRequestAsync(AzureAISearchIndexRebuildD

foreach (var settings in indexSettings)
{
settings.SetLastTaskId(0);
settings.IndexMappings = await indexDocumentManager.GetMappingsAsync(settings);
await indexSettingsService.SetMappingsAsync(settings);
await indexSettingsService.ResetAsync(settings);
await indexSettingsService.UpdateAsync(settings);

await indexManager.RebuildAsync(settings);
await indexSettingsService.SynchronizeAsync(settings);
}

await searchIndexingService.ProcessContentItemsAsync(indexSettings.Select(settings => settings.IndexName).ToArray());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ protected override async Task HandleAsync(RecipeExecutionContext context)

await HttpBackgroundJob.ExecuteAfterEndOfRequestAsync(AzureAISearchIndexRebuildDeploymentSource.Name, async scope =>
{
var searchIndexingService = scope.ServiceProvider.GetService<AzureAISearchIndexingService>();
var indexSettingsService = scope.ServiceProvider.GetService<AzureAISearchIndexSettingsService>();
var indexManager = scope.ServiceProvider.GetRequiredService<AzureAISearchIndexManager>();
var indexDocumentManager = scope.ServiceProvider.GetRequiredService<AzureAIIndexDocumentManager>();
Expand All @@ -42,20 +41,18 @@ await HttpBackgroundJob.ExecuteAfterEndOfRequestAsync(AzureAISearchIndexRebuildD

foreach (var settings in indexSettings)
{
settings.SetLastTaskId(0);
settings.IndexMappings = await indexDocumentManager.GetMappingsAsync(settings);

await indexSettingsService.SetMappingsAsync(settings);
await indexSettingsService.ResetAsync(settings);
await indexSettingsService.UpdateAsync(settings);
if (!await indexManager.ExistsAsync(settings.IndexName))
{
settings.IndexFullName = indexManager.GetFullIndexName(settings.IndexName);

await indexManager.CreateAsync(settings);
}

await indexSettingsService.UpdateAsync(settings);
await indexSettingsService.SynchronizeAsync(settings);
}

await searchIndexingService.ProcessContentItemsAsync(indexSettings.Select(settings => settings.IndexName).ToArray());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,17 @@ public sealed class AzureAISearchIndexSettingsStep : NamedRecipeStepHandler
public const string Name = "azureai-index-create";

private readonly AzureAISearchIndexManager _indexManager;
private readonly AzureAIIndexDocumentManager _azureAIIndexDocumentManager;
private readonly AzureAISearchIndexSettingsService _azureAISearchIndexSettingsService;

internal readonly IStringLocalizer S;

public AzureAISearchIndexSettingsStep(
AzureAISearchIndexManager indexManager,
AzureAIIndexDocumentManager azureAIIndexDocumentManager,
AzureAISearchIndexSettingsService azureAISearchIndexSettingsService,
IStringLocalizer<AzureAISearchIndexSettingsStep> stringLocalizer)
: base(Name)
{
_indexManager = indexManager;
_azureAIIndexDocumentManager = azureAIIndexDocumentManager;
_azureAISearchIndexSettingsService = azureAISearchIndexSettingsService;
S = stringLocalizer;
}
Expand Down Expand Up @@ -68,7 +65,7 @@ protected override async Task HandleAsync(RecipeExecutionContext context)
{
var indexSettings = await _azureAISearchIndexSettingsService.NewAsync(sourceName, token);
indexSettings.IndexFullName = _indexManager.GetFullIndexName(indexName);
indexSettings.IndexMappings = await _azureAIIndexDocumentManager.GetMappingsAsync(indexSettings);
await _azureAISearchIndexSettingsService.SetMappingsAsync(indexSettings);

var validationResult = await _azureAISearchIndexSettingsService.ValidateAsync(indexSettings);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,17 @@ public class AzureAIIndexDocumentManager
private readonly AzureAIClientFactory _clientFactory;
private readonly AzureAISearchIndexManager _indexManager;
private readonly IEnumerable<IAzureAISearchDocumentEvents> _documentEvents;
private readonly IEnumerable<IAzureAISearchEvents> _azureAISearchEvents;
private readonly ILogger _logger;

public AzureAIIndexDocumentManager(
AzureAIClientFactory clientFactory,
AzureAISearchIndexManager indexManager,
IEnumerable<IAzureAISearchDocumentEvents> documentEvents,
IEnumerable<IAzureAISearchEvents> azureAISearchEvents,
ILogger<AzureAIIndexDocumentManager> logger)
{
_clientFactory = clientFactory;
_indexManager = indexManager;
_documentEvents = documentEvents;
_azureAISearchEvents = azureAISearchEvents;
_logger = logger;
}

Expand Down Expand Up @@ -191,17 +188,6 @@ public async Task UploadDocumentsAsync(string indexName, IEnumerable<DocumentInd
}
}

public async Task<IList<AzureAISearchIndexMap>> GetMappingsAsync(AzureAISearchIndexSettings settings)
{
ArgumentNullException.ThrowIfNull(settings);

var mappingContext = new AzureAISearchMappingContext(settings);

await _azureAISearchEvents.InvokeAsync((handler, context) => handler.MappingAsync(context), mappingContext, _logger);

return mappingContext.Mappings;
}

private static IEnumerable<SearchDocument> CreateSearchDocuments(IEnumerable<DocumentIndexBase> indexDocuments, Dictionary<string, IEnumerable<AzureAISearchIndexMap>> mappings)
{
foreach (var indexDocument in indexDocuments)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using OrchardCore.Search.AzureAI.Models;
using OrchardCore.Search.AzureAI.Models;

namespace OrchardCore.Search.AzureAI.Services;

Expand All @@ -14,6 +14,11 @@ public virtual Task InitializingAsync(AzureAISearchIndexSettingsInitializingCont
return Task.CompletedTask;
}

public virtual Task MappingAsync(AzureAISearchMappingContext context)
{
return Task.CompletedTask;
}

public virtual Task ResetAsync(AzureAISearchIndexSettingsResetContext context)
{
return Task.CompletedTask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ public async Task<ValidationResultDetails> ValidateAsync(AzureAISearchIndexSetti
return validatingContext.Result;
}

public async Task SetMappingsAsync(AzureAISearchIndexSettings settings)
{
ArgumentNullException.ThrowIfNull(settings);

var mappingContext = new AzureAISearchMappingContext(settings);
await _handlers.InvokeAsync((handler, ctx) => handler.MappingAsync(ctx), mappingContext, _logger);
}

public async Task ResetAsync(AzureAISearchIndexSettings settings)
{
ArgumentNullException.ThrowIfNull(settings);
Expand Down
Loading

0 comments on commit 6d2faf3

Please sign in to comment.