You can specify the sort clause to sort query results in ascending order or descending order based on the specified fields.
The syntax of the sort clause: +Field 1;-Field 2.
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 multi-dimension sorting. You can specify multiple sorting rules in the sort clause and separate 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 the sort clause. RANK specifies the relevance scores that are calculated based on the sort expression that you specified.
The sort clause is optional. If you do not include the 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 the sort clause, the sort expression that you specified does not take effect.
The fields that you specify in the sort clause must be attribute fields that you specified in the schema.json file.
Functionality functions whose return values are of the INT or FLOAT type can be used as fields in the sort clause.
If you specify a field of the LITERAL type in the sort clause, the system sorts letters in alphabetical order, sorts numbers in descending order, and sorts Chinese characters based on their ASCII values.
In most cases, the sort clause does not support fields of the ARRAY type.
If the 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. For example, a fine sort expression can be normalize(field1)*100+normalize(field2)*10+normalize(field3)+first_phase_score*10000, where 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. In most cases, 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 keywords in a search query with tags of documents and generates scores for the documents based on the weights of matched tags. 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. For more information, see Configure sort expressions.
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: