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

Sort

Sort your results in an order other than document score.

A special sorting field name is available, _score, to order by relevance.

If a sort parameter is not given, results will be ordered by descending relevance.

Type Sorting
text Yes, alphanumerically.
number Yes, numerically.
date Yes, historically.
geolocation No

Sorting

Sort on one field.

Example - Sorting alphanumerically by the title of the national park.
curl -X GET 'https://host-2376rb.api.swiftype.com/api/as/v1/engines/national-parks-demo/search' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer search-soaewu2ye6uc45dr8mcd54v8' \
-d '{
  "query": "everglade",
  "sort": {
    "title": "desc"
  }
}'

Example - Sorting alphanumerically by the title of the national park.
import com.swiftype.appsearch.Client;
import com.swiftype.appsearch.ClientException;
import java.util.*;

Client client = new Client("host-2376rb", "search-soaewu2ye6uc45dr8mcd54v8");

String engineName = "national-parks-demo"; String query = "everglade";

Map<String, String> sort = new HashMap<>(); sort.put("title", title);

Map<String, Object> options = new HashMap<>(); options.put("sort", sort);

try { Map<String, Object> response = client.search(engineName, query, options); System.out.println(response); } catch (ClientException e) { System.out.println(e); }

Example - Sorting alphanumerically by the title of the national park.
const SwiftypeAppSearchClient = require('swiftype-app-search-node')

const client = new SwiftypeAppSearchClient('host-2376rb', 'search-soaewu2ye6uc45dr8mcd54v8')

const engineName = 'national-parks-demo' const query = 'everglade'

const options = { sort: { title: 'desc' } }

client .search(engineName, query, options) .then(response => console.log(response)) .catch(error => console.log(error))

Example - Sorting alphanumerically by the title of the national park.
require 'swiftype-app-search'

host_identifier = 'host-2376rb' api_key = 'search-soaewu2ye6uc45dr8mcd54v8' client = SwiftypeAppSearch::Client.new(host_identifier: host_identifier, api_key: api_key)

engine_name = 'national-parks-demo' query = 'everglade'

options = { sort: { title: 'desc' } }

begin puts client.search(engine_name, query, options) rescue SwiftypeAppSearch::ClientException => e puts e end

Example - Sorting alphanumerically by the title of the national park.
from swiftype_app_search import Client
from swiftype_app_search.exceptions import SwiftypeAppSearchError

host_identifier = 'host-2376rb' api_key = 'search-soaewu2ye6uc45dr8mcd54v8' client = Client(host_identifier, api_key)

engine_name = 'national-parks-demo' query = 'everglade'

options = options = { 'sort': { 'title': 'desc' } }

try: print(client.search(engine_name, query, options)) except SwiftypeAppSearchError as e: print(e)

Example - Sorting alphanumerically by the title of the national park.
const client = SwiftypeAppSearch.createClient({
  hostIdentifier: 'host-2376rb',
  searchKey: 'search-soaewu2ye6uc45dr8mcd54v8',
  engineName: 'national-parks-demo'
})

const query = 'everglade'

const options = { sort: { title: 'desc' } }

client .search(query, options) .then(function(response){ console.log(response) }) .catch(function(error){ console.log(error) })

Sorting on Multiple Fields

Sort on multiple fields by using an array.

Example - Sorting by document score and tie-breaking by the title of the national park.
curl -X GET 'https://host-2376rb.api.swiftype.com/api/as/v1/engines/national-parks-demo/search' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer search-soaewu2ye6uc45dr8mcd54v8' \
-d '{
  "query": "everglade",
  "sort": [
    { "_score": "desc" },
    { "title": "desc" }
  ]
}'

Example - Sorting by document score and tie-breaking by the title of the national park.
import com.swiftype.appsearch.Client;
import com.swiftype.appsearch.ClientException;
import java.util.*;

Client client = new Client("host-2376rb", "search-soaewu2ye6uc45dr8mcd54v8");

String engineName = "national-parks-demo"; String query = "everglade";

Map<String, String> sortByScore = new HashMap<>(); sortByScore.put("_score", "desc");

Map<String, String> sortByTitle = new HashMap<>(); sortByTitle.put("title", "desc");

Map<String, Object> options = new HashMap<>(); options.put("sort", Arrays.asList(sortByScore, sortByTitle));

try { Map<String, Object> response = client.search(engineName, query, options); System.out.println(response); } catch (ClientException e) { System.out.println(e); }

Example - Sorting by document score and tie-breaking by the title of the national park.
const SwiftypeAppSearchClient = require('swiftype-app-search-node')

const client = new SwiftypeAppSearchClient('host-2376rb', 'search-soaewu2ye6uc45dr8mcd54v8')

const engineName = 'national-parks-demo' const query = 'everglade'

const options = { sort: [ { _score: 'desc' }, { make: 'desc' } ] }

client .search(engineName, query, options) .then(response => console.log(response)) .catch(error => console.log(error))

Example - Sorting by document score and tie-breaking by the title of the national park.
require 'swiftype-app-search'

host_identifier = 'host-2376rb' api_key = 'search-soaewu2ye6uc45dr8mcd54v8' client = SwiftypeAppSearch::Client.new(host_identifier: host_identifier, api_key: api_key)

engine_name = 'national-parks-demo' query = 'everglade'

options = { sort: [ { _score: 'desc' }, { make: 'desc' } ] }

begin puts client.search(engine_name, query, options) rescue SwiftypeAppSearch::ClientException => e puts e end

Example - Sorting by document score and tie-breaking by the title of the national park.
from swiftype_app_search import Client
from swiftype_app_search.exceptions import SwiftypeAppSearchError

host_identifier = 'host-2376rb' api_key = 'search-soaewu2ye6uc45dr8mcd54v8' client = Client(host_identifier, api_key)

engine_name = 'national-parks-demo' query = 'everglade'

options = { 'sort': [ {'_score': 'desc'}, {'make': 'desc'} ] }

try: print(client.search(engine_name, query, options)) except SwiftypeAppSearchError as e: print(e)

Example - Sorting by document score and tie-breaking by the title of the national park.
const client = SwiftypeAppSearch.createClient({
  hostIdentifier: 'host-2376rb',
  searchKey: 'search-soaewu2ye6uc45dr8mcd54v8',
  engineName: 'national-parks-demo'
})

const query = 'everglade'

const options = { sort: [ { _score: 'desc' }, { make: 'desc' } ] }

client .search(query, options) .then(function(response){ console.log(response) }) .catch(function(error){ console.log(error) })

Errors

sortIf the field used to sort is not in the schema and is not _score.
If the sort JSON object has more than one key.

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