diff --git a/en/ranking/ranking-expressions-features.html b/en/ranking/ranking-expressions-features.html index 880737235e..137c9c7797 100644 --- a/en/ranking/ranking-expressions-features.html +++ b/en/ranking/ranking-expressions-features.html @@ -233,7 +233,16 @@

Accessing feature/function -

The "if" function and string equality tests

+

Conditional expressions

+ +

+Ranking expressions support conditional logic to choose between different sub-expressions based on document attributes, query parameters, or other features. +This enables ranking to adapt to different document types, user segments, or business rules within a single rank profile. +

+ + +

The if function

+

if can be used for other purposes than encoding MLR trained decision trees. @@ -268,6 +277,27 @@

The "if" function and string

+

The switch function

+

+When comparing many values against the same discriminant value, +the switch function provides a more readable alternative to deeply nested if statements. +For example, the nested if expression above can be written more clearly as: +

+
+switch (attribute(category)) {
+    case "restaurant": …restaurant function,
+    case "hotel": …hotel function,
+    default: …default function
+}
+
+

+Use switch when testing a single expression for equality against multiple values. +Continue using if for different comparison operators or when each condition tests different expressions, +such as in the tiering example above. +See the switch function reference for details. +

+ +

Using constants

diff --git a/en/reference/ranking/ranking-expressions.html b/en/reference/ranking/ranking-expressions.html index 5529a68069..a7f71ea2ec 100644 --- a/en/reference/ranking/ranking-expressions.html +++ b/en/reference/ranking/ranking-expressions.html @@ -172,6 +172,31 @@

The if function

+

The switch function

+{% include note.html content='Available from Vespa 8.626.55' %} +

+The switch function chooses between multiple sub-expressions based on matching a value. +It provides a more readable alternative to chained if statements when selecting from several options. +

+
+switch (discriminant) {
+    case value1: result1,
+    case value2: result2,
+    ...
+    default: defaultResult
+}
+
+

+The discriminant expression is compared for equality against each case value expression in order. +When a match is found, the corresponding result expression is evaluated and returned. +If no case matches, the default result is returned. +All expressions may be any ranking expression. +At least one case must be specified. +The default must be specified. +

+ + +

The foreach function

The foreach function is not really part of the expression language but implemented as a