Sort

The search requests accept a sort parameter.

This parameter can be used to define the order in which the matched documents are returned.

Syntax

The sort attribute is a string of one or more sort conditions, separated by spaces. Each condition specifies a field to sort by, followed by a colon (:), then the sort direction.

The possible direction values are:

  • asc
  • desc

You can also sort results based on the relevancy score calculated by the search engine using the _score attribute.

The _score attribute doesn't accept a direction.

This is the sort syntax definition:

{field}:{direction}
  • field: The field by which to sort the results.
  • direction: The sort direction, either asc for ascending or desc for descending.

Example

Fields are sorted in the order they are specified in the sort string.

If you want to sort by the category field in ascending order, you would use:

category:asc

If you want to sort by the price field in descending order, you would use:

price:desc

You can also combine multiple fields. For example, to sort by category in ascending order and then by price in descending order, you would use:

category:asc price:desc

To sort results based on the relevancy score calculated by the search engine, you would use the _score attribute. For example:

_score

You can also combine the _score attribute with other fields. For example, to sort by category in ascending order and then by the relevancy score, you would use:

category:asc _score