Allow multiple sort fields

Created on 9 May 2025, about 2 months ago

Problem/Motivation

I would like to be able to sort search results by two fields. In my case, sorting by date (descending) and by title (ascending). Currently, the configuration options for an endpoint allow only one sorting field and one sorting order. Also, I have examined the prepareQuery function in the SearchApiEndpointController and it does not appear possible to provide a query parameter with the endpoint url indicating more than one sort field (like "&sort=date,title").

Proposed resolution

As a test, I added a hard-coded secondary sort in prepareQuery function (right below the existing sort declaration on line 225 of SearchApiEndpointController):

<pre>$query->sort($sort, $order); // existing line of code
$query->sort('title', 'asc');</pre>

This had the desired effect. My search results were sorted both by the default sort (date) and then the additional sort (title).

My suggestion is twofold:

  1. Update the prepareQuery function so that it could recognize and accept a comma-delimited string for the 'sort' parameter and the 'order' parameter
  2. Update the SearchApiEndpointForm in some way to allow adding multiple sort/order pairs

These changes might necessitate additional modifications elsewhere.

Data model changes

I'm not sure if there are data model changes. It might be necessary to store the default sort and order values in a different way so as to maintain the sort/order pairs.

✨ Feature request
Status

Active

Version

1.0

Component

Endpoint

Created by

πŸ‡ΊπŸ‡ΈUnited States aaronpinero

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @aaronpinero
  • πŸ‡ΊπŸ‡ΈUnited States aaronpinero

    I created a new branch for this modification and implemented the following:

    • Updated the config/schema for the module to add mapping for the sortrules array.
    • Updated the SearchApiEndpoint entity and the SearchApiEndpointInterface to add a new sortrules property and a getSortRules() function to retrieve the saved sortrules values.
    • Updated the SearchApiEndpointForm to add fields for specifying one or more sortrules.
    • Updated the SearchApiEndpointController to apply the sortrules when building the API response.
    • Provided some styling for the SearchApiEndpointForm

    I my changes, I have not removed the previous default_sort and default_order properties of the entity. I have also not removed the related interface functions or form fields. This was just to see if I could get this to work and it does appear to work in limited testing.

    These changes will cause a problem for the UI elements if someone is using those submodules. I'm not using those submodules, but I've inspected the code enough to know that the changes I am making would break something further down the line.

    But it's a start.

  • πŸ‡ΊπŸ‡ΈUnited States aaronpinero

    If anyone wants to try this modification, I am going to add a .patch file. I've successfully applied this patch file using composer to test the new setting of multiple sort rules.

  • πŸ‡ΊπŸ‡ΈUnited States aaronpinero

    To use the new multiple sort options, you modify an existing endpoint (which is what I did) after patching the module. When you go to the edit form for the endpoint, you will see a new fieldset at the bottom of the form for Sort Rules.

    This screenshot shows my endpoint with some sort rules already provided. You can remove sort roles with the remove buttons or add a new sort rule with the add button. The sort rules will be applied in the order shown.

  • πŸ‡ΊπŸ‡ΈUnited States aaronpinero
  • πŸ‡©πŸ‡ͺGermany a.dmitriiev

    The changes look good so far. I will need to verify how they work together with UI module. Definitely it should be possible to change the sorting with query parameter even if the default sorting rule(s) are set. But it is a very good start. I have opened the MR so that it is easier to see and review the changes.

    Thanks @aaronpinero !

  • πŸ‡©πŸ‡ͺGermany a.dmitriiev

    I have rebased the branch, as predefined filters functionality from ✨ Add pre-configurable filters to endpoint Active was merged in already.

Production build 0.71.5 2024