|
6 | 6 | use Ensi\LaravelElasticQuery\Contracts\DSLAware; |
7 | 7 | use Ensi\LaravelElasticQuery\Contracts\FunctionScoreItem; |
8 | 8 | use Ensi\LaravelElasticQuery\Contracts\FunctionScoreOptions; |
| 9 | +use Ensi\LaravelElasticQuery\Contracts\FunctionScoreScript; |
9 | 10 | use stdClass; |
10 | 11 | use Webmozart\Assert\Assert; |
11 | 12 |
|
12 | 13 | class FunctionScore implements Criteria |
13 | 14 | { |
14 | 15 | /** |
15 | 16 | * @param array<FunctionScoreItem> $functions |
16 | | - * @param FunctionScoreOptions|null $options |
17 | 17 | */ |
18 | 18 | public function __construct( |
19 | | - private array $functions, |
20 | | - private ?DSLAware $query = null, |
21 | | - private ?FunctionScoreOptions $options = null, |
| 19 | + protected ?DSLAware $query = null, |
| 20 | + protected ?FunctionScoreOptions $options = null, |
| 21 | + protected array $functions = [], |
| 22 | + protected ?FunctionScoreScript $scriptScore = null, |
| 23 | + protected ?float $weight = null, |
22 | 24 | ) { |
23 | | - array_map(fn ($function) => Assert::isInstanceOfAny($function, [FunctionScoreItem::class]), $functions); |
| 25 | + Assert::allIsInstanceOfAny($functions, [FunctionScoreItem::class]); |
24 | 26 | } |
25 | 27 |
|
26 | 28 | public function toDSL(): array |
27 | 29 | { |
28 | 30 | $body = [ |
29 | 31 | 'query' => $this->query?->toDSL() ?? ['match_all' => new stdClass()], |
30 | | - 'functions' => array_map(fn (FunctionScoreItem $function) => $function->toArray(), $this->functions), |
31 | 32 | ]; |
32 | 33 |
|
| 34 | + if ($this->functions) { |
| 35 | + $body['functions'] = array_map(fn (FunctionScoreItem $function) => $function->toArray(), $this->functions); |
| 36 | + } |
| 37 | + |
| 38 | + if ($this->scriptScore) { |
| 39 | + $body['script_score'] = $this->scriptScore->toDSL(); |
| 40 | + } |
| 41 | + |
| 42 | + if (!is_null($this->weight)) { |
| 43 | + $body['weight'] = $this->weight; |
| 44 | + } |
| 45 | + |
33 | 46 | if ($this->options) { |
34 | 47 | $body = array_merge($this->options->toArray(), $body); |
35 | 48 | } |
|
0 commit comments