Can I set default sorting for queries?

I know that sorting is based on relevance if no other sorting is defined - and that's how I would like to have it.

However, when my search is facet-only, the score of each result is 1 and I'm not quite sure how the results get sorted. Would it be possible to specify a secondary sorting option, using the search-ui packages? I found this topic but I can't work out how I can set that on the client side.

Hi @reka.gay :

You can define the sorting fields in engine.json (see the Reference UI Guide for configuration details).

The sort component can be used in the search-ui library for achieving sorting, and it can be combined with the facets component.

Combining both sorting and facet components will do the proper search request for performing facets filtering and sorting.

Let us know about your progress!

Hi @Carlos_D , thanks for your reply!
My issue is that I would like to have relevance sorting (which is the default) when a term is being used but alphabetical when only facet filters are being used. I don't want to give the users an option to select the sort field.

Is there a way to achieve this? The sort component you're referring to is a user control, but I would like to set this as default behaviour.

From the Search API reference guide:

curl -X GET 'https://[instance id].ent-search.[region].[provider].cloud.es.io/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" }
  ]
}'

Is what I would like to have, but I can't work out how to edit the query when using the search-ui driver

Got it @reka.gay !

You can add specific options for each query that are not supported by the UI components using API Config.

This is an example on using the grouping feature from the Search API:

const connector = new AppSearchAPIConnector({
  searchKey: "search-371auk61r2bwqtdzocdgutmg",
  engineName: "search-ui-examples",
  hostIdentifier: "host-2376rb",
  beforeSearchCall: (existingSearchOptions, next) =>
    next({
      ...existingSearchOptions,
      group: { field: "title" }
    })
});

I believe the same can be used to add the Sort Search API options to the query.

@Carlos_D works like a charm, thank you!!