diff --git a/src/VirtoCommerce.CatalogModule.Data/Search/Indexing/AggregationConverter.cs b/src/VirtoCommerce.CatalogModule.Data/Search/Indexing/AggregationConverter.cs index 2f390a9cb..fcc6c3c9b 100644 --- a/src/VirtoCommerce.CatalogModule.Data/Search/Indexing/AggregationConverter.cs +++ b/src/VirtoCommerce.CatalogModule.Data/Search/Indexing/AggregationConverter.cs @@ -483,11 +483,11 @@ protected virtual async Task AddLabelsAsync(IList aggregations, str } } - var outlineAggregations = aggregations.Where(x => x.Field == "__outline"); - await AddLabelsForOutlinesAsync(outlineAggregations); + var outlineNamedAggregations = aggregations.Where(x => x.Field == "__outline_named"); + await TryAddLabelsAsyncForOutlinesItems(outlineNamedAggregations); } - private async Task AddLabelsForOutlinesAsync(IEnumerable outlineAggregations) + private async Task TryAddLabelsAsyncForOutlinesItems(IEnumerable outlineAggregations) { var caregoriesDictionary = new Dictionary(); @@ -496,44 +496,63 @@ private async Task AddLabelsForOutlinesAsync(IEnumerable outlineAgg { foreach (var aggregationItem in outlineAggregation.Items) { - var aggregationItemLabel = aggregationItem.Labels.FirstOrDefault(); - if (aggregationItemLabel != null) + var categoryId = GetNamedOutlineId(aggregationItem.Value as string); + if (categoryId != null) { - caregoriesDictionary.TryAdd(aggregationItemLabel.Label, null); + caregoriesDictionary.TryAdd(categoryId, null); } } } + // Load categories var categries = await _categoryService.GetAsync(caregoriesDictionary.Keys.ToArray(), clone: false); foreach (var category in categries) { caregoriesDictionary[category.Id] = category; } - // Replace category ids with category names + // Add localized category names to labels foreach (var outlineAggregation in outlineAggregations) { foreach (var aggregationItem in outlineAggregation.Items) { - var aggregationItemLabel = aggregationItem.Labels.FirstOrDefault(); - if (aggregationItemLabel != null && - caregoriesDictionary.TryGetValue(aggregationItemLabel.Label, out var category) && - category != null) + var categoryId = GetNamedOutlineId(aggregationItem.Value as string); + if (categoryId != null && + caregoriesDictionary.TryGetValue(categoryId, out var category) && + category != null && + category.LocalizedName.Values.Count > 0) { - aggregationItemLabel.Label = category.Name; + var lozalizedLabels = category.LocalizedName.Values + .Select(x => new AggregationLabel { Language = x.Key, Label = !string.IsNullOrEmpty(x.Value) ? x.Value : category.Name }).ToList(); - if (category.LocalizedName.Values.Count > 0) // Add the language code to the label + var defaultCategoryLabel = aggregationItem.Labels.FirstOrDefault(x => x.Language == null); + if (defaultCategoryLabel != null) { - var lozalizedLabels = category.LocalizedName.Values - .Select(x => new AggregationLabel { Language = x.Key, Label = !string.IsNullOrEmpty(x.Value) ? x.Value : category.Name }).ToList(); - lozalizedLabels.Add(aggregationItemLabel); - aggregationItem.Labels = lozalizedLabels.ToArray(); + defaultCategoryLabel.Label = category.Name; + lozalizedLabels.Add(defaultCategoryLabel); } + + aggregationItem.Labels = lozalizedLabels.ToArray(); } } } } + private static string GetNamedOutlineId(string namedOutlineValue) + { + if (string.IsNullOrEmpty(namedOutlineValue)) + { + return null; + } + + // Outline structure: catalog/category1/.../categoryN/current-category-id___current-category-name + var outlineParts = namedOutlineValue.Split("/", StringSplitOptions.RemoveEmptyEntries); + var namedOutline = outlineParts[^1]; + + var namedOutlineParts = namedOutline.Split(ModuleConstants.OutlineDelimiter, StringSplitOptions.RemoveEmptyEntries); + return namedOutlineParts.Length == 2 ? namedOutlineParts[0] : namedOutline; + } + private async Task TryAddLabelsAsyncForProperty(IEnumerable allProperties, Aggregation aggregation) { // There can be many properties with the same name