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 <kja@umbraco.dk>
This commit is contained in:
Elitsa Marinovska
2023-05-08 09:34:43 +02:00
committed by GitHub
parent 83230d377c
commit 8f8eeefd90
3 changed files with 7 additions and 53 deletions

View File

@@ -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<IndexFieldValue> GetFieldValues(IContent content)
=> new[] { new IndexFieldValue { FieldName = FieldName, Value = content.Path } };
public IEnumerable<IndexField> GetFields()
=> new[] { new IndexField { FieldName = FieldName, FieldType = FieldType.StringSortable } };
}

View File

@@ -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:";
/// <inheritdoc />
public bool CanHandle(string query)
=> query.StartsWith(SortOptionSpecifier, StringComparison.OrdinalIgnoreCase);
/// <inheritdoc/>
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
};
}
}

View File

@@ -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<string> _itemIdOnlyFieldSet = new HashSet<string> { "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<Guid>(results.TotalItemCount, items));
@@ -178,7 +177,6 @@ internal sealed class ApiContentQueryService : IApiContentQueryService // Examin
return true;
}
private IOrdering? HandleSorting(IEnumerable<string> 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();
}
}