Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize ElasticIndexingService #15227

Merged
merged 7 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
using Microsoft.AspNetCore.Mvc.Localization;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using OrchardCore.BackgroundJobs;
using OrchardCore.ContentManagement.Metadata;
using OrchardCore.DisplayManagement;
using OrchardCore.DisplayManagement.Notify;
Expand Down Expand Up @@ -325,7 +327,7 @@ public async Task<ActionResult> Reset(string id)
}

await _elasticIndexingService.ResetIndexAsync(id);
await _elasticIndexingService.ProcessContentItemsAsync(id);
await ProcessContentItemsAsync(id);

await _notifier.SuccessAsync(H["Index <em>{0}</em> reset successfully.", id]);

Expand Down Expand Up @@ -363,7 +365,7 @@ public async Task<ActionResult> Rebuild(string id)
await _elasticIndexSettingsService.UpdateIndexAsync(settings);
}

await _elasticIndexingService.ProcessContentItemsAsync(id);
await ProcessContentItemsAsync(id);

await _notifier.SuccessAsync(H["Index <em>{0}</em> rebuilt successfully.", id]);

Expand Down Expand Up @@ -577,7 +579,7 @@ public async Task<ActionResult> IndexPost(ContentOptions options, IEnumerable<st
}

await _elasticIndexingService.ResetIndexAsync(item.IndexName);
await _elasticIndexingService.ProcessContentItemsAsync(item.IndexName);
await ProcessContentItemsAsync(item.IndexName);

await _notifier.SuccessAsync(H["Index <em>{0}</em> reset successfully.", item.IndexName]);
}
Expand All @@ -591,7 +593,8 @@ public async Task<ActionResult> IndexPost(ContentOptions options, IEnumerable<st
}

await _elasticIndexingService.RebuildIndexAsync(await _elasticIndexSettingsService.GetSettingsAsync(item.IndexName));
await _elasticIndexingService.ProcessContentItemsAsync(item.IndexName);

await ProcessContentItemsAsync(item.IndexName);
await _notifier.SuccessAsync(H["Index <em>{0}</em> rebuilt successfully.", item.IndexName]);
}
break;
Expand Down Expand Up @@ -636,5 +639,12 @@ private async Task PopulateMenuOptionsAsync(ElasticIndexSettingsViewModel model)

private ViewResult NotConfigured()
=> View("NotConfigured");

private static Task ProcessContentItemsAsync(string indexName)
=> HttpBackgroundJob.ExecuteAfterEndOfRequestAsync("sync-content-items-elasticsearch-" + indexName, async (scope) =>
{
var indexingService = scope.ServiceProvider.GetRequiredService<ElasticIndexingService>();
await indexingService.ProcessContentItemsAsync(indexName);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,32 @@ public async Task ExecuteAsync(RecipeExecutionContext context)
{
await HttpBackgroundJob.ExecuteAfterEndOfRequestAsync("elastic-index-rebuild", async scope =>
{

var elasticIndexingService = scope.ServiceProvider.GetService<ElasticIndexingService>();
var elasticIndexSettingsService = scope.ServiceProvider.GetService<ElasticIndexSettingsService>();
var elasticIndexManager = scope.ServiceProvider.GetRequiredService<ElasticIndexManager>();

var indices = model.IncludeAll ? (await elasticIndexSettingsService.GetSettingsAsync()).Select(x => x.IndexName).ToArray() : model.Indices;
var indexNames = model.IncludeAll ? (await elasticIndexSettingsService.GetSettingsAsync()).Select(x => x.IndexName).ToArray() : model.Indices;

foreach (var indexName in indices)
foreach (var indexName in indexNames)
{
var elasticIndexSettings = await elasticIndexSettingsService.GetSettingsAsync(indexName);

if (elasticIndexSettings != null)
if (elasticIndexSettings == null)
{
continue;
}
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved

if (!await elasticIndexManager.ExistsAsync(indexName))
{
if (!await elasticIndexManager.ExistsAsync(indexName))
{
await elasticIndexingService.CreateIndexAsync(elasticIndexSettings);
}
else
{
await elasticIndexingService.RebuildIndexAsync(elasticIndexSettings);
}

await elasticIndexingService.ProcessContentItemsAsync(indexName);
await elasticIndexingService.CreateIndexAsync(elasticIndexSettings);
}
else
{
await elasticIndexingService.RebuildIndexAsync(elasticIndexSettings);
}
}

await elasticIndexingService.ProcessContentItemsAsync(indexNames);
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,32 @@ public async Task ExecuteAsync(RecipeExecutionContext context)
{
await HttpBackgroundJob.ExecuteAfterEndOfRequestAsync("elastic-index-reset", async scope =>
{

var elasticIndexingService = scope.ServiceProvider.GetService<ElasticIndexingService>();
var elasticIndexSettingsService = scope.ServiceProvider.GetService<ElasticIndexSettingsService>();
var elasticIndexManager = scope.ServiceProvider.GetRequiredService<ElasticIndexManager>();

var indices = model.IncludeAll ? (await elasticIndexSettingsService.GetSettingsAsync()).Select(x => x.IndexName).ToArray() : model.Indices;
var indexNames = model.IncludeAll ? (await elasticIndexSettingsService.GetSettingsAsync()).Select(x => x.IndexName).ToArray() : model.Indices;

foreach (var indexName in indices)
foreach (var indexName in indexNames)
{
var elasticIndexSettings = await elasticIndexSettingsService.GetSettingsAsync(indexName);

if (elasticIndexSettings != null)
if (elasticIndexSettings == null)
{
continue;
}
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved

if (!await elasticIndexManager.ExistsAsync(indexName))
{
if (!await elasticIndexManager.ExistsAsync(indexName))
{
await elasticIndexingService.CreateIndexAsync(elasticIndexSettings);
}
else
{
await elasticIndexingService.ResetIndexAsync(elasticIndexSettings.IndexName);
}

await elasticIndexingService.ProcessContentItemsAsync(indexName);
await elasticIndexingService.CreateIndexAsync(elasticIndexSettings);
}
else
{
await elasticIndexingService.ResetIndexAsync(elasticIndexSettings.IndexName);
}
}

await elasticIndexingService.ProcessContentItemsAsync(indexNames);
});
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using OrchardCore.BackgroundJobs;
using OrchardCore.Environment.Shell;
using OrchardCore.Environment.Shell.Removing;
using OrchardCore.Environment.Shell.Scope;
using OrchardCore.Modules;
using OrchardCore.Search.Elasticsearch.Core.Services;

Expand Down Expand Up @@ -37,30 +38,36 @@ public ElasticIndexInitializerService(
_logger = logger;
}

public override Task ActivatedAsync()
public override async Task ActivatedAsync()
{
if (_shellSettings.IsRunning())
if (!_shellSettings.IsRunning())
{
ShellScope.AddDeferredTask(async scope =>
{
var elasticIndexSettingsService = scope.ServiceProvider.GetRequiredService<ElasticIndexSettingsService>();
var elasticIndexingService = scope.ServiceProvider.GetRequiredService<ElasticIndexingService>();
var indexManager = scope.ServiceProvider.GetRequiredService<ElasticIndexManager>();
return;
}

var elasticIndexSettings = await elasticIndexSettingsService.GetSettingsAsync();
await HttpBackgroundJob.ExecuteAfterEndOfRequestAsync("elastic-initialize", async scope =>
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved
{
var elasticIndexSettingsService = scope.ServiceProvider.GetRequiredService<ElasticIndexSettingsService>();
var elasticIndexingService = scope.ServiceProvider.GetRequiredService<ElasticIndexingService>();
var indexManager = scope.ServiceProvider.GetRequiredService<ElasticIndexManager>();

foreach (var settings in elasticIndexSettings)
var elasticIndexSettings = await elasticIndexSettingsService.GetSettingsAsync();
var createdIndexes = new List<string>();

foreach (var settings in elasticIndexSettings)
{
if (!await indexManager.ExistsAsync(settings.IndexName))
{
if (!await indexManager.ExistsAsync(settings.IndexName))
{
await elasticIndexingService.CreateIndexAsync(settings);
await elasticIndexingService.ProcessContentItemsAsync(settings.IndexName);
}
await elasticIndexingService.CreateIndexAsync(settings);
createdIndexes.Add(settings.IndexName);
}
});
}
}

return Task.CompletedTask;
if (createdIndexes.Count > 0)
{
await elasticIndexingService.ProcessContentItemsAsync(createdIndexes.ToArray());
}
});
}

public override async Task RemovingAsync(ShellRemovingContext context)
Expand Down
Loading
Loading