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
|
|
|
namespace ONGR\ElasticsearchDSL\Query\Compound; |
13
|
|
|
|
14
|
|
|
use ONGR\ElasticsearchDSL\BuilderInterface; |
15
|
|
|
use ONGR\ElasticsearchDSL\ParametersTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Represents Elasticsearch "function_score" query. |
19
|
|
|
* |
20
|
|
|
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html |
21
|
|
|
*/ |
22
|
|
|
class FunctionScoreQuery implements BuilderInterface |
23
|
|
|
{ |
24
|
|
|
use ParametersTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var BuilderInterface |
28
|
|
|
*/ |
29
|
|
|
private $query; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array[] |
33
|
|
|
*/ |
34
|
|
|
private $functions; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param BuilderInterface $query |
38
|
|
|
* @param array $parameters |
39
|
|
|
*/ |
40
|
|
|
public function __construct(BuilderInterface $query, array $parameters = []) |
41
|
|
|
{ |
42
|
|
|
$this->query = $query; |
43
|
|
|
$this->setParameters($parameters); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Creates field_value_factor function. |
48
|
|
|
* |
49
|
|
|
* @param string $field |
50
|
|
|
* @param float $factor |
51
|
|
|
* @param string $modifier |
52
|
|
|
* @param BuilderInterface $query |
53
|
|
|
* |
54
|
|
|
* @return $this |
55
|
|
|
*/ |
56
|
|
|
public function addFieldValueFactorFunction($field, $factor, $modifier = 'none', BuilderInterface $query = null) |
57
|
|
|
{ |
58
|
|
|
$function = [ |
59
|
|
|
'field_value_factor' => [ |
60
|
|
|
'field' => $field, |
61
|
|
|
'factor' => $factor, |
62
|
|
|
'modifier' => $modifier, |
63
|
|
|
], |
64
|
|
|
]; |
65
|
|
|
|
66
|
|
|
$this->applyFilter($function, $query); |
67
|
|
|
|
68
|
|
|
$this->functions[] = $function; |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Modifier to apply filter to the function score function. |
75
|
|
|
* |
76
|
|
|
* @param array $function |
77
|
|
|
* @param BuilderInterface $query |
78
|
|
|
*/ |
79
|
|
|
private function applyFilter(array &$function, BuilderInterface $query = null) |
80
|
|
|
{ |
81
|
|
|
if ($query) { |
82
|
|
|
$function['filter'] = $query->toArray(); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Add decay function to function score. Weight and query are optional. |
88
|
|
|
* |
89
|
|
|
* @param string $type |
90
|
|
|
* @param string $field |
91
|
|
|
* @param array $function |
92
|
|
|
* @param array $options |
93
|
|
|
* @param BuilderInterface $query |
94
|
|
|
* |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
|
|
public function addDecayFunction( |
98
|
|
|
$type, |
99
|
|
|
$field, |
100
|
|
|
array $function, |
101
|
|
|
array $options = [], |
102
|
|
|
BuilderInterface $query = null |
103
|
|
|
) { |
104
|
|
|
$function = [ |
105
|
|
|
$type => array_merge( |
106
|
|
|
[$field => $function], |
107
|
|
|
$options |
108
|
|
|
), |
109
|
|
|
]; |
110
|
|
|
|
111
|
|
|
$this->applyFilter($function, $query); |
112
|
|
|
|
113
|
|
|
$this->functions[] = $function; |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Adds function to function score without decay function. Influence search score only for specific query. |
120
|
|
|
* |
121
|
|
|
* @param float $weight |
122
|
|
|
* @param BuilderInterface $query |
123
|
|
|
* |
124
|
|
|
* @return $this |
125
|
|
|
*/ |
126
|
|
|
public function addWeightFunction($weight, BuilderInterface $query = null) |
127
|
|
|
{ |
128
|
|
|
$function = [ |
129
|
|
|
'weight' => $weight, |
130
|
|
|
]; |
131
|
|
|
|
132
|
|
|
$this->applyFilter($function, $query); |
133
|
|
|
|
134
|
|
|
$this->functions[] = $function; |
135
|
|
|
|
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Adds random score function. Seed is optional. |
141
|
|
|
* |
142
|
|
|
* @param mixed $seed |
143
|
|
|
* @param BuilderInterface $query |
144
|
|
|
* |
145
|
|
|
* @return $this |
146
|
|
|
*/ |
147
|
|
|
public function addRandomFunction($seed = null, BuilderInterface $query = null) |
148
|
|
|
{ |
149
|
|
|
$function = [ |
150
|
|
|
'random_score' => $seed ? [ 'seed' => $seed ] : new \stdClass(), |
151
|
|
|
]; |
152
|
|
|
|
153
|
|
|
$this->applyFilter($function, $query); |
154
|
|
|
|
155
|
|
|
$this->functions[] = $function; |
156
|
|
|
|
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Adds script score function. |
162
|
|
|
* |
163
|
|
|
* @param string $inline |
164
|
|
|
* @param array $params |
165
|
|
|
* @param array $options |
166
|
|
|
* @param BuilderInterface $query |
167
|
|
|
* |
168
|
|
|
* @return $this |
169
|
|
|
*/ |
170
|
|
|
public function addScriptScoreFunction( |
171
|
|
|
$inline, |
172
|
|
|
array $params = [], |
173
|
|
|
array $options = [], |
174
|
|
|
BuilderInterface $query = null |
175
|
|
|
) { |
176
|
|
|
$function = [ |
177
|
|
|
'script_score' => [ |
178
|
|
|
'script' => |
179
|
|
|
array_filter( |
180
|
|
|
array_merge( |
181
|
|
|
[ |
182
|
|
|
'lang' => 'painless', |
183
|
|
|
'inline' => $inline, |
184
|
|
|
'params' => $params |
185
|
|
|
], |
186
|
|
|
$options |
187
|
|
|
) |
188
|
|
|
) |
189
|
|
|
], |
190
|
|
|
]; |
191
|
|
|
|
192
|
|
|
$this->applyFilter($function, $query); |
193
|
|
|
$this->functions[] = $function; |
194
|
|
|
|
195
|
|
|
return $this; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Adds custom simple function. You can add to the array whatever you want. |
200
|
|
|
* |
201
|
|
|
* @param array $function |
202
|
|
|
* |
203
|
|
|
* @return $this |
204
|
|
|
*/ |
205
|
|
|
public function addSimpleFunction(array $function) |
206
|
|
|
{ |
207
|
|
|
$this->functions[] = $function; |
208
|
|
|
|
209
|
|
|
return $this; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* {@inheritdoc} |
214
|
|
|
*/ |
215
|
|
|
public function toArray() |
216
|
|
|
{ |
217
|
|
|
$query = [ |
218
|
|
|
'query' => $this->query->toArray(), |
219
|
|
|
'functions' => $this->functions, |
220
|
|
|
]; |
221
|
|
|
|
222
|
|
|
$output = $this->processArray($query); |
223
|
|
|
|
224
|
|
|
return [$this->getType() => $output]; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* {@inheritdoc} |
229
|
|
|
*/ |
230
|
|
|
public function getType() |
231
|
|
|
{ |
232
|
|
|
return 'function_score'; |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|