diff --git a/backend/src/Squidex.Domain.Apps.Entities/Contents/Text/Elastic/ElasticSearchTextIndex.cs b/backend/src/Squidex.Domain.Apps.Entities/Contents/Text/Elastic/ElasticSearchTextIndex.cs index 5ebc40ba3..8818217c8 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/Contents/Text/Elastic/ElasticSearchTextIndex.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Contents/Text/Elastic/ElasticSearchTextIndex.cs @@ -50,31 +50,52 @@ namespace Squidex.Domain.Apps.Entities.Contents.Text.Elastic public async Task ExecuteAsync(params IndexCommand[] commands) { + var args = new List(); + foreach (var command in commands) { switch (command) { case UpsertIndexEntry upsert: - await UpsertAsync(upsert); + Upsert(upsert, args); break; case UpdateIndexEntry update: - await UpdateAsync(update); + Update(update, args); break; case DeleteIndexEntry delete: - await DeleteAsync(delete); + Delete(delete, args); break; } } + if (args.Count > 0) + { + var result = await client.BulkAsync(PostData.MultiJson(args)); + + if (!result.Success) + { + throw new InvalidOperationException($"Failed with ${result.Body}", result.OriginalException); + } + } + if (waitForTesting) { await Task.Delay(1000); } } - private async Task UpsertAsync(UpsertIndexEntry upsert) + private void Upsert(UpsertIndexEntry upsert, List args) { - var data = new + args.Add(new + { + index = new + { + _id = upsert.DocId, + _index = indexName, + } + }); + + args.Add(new { appId = upsert.AppId.Id.ToString(), appName = upsert.AppId.Name, @@ -84,38 +105,40 @@ namespace Squidex.Domain.Apps.Entities.Contents.Text.Elastic serveAll = upsert.ServeAll, servePublished = upsert.ServePublished, texts = upsert.Texts - }; - - var result = await client.IndexAsync(indexName, upsert.DocId, CreatePost(data)); - - if (!result.Success) - { - throw new InvalidOperationException($"Failed with ${result.Body}", result.OriginalException); - } + }); } - private async Task UpdateAsync(UpdateIndexEntry update) + private void Update(UpdateIndexEntry update, List args) { - var data = new + args.Add(new + { + update = new + { + _id = update.DocId, + _index = indexName, + } + }); + + args.Add(new { doc = new { serveAll = update.ServeAll, servePublished = update.ServePublished } - }; - - var result = await client.UpdateAsync(indexName, update.DocId, CreatePost(data)); - - if (!result.Success) - { - throw new InvalidOperationException($"Failed with ${result.Body}", result.OriginalException); - } + }); } - private Task DeleteAsync(DeleteIndexEntry delete) + private void Delete(DeleteIndexEntry delete, List args) { - return client.DeleteAsync(indexName, delete.DocId); + args.Add(new + { + delete = new + { + _id = delete.DocId, + _index = indexName, + } + }); } public async Task?> SearchAsync(string? queryText, IAppEntity app, SearchFilter? filter, SearchScope scope) diff --git a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/Text/TextIndexerTestsBase.cs b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/Text/TextIndexerTestsBase.cs index f7cbc38b3..0dc223aad 100644 --- a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/Text/TextIndexerTestsBase.cs +++ b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/Text/TextIndexerTestsBase.cs @@ -16,7 +16,6 @@ using Squidex.Domain.Apps.Entities.TestHelpers; using Squidex.Domain.Apps.Events.Contents; using Squidex.Infrastructure; using Squidex.Infrastructure.EventSourcing; -using Squidex.Infrastructure.Validation; using Xunit; #pragma warning disable SA1401 // Fields should be private @@ -297,35 +296,31 @@ namespace Squidex.Domain.Apps.Entities.Contents.Text protected IndexOperation Create(DomainId id, string language, string text) { - var data = - new NamedContentData() - .AddField("text", - new ContentFieldData() - .AddValue(language, text)); + var data = Data(language, text); return Op(id, new ContentCreated { Data = data }); } protected IndexOperation Update(DomainId id, string language, string text) { - var data = - new NamedContentData() - .AddField("text", - new ContentFieldData() - .AddValue(language, text)); + var data = Data(language, text); return Op(id, new ContentUpdated { Data = data }); } protected IndexOperation CreateDraftWithData(DomainId id, string language, string text) { - var data = - new NamedContentData() + var data = Data(language, text); + + return Op(id, new ContentDraftCreated { MigratedData = data }); + } + + private static NamedContentData Data(string language, string text) + { + return new NamedContentData() .AddField("text", new ContentFieldData() .AddValue(language, text)); - - return Op(id, new ContentDraftCreated { MigratedData = data }); } protected IndexOperation CreateDraft(DomainId id) @@ -338,7 +333,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.Text return Op(id, new ContentStatusChanged { Status = Status.Published }); } - protected IndexOperation Unpublish( DomainId id) + protected IndexOperation Unpublish(DomainId id) { return Op(id, new ContentStatusChanged { Status = Status.Draft }); } @@ -376,7 +371,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.Text } else { - Assert.Empty(result); + result.Should().BeEmpty(); } }; } diff --git a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/Text/TextIndexerTests_Elastic.cs b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/Text/TextIndexerTests_Elastic.cs index 2d18ca660..0f413b857 100644 --- a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/Text/TextIndexerTests_Elastic.cs +++ b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/Text/TextIndexerTests_Elastic.cs @@ -17,7 +17,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.Text [Trait("Category", "Dependencies")] public class TextIndexerTests_Elastic : TextIndexerTestsBase { - private sealed class TheFactory : IIndexerFactory + private sealed class ElasticFactory : IIndexerFactory { public Task CleanupAsync() { @@ -34,7 +34,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.Text } } - public override IIndexerFactory Factory { get; } = new TheFactory(); + public override IIndexerFactory Factory { get; } = new ElasticFactory(); public TextIndexerTests_Elastic() { diff --git a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/Text/TextIndexerTests_Mongo.cs b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/Text/TextIndexerTests_Mongo.cs index 48e916486..5b5e1bc36 100644 --- a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/Text/TextIndexerTests_Mongo.cs +++ b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/Text/TextIndexerTests_Mongo.cs @@ -19,7 +19,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.Text [Trait("Category", "Dependencies")] public class TextIndexerTests_Mongo : TextIndexerTestsBase { - private sealed class TheFactory : IIndexerFactory + private sealed class MongoFactory : IIndexerFactory { private readonly MongoClient mongoClient = new MongoClient("mongodb://localhost"); @@ -40,7 +40,7 @@ namespace Squidex.Domain.Apps.Entities.Contents.Text } } - public override IIndexerFactory Factory { get; } = new TheFactory(); + public override IIndexerFactory Factory { get; } = new MongoFactory(); public TextIndexerTests_Mongo() {