search mobile facets autocomplete spellcheck crawler rankings weights synonyms analytics engage api customize documentation install setup technology content domains user history info home business cart chart contact email activate analyticsalt analytics autocomplete cart contact content crawling custom documentation domains email engage faceted history info install mobile person querybuilder search setup spellcheck synonyms weights engage_search_term engage_related_content engage_next_results engage_personalized_results engage_recent_results success add arrow-down arrow-left arrow-right arrow-up caret-down caret-left caret-right caret-up check close content conversions-small conversions details edit grid help small-info error live magento minus move photo pin plus preview refresh search settings small-home stat subtract text trash unpin wordpress x alert case_deflection advanced-permissions keyword-detection predictive-ai sso

Sorting

Engine Type Supported?
Crawler-based Engine YES
API-based Engine YES

Sort search results by a specific field, in either ascending or descending order.

If you need to sort by a string field, index a second version of the field as an enum.

By default, results will be sorted in descending order by the _score field.

The relevance calculation can be affected by Field Weights or applying Boosts.

q
required
The query used within your search.
engine_key
required
The Engine Key associated with your Engine. Found within your dashboard.
sort_field
optional
Selects the field over which to sort. Must be in relation to the fields in your Engine schema.
sort_direction
optional
Can be either asc or desc, ascending or descending.
Example - Return from the "books" DocumentType results that match the "the" search query, ordered by price from lowest to highest. Crawler based Engines use page as the default DocumentType.
curl -XGET 'https://search-api.swiftype.com/api/v1/public/engines/search.json' \
  -H 'Content-Type: application/json' \
  -d '{
        "engine_key": "YOUR_ENGINE_KEY",
        "q":"the",
        "sort_field":{"books":"price"},
        "sort_direction":{"books":"asc"}
      }'

Supported Field Types

Type Sorting, Sort Style
string Yes, alphanumerically.
text No
enum Yes, alphanumerically.
integer Yes, numerically.
float Yes, numerically.
date Yes, historically.
location No

Errors

Note that a 200 response code will still return in the result of errors.

Look at the returning array for error information.

Example - Errors for sorting on an unsortable field
{
  "auth_token": "YOUR_API_KEY",
  "q": "brothers",
  "sort_field": {"books": "title"}
}
Example Response
{
  "records": {
    "books": [...]
  },
  "info": {...},
  "errors": {
    "sort_field": {
      "books": ["Cannot sort by 'title'. Only enum, numeric, and date fields can be sorted."]
    }
  }
}

Stuck? Looking for help? Contact support or check out the Site Search community forum!