Skip to content

Commit 004a7d0

Browse files
Zeegaankjac
andauthored
Rebuild index after language deletion (#33)
* Rebuild indexes when language gets deleted * Add setting to only rebuild content indexes * Move IncludeRebuildWhenLanguageDeleted to IndexOptions * Rebuild when index has object type document instead, so we don't rebuild members etc * Verify that there are results before language deletion --------- Co-authored-by: kjac <kja@umbraco.dk>
1 parent 325175e commit 004a7d0

File tree

7 files changed

+55
-13
lines changed

7 files changed

+55
-13
lines changed

src/Umbraco.Cms.Search.Core/NotificationHandlers/RebuildIndexesNotificationHandler.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
using Microsoft.Extensions.Logging;
2+
using Microsoft.Extensions.Options;
23
using Umbraco.Cms.Core.Events;
4+
using Umbraco.Cms.Core.Models;
35
using Umbraco.Cms.Core.Notifications;
6+
using Umbraco.Cms.Search.Core.Configuration;
47
using Umbraco.Cms.Search.Core.Services.ContentIndexing;
58

69
namespace Umbraco.Cms.Search.Core.NotificationHandlers;
710

8-
internal sealed class RebuildIndexesNotificationHandler : INotificationHandler<UmbracoApplicationStartedNotification>
11+
internal sealed class RebuildIndexesNotificationHandler : INotificationHandler<UmbracoApplicationStartedNotification>, INotificationHandler<LanguageDeletedNotification>
912
{
1013
private readonly IContentIndexingService _contentIndexingService;
1114
private readonly ILogger<RebuildIndexesNotificationHandler> _logger;
15+
private readonly IndexOptions _options;
1216

1317
public RebuildIndexesNotificationHandler(
1418
IContentIndexingService contentIndexingService,
15-
ILogger<RebuildIndexesNotificationHandler> logger)
19+
ILogger<RebuildIndexesNotificationHandler> logger,
20+
IOptions<IndexOptions> options)
1621
{
1722
_contentIndexingService = contentIndexingService;
1823
_logger = logger;
24+
_options = options.Value;
1925
}
2026

2127
public void Handle(UmbracoApplicationStartedNotification notification)
@@ -26,4 +32,17 @@ public void Handle(UmbracoApplicationStartedNotification notification)
2632
_contentIndexingService.Rebuild(Constants.IndexAliases.DraftMedia);
2733
_contentIndexingService.Rebuild(Constants.IndexAliases.DraftMembers);
2834
}
35+
36+
public void Handle(LanguageDeletedNotification notification)
37+
{
38+
_logger.LogInformation("Rebuilding search indexes after language deletion...");
39+
40+
foreach (var indexRegistration in _options.GetIndexRegistrations())
41+
{
42+
if (indexRegistration.ContainedObjectTypes.Contains(UmbracoObjectTypes.Document))
43+
{
44+
_contentIndexingService.Rebuild(indexRegistration.IndexAlias);
45+
}
46+
}
47+
}
2948
}

src/Umbraco.Cms.Search.Core/Umbraco.Cms.Search.Core.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
2424
<_Parameter1>Umbraco.Test.Search.Unit</_Parameter1>
2525
</AssemblyAttribute>
26+
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
27+
<_Parameter1>Umbraco.Test.Search.Examine.Integration</_Parameter1>
28+
</AssemblyAttribute>
2629
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
2730
<_Parameter1>DynamicProxyGenAssembly2</_Parameter1>
2831
</AssemblyAttribute>

src/Umbraco.Test.Search.Examine.Integration/Tests/ContentTests/IndexService/VariantContentTreeTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,8 @@ public async Task CreateVariantDocumentTree()
228228
.WithCultureInfo("ja-JP")
229229
.Build();
230230

231-
LocalizationService.Save(langDk);
232-
LocalizationService.Save(langJp);
231+
await LanguageService.CreateAsync(langDk, Constants.Security.SuperUserKey);
232+
await LanguageService.CreateAsync(langJp, Constants.Security.SuperUserKey);
233233

234234
IContentType contentType = new ContentTypeBuilder()
235235
.WithAlias("variant")

src/Umbraco.Test.Search.Examine.Integration/Tests/ContentTests/IndexService/VariantDocumentTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ private async Task CreateVariantDocument()
177177
.WithCultureInfo("ja-JP")
178178
.Build();
179179

180-
LocalizationService.Save(langDk);
181-
LocalizationService.Save(langJp);
180+
await LanguageService.CreateAsync(langDk, Cms.Core.Constants.Security.SuperUserKey);
181+
await LanguageService.CreateAsync(langJp, Cms.Core.Constants.Security.SuperUserKey);
182182

183183
IContentType contentType = new ContentTypeBuilder()
184184
.WithAlias("variant")

src/Umbraco.Test.Search.Examine.Integration/Tests/ContentTests/SearchService/VariantDocumentTests.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ namespace Umbraco.Test.Search.Examine.Integration.Tests.ContentTests.SearchServi
99

1010
public class VariantDocumentTests : SearcherTestBase
1111
{
12-
1312
[TestCase(true, "en-US", "Name")]
1413
[TestCase(false, "en-US", "Name")]
1514
[TestCase(true, "da-DK", "Navn")]
@@ -83,20 +82,37 @@ public async Task CanSearchVariantTextBySegment(bool publish, string culture, st
8382
Assert.That(results.Documents.First().Id, Is.EqualTo(RootKey));
8483
}
8584

85+
[TestCase(true, "da-DK", "Roden")]
86+
[TestCase(true, "ja-JP", "ル-ト")]
87+
public async Task CannotSearchNonExistingNameAfterLanguageDelete(bool publish, string culture, string name)
88+
{
89+
var indexAlias = GetIndexAlias(publish);
90+
91+
SearchResult results = await Searcher.SearchAsync(indexAlias, name, null, null, null, culture, null, null, 0, 100);
92+
Assert.That(results.Total, Is.EqualTo(1));
93+
94+
await LanguageService.DeleteAsync(culture, Constants.Security.SuperUserKey);
95+
96+
// We can't wait for indexing here, as it's an entire rebuild, not just a single action.
97+
await Task.Delay(4000);
98+
99+
results = await Searcher.SearchAsync(indexAlias, name, null, null, null, culture, null, null, 0, 100);
100+
Assert.That(results.Total, Is.EqualTo(0));
101+
}
102+
86103
[SetUp]
87104
public async Task CreateVariantDocument()
88105
{
89106

90107
ILanguage langDk = new LanguageBuilder()
91108
.WithCultureInfo("da-DK")
92-
.WithIsDefault(true)
93109
.Build();
94110
ILanguage langJp = new LanguageBuilder()
95111
.WithCultureInfo("ja-JP")
96112
.Build();
97113

98-
LocalizationService.Save(langDk);
99-
LocalizationService.Save(langJp);
114+
await LanguageService.CreateAsync(langDk, Constants.Security.SuperUserKey);
115+
await LanguageService.CreateAsync(langJp, Constants.Security.SuperUserKey);
100116

101117
IContentType contentType = new ContentTypeBuilder()
102118
.WithAlias("variant")

src/Umbraco.Test.Search.Examine.Integration/Tests/ContentTests/SearchService/VariantFilterTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ public async Task CreateVariantDocument()
100100
.WithCultureInfo("ja-JP")
101101
.Build();
102102

103-
LocalizationService.Save(langDk);
104-
LocalizationService.Save(langJp);
103+
await LanguageService.CreateAsync(langDk, Constants.Security.SuperUserKey);
104+
await LanguageService.CreateAsync(langJp, Constants.Security.SuperUserKey);
105105

106106
IContentType contentType = new ContentTypeBuilder()
107107
.WithAlias("variant")

src/Umbraco.Test.Search.Examine.Integration/Tests/TestBase.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
using NUnit.Framework;
66
using Umbraco.Cms.Core.HostedServices;
77
using Umbraco.Cms.Core.Models;
8+
using Umbraco.Cms.Core.Notifications;
89
using Umbraco.Cms.Core.Services;
910
using Umbraco.Cms.Core.Sync;
1011
using Umbraco.Cms.Search.Core.DependencyInjection;
12+
using Umbraco.Cms.Search.Core.NotificationHandlers;
1113
using Umbraco.Cms.Tests.Common.Testing;
1214
using Umbraco.Cms.Tests.Integration.Testing;
1315
using Umbraco.Test.Search.Examine.Integration.Attributes;
@@ -37,7 +39,8 @@ public abstract class TestBase : UmbracoIntegrationTest
3739

3840
protected IDataTypeService DataTypeService => GetRequiredService<IDataTypeService>();
3941

40-
protected ILocalizationService LocalizationService => GetRequiredService<ILocalizationService>();
42+
protected ILanguageService LanguageService => GetRequiredService<ILanguageService>();
43+
4144

4245
protected void SaveAndPublish(IContent content)
4346
{
@@ -55,6 +58,7 @@ protected override void CustomTestSetup(IUmbracoBuilder builder)
5558

5659
builder.Services.AddUnique<IBackgroundTaskQueue, ImmediateBackgroundTaskQueue>();
5760
builder.Services.AddUnique<IServerMessenger, LocalServerMessenger>();
61+
builder.AddNotificationHandler<LanguageDeletedNotification, RebuildIndexesNotificationHandler>();
5862

5963
// the core ConfigureBuilderAttribute won't execute from other assemblies at the moment, so this is a workaround
6064
var testType = Type.GetType(TestContext.CurrentContext.Test.ClassName!);

0 commit comments

Comments
 (0)