All Products
Search
Document Center

OpenSearch:Sort expressions

Last Updated:Feb 28, 2024

Syntax

OpenSearch Retrieval Engine Edition allows you to create calculation formulas by using the built-in operators. The following table describes the supported operators.

Operation

Operator

Description

Unary operation

-

The minus sign (-) is used to obtain the negative of the value that is obtained by using a specific expression. Examples: -1 and -max(width).

Arithmetic operation

+, -, *, /

Example: width/10.

Relational operation

==,!= ,>, <, >=, <=

Example: width >= 400.

Logical operation

and ,or,!

Example: width >= 400 and height >= 300, !(a > 1 and b < 2).

Bitwise operation

&, |,^

Example: 3 & (price ^ pubtime) + (price | pubtime).

Conditional operation

if(cond, thenValue, elseValue)

thenValue is returned if the cond parameter value is non-zero, and elseValue is returned if the cond parameter value is zero. For example, if(2, 3, 5) returns 3, and if(0, 3, 5) returns 5. Note: The value of the cond parameter cannot be a string, such as a value of the LITERAL or TEXT type. The value range must be the same as the value range of the INT32 type.

IN operation

i in [value1, value2, …, valuen]

The expression returns 1 if i is contained in the set [value1, value2, …, valuen]. Otherwise, 0 is returned. For example, 2 in [2, 4, 6] returns 1, and 3 in [2, 4, 6] returns 0.

You can use the attribute fields that are defined in the schema, the built-in functions, and various other feature functions to create expressions.​

Built-in functions

Function

Description

max(a, b)

Returns the larger value between a and b.

min(a, b)

Returns the smaller value between a and b.

ln(a)

Returns the natural logarithm of a.

log2(a)

Returns the logarithm of a with the base number of 2.

log10(a)

Returns the logarithm of a with the base number of 10.

sin(a)

Returns the sine of a.

cos(a)

Returns the cosine of a.

tan(a)

Returns the tangent of a.

asin(a)

Returns the arcsine of a.

acos(a)

Returns the arccosine of a.

atan(a)

Returns the arctangent of a.

ceil(a)

Returns the smallest integer that is greater than or equal to a. For example, ceil(4.2) returns 5.

floor(a)

Returns the greatest integer that is smaller than or equal to a. 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 the result of a raised to the power of b. For example, pow(2, 3) returns 8.

now()

Returns the number of seconds that have elapsed since 00:00:00 January 1, 1970 in UTC.

random()

Returns a random value from 0 to 1.

Feature functions

Function

Description

Rough sort expression

Fine sort expression

static_bm25

Returns the score of the static text relevance that indicates the matching degree between a query and a document.

Supported

Not supported

exact_match_boost

Returns the maximum weight of a specific search query.

Supported

Supported

timeliness

Returns the timeliness score of a document.

Supported

Supported

timeliness_ms

Returns the timeliness score of a document.

Supported

Supported

normalize

Normalizes a numeric value to a value in the [0,1] range based on different algorithms.

Supported

Supported

distance

Returns the spherical distance between two points. In most cases, you can use this function to calculate distances for a location-based service (LBS).

Supported

Supported

gauss_decay

Uses a Gaussian function to calculate a decay degree based on the distance between a value and a specified start point.

Supported

Supported

linear_decay

Uses a linear function to calculate a decay degree based on the distance between a value and a specified start point.

Supported

Supported

exp_decay

Uses an exponential function to calculate a decay degree based on the distance between a value and a specified start point.

Supported

Supported

kvpairs_value

Returns the value of a field that is specified in the kvpairs clause in a query statement.

Supported

Supported

in/notin

Checks whether the values of a specified field are in a specified value set or not in a specified value set.

Supported

Supported

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.

Supported

Supported

first_phase_score

Returns the score that is calculated by using a rough sort expression.

Not supported

Supported

text_relevance

Calculates the relevance score between the keywords in the search query and the text in a specified field.

Not supported

Supported

field_match_ratio

Returns the ratio of the number of terms that match the search query to the total number of terms in a specific field.

Not supported

Supported

query_match_ratio

Returns the ratio of the number of matched terms in a specific field to the total number of keywords in the search query.

Not supported

Supported

fieldterm_proximity

Returns the proximity between the keywords in the search query and the terms in a specific field.

Not supported

Supported

field_length

Returns the number of terms in a specific field.

Not supported

Supported

query_term_count

Returns the number of terms after the specified analyzer tokenizes the search query.

Not supported

Supported

query_term_match_count

Returns the number of terms in the search query that match terms in a document.

Not supported

Supported

field_term_match_count

Returns the number of terms in a specific field that match the search query.

Not supported

Supported

query_min_slide_window

Returns the ratio of the number of terms in a specific field that the search query matches to the minimum window of these terms in the field.

Not supported

Supported

Create a sort expression

Create a rough sort expression

To create a rough sort expression, you need to include the first_formula parameter in the kvpairs clause. Set the first_formula parameter to the expression that you want to use. You can also include the rank_trace parameter in the config clause to view the calculation logic. Example:

config=start:0,hit:10,rank_trace:DEBUG,format:json&&query=default:'Search'&&kvpairs=first_formula:price*0.1

Create a fine sort expression

To create a fine sort expression, you need to include the formula parameter in the kvpairs clause. Set the value of the formula parameter to the expression that you want to use. You can also include the rank_trace parameter in the config clause to view the calculation logic. Example:

config=start:0,hit:10,rank_trace:DEBUG,format:json&&query=default:'Search'&&kvpairs=formula:price*0.1

Usage notes

  • If you query a large number of documents, specify a suitable number of documents in a rough row expression. If you use a rough sort expression that requires a long period of time to process data, the query may fail due to a timeout error.

  • You can specify rough sort expressions and fine sort expressions in the same query.