All Products
Search
Document Center

OpenSearch:sort clause

Last Updated:Feb 23, 2023

Overview

You can use sort clauses to configure sorting rules for query results. A sorting rule specifies a field based on which query results are to be sorted and a sorting order. Sorting orders include the ascending order and the descending order.

Syntax

Syntax of a sort clause: +field1;-field2

  • field1 and field2 are fields based on which query results are to be sorted. The plus sign (+) specifies that query results are to be sorted based on field1 in ascending order. The minus sign (-) specifies that query results are to be sorted based on field2 in descending order.

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

  • OpenSearch supports multidimensional sorting. This indicates that a sort clause can contain multiple sorting rules. The sorting rules must be separated by semicolons (;). Query results are sorted first based on the first sorting rule in the sort clause. If the query results have the same value of the first specified field, the query results are then sorted based on the second sorting rule. The process continues until all the query results are properly sorted.

  • Query results can also be sorted based on the RANK field, whose value is calculated by a sort expression and indicates the relevance of results.

Limits

  1. A sort clause is optional. If a query does not contain a sort clause, the query results are sorted in descending order based on the RANK field by default. If a query explicitly contains a sort clause that does not involve the RANK field, sort expressions that are defined do not take effect.

  2. Fields that are used in a sort clause must be those that are specified as attribute fields in the schema of an application.

  3. Functionality functions whose return values are of the INT or FLOAT type can be used as fields in sort clauses.

  4. If a sort clause contains a field of the LITERAL type, letters in query results are sorted in alphabetical order and numbers are sorted based on the digits at each position one by one. Chinese characters are sorted based on their American Standard Code for Information Interchange (ASCII) values.

  5. In most scenarios, sort clauses do not support fields of the ARRAY type.

  6. The performance of multidimensional sorting, such as sort=-field1;-field2;-field3, depends on the characteristics of each field. Therefore, the performance of multidimensional sorting is not guaranteed. We recommend that you use sort expressions. For example, you can define the following fine sort expression: normalize(field1)*100+normalize(field2)*10+normalize(field3)+first_phase_score*10000. In the expression, first_phase_score specifies the score calculated by a rough sort expression. For more information, see Fine sort functions.

Supported functionality functions

  • distance: returns the spherical distance between two points. Generally, this function is used in distance calculation for a location-based service (LBS).

Example:

The following query searches for restaurants. The distance function is used to sort query results based on the distance in ascending order:

query=default:'Restaurant name'&&sort=+distance(lon,lat,"120.34256","30.56982")
  • tag_match: matches query clauses with documents based on tags and scores the documents by calculating the weights of matched tags.

Example (To view details about the function, click the preceding link):

sort=-tag_match("user_options", options, "mul", "sum", "false", "true", 100)

Examples

  1. Query documents in an application that contain "Zhejiang University" and sort the results by type in ascending order. If some documents are of the same type, sort the documents by text relevance. For more information, see Configure sort policies.

    query=default:'Zhejiang University'&&sort=+type;-RANK    // The fine sort expression can be text_relevance(field).
  2. Query documents in an application that contain "Zhejiang University" and sort the results by the sum of hits and comments in descending order:

    query=default:'Zhejiang University'&&sort=-(hits+comments)