Global sorting - sort clause

更新时间:
复制 MD 格式

This topic describes the syntax of the `sort` clause for global sorting. It also provides notes and examples of function usage.

Clause description

You can use search statements to control the sort order of results. Specify the sort field and whether to sort in ascending or descending order.

Syntax

The sort clause uses the following format: +field1;-field2.

  • `field` is the field to sort by. `+` sorts in ascending order based on the field value, and `-` sorts in descending order.

  • `field` also supports simple arithmetic operations, such as +, -, *, and /. The fields involved in the operation must have the same data type.

  • Multi-dimensional sorting is supported. Use a semicolon (;) to separate dimensions. For multi-dimensional sorting, results are first sorted by the score of the first dimension. If scores in the first dimension are the same, the results are then sorted by the score of the second dimension, and so on.

  • The `field` can also be `RANK`. This sorts the results by relevance, which is the score calculated by the sort expression.

Notes

  1. The `sort` clause is optional. If you do not specify this clause, the default is `sort=-RANK`. This returns results in descending order of relevance score. If you explicitly use the `sort` clause and it does not include `RANK`, the defined sort expression does not take effect.

  2. Fields that appear in the `sort` clause must be created as properties when you define the application schema.

  3. Functions that return a numeric value, such as int or float, can also be used in the `sort` clause.

  4. For `literal` type fields, English letters are sorted alphabetically. Numbers are compared and sorted digit by digit. Chinese characters are sorted by their ASCII codes.

  5. Fields of the `array` type are not supported in most scenarios.

  6. The performance of multi-dimensional sorting, such as `sort=-field1;-field2;-field3`, depends on the data characteristics of each dimension. Because of this, stable performance cannot be guaranteed. We recommend that you use a sort expression for sorting. For example, to set a fine sort expression: `normalize(field1)*100+normalize(field2)*10+normalize(field3)+first_phase_score*10000`. This expression obtains the score from the basic sort expression. For more information, see Fine sort functions.

Using functions

distance: Calculates the spherical distance between two points. This is generally used for distance calculations in location-based services (LBS).

Example:

Find locations for 'Grandma's house' and sort them by distance from nearest to farthest:

query=default:'Grandma's house'&&sort=+distance(lon,lat,"120.34256","30.56982")

tag_match: Matches tags between a search statement and a document. It uses the match result to calculate a weighted score for the document.

Example: (For a detailed example, click the link above.)

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

Examples

  1. Find documents in the application that contain 'Zhejiang University'. Sort them in ascending order by `type`. If the `type` is the same, sort by text relevance. For more information, see Configure a sort policy:

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

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