ScoreExpression
The two classes a filter uses to contribute to the ranking score. Both live in Raxos\Search and implement QueryExpressionInterface from raxos/database.
ScoreExpression
Raxos\Search\ScoreExpression
Wraps one scoring expression and its weight. This is what a filter's apply() method returns. It is a final readonly class.
Signature
public function __construct(
public QueryLiteralInterface|QueryExpressionInterface $expression,
public array $params = [],
public int $weight = 1
)$expressionis the SQL expression to score with, either a literal or another query expression.$paramsholds any bound parameters for the expression.$weightmultiplies the expression when it is compiled, so a higher weight makes the filter count for more.
Methods
public function compile(
QueryInterface $query,
ConnectionInterface $connection,
GrammarInterface $grammar
): voidCompiles the wrapped expression followed by * {weight}.
Usage
A restriction-only filter returns a zero expression so it does not affect ranking:
use Raxos\Database\Query\Literal\Literal;
use Raxos\Search\ScoreExpression;
return new ScoreExpression(
expression: Literal::of(0),
weight: $this->weight
);A ranking filter returns a real expression, for example a closeness score:
use Raxos\Search\ScoreExpression;
use function Raxos\Database\Query\literal;
return new ScoreExpression(literal(
"case when {$col} = {$value} then 100 else 0 end"
));ScoreExpressions
Raxos\Search\ScoreExpressions
Sums a list of ScoreExpression values into the __score column that SearchProvider selects and orders by. It is a final readonly class.
Signature
public function __construct(
public array $expressions
)$expressions is the list of ScoreExpression values collected from every filter that matched for a model.
Methods
public function compile(
QueryInterface $query,
ConnectionInterface $connection,
GrammarInterface $grammar
): voidCompiles the expressions joined with +, wrapped in parentheses. SearchProvider builds this internally; you rarely construct it yourself.
Related
- Scoring: how these expressions combine into the final ranking.
- Filter classes: which built in filters return a real score and which return zero.