All Products
Search
Document Center

OpenSearch:sort clause

Last Updated:Feb 28, 2024

Description

You can specify a sort clause to sort query results in ascending order or descending order based on the specified fields.

Syntax

Syntax of a sort clause: +Field 1;-Field 2.

  • Field 1 and Field 2 are the fields based on which the system sorts query results. The plus sign (+) specifies to sort the query results in ascending order. The minus sign (-) specifies to sort the documents in descending order.

  • You can join fields by using basic arithmetic operators, such as the addition operator (+), subtraction operator (-), multiplication operator (*), and division operator (/). The fields that you join by using an operator must be of the same data type.

  • OpenSearch Retrieval Engine Edition supports multi-dimension sorting. You can specify multiple sorting rules in a sort clause and separate the sorting rules with semicolons (;). If you specify multiple sorting rules in a query, the system sorts the query results based on the first sorting rule in the sort clause and then sorts the query results whose relevance scores that are obtained during the first sorting are the same based on the second sorting rule. The process continues until all query results are sorted as expected.

  • You can specify RANK as a sorting rule in a sort clause. RANK specifies the relevance scores that are calculated based on the sort expression that you specified.

Examples:

sort=+type;-RANK: specifies that the system sorts query results in ascending order based on values in the type field and then sorts the query results that contain the same value in the type field in descending order based on the relevance scores that are calculated by using the sort expression that you specified.
sort=-(hits + comments): specifies that the system sorts query results in descending order based on the sum of the value in the hits field and the value in the comments field in each row.
sort=+distance(lon,lat,"120.34256","30.56982"): specifies that the system sorts query results in ascending order based on the distances between the customer whose location is (120.34256,30.56982) and sellers whose locations are specified by the lon parameter and lat parameter.

Usage notes

  • A sort clause is optional. If you do not include a sort clause in a statement, the default sort clause sort=-RANK is used. The system sorts query results in descending order based on the relevance scores that are calculated based on the sort expression that you specified. If you do not specify RANK in a sort clause, the sort expression that you specified does not take effect.

  • The fields that you specify in a sort clause must be attribute fields that you specified in the schema.json file.

  • You can also use sort functions that return values of the INT type or FLOAT type in a sort clause.

  • If you specify a field of the STRING type in a sort clause, the system sorts letters in alphabetical order, sorts numbers in descending order, and sorts Chinese characters based on their ASCII values.

  • You cannot specify multi-value fields in a sort clause.

  • If a sort clause contains multiple sorting rules, such as sort=-field1;-field2;-field3, the sorting performance varies based on the data in each field that is specified in the sorting rules. To ensure stable sorting performance, we recommend that you specify RANK to create sorting rules.