diff --git a/examples/DancingGoat/Search/AdvancedSearchCollectionStrategy.cs b/examples/DancingGoat/Search/AdvancedSearchCollectionStrategy.cs index 26c6c21..2919e87 100644 --- a/examples/DancingGoat/Search/AdvancedSearchCollectionStrategy.cs +++ b/examples/DancingGoat/Search/AdvancedSearchCollectionStrategy.cs @@ -101,7 +101,7 @@ public AdvancedSearchCollectionStrategy() return res; } - public override async Task> FindItemsToReindex(CollectionEventWebPageItemModel changedItem) + /*public override async Task> FindItemsToReindex(CollectionEventWebPageItemModel changedItem) { var reindexedItems = new List(); @@ -148,7 +148,7 @@ public override async Task> FindItemsToRe } return reindexedItems; - } + }*/ private async Task GetPage(Guid id, string channelName, string languageName, string contentTypeName) where T : IWebPageFieldsSource, new() diff --git a/src/Kentico.Xperience.Typesense/Collection/DefaultTypesenseClient.cs b/src/Kentico.Xperience.Typesense/Collection/DefaultTypesenseClient.cs index 9a69021..968bd85 100644 --- a/src/Kentico.Xperience.Typesense/Collection/DefaultTypesenseClient.cs +++ b/src/Kentico.Xperience.Typesense/Collection/DefaultTypesenseClient.cs @@ -154,7 +154,7 @@ public async Task TryDeleteCollection(ITypesenseConfigurationModel? config } /// - public Task UpsertRecords(IEnumerable dataObjects, string collectionName, CancellationToken cancellationToken) + public Task UpsertRecords(IEnumerable dataObjects, string collectionName, ImportType importType = ImportType.Create, CancellationToken cancellationToken = default) { if (string.IsNullOrEmpty(collectionName)) { @@ -166,7 +166,7 @@ public Task UpsertRecords(IEnumerable dataObjec return Task.FromResult(0); } - return UpsertRecordsInternal(dataObjects, collectionName, cancellationToken); + return UpsertRecordsInternal(dataObjects, collectionName, importType, cancellationToken); } private async Task DeleteRecordsInternal(IEnumerable objectIds, string collectionName) @@ -260,11 +260,11 @@ private async Task MapToEventItem(IWebPageConte return item; } - private async Task UpsertRecordsInternal(IEnumerable dataObjects, string collectionName, CancellationToken cancellationToken) + private async Task UpsertRecordsInternal(IEnumerable dataObjects, string collectionName, ImportType importType = ImportType.Create, CancellationToken cancellationToken = default) { try { - var response = await typesenseClient.ImportDocuments(collectionName, dataObjects); + var response = await typesenseClient.ImportDocuments(collectionName, dataObjects, importType: importType); return response.Count; } catch (Exception ex) @@ -273,21 +273,6 @@ private async Task UpsertRecordsInternal(IEnumerable> GetAllLanguages() => diff --git a/src/Kentico.Xperience.Typesense/Collection/DefaultTypesenseTaskProcessor.cs b/src/Kentico.Xperience.Typesense/Collection/DefaultTypesenseTaskProcessor.cs index b3ca511..63253b7 100644 --- a/src/Kentico.Xperience.Typesense/Collection/DefaultTypesenseTaskProcessor.cs +++ b/src/Kentico.Xperience.Typesense/Collection/DefaultTypesenseTaskProcessor.cs @@ -5,6 +5,8 @@ using Microsoft.Extensions.DependencyInjection; +using Typesense; + namespace Kentico.Xperience.Typesense.Collection; internal class DefaultTypesenseTaskProcessor : ITypesenseTaskProcessor @@ -35,7 +37,6 @@ public async Task ProcessTypesenseTasks(IEnumerable que .Where(item => item.TaskType != TypesenseTaskType.END_OF_REBUILD) .GroupBy(item => item.CollectionName); - foreach (var group in groups) { try @@ -46,6 +47,7 @@ public async Task ProcessTypesenseTasks(IEnumerable que var updateTasks = group.Where(queueItem => queueItem.TaskType is TypesenseTaskType.PUBLISH_INDEX or TypesenseTaskType.UPDATE); var upsertData = new List(); + var updateData = new List(); foreach (var queueItem in updateTasks) { var documents = await GetDocument(queueItem); @@ -53,7 +55,14 @@ public async Task ProcessTypesenseTasks(IEnumerable que { foreach (var document in documents) { - upsertData.Add(document); + if (queueItem.TaskType is TypesenseTaskType.UPDATE) + { + updateData.Add(document); + } + else + { + upsertData.Add(document); + } } } else @@ -67,7 +76,8 @@ public async Task ProcessTypesenseTasks(IEnumerable que .Select(x => x ?? "")); successfulOperations += await typesenseClient.DeleteRecords(deleteIds, group.Key, cancellationToken); - successfulOperations += await typesenseClient.UpsertRecords(upsertData, group.Key, cancellationToken); + successfulOperations += await typesenseClient.UpsertRecords(upsertData, group.Key, ImportType.Create, cancellationToken); + successfulOperations += await typesenseClient.UpsertRecords(updateData, group.Key, ImportType.Update, cancellationToken); } catch (Exception ex) { @@ -136,7 +146,6 @@ private async Task AddBaseProperties(ICollectionEventItemModel baseItem, Typesen } } } - } private static IEnumerable GetIdsToDelete(IEnumerable deleteTasks) diff --git a/src/Kentico.Xperience.Typesense/Collection/IXperienceTypesenseClient.cs b/src/Kentico.Xperience.Typesense/Collection/IXperienceTypesenseClient.cs index 08e6685..4af7a7d 100644 --- a/src/Kentico.Xperience.Typesense/Collection/IXperienceTypesenseClient.cs +++ b/src/Kentico.Xperience.Typesense/Collection/IXperienceTypesenseClient.cs @@ -1,6 +1,8 @@ using Kentico.Xperience.Typesense.Search; using Kentico.Xperience.Typesense.Xperience; +using Typesense; + namespace Kentico.Xperience.Typesense.Collection; @@ -44,13 +46,14 @@ public interface IXperienceTypesenseClient /// Logs an error if there are issues loading the node data. /// The objects to upsert into Typesense. /// The index to upsert the data to. + /// Create or update type /// The cancellation token for the task. /// /// /// /// /// The number of objects processed. - Task UpsertRecords(IEnumerable dataObjects, string collectionName, CancellationToken cancellationToken); + Task UpsertRecords(IEnumerable dataObjects, string collectionName, ImportType importType = ImportType.Create, CancellationToken cancellationToken = default); /// /// Rebuilds the Typesense index by removing existing data from Typesense and indexing all diff --git a/src/Kentico.Xperience.Typesense/Collection/IndexedItemModelExtensions.cs b/src/Kentico.Xperience.Typesense/Collection/IndexedItemModelExtensions.cs index 9fac47c..9a78599 100644 --- a/src/Kentico.Xperience.Typesense/Collection/IndexedItemModelExtensions.cs +++ b/src/Kentico.Xperience.Typesense/Collection/IndexedItemModelExtensions.cs @@ -50,9 +50,14 @@ public static bool IsCollectionedByCollection(this CollectionEventWebPageItemMod } // Supports wildcard matching - if (path.AliasPath.EndsWith("/%", StringComparison.OrdinalIgnoreCase)) + if (path.AliasPath.EndsWith("%", StringComparison.OrdinalIgnoreCase)) { - string pathToMatch = path.AliasPath[..^2]; + string pathToMatch = string.Empty; + if (!path.AliasPath.Equals("%")) + { + pathToMatch = path.AliasPath[..^2]; + } + var pathsOnPath = TreePathUtils.GetTreePathsOnPath(indexedItemModel.WebPageItemTreePath, true, false).ToHashSet(); return pathsOnPath.Any(p => p.StartsWith(pathToMatch, StringComparison.OrdinalIgnoreCase));