sort command - Amazon OpenSearch Service

sort command

Use the sort command to sort search result by specified fields.

Note

To see which AWS data source integrations support this PPL command, see Commands.

Syntax

Use the following syntax:

sort <[+|-] sort-field>...
[+|-]
  • Optional.

  • The plus [+] stands for ascending order with NULL/MISSING values first.

  • The minus [-] stands for descending order with NULL/MISSING values last.

  • Default: Ascending order with NULL/MISSING values first.

sort-field
  • Mandatory.

  • The field used for sorting.

Example 1: Sort by one field

The example shows how to sort the document with the age field in ascending order.

PPL query:

os> source=accounts | sort age | fields account_number, age; fetched rows / total rows = 4/4 +------------------+-------+ | account_number | age | |------------------+-------| | 13 | 28 | | 1 | 32 | | 18 | 33 | | 6 | 36 | +------------------+-------+
Example 2: Sort by one field and return all the results

The example shows how to sort the document with the age field in ascending order.

PPL query:

os> source=accounts | sort age | fields account_number, age; fetched rows / total rows = 4/4 +------------------+-------+ | account_number | age | |------------------+-------| | 13 | 28 | | 1 | 32 | | 18 | 33 | | 6 | 36 | +------------------+-------+
Example 3: Sort by one field in descending order

The example shows how to sort the document with the age field in descending order.

PPL query:

os> source=accounts | sort - age | fields account_number, age; fetched rows / total rows = 4/4 +------------------+-------+ | account_number | age | |------------------+-------| | 6 | 36 | | 18 | 33 | | 1 | 32 | | 13 | 28 | +------------------+-------+
Example 4: Sort by multiple fields

The example shows how to sort the document with the gender field in ascending order and the age field in descending order.

PPL query:

os> source=accounts | sort + gender, - age | fields account_number, gender, age; fetched rows / total rows = 4/4 +------------------+----------+-------+ | account_number | gender | age | |------------------+----------+-------| | 13 | F | 28 | | 6 | M | 36 | | 18 | M | 33 | | 1 | M | 32 | +------------------+----------+-------+
Example 5: Sort by field include null value

The example shows how to sort the employer field by the default option (ascending order and null first). The result shows that the null value is in the first row.

PPL query:

os> source=accounts | sort employer | fields employer; fetched rows / total rows = 4/4 +------------+ | employer | |------------| | null | | AnyCompany | | AnyCorp | | AnyOrgty | +------------+