Sort clauses are used 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.
The format of a sort clause is +field1;-field2.
field1 and field2 are fields based on which query results are to be sorted. The plus sign (+) indicates that query results are to be sorted based on field1 in ascending order. The minus sign (-) indicates 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 means a sort clause can contain multiple sorting rules, which 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.
Sort clauses are 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, if defined, do not take effect.
Fields that are used in a sort clause must be those that are specified as attribute fields in the schema of an application.
Functionality functions whose return values are of the INT or FLOAT type can be used as fields in sort clauses.
If a sort clause contains a field of a literal data type, letters in query results are sorted in alphabetical order and numbers are sorted based on the values at each position one by one. Chinese characters are sorted based on their American Standard Code for Information Interchange (ASCII) values.
In most scenarios, sort clauses do not support fields of the ARRAY type.
The performance of multidimensional sorting, such as sort=-field1;-field2;-field3, depends on the characteristics of each field. Therefore, good performance is not ensured. We recommend that you use sort expressions. For example, a fine sort expression can be normalize(field1)*100+normalize(field2)*10+normalize(field3)+first_phase_score*10000, where first_phase_score indicates 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).
The following query shows that a user searches for a restaurant. The distance function is used to sort query results based on the distance in ascending order:
tag_match: matches query terms in a request with tags in documents and then allocates weights to the documents based on match results.
Example (To view details about the function, click the preceding link):
sort=-tag_match("user_options", options, "mul", "sum", "false", "true", 100).
Query documents in an application that contain "Zhejiang University" and sort the results by type in ascending order. If the types of multiple documents are the same, sort the documents by text relevance.
query=default:'Zhejiang University'&&sort=+type;-RANK // The fine sort expression can contain text_relevance(field).
Query documents in an application that contain "Zhejiang University" and sort the results by the sum of hits and comments in descending order: