Configure sort policies
To meet the demanding retrieval performance requirements of search engines, the system provides two sorting stages: basic sort and business sort, also known as rough sort and fine sort. Basic sort quickly selects high-quality documents from the search results. The TOP N results then undergo detailed scoring in fine sort, and the best results are returned to the user. Basic sort therefore has a greater impact on performance, whereas business sort has a greater impact on the final ordering of results. Keep basic sort as simple and effective as possible by using only the key factors from business sort. Both stages are currently configured using sort expressions.
Sort expressions (Ranking Formula) let you customize the order of search results for an application. Specify an expression in a query request to sort the results. Sort expressions support basic operations (arithmetic, relational, logical, bitwise, and conditional operations), mathematical functions, and sort features. OpenSearch provides expression templates for common applications such as forums and news services. Select a template that suits your data and modify it to create your own expression.
Before configuring relevance sorting (business sort), understand the system sort policy. After clauses such as query retrieve the documents that meet the conditions, the documents enter the sorting stage. For more information, see sort clause. If no sort clause is specified, or if RANK is explicitly specified in the sort clause, the documents enter the relevance scoring stage.
The design of basic sort and business sort expressions depends primarily on your search requirements. The feature best practices include an article titled "Relevance in practice", which explains how to select and design sort factors for several common scenarios.
In sort expressions, use numeric values or numeric fields for basic operations, such as arithmetic, relational, logical, and conditional operations. Most functions do not support string values.
Basic operations
|
Operation |
Operator |
Description |
|
Unary operation |
- |
The minus sign negates the value of an expression, such as -1 or -max(width). |
|
Arithmetic operation |
+, -, *, / |
Example: width / 10 |
|
Relational operation |
==,!= ,>, <, >=, <= |
Example: width>=400 |
|
Logical operation |
and ,or,! |
Examples: width>=400 and height >= 300, !(a > 1 and b < 2) |
|
Bitwise operation |
&, |,^ |
Example: 3 & (price ^ pubtime) + (price | pubtime) |
|
Conditional operation |
if(cond, thenValue, elseValue) |
If cond is non-0, the if expression evaluates to thenValue. Otherwise, it evaluates to elseValue. For example, if(2, 3, 5) evaluates to 3, and if(0, 3, 5) evaluates to 5. (Note: String field types, such as literal and text, are not supported. The value range is that of int32.) |
|
in operation |
i in (value1, value2, ..., valuen) |
If i is in the set (value1, value2, ..., valuen), the expression evaluates to 1. Otherwise, it evaluates to 0. For example, 2 in (2, 4, 6) evaluates to 1, and 3 in (2, 4, 6) evaluates to 0. |
Mathematical functions
|
Function |
Description |
|
max(a, b) |
Returns the larger of a and b. |
|
min(a, b) |
Returns the smaller of a and b. |
|
ln(a) |
Returns the natural logarithm of a. |
|
log2(a) |
Returns the base-2 logarithm of a. |
|
log10(a) |
Returns the base-10 logarithm of a. |
|
sin(a) |
The sine function. |
|
cos(a) |
The cosine function. |
|
tan(a) |
The tangent function. |
|
asin(a) |
The arcsine function. |
|
acos(a) |
The arccosine function. |
|
atan(a) |
The arctangent function. |
|
ceil(a) |
Rounds a up to the nearest integer. For example, ceil(4.2) returns 5. |
|
floor(a) |
Rounds a down to the nearest integer. For example, floor(4.6) returns 4. |
|
sqrt(a) |
Returns the square root of a. For example, sqrt(4) returns 2. |
|
pow(a,b) |
Returns a raised to the power of b. For example, pow(2, 3) returns 8. |
|
now() |
Returns the current time in seconds since the Epoch (00:00:00 UTC, January 1, 1970). |
|
random() |
Returns a random value in the range [0, 1]. |
Built-in feature functions
OpenSearch provides a wide range of built-in feature functions, including LBS, text, and timeliness functions. Use and combine them in sort expressions to implement powerful relevance sorting.
Cava plug-ins
Cava is an efficient programming language developed by the OpenSearch engine team using LLVM. Its syntax is similar to Java, and its performance is comparable to C++. Cava is object-oriented and supports just-in-time (JIT) compilation and various safety checks that improve program robustness. Use Cava and the Cava libraries provided by OpenSearch to create custom sort plug-ins. Compared with the expressions supported by OpenSearch, sort plug-ins written in Cava offer the following benefits:
-
Greater flexibility: Cava supports richer syntax than expressions, including for loops, function definitions, and class definitions, so you can implement your business requirements.
-
Easier maintenance: Sort plug-ins written in Cava are more readable and easier to maintain than expressions.
-
Easier adoption: Because Cava syntax is similar to Java, developers who know Java can quickly learn to develop in Cava.
Tip: Cava plug-ins can be configured only for exclusive applications.
Configuration procedure
The following example configures a text relevance sort function to demonstrate how to configure basic sort and business sort:
1. Create a basic sort policy. In the OpenSearch console, choose Sort Configuration > Policy Management and click Create. Enter a policy name, set the scope to Basic Sort, set the type to Expression, and click Next:
Select static_bm25 as the scoring feature and set its weight to 10. (Note: A weight of 10 means that the score is multiplied by 10.) You can also select a search field and set a weight. The field must be an attribute field of a numeric type, such as INT, DOUBLE, or FLOAT. The field value multiplied by the weight is then added to the sort score:
For example, under Search Field, select the sale_price field and set its weight to 0.08.
After configuration, return to the Policy Management page.
2. Create a business sort policy. In the OpenSearch console, choose Sort Configuration > Policy Management and click Create. Enter a policy name, set the scope to Business Sort, set the type to Expression, and click Next:
Select text_relevance from the built-in functions, enter the name of a field configured in the index to query between the parentheses, and click Finish. In the sort configuration step, use the Field drop-down list to select a field, enter the sort expression text_relevance(brand) in the editor, and click Finish. After configuration, return to the Sort Configuration page.
3. View the sort results. On the search test page, configure the basic sort and business sort parameters and enable sort details. On the Search Test page, set first_rank_name to the name of the basic sort policy you created, such as test_1, set second_rank_name to the name of the business sort policy you created, such as test_2, and turn on Show Sort Details to view document score details. Check the scores calculated by each function.
Documents are scored in two stages: basic sort and business sort. Documents retrieved by query and filtered by filter first enter basic sort, where the basic sort expression selects documents with higher scores. The TOP N results then undergo detailed scoring using the business sort expression, and the best results are returned to the user. The scoring logic is as follows:
-
If only basic sort is configured, the document score is (10000 + the result of the basic sort expression). The maximum total score is 20000. A result above 20000 is capped at 20000.
-
If only business sort is configured, the document score is (10000 + the result of the business sort expression). There is no upper limit on the total score.
-
If both basic sort and business sort are configured, the final score of documents that enter business sort is (10000 + the result of the business sort expression). For all other documents, the final score is (10000 + the result of the basic sort expression), with a maximum total score of 20000. A result above 20000 is capped at 20000.
-
You can create multiple basic sort and business sort rules. However, each query request can use only 1 basic sort rule and one business sort rule at a time.
-
first_rank_name accepts only one sort expression name. You cannot use multiple basic sort expressions at the same time.
-
second_rank_name accepts only one sort expression name. You cannot use multiple business sort expressions at the same time.
SDK configuration examples
Java SDK example:
// Set the rough sort and fine sort expressions. This example uses the defaults.
Rank rank =new Rank();
rank.setFirstRankName("default");// Name of the basic sort policy
rank.setSecondRankName("default");// Name of the business sort policy
rank.setReRankSize(5);// Number of documents to score in fine sort
PHP SDK example:
// Specify the rough sort expression
$params->setFirstRankName('default');
// Specify the fine sort expression
$params->setSecondRankName('default');
Note:
-
If you configure default basic sort and business sort policies in the console and specify policies again in your code, the query API uses the policies specified in the code when the program runs.
-
To view sort details in code:
Add the format:fulljson parameter to the config clause.
In the response, sortExprValues contains the document score.
"sortExprValues": [ "10000.0399786383" ], "property": { }sortExprValues is an array of the values of the sort fields specified in the sort clause. For example:
sort=-price;-RANKIn this case, sortExprValues is [price, document score].
If sort is not specified, the document score is used by default.