All Products
Search
Document Center

OpenSearch:Sort clause

Last Updated:Feb 03, 2023

Overview

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

Description

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.

Usage notes

  1. 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.

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

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

  4. 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.

  5. In most cases, the sort clause does not support fields of the ARRAY type.

  6. 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).

Example:

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:

query=default:'Restaurant name'&&sort=+distance(lon,lat,"120.34256","30.56982")
  • 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.

Example:

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

Example

  1. 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).
  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)