This is a follow-up to #3111984: Support for sorting.

With the logic we currently have in applySortingToQuery(), there's no way to filter by the "special" fields that Search API support.

\Drupal\search_api\QueryInterface::sort() which also support the following fields:

   *   - search_api_relevance: Sort by relevance.
   *   - search_api_datasource: Sort by datasource.
   *   - search_api_language: Sort by language.
   *   - search_api_id: Sort by item ID.

My proposal is not to add custom logic before adding the sort to the query and let Search API fail in case the passed sort is not supported (Patch to follow).

Comments

jsacksick created an issue. See original summary.

jsacksick’s picture

Status: Active » Needs review
StatusFileSize
new1.56 KB
jsacksick’s picture

StatusFileSize
new1.89 KB
new566 bytes

Expanding the tests with a sort on the "search_api_id" field.

mglaman’s picture

So we removed validation for invalid field values though. That prevents the API from letting the client know they chose an incorrect sort option

jsacksick’s picture

"So we removed validation for invalid field values though. That prevents the API from letting the client know they chose an incorrect sort option"

No, because Search API will throw an exception that's then caught by us when the query is executed. We can add a specific method to test this if you'd like.

jsacksick’s picture

When I try to sort by an invalid field, I get the following response:

    "errors": [
        {
            "title": "Bad Request",
            "status": "400",
            "detail": "Trying to sort on unknown field 'blabla'.",
            "links": {
                "via": {
                    "href": "http://xxx.test/jsonapi/index/products?sort=blabla"
                },
                "info": {
                    "href": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1"
                }
            }
        }
    ]

  • mglaman committed c9acfd8 on 8.x-1.x authored by jsacksick
    Issue #3122594 by jsacksick: Support "special" Search API sort fields
    
mglaman’s picture

Status: Needs review » Fixed

Woo!

Status: Fixed » Closed (fixed)

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