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):

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

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.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

aaronpinero created an issue. See original summary.

aaronpinero’s picture

Issue summary: View changes
aaronpinero’s picture

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 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. I just wanted 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.

aaronpinero’s picture

StatusFileSize
new11.27 KB

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.

aaronpinero’s picture

StatusFileSize
new175.84 KB

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.

screen shot of new form fields

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.

aaronpinero’s picture

Status: Active » Needs work

a.dmitriiev’s picture

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 !

a.dmitriiev’s picture

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

aaronpinero’s picture

Version: 1.0.0-alpha39 » 1.0.0-alpha42
aaronpinero’s picture

I've rebased the issue fork to the latest 1.x and changed the version in the issue metadata to the latest release.

a.dmitriiev’s picture

Status: Needs work » Fixed

Merging this in

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

aaronpinero’s picture

Status: Fixed » Needs work

There are still some issues here. In one of the rebases, an error creeped in where sort rules can't be saved. Working on fixing this.

aaronpinero’s picture

After the new filter feature was added to the endpoint form, the sort rules section of the form (which appeared after the filter option) didn't seem to be grouped with the rest of the form. In src/Form/SearchApiEndpointForm.php, I tried moving the sort rules section of the form to appear *before* the filter option. This seems to have restored the ability to add, remove, and save sort rules.

My guess is that there is something odd with the HTML wrapping the filter options that caused the problem, and that having the sort rules appear before the filter option avoids the issue. So, I think this works. I will need to verify. It might also be a good idea to re-test the filter options. Ideally, the filter part of the form should be inspected to see what was causing this problem.

I opened a new merge request.

a.dmitriiev’s picture

Status: Needs work » Fixed

This was a problem https://git.drupalcode.org/project/search_api_decoupled/-/merge_requests... . I had fixed it and merged already. Do you still see problems with sort rules not being saved in the latest 1.x branch?

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

aaronpinero’s picture

Status: Fixed » Needs work

Okay, my apologies. I guess I didn't understand this had already been merged into -dev. For some reason I thought it had been merged into an earlier version.

Yes, if I load the -dev version of the module, I am able to successfully set and remove sort rules. For my part, that solves the issue.

aaronpinero’s picture

Status: Needs work » Reviewed & tested by the community
a.dmitriiev’s picture

Status: Reviewed & tested by the community » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.