From ff58fbd2b18c242cefe67d75ecc585e6b6ec5cf2 Mon Sep 17 00:00:00 2001 From: Kenn Jacobsen Date: Wed, 9 Apr 2025 18:13:18 +0200 Subject: [PATCH] Make it possible to disable Examine indexes by composition (#18988) --- .../UmbracoBuilder.BackOffice.cs | 4 +--- .../AddExamineComposer.cs | 14 +++++++++++ .../UmbracoBuilder.CoreServices.cs | 3 ++- .../UmbracoBuilder.Examine.cs | 3 +++ .../Examine/NoopExamineManager.cs | 24 +++++++++++++++++++ .../Examine/NoopIndexRebuilder.cs | 10 ++++++++ 6 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 src/Umbraco.Examine.Lucene/AddExamineComposer.cs create mode 100644 src/Umbraco.Infrastructure/Examine/NoopExamineManager.cs create mode 100644 src/Umbraco.Infrastructure/Examine/NoopIndexRebuilder.cs diff --git a/src/Umbraco.Cms.Api.Management/DependencyInjection/UmbracoBuilder.BackOffice.cs b/src/Umbraco.Cms.Api.Management/DependencyInjection/UmbracoBuilder.BackOffice.cs index ef0a1548b8..2b3e4e06e6 100644 --- a/src/Umbraco.Cms.Api.Management/DependencyInjection/UmbracoBuilder.BackOffice.cs +++ b/src/Umbraco.Cms.Api.Management/DependencyInjection/UmbracoBuilder.BackOffice.cs @@ -35,9 +35,7 @@ public static partial class UmbracoBuilderExtensions .AddRecurringBackgroundJobs() .AddUmbracoHybridCache() .AddDistributedCache() - .AddCoreNotifications() - .AddExamine() - .AddExamineIndexes(); + .AddCoreNotifications(); public static IUmbracoBuilder AddBackOfficeCore(this IUmbracoBuilder builder) { diff --git a/src/Umbraco.Examine.Lucene/AddExamineComposer.cs b/src/Umbraco.Examine.Lucene/AddExamineComposer.cs new file mode 100644 index 0000000000..4c8e6e2e37 --- /dev/null +++ b/src/Umbraco.Examine.Lucene/AddExamineComposer.cs @@ -0,0 +1,14 @@ +using Umbraco.Cms.Core.Composing; +using Umbraco.Cms.Core.DependencyInjection; +using Umbraco.Cms.Infrastructure.DependencyInjection; +using Umbraco.Cms.Infrastructure.Examine.DependencyInjection; + +namespace Umbraco.Cms.Infrastructure.Examine; + +public sealed class AddExamineComposer : IComposer +{ + public void Compose(IUmbracoBuilder builder) + => builder + .AddExamine() + .AddExamineIndexes(); +} diff --git a/src/Umbraco.Infrastructure/DependencyInjection/UmbracoBuilder.CoreServices.cs b/src/Umbraco.Infrastructure/DependencyInjection/UmbracoBuilder.CoreServices.cs index bc01e0474c..aee4fdac67 100644 --- a/src/Umbraco.Infrastructure/DependencyInjection/UmbracoBuilder.CoreServices.cs +++ b/src/Umbraco.Infrastructure/DependencyInjection/UmbracoBuilder.CoreServices.cs @@ -186,7 +186,8 @@ public static partial class UmbracoBuilderExtensions builder.Services.AddTransient(); builder.Services.AddTransient(); - builder.Services.AddSingleton(); + builder.Services.AddSingleton(); + builder.Services.AddSingleton(); builder.Services.AddScoped(); diff --git a/src/Umbraco.Infrastructure/DependencyInjection/UmbracoBuilder.Examine.cs b/src/Umbraco.Infrastructure/DependencyInjection/UmbracoBuilder.Examine.cs index 887b9e4305..59d1c68614 100644 --- a/src/Umbraco.Infrastructure/DependencyInjection/UmbracoBuilder.Examine.cs +++ b/src/Umbraco.Infrastructure/DependencyInjection/UmbracoBuilder.Examine.cs @@ -1,3 +1,4 @@ +using Examine; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Umbraco.Cms.Core.DeliveryApi; @@ -21,6 +22,8 @@ public static partial class UmbracoBuilderExtensions { public static IUmbracoBuilder AddExamine(this IUmbracoBuilder builder) { + builder.Services.AddUnique(); + // populators are not a collection: one cannot remove ours, and can only add more // the container can inject IEnumerable and get them all builder.Services.AddSingleton(); diff --git a/src/Umbraco.Infrastructure/Examine/NoopExamineManager.cs b/src/Umbraco.Infrastructure/Examine/NoopExamineManager.cs new file mode 100644 index 0000000000..a2316b5d87 --- /dev/null +++ b/src/Umbraco.Infrastructure/Examine/NoopExamineManager.cs @@ -0,0 +1,24 @@ +using Examine; + +namespace Umbraco.Cms.Infrastructure.Examine; + +internal sealed class NoopExamineManager : IExamineManager +{ + public void Dispose() {} + + public bool TryGetIndex(string indexName, out IIndex index) + { + index = null!; + return false; + } + + public bool TryGetSearcher(string searcherName, out ISearcher searcher) + { + searcher = null!; + return false; + } + + public IEnumerable Indexes => Array.Empty(); + + public IEnumerable RegisteredSearchers => Array.Empty(); +} diff --git a/src/Umbraco.Infrastructure/Examine/NoopIndexRebuilder.cs b/src/Umbraco.Infrastructure/Examine/NoopIndexRebuilder.cs new file mode 100644 index 0000000000..bfa1c9f0f0 --- /dev/null +++ b/src/Umbraco.Infrastructure/Examine/NoopIndexRebuilder.cs @@ -0,0 +1,10 @@ +namespace Umbraco.Cms.Infrastructure.Examine; + +internal sealed class NoopIndexRebuilder : IIndexRebuilder +{ + public bool CanRebuild(string indexName) => false; + + public void RebuildIndex(string indexName, TimeSpan? delay = null, bool useBackgroundThread = true) {} + + public void RebuildIndexes(bool onlyEmptyIndexes, TimeSpan? delay = null, bool useBackgroundThread = true) {} +}