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
ordesc
, 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!