From ddade09b145c8fbe9184829841341e431a4b62a1 Mon Sep 17 00:00:00 2001 From: Kenn Jacobsen Date: Thu, 8 Jun 2023 10:59:29 +0200 Subject: [PATCH] Fix broken name sorting in the Delivery API (#14350) --- ...ryApiContentIndexFieldDefinitionBuilder.cs | 2 +- .../DeliveryApiContentIndexValueSetBuilder.cs | 20 ++++++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Umbraco.Infrastructure/Examine/DeliveryApiContentIndexFieldDefinitionBuilder.cs b/src/Umbraco.Infrastructure/Examine/DeliveryApiContentIndexFieldDefinitionBuilder.cs index f7d4024c57..0baaa4636d 100644 --- a/src/Umbraco.Infrastructure/Examine/DeliveryApiContentIndexFieldDefinitionBuilder.cs +++ b/src/Umbraco.Infrastructure/Examine/DeliveryApiContentIndexFieldDefinitionBuilder.cs @@ -69,7 +69,7 @@ internal sealed class DeliveryApiContentIndexFieldDefinitionBuilder : IDeliveryA FieldType.Number => FieldDefinitionTypes.Integer, FieldType.StringRaw => FieldDefinitionTypes.Raw, FieldType.StringAnalyzed => FieldDefinitionTypes.FullText, - FieldType.StringSortable => FieldDefinitionTypes.InvariantCultureIgnoreCase, + FieldType.StringSortable => FieldDefinitionTypes.FullTextSortable, _ => throw new ArgumentOutOfRangeException(nameof(field.FieldType)) }; diff --git a/src/Umbraco.Infrastructure/Examine/DeliveryApiContentIndexValueSetBuilder.cs b/src/Umbraco.Infrastructure/Examine/DeliveryApiContentIndexValueSetBuilder.cs index 4d95dc1cde..5e4796f3b2 100644 --- a/src/Umbraco.Infrastructure/Examine/DeliveryApiContentIndexValueSetBuilder.cs +++ b/src/Umbraco.Infrastructure/Examine/DeliveryApiContentIndexValueSetBuilder.cs @@ -15,6 +15,7 @@ internal sealed class DeliveryApiContentIndexValueSetBuilder : IDeliveryApiConte private readonly IContentService _contentService; private readonly IPublicAccessService _publicAccessService; private readonly ILogger _logger; + private readonly IDeliveryApiContentIndexFieldDefinitionBuilder _deliveryApiContentIndexFieldDefinitionBuilder; private DeliveryApiSettings _deliveryApiSettings; public DeliveryApiContentIndexValueSetBuilder( @@ -22,11 +23,13 @@ internal sealed class DeliveryApiContentIndexValueSetBuilder : IDeliveryApiConte IContentService contentService, IPublicAccessService publicAccessService, ILogger logger, + IDeliveryApiContentIndexFieldDefinitionBuilder deliveryApiContentIndexFieldDefinitionBuilder, IOptionsMonitor deliveryApiSettings) { _contentIndexHandlerCollection = contentIndexHandlerCollection; _publicAccessService = publicAccessService; _logger = logger; + _deliveryApiContentIndexFieldDefinitionBuilder = deliveryApiContentIndexFieldDefinitionBuilder; _contentService = contentService; _deliveryApiSettings = deliveryApiSettings.CurrentValue; deliveryApiSettings.OnChange(settings => _deliveryApiSettings = settings); @@ -35,6 +38,7 @@ internal sealed class DeliveryApiContentIndexValueSetBuilder : IDeliveryApiConte /// public IEnumerable GetValueSets(params IContent[] contents) { + FieldDefinitionCollection fieldDefinitions = _deliveryApiContentIndexFieldDefinitionBuilder.Build(); foreach (IContent content in contents.Where(CanIndex)) { var publishedCultures = PublishedCultures(content); @@ -56,7 +60,7 @@ internal sealed class DeliveryApiContentIndexValueSetBuilder : IDeliveryApiConte [UmbracoExamineFieldNames.NodeNameFieldName] = new object[] { content.GetPublishName(culture) ?? content.GetCultureName(culture) ?? string.Empty }, // primarily needed for backoffice index browsing }; - AddContentIndexHandlerFields(content, culture, indexValues); + AddContentIndexHandlerFields(content, culture, fieldDefinitions, indexValues); yield return new ValueSet(DeliveryApiContentIndexUtilites.IndexId(content, indexCulture), IndexTypes.Content, content.ContentType.Alias, indexValues); } @@ -108,7 +112,7 @@ internal sealed class DeliveryApiContentIndexValueSetBuilder : IDeliveryApiConte return cultures; } - private void AddContentIndexHandlerFields(IContent content, string? culture, Dictionary> indexValues) + private void AddContentIndexHandlerFields(IContent content, string? culture, FieldDefinitionCollection fieldDefinitions, Dictionary> indexValues) { foreach (IContentIndexHandler handler in _contentIndexHandlerCollection) { @@ -121,7 +125,17 @@ internal sealed class DeliveryApiContentIndexValueSetBuilder : IDeliveryApiConte continue; } - indexValues[fieldValue.FieldName] = fieldValue.Values.ToArray(); + // Examine will be case sensitive in the default setup; we need to deal with that for sortable text fields + if (fieldDefinitions.TryGetValue(fieldValue.FieldName, out FieldDefinition fieldDefinition) + && fieldDefinition.Type == FieldDefinitionTypes.FullTextSortable + && fieldValue.Values.All(value => value is string)) + { + indexValues[fieldValue.FieldName] = fieldValue.Values.OfType().Select(value => value.ToLowerInvariant()).ToArray(); + } + else + { + indexValues[fieldValue.FieldName] = fieldValue.Values.ToArray(); + } } } }