Adding support for sorting would be nice. I gave it a first quick shot.

Comments

opdrop created an issue. See original summary.

luksak’s picture

Status: Active » Needs work

This works perfectly sorting by title. Sorting by a date field doesn't. I can't find a pattern what's going wrong...

Also sorting by score would be nice.

luksak’s picture

Just to update my findings: Date field sorting worked for me once I figured out that #3094442: Support expanded filter parameters was causing the problem.

jsacksick’s picture

The patch looks good to me! It wouldn't hurt to expand the tests to test sorting though :).

luksak’s picture

Issue tags: +Needs tests
mglaman’s picture

Assigned: Unassigned » mglaman

Working on a test.

+++ b/src/Resource/IndexResource.php
@@ -135,6 +141,36 @@ final class IndexResource extends EntityResourceBase implements ContainerInjecti
+      $direction = isset($field[Sort::DIRECTION_KEY]) ? $field[Sort::DIRECTION_KEY] : 'ASC';

since Drupal 8 requires PHP7, we can go ahead and use ?? to streamline this.

mglaman’s picture

StatusFileSize
new2.48 KB

This didn't apply cleanly on HEAD, so here is a rerolled patch from using 3way merge.

mglaman’s picture

Assigned: mglaman » Unassigned
Status: Needs work » Needs review
StatusFileSize
new3.72 KB

Quick test attempt, and I will follow up later today.

Status: Needs review » Needs work

The last submitted patch, 8: 3111984-8.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

jsacksick’s picture

Tests are failing because of the wrong sort parameter (See https://www.drupal.org/docs/8/modules/jsonapi/sorting).

You can either specify a "direction" key, or just prepend a minus (-) before the field name to sort in a descending order.

mglaman’s picture

Status: Needs work » Needs review
StatusFileSize
new3.98 KB

Oops! Reroll per #10. Tests the short and expanded versions.

jsacksick’s picture

I think 'sort' => ['id'], should just be 'sort' => 'id',

Status: Needs review » Needs work

The last submitted patch, 11: 3111984-11.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

mglaman’s picture

Status: Needs work » Needs review
StatusFileSize
new3.97 KB

This is what I get for rolling patches while doing morning chores.

  • mglaman committed f267389 on 8.x-1.x
    Issue #3111984 by mglaman, opdrop, Lukas von Blarer, jsacksick: Support...
mglaman’s picture

Status: Needs review » Fixed
Issue tags: -Needs tests

Committed! 🥳

luksak’s picture

Thanks, sorting works perfectly like this.

If i am not missing something, the only really important feature missing is sorting by relevance. Setting a breakpoint in Drupal\jsonapi_search_api\Resource\IndexResource::applySortingToQuery() reveals only fields I added to the index, but the relevance is missing.

Should we create a follow-up for this?

mglaman’s picture

Lukas von Blarer, yes please open a follow up. There are specific Search API things we'll need to handle. We couldn't sort by relevance because we didn't support sorting. Now that we do, we need to add a specific handler for it.

Like I did here in the fulltext filter:

    $filter = $request->query->get(Filter::KEY_NAME);
    if (isset($filter['fulltext'])) {
      $query->keys($filter['fulltext']);
      unset($filter['fulltext']);
    }
jsacksick’s picture

@mglaman: Actually... I think our approach here should be to not worry ourselves about the fields that are passed (We should just add the sorts to the query, and let Search API throw an exception when a wrong sort parameter is passed.

But let's open a follow-up issue anyway.

jsacksick’s picture

Status: Fixed » Closed (fixed)

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