All Products
Search
Document Center

OpenSearch:sort clause

Last Updated:Apr 01, 2026

A sort clause controls the order of query results by one or more fields. Without a sort clause, results default to descending order by relevance score (RANK).

Syntax

A sort clause follows this format:

+field1;-field2
  • + sorts by the field in ascending order.

  • - sorts by the field in descending order.

  • Separate multiple sorting rules with semicolons (;).

  • Connect fields with arithmetic operators (+, -, *, /) to sort by a computed value. Fields joined by an operator must share the same data type.

  • Sort by the RANK field to order results by relevance score, which is calculated by a sort expression.

Quick reference

ExampleResult
+priceAscending by price
-priceDescending by price
-(hits+comments)Descending by the sum of hits and comments
+type;-RANKAscending by type; ties broken by descending relevance
+distance(lon,lat,"120.34256","30.56982")Ascending by distance from the given coordinates

Multidimensional sorting

A sort clause can contain multiple sorting rules separated by semicolons. Results are sorted by the first rule; when values are equal, the next rule applies, and so on.

sort=-field1;-field2;-field3

Multidimensional sorting performance depends on field characteristics and is not guaranteed. For better performance, use a fine sort expression to combine multiple field signals into a single numeric score:

normalize(field1)*100+normalize(field2)*10+normalize(field3)+first_phase_score*10000

Here, first_phase_score is the score computed by the rough sort expression. This approach gives you predictable sort performance and lets you tune field weights explicitly. For details, see Fine sort functions.

Limitations

  1. A sort clause is optional. Without one, results are sorted by RANK in descending order. If a sort clause does not involve the RANK field, sort expressions do not take effect.

  2. Fields in a sort clause must be attribute fields defined in the application schema.

  3. Functionality functions that return INT or FLOAT values can be used as sort fields.

  4. For LITERAL fields: letters are sorted alphabetically, numbers are sorted digit-by-digit at each position, and Chinese characters are sorted by ASCII value.

  5. ARRAY fields are not supported in most scenarios.

Supported functionality functions

distance

Returns the spherical distance between two points. Commonly used to sort results by proximity in location-based service (LBS) scenarios.

Example: Sort restaurant search results by distance in ascending order.

query=default:'Restaurant name'&&sort=+distance(lon,lat,"120.34256","30.56982")

For the full function reference, see distance.

tag_match

Matches query clauses against documents by tags and scores documents based on the weights of matched tags.

Example:

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

For the full function reference, see tag_match.

Examples

Sort by field, then by relevance

Search for "Zhejiang University" and sort results by type ascending, with ties broken by text relevance descending.

query=default:'Zhejiang University'&&sort=+type;-RANK

The fine sort expression can be text_relevance(field). For sort policy configuration details, see Configure sort policies.

Sort by computed field value

Search for "Zhejiang University" and sort results by the combined total of hits and comments in descending order.

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

What's next