# Can I set default sorting for queries?

**URL:** <https://discuss.elastic.co/t/can-i-set-default-sorting-for-queries/280074>\
**Category:** Elastic Search\
**Created:** [July 30, 2021, 11:27am UTC](https://discuss.elastic.co/t/can-i-set-default-sorting-for-queries/280074 "2021-07-30T11:27:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![reka.gay](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/reka.gay/32/91470_2.png) [@reka.gay](https://discuss.elastic.co/u/reka.gay)\
**Post date:** [July 30, 2021, 11:27am UTC](https://discuss.elastic.co/t/can-i-set-default-sorting-for-queries/280074/1 "2021-07-30T11:27:19Z")

</div>

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](https://www.elastic.co/guide/en/app-search/current/sort.html#sort-multi-field-sorting) topic but I can't work out how I can set that on the client side.

---

<div class="post-metadata">

**Author:** ![Carlos\_D](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carlos_d/32/126245_2.png) [@Carlos\_D](https://discuss.elastic.co/u/Carlos_D)\
**Post date:** [August 3, 2021, 6:11pm UTC](https://discuss.elastic.co/t/can-i-set-default-sorting-for-queries/280074/2 "2021-08-03T18:11:49Z")

</div>

Hi @reka.gay :

You can define the sorting fields in engine.json (see the [Reference UI Guide](https://www.elastic.co/guide/en/app-search/current/reference-ui-guide.html) for configuration details).

The [sort component](https://github.com/elastic/search-ui/blob/master/ADVANCED.md#sorting) can be used in the [search-ui library](https://github.com/elastic/search-ui/) for achieving sorting, and it can be combined with the [facets component](https://github.com/elastic/search-ui/blob/master/ADVANCED.md#facet).

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

Let us know about your progress!

---

<div class="post-metadata">

**Author:** ![reka.gay](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/reka.gay/32/91470_2.png) [@reka.gay](https://discuss.elastic.co/u/reka.gay)\
**Post date:** [August 4, 2021, 4:50am UTC](https://discuss.elastic.co/t/can-i-set-default-sorting-for-queries/280074/3 "2021-08-04T04:50:23Z")

</div>

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:

```auto
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

---

<div class="post-metadata">

**Author:** ![Carlos\_D](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carlos_d/32/126245_2.png) [@Carlos\_D](https://discuss.elastic.co/u/Carlos_D)\
**Post date:** [August 5, 2021, 9:17am UTC](https://discuss.elastic.co/t/can-i-set-default-sorting-for-queries/280074/4 "2021-08-05T09:17:34Z")

</div>

Got it @reka.gay !

You can add specific options for each query that are not supported by the UI components using [API Config](https://github.com/elastic/search-ui/blob/master/ADVANCED.md#api-config).

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

```auto
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](https://www.elastic.co/guide/en/app-search/current/sort.html) options to the query.

---

<div class="post-metadata">

**Author:** ![reka.gay](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/reka.gay/32/91470_2.png) [@reka.gay](https://discuss.elastic.co/u/reka.gay)\
**Post date:** [August 9, 2021, 6:04am UTC](https://discuss.elastic.co/t/can-i-set-default-sorting-for-queries/280074/5 "2021-08-09T06:04:34Z")

</div>

@Carlos_D works like a charm, thank you!!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 4, 2022, 8:31am UTC](https://discuss.elastic.co/t/can-i-set-default-sorting-for-queries/280074/6 "2022-11-04T08:31:39Z")

</div>


