From 8f8eeefd900357e8ce19edea7683dc2ce654c6bb Mon Sep 17 00:00:00 2001 From: Elitsa Marinovska <21998037+elit0451@users.noreply.github.com> Date: Mon, 8 May 2023 09:34:43 +0200 Subject: [PATCH] Delivery API: Fix default sorting when querying (#14207) * Fix default sort for the query endpoint * Revert changes in PathSortIndexer * If no sort query option is provided, keep the index sorting as default * Delete path sort and path sort indexer --------- Co-authored-by: kjac --- .../Indexing/Sorts/PathSortIndexer.cs | 15 ----------- .../Querying/Sorts/PathSort.cs | 27 ------------------- .../Services/ApiContentQueryService.cs | 18 +++++-------- 3 files changed, 7 insertions(+), 53 deletions(-) delete mode 100644 src/Umbraco.Cms.Api.Delivery/Indexing/Sorts/PathSortIndexer.cs delete mode 100644 src/Umbraco.Cms.Api.Delivery/Querying/Sorts/PathSort.cs diff --git a/src/Umbraco.Cms.Api.Delivery/Indexing/Sorts/PathSortIndexer.cs b/src/Umbraco.Cms.Api.Delivery/Indexing/Sorts/PathSortIndexer.cs deleted file mode 100644 index e85cb88eee..0000000000 --- a/src/Umbraco.Cms.Api.Delivery/Indexing/Sorts/PathSortIndexer.cs +++ /dev/null @@ -1,15 +0,0 @@ -using Umbraco.Cms.Core.DeliveryApi; -using Umbraco.Cms.Core.Models; - -namespace Umbraco.Cms.Api.Delivery.Indexing.Sorts; - -public sealed class PathSortIndexer : IContentIndexHandler -{ - internal const string FieldName = "path"; - - public IEnumerable GetFieldValues(IContent content) - => new[] { new IndexFieldValue { FieldName = FieldName, Value = content.Path } }; - - public IEnumerable GetFields() - => new[] { new IndexField { FieldName = FieldName, FieldType = FieldType.StringSortable } }; -} diff --git a/src/Umbraco.Cms.Api.Delivery/Querying/Sorts/PathSort.cs b/src/Umbraco.Cms.Api.Delivery/Querying/Sorts/PathSort.cs deleted file mode 100644 index 09eeeb0398..0000000000 --- a/src/Umbraco.Cms.Api.Delivery/Querying/Sorts/PathSort.cs +++ /dev/null @@ -1,27 +0,0 @@ -using Umbraco.Cms.Api.Delivery.Indexing.Sorts; -using Umbraco.Cms.Core; -using Umbraco.Cms.Core.DeliveryApi; - -namespace Umbraco.Cms.Api.Delivery.Querying.Sorts; - -public sealed class PathSort : ISortHandler -{ - private const string SortOptionSpecifier = "path:"; - - /// - public bool CanHandle(string query) - => query.StartsWith(SortOptionSpecifier, StringComparison.OrdinalIgnoreCase); - - /// - public SortOption BuildSortOption(string sort) - { - var sortDirection = sort.Substring(SortOptionSpecifier.Length); - - return new SortOption - { - FieldName = PathSortIndexer.FieldName, - Direction = sortDirection.StartsWith("asc") ? Direction.Ascending : Direction.Descending, - FieldType = FieldType.String - }; - } -} diff --git a/src/Umbraco.Cms.Api.Delivery/Services/ApiContentQueryService.cs b/src/Umbraco.Cms.Api.Delivery/Services/ApiContentQueryService.cs index 83d0f1652f..efff5ef6b7 100644 --- a/src/Umbraco.Cms.Api.Delivery/Services/ApiContentQueryService.cs +++ b/src/Umbraco.Cms.Api.Delivery/Services/ApiContentQueryService.cs @@ -1,7 +1,6 @@ using Examine; using Examine.Search; using Umbraco.Cms.Api.Delivery.Indexing.Selectors; -using Umbraco.Cms.Api.Delivery.Indexing.Sorts; using Umbraco.Cms.Core; using Umbraco.Cms.Core.DeliveryApi; using Umbraco.Cms.Core.Models.PublishedContent; @@ -13,13 +12,13 @@ namespace Umbraco.Cms.Api.Delivery.Services; internal sealed class ApiContentQueryService : IApiContentQueryService // Examine-specific implementation - can be swapped out { + private const string ItemIdFieldName = "itemId"; private readonly IExamineManager _examineManager; private readonly IRequestStartItemProviderAccessor _requestStartItemProviderAccessor; private readonly SelectorHandlerCollection _selectorHandlers; private readonly FilterHandlerCollection _filterHandlers; private readonly SortHandlerCollection _sortHandlers; private readonly string _fallbackGuidValue; - private readonly ISet _itemIdOnlyFieldSet = new HashSet { "itemId" }; public ApiContentQueryService( IExamineManager examineManager, @@ -70,7 +69,7 @@ internal sealed class ApiContentQueryService : IApiContentQueryService // Examin } // Handle Sorting - IOrdering? sortQuery = HandleSorting(sorts, queryOperation)?.SelectFields(_itemIdOnlyFieldSet); + IOrdering? sortQuery = HandleSorting(sorts, queryOperation); // If there is an invalid Sort option, we return no results if (sortQuery is null) @@ -79,7 +78,7 @@ internal sealed class ApiContentQueryService : IApiContentQueryService // Examin } ISearchResults? results = sortQuery - .SelectFields(_itemIdOnlyFieldSet) + .SelectField(ItemIdFieldName) .Execute(QueryOptions.SkipTake(skip, take)); if (results is null) @@ -89,8 +88,8 @@ internal sealed class ApiContentQueryService : IApiContentQueryService // Examin } Guid[] items = results - .Where(r => r.Values.ContainsKey("itemId")) - .Select(r => Guid.Parse(r.Values["itemId"])) + .Where(r => r.Values.ContainsKey(ItemIdFieldName)) + .Select(r => Guid.Parse(r.Values[ItemIdFieldName])) .ToArray(); return Attempt.SucceedWithStatus(ApiContentQueryOperationStatus.Success, new PagedModel(results.TotalItemCount, items)); @@ -178,7 +177,6 @@ internal sealed class ApiContentQueryService : IApiContentQueryService // Examin return true; } - private IOrdering? HandleSorting(IEnumerable sorts, IBooleanOperation queryCriteria) { IOrdering? orderingQuery = null; @@ -210,9 +208,7 @@ internal sealed class ApiContentQueryService : IApiContentQueryService // Examin }; } - return orderingQuery ?? // Apply default sorting (left-aligning the content tree) if no valid sort query params - queryCriteria - .OrderBy(new SortableField(PathSortIndexer.FieldName, SortType.String)) - .OrderBy(new SortableField(SortOrderSortIndexer.FieldName, SortType.Int)); + // Keep the index sorting as default + return orderingQuery ?? queryCriteria.OrderBy(); } }