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

Lucene index rebuild and reset steps (Lombiq Technologies: OCORE-101) #12302

Closed
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using OrchardCore.Deployment;

namespace OrchardCore.Lucene.Deployment
{
public class LuceneIndexRebuildDeploymentSource : IDeploymentSource
{
public Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlanResult result)
{
var luceneIndexRebuildStep = step as LuceneIndexRebuildDeploymentStep;

if (luceneIndexRebuildStep == null)
{
return Task.CompletedTask;
}

var indicesToRebuild = luceneIndexRebuildStep.IncludeAll ? Array.Empty<string>() : luceneIndexRebuildStep.IndexNames;

result.Steps.Add(new JObject(
new JProperty("name", "lucene-index-rebuild"),
new JProperty("includeAll", luceneIndexRebuildStep.IncludeAll),
new JProperty("Indices", new JArray(indicesToRebuild))
));

return Task.CompletedTask;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using OrchardCore.Deployment;

namespace OrchardCore.Lucene.Deployment
{
/// <summary>
/// Adds rebuild Lucene index task to a <see cref="DeploymentPlanResult"/>.
/// </summary>
public class LuceneIndexRebuildDeploymentStep : DeploymentStep
{
public LuceneIndexRebuildDeploymentStep()
{
Name = "LuceneIndexRebuild";
}

public bool IncludeAll { get; set; } = true;

public string[] IndexNames { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using OrchardCore.Deployment;
using OrchardCore.DisplayManagement.Handlers;
using OrchardCore.DisplayManagement.ModelBinding;
using OrchardCore.DisplayManagement.Views;
using OrchardCore.Lucene.ViewModels;

namespace OrchardCore.Lucene.Deployment
{
public class LuceneIndexRebuildDeploymentStepDriver : DisplayDriver<DeploymentStep, LuceneIndexRebuildDeploymentStep>
{
private readonly LuceneIndexSettingsService _luceneIndexSettingsService;

public LuceneIndexRebuildDeploymentStepDriver(LuceneIndexSettingsService luceneIndexSettingsService)
{
_luceneIndexSettingsService = luceneIndexSettingsService;
}

public override IDisplayResult Display(LuceneIndexRebuildDeploymentStep step)
{
return
Combine(
View("LuceneIndexRebuildDeploymentStep_Fields_Summary", step).Location("Summary", "Content"),
View("LuceneIndexRebuildDeploymentStep_Fields_Thumbnail", step).Location("Thumbnail", "Content")
);
}

public override IDisplayResult Edit(LuceneIndexRebuildDeploymentStep step)
{
return Initialize<LuceneIndexRebuildDeploymentStepViewModel>("LuceneIndexRebuildDeploymentStep_Fields_Edit", async model =>
{
model.IncludeAll = step.IncludeAll;
lampersky marked this conversation as resolved.
Show resolved Hide resolved
model.IndexNames = step.IndexNames;
model.AllIndexNames = (await _luceneIndexSettingsService.GetSettingsAsync()).Select(x => x.IndexName).ToArray();
}).Location("Content");
}

public override async Task<IDisplayResult> UpdateAsync(LuceneIndexRebuildDeploymentStep rebuildIndexStep, IUpdateModel updater)
{
rebuildIndexStep.IndexNames = Array.Empty<string>();

await updater.TryUpdateModelAsync(rebuildIndexStep, Prefix, step => step.IndexNames, step => step.IncludeAll);

if (rebuildIndexStep.IncludeAll)
{
rebuildIndexStep.IndexNames = Array.Empty<string>();
}

return Edit(rebuildIndexStep);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using OrchardCore.Deployment;

namespace OrchardCore.Lucene.Deployment
{
public class LuceneIndexResetDeploymentSource : IDeploymentSource
{
public Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlanResult result)
{
var luceneIndexResetStep = step as LuceneIndexResetDeploymentStep;

if (luceneIndexResetStep == null)
{
return Task.CompletedTask;
}

var indicesToReset = luceneIndexResetStep.IncludeAll ? Array.Empty<string>() : luceneIndexResetStep.IndexNames;

result.Steps.Add(new JObject(
new JProperty("name", "lucene-index-reset"),
new JProperty("includeAll", luceneIndexResetStep.IncludeAll),
new JProperty("Indices", new JArray(indicesToReset))
));

return Task.CompletedTask;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using OrchardCore.Deployment;

namespace OrchardCore.Lucene.Deployment
{
/// <summary>
/// Adds reset Lucene index task to a <see cref="DeploymentPlanResult"/>.
/// </summary>
public class LuceneIndexResetDeploymentStep : DeploymentStep
{
public LuceneIndexResetDeploymentStep()
{
Name = "LuceneIndexReset";
}

public bool IncludeAll { get; set; } = true;

public string[] IndexNames { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using OrchardCore.Deployment;
using OrchardCore.DisplayManagement.Handlers;
using OrchardCore.DisplayManagement.ModelBinding;
using OrchardCore.DisplayManagement.Views;
using OrchardCore.Lucene.ViewModels;

namespace OrchardCore.Lucene.Deployment
{
public class LuceneIndexResetDeploymentStepDriver : DisplayDriver<DeploymentStep, LuceneIndexResetDeploymentStep>
{
private readonly LuceneIndexSettingsService _luceneIndexSettingsService;

public LuceneIndexResetDeploymentStepDriver(LuceneIndexSettingsService luceneIndexSettingsService)
{
_luceneIndexSettingsService = luceneIndexSettingsService;
}

public override IDisplayResult Display(LuceneIndexResetDeploymentStep step)
{
return
Combine(
View("LuceneIndexResetDeploymentStep_Fields_Summary", step).Location("Summary", "Content"),
View("LuceneIndexResetDeploymentStep_Fields_Thumbnail", step).Location("Thumbnail", "Content")
);
}

public override IDisplayResult Edit(LuceneIndexResetDeploymentStep step)
{
return Initialize<LuceneIndexResetDeploymentStepViewModel>("LuceneIndexResetDeploymentStep_Fields_Edit", async model =>
{
model.IncludeAll = step.IncludeAll;
model.IndexNames = step.IndexNames;
model.AllIndexNames = (await _luceneIndexSettingsService.GetSettingsAsync()).Select(x => x.IndexName).ToArray();
}).Location("Content");
}

public override async Task<IDisplayResult> UpdateAsync(LuceneIndexResetDeploymentStep resetIndexStep, IUpdateModel updater)
{
resetIndexStep.IndexNames = Array.Empty<string>();

await updater.TryUpdateModelAsync(resetIndexStep, Prefix, step => step.IndexNames, step => step.IncludeAll);

if (resetIndexStep.IncludeAll)
{
resetIndexStep.IndexNames = Array.Empty<string>();
}

return Edit(resetIndexStep);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using OrchardCore.BackgroundJobs;
using OrchardCore.Recipes.Models;
using OrchardCore.Recipes.Services;

namespace OrchardCore.Lucene.Recipes
{
/// <summary>
/// This recipe step rebuilds a lucene index.
/// </summary>
public class LuceneIndexRebuildStep : IRecipeStepHandler
{
public async Task ExecuteAsync(RecipeExecutionContext context)
{
if (!String.Equals(context.Name, "lucene-index-rebuild", StringComparison.OrdinalIgnoreCase))
{
return;
}

var model = context.Step.ToObject<LuceneIndexRebuildStepModel>();

if (model.IncludeAll || model.Indices.Length > 0)
{
await HttpBackgroundJob.ExecuteAfterEndOfRequestAsync("lucene-index-rebuild", async (scope) =>
{
var luceneIndexSettingsService = scope.ServiceProvider.GetRequiredService<LuceneIndexSettingsService>();
var luceneIndexingService = scope.ServiceProvider.GetRequiredService<LuceneIndexingService>();

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

foreach (var indexName in indices)
{
var luceneIndexSettings = await luceneIndexSettingsService.GetSettingsAsync(indexName);
if (luceneIndexSettings != null)
{
await luceneIndexingService.RebuildIndexAsync(indexName);
await luceneIndexingService.ProcessContentItemsAsync(indexName);
}
}
}, true);
Piedone marked this conversation as resolved.
Show resolved Hide resolved
}
}

private class LuceneIndexRebuildStepModel
{
public bool IncludeAll { get; set; } = false;
public string[] Indices { get; set; } = Array.Empty<string>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using OrchardCore.BackgroundJobs;
using OrchardCore.Recipes.Models;
using OrchardCore.Recipes.Services;

namespace OrchardCore.Lucene.Recipes
{
/// <summary>
/// This recipe step resets a lucene index.
/// </summary>
public class LuceneIndexResetStep : IRecipeStepHandler
{
public async Task ExecuteAsync(RecipeExecutionContext context)
{
if (!String.Equals(context.Name, "lucene-index-reset", StringComparison.OrdinalIgnoreCase))
{
return;
}

var model = context.Step.ToObject<LuceneIndexResetStepModel>();

if (model.IncludeAll || model.Indices.Length > 0)
{
await HttpBackgroundJob.ExecuteAfterEndOfRequestAsync("lucene-index-reset", async (scope) => {
var luceneIndexSettingsService = scope.ServiceProvider.GetRequiredService<LuceneIndexSettingsService>();
var luceneIndexingService = scope.ServiceProvider.GetRequiredService<LuceneIndexingService>();
var luceneIndexManager = scope.ServiceProvider.GetRequiredService<LuceneIndexManager>();

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

foreach (var indexName in indices)
{
var luceneIndexSettings = await luceneIndexSettingsService.GetSettingsAsync(indexName);
if (luceneIndexSettings != null)
{
if (!luceneIndexManager.Exists(indexName))
{
await luceneIndexingService.CreateIndexAsync(luceneIndexSettings);
}
else
{
luceneIndexingService.ResetIndex(indexName);
}
await luceneIndexingService.ProcessContentItemsAsync(indexName);
}
}
}, true);
}
}

private class LuceneIndexResetStepModel
{
public bool IncludeAll { get; set; } = false;
public string[] Indices { get; set; } = Array.Empty<string>();
}
}
}
10 changes: 10 additions & 0 deletions src/OrchardCore.Modules/OrchardCore.Lucene/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public override void ConfigureServices(IServiceCollection services)
services.AddScoped<IQuerySource, LuceneQuerySource>();
services.AddScoped<LuceneQuerySource>();
services.AddRecipeExecutionStep<LuceneIndexStep>();
services.AddRecipeExecutionStep<LuceneIndexRebuildStep>();
services.AddRecipeExecutionStep<LuceneIndexResetStep>();

services.AddScoped<IShapeTableProvider, SearchShapesTableProvider>();
services.AddShapeAttributes<SearchShapes>();
Expand Down Expand Up @@ -145,6 +147,14 @@ public override void ConfigureServices(IServiceCollection services)
services.AddTransient<IDeploymentSource, LuceneSettingsDeploymentSource>();
services.AddSingleton<IDeploymentStepFactory>(new DeploymentStepFactory<LuceneSettingsDeploymentStep>());
services.AddScoped<IDisplayDriver<DeploymentStep>, LuceneSettingsDeploymentStepDriver>();

services.AddTransient<IDeploymentSource, LuceneIndexRebuildDeploymentSource>();
services.AddSingleton<IDeploymentStepFactory>(new DeploymentStepFactory<LuceneIndexRebuildDeploymentStep>());
services.AddScoped<IDisplayDriver<DeploymentStep>, LuceneIndexRebuildDeploymentStepDriver>();

services.AddTransient<IDeploymentSource, LuceneIndexResetDeploymentSource>();
services.AddSingleton<IDeploymentStepFactory>(new DeploymentStepFactory<LuceneIndexResetDeploymentStep>());
services.AddScoped<IDisplayDriver<DeploymentStep>, LuceneIndexResetDeploymentStepDriver>();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace OrchardCore.Lucene.ViewModels
{
public class LuceneIndexRebuildDeploymentStepViewModel
{
public bool IncludeAll { get; set; }
public string[] IndexNames { get; set; }
public string[] AllIndexNames { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace OrchardCore.Lucene.ViewModels
{
public class LuceneIndexResetDeploymentStepViewModel
{
public bool IncludeAll { get; set; }
public string[] IndexNames { get; set; }
public string[] AllIndexNames { get; set; }
}
}
Loading