1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace ONGR\ElasticsearchDSL\Query\Compound; |
15
|
|
|
|
16
|
|
|
use ONGR\ElasticsearchDSL\BuilderInterface; |
17
|
|
|
use ONGR\ElasticsearchDSL\ParametersTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Represents Elasticsearch "function_score" query. |
21
|
|
|
* |
22
|
|
|
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html |
23
|
|
|
*/ |
24
|
|
|
class FunctionScoreQuery implements BuilderInterface |
25
|
|
|
{ |
26
|
|
|
use ParametersTrait; |
27
|
|
|
|
28
|
|
|
private array $functions; |
|
|
|
|
29
|
|
|
|
30
|
|
|
public function __construct(private ?BuilderInterface $query = null, array $parameters = []) |
31
|
|
|
{ |
32
|
|
|
$this->setParameters($parameters); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function getQuery(): ?BuilderInterface |
36
|
|
|
{ |
37
|
|
|
return $this->query; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function addFieldValueFactorFunction( |
41
|
|
|
string $field, |
42
|
|
|
float $factor, |
43
|
|
|
string $modifier = 'none', |
44
|
|
|
BuilderInterface $query = null, |
45
|
|
|
mixed $missing = null |
46
|
|
|
): static { |
47
|
|
|
$function = [ |
48
|
|
|
'field_value_factor' => array_filter( |
49
|
|
|
[ |
50
|
|
|
'field' => $field, |
51
|
|
|
'factor' => $factor, |
52
|
|
|
'modifier' => $modifier, |
53
|
|
|
'missing' => $missing |
54
|
|
|
] |
55
|
|
|
), |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
$this->applyFilter($function, $query); |
59
|
|
|
|
60
|
|
|
$this->functions[] = $function; |
61
|
|
|
|
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private function applyFilter(array &$function, BuilderInterface $query = null): void |
66
|
|
|
{ |
67
|
|
|
if ($query) { |
68
|
|
|
$function['filter'] = $query->toArray(); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function addDecayFunction( |
73
|
|
|
string $type, |
74
|
|
|
string $field, |
75
|
|
|
array $function, |
76
|
|
|
array $options = [], |
77
|
|
|
BuilderInterface $query = null, |
78
|
|
|
int $weight = null |
79
|
|
|
): static { |
80
|
|
|
$function = array_filter( |
81
|
|
|
[ |
82
|
|
|
$type => array_merge( |
83
|
|
|
[$field => $function], |
84
|
|
|
$options |
85
|
|
|
), |
86
|
|
|
'weight' => $weight, |
87
|
|
|
] |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
$this->applyFilter($function, $query); |
91
|
|
|
|
92
|
|
|
$this->functions[] = $function; |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function addWeightFunction(float $weight, BuilderInterface $query = null): static |
98
|
|
|
{ |
99
|
|
|
$function = [ |
100
|
|
|
'weight' => $weight, |
101
|
|
|
]; |
102
|
|
|
|
103
|
|
|
$this->applyFilter($function, $query); |
104
|
|
|
|
105
|
|
|
$this->functions[] = $function; |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function addRandomFunction(mixed $seed = null, BuilderInterface $query = null): static |
111
|
|
|
{ |
112
|
|
|
$function = [ |
113
|
|
|
'random_score' => $seed ? ['seed' => $seed] : new \stdClass(), |
114
|
|
|
]; |
115
|
|
|
|
116
|
|
|
$this->applyFilter($function, $query); |
117
|
|
|
|
118
|
|
|
$this->functions[] = $function; |
119
|
|
|
|
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function addScriptScoreFunction( |
124
|
|
|
string $inline, |
125
|
|
|
array $params = [], |
126
|
|
|
array $options = [], |
127
|
|
|
BuilderInterface $query = null |
128
|
|
|
): static { |
129
|
|
|
$function = [ |
130
|
|
|
'script_score' => [ |
131
|
|
|
'script' => |
132
|
|
|
array_filter( |
133
|
|
|
array_merge( |
134
|
|
|
[ |
135
|
|
|
'lang' => 'painless', |
136
|
|
|
'inline' => $inline, |
137
|
|
|
'params' => $params |
138
|
|
|
], |
139
|
|
|
$options |
140
|
|
|
) |
141
|
|
|
) |
142
|
|
|
], |
143
|
|
|
]; |
144
|
|
|
|
145
|
|
|
$this->applyFilter($function, $query); |
146
|
|
|
$this->functions[] = $function; |
147
|
|
|
|
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function addSimpleFunction(array $function): static |
152
|
|
|
{ |
153
|
|
|
$this->functions[] = $function; |
154
|
|
|
|
155
|
|
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function toArray(): array |
159
|
|
|
{ |
160
|
|
|
$query = [ |
161
|
|
|
'query' => $this->query->toArray(), |
162
|
|
|
'functions' => $this->functions, |
163
|
|
|
]; |
164
|
|
|
|
165
|
|
|
$output = $this->processArray($query); |
166
|
|
|
|
167
|
|
|
return [$this->getType() => $output]; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function getType(): string |
171
|
|
|
{ |
172
|
|
|
return 'function_score'; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|