Adding support for sorting would be nice. I gave it a first quick shot.
| Comment | File | Size | Author |
|---|---|---|---|
| #14 | 3111984-14.patch | 3.97 KB | mglaman |
| #11 | 3111984-11.patch | 3.98 KB | mglaman |
| #8 | 3111984-8.patch | 3.72 KB | mglaman |
| #7 | 3111984-7.patch | 2.48 KB | mglaman |
| jsonapi-searchapi-sorting.patch | 2.57 KB | opdrop |
Comments
Comment #2
luksakThis 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.
Comment #3
luksakJust to update my findings: Date field sorting worked for me once I figured out that #3094442: Support expanded filter parameters was causing the problem.
Comment #4
jsacksick commentedThe patch looks good to me! It wouldn't hurt to expand the tests to test sorting though :).
Comment #5
luksakComment #6
mglamanWorking on a test.
since Drupal 8 requires PHP7, we can go ahead and use ?? to streamline this.
Comment #7
mglamanThis didn't apply cleanly on HEAD, so here is a rerolled patch from using 3way merge.
Comment #8
mglamanQuick test attempt, and I will follow up later today.
Comment #10
jsacksick commentedTests 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.
Comment #11
mglamanOops! Reroll per #10. Tests the short and expanded versions.
Comment #12
jsacksick commentedI think
'sort' => ['id'],should just be'sort' => 'id',Comment #14
mglamanThis is what I get for rolling patches while doing morning chores.
Comment #16
mglamanCommitted! 🥳
Comment #17
luksakThanks, 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?
Comment #18
mglamanLukas 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:
Comment #19
jsacksick commented@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.
Comment #20
jsacksick commentedFollow-up: #3122594: Support "special" Search API sort fields.