Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion en/ranking/ranking-expressions-features.html
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,16 @@ <h3 id="accessing-feature-function-values-in-results">Accessing feature/function



<h2 id="the-if-function-and-string-equality-tests">The "if" function and string equality tests</h2>
<h2 id="conditional-expressions">Conditional expressions</h2>

<p>
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.
</p>


<h3 id="the-if-function">The if function</h3>
<span id="the-if-function-and-string-equality-tests"></span>

<p>
<code>if</code> can be used for other purposes than encoding MLR trained decision trees.
Expand Down Expand Up @@ -268,6 +277,27 @@ <h2 id="the-if-function-and-string-equality-tests">The "if" function and string
</p>


<h3 id="the-switch-function">The switch function</h3>
<p>
When comparing many values against the same discriminant value,
the <code>switch</code> function provides a more readable alternative to deeply nested <code>if</code> statements.
For example, the nested if expression above can be written more clearly as:
</p>
<pre>
switch (attribute(category)) {
case "restaurant": <em>&hellip;restaurant function</em>,
case "hotel": <em>&hellip;hotel function</em>,
default: <em>&hellip;default function</em>
}
</pre>
<p>
Use <code>switch</code> when testing a single expression for equality against multiple values.
Continue using <code>if</code> for different comparison operators or when each condition tests different expressions,
such as in the tiering example above.
See the <a href="../reference/ranking/ranking-expressions.html#the-switch-function">switch function reference</a> for details.
</p>


<h2 id="using-constants">Using constants</h2>

<p>
Expand Down
25 changes: 25 additions & 0 deletions en/reference/ranking/ranking-expressions.html
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,31 @@ <h2 id="the-if-function">The if function</h2>



<h2 id="the-switch-function">The switch function</h2>
{% include note.html content='Available from Vespa 8.626.55' %}
<p>
The <code>switch</code> function chooses between multiple sub-expressions based on matching a value.
It provides a more readable alternative to chained <code>if</code> statements when selecting from several options.
</p>
<pre>
switch (discriminant) {
case value1: result1,
case value2: result2,
...
default: defaultResult
}
</pre>
<p>
The <code>discriminant</code> expression is compared for equality against each <code>case</code> value expression in order.
When a match is found, the corresponding result expression is evaluated and returned.
If no case matches, the <code>default</code> result is returned.
All expressions may be any ranking expression.
At least one <code>case</code> must be specified.
The <code>default</code> must be specified.
</p>



<h2 id="the-foreach-function">The foreach function</h2>
<p>
The foreach function is not really part of the expression language but implemented as a
Expand Down