These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Elastica\Test\Query; |
||
4 | |||
5 | use Elastica\Document; |
||
6 | use Elastica\Mapping; |
||
7 | use Elastica\Query\FunctionScore; |
||
8 | use Elastica\Query\MatchAll; |
||
9 | use Elastica\Query\Term; |
||
10 | use Elastica\Script\Script; |
||
11 | use Elastica\Test\Base as BaseTest; |
||
12 | |||
13 | /** |
||
14 | * @internal |
||
15 | */ |
||
16 | class FunctionScoreTest extends BaseTest |
||
17 | { |
||
18 | protected $locationOrigin = '32.804654, -117.242594'; |
||
19 | |||
20 | /** |
||
21 | * @group unit |
||
22 | */ |
||
23 | public function testToArray(): void |
||
24 | { |
||
25 | $priceOrigin = 0; |
||
26 | $locationScale = '2mi'; |
||
27 | $priceScale = 9.25; |
||
28 | $query = new FunctionScore(); |
||
29 | $childQuery = new MatchAll(); |
||
30 | $query->setQuery($childQuery); |
||
31 | $query->addDecayFunction(FunctionScore::DECAY_GAUSS, 'location', $this->locationOrigin, $locationScale); |
||
32 | $query->addDecayFunction(FunctionScore::DECAY_GAUSS, 'price', $priceOrigin, $priceScale); |
||
33 | $expected = [ |
||
34 | 'function_score' => [ |
||
35 | 'query' => $childQuery->toArray(), |
||
36 | 'functions' => [ |
||
37 | [ |
||
38 | 'gauss' => [ |
||
39 | 'location' => [ |
||
40 | 'origin' => $this->locationOrigin, |
||
41 | 'scale' => $locationScale, |
||
42 | ], |
||
43 | ], |
||
44 | ], |
||
45 | [ |
||
46 | 'gauss' => [ |
||
47 | 'price' => [ |
||
48 | 'origin' => $priceOrigin, |
||
49 | 'scale' => $priceScale, |
||
50 | ], |
||
51 | ], |
||
52 | ], |
||
53 | ], |
||
54 | ], |
||
55 | ]; |
||
56 | $this->assertEquals($expected, $query->toArray()); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @group unit |
||
61 | */ |
||
62 | public function testDecayWeight(): void |
||
63 | { |
||
64 | $priceOrigin = 0; |
||
65 | $locationScale = '2mi'; |
||
66 | $priceScale = 9.25; |
||
67 | $query = new FunctionScore(); |
||
68 | $childQuery = new MatchAll(); |
||
69 | $query->setQuery($childQuery); |
||
70 | $query->addDecayFunction( |
||
71 | FunctionScore::DECAY_GAUSS, |
||
72 | 'location', |
||
73 | $this->locationOrigin, |
||
74 | $locationScale, |
||
75 | null, |
||
76 | null, |
||
77 | .5, |
||
78 | null, |
||
79 | FunctionScore::MULTI_VALUE_MODE_AVG |
||
80 | ); |
||
81 | $query->addDecayFunction( |
||
82 | FunctionScore::DECAY_GAUSS, |
||
83 | 'price', |
||
84 | $priceOrigin, |
||
85 | $priceScale, |
||
86 | null, |
||
87 | null, |
||
88 | 2, |
||
89 | null, |
||
90 | FunctionScore::MULTI_VALUE_MODE_MAX |
||
91 | ); |
||
92 | $expected = [ |
||
93 | 'function_score' => [ |
||
94 | 'query' => $childQuery->toArray(), |
||
95 | 'functions' => [ |
||
96 | [ |
||
97 | 'gauss' => [ |
||
98 | 'location' => [ |
||
99 | 'origin' => $this->locationOrigin, |
||
100 | 'scale' => $locationScale, |
||
101 | ], |
||
102 | 'multi_value_mode' => FunctionScore::MULTI_VALUE_MODE_AVG, |
||
103 | ], |
||
104 | 'weight' => .5, |
||
105 | ], |
||
106 | [ |
||
107 | 'gauss' => [ |
||
108 | 'price' => [ |
||
109 | 'origin' => $priceOrigin, |
||
110 | 'scale' => $priceScale, |
||
111 | ], |
||
112 | 'multi_value_mode' => FunctionScore::MULTI_VALUE_MODE_MAX, |
||
113 | ], |
||
114 | 'weight' => 2, |
||
115 | ], |
||
116 | ], |
||
117 | ], |
||
118 | ]; |
||
119 | $this->assertEquals($expected, $query->toArray()); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * @group functional |
||
124 | */ |
||
125 | public function testGauss(): void |
||
126 | { |
||
127 | $query = new FunctionScore(); |
||
128 | $query->addDecayFunction(FunctionScore::DECAY_GAUSS, 'location', $this->locationOrigin, '4mi'); |
||
129 | $query->addDecayFunction(FunctionScore::DECAY_GAUSS, 'price', 0, 10); |
||
130 | $response = $this->_getIndexForTest()->search($query); |
||
0 ignored issues
–
show
|
|||
131 | $results = $response->getResults(); |
||
132 | |||
133 | // the document with the closest location and lowest price should be scored highest |
||
134 | $result0 = $results[0]->getData(); |
||
135 | $this->assertEquals("Mr. Frostie's", $result0['name']); |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @group functional |
||
140 | */ |
||
141 | public function testGaussMultiValue(): void |
||
142 | { |
||
143 | $query = new FunctionScore(); |
||
144 | $query->addDecayFunction( |
||
145 | FunctionScore::DECAY_GAUSS, |
||
146 | 'location', |
||
147 | $this->locationOrigin, |
||
148 | '4mi', |
||
149 | null, |
||
150 | null, |
||
151 | null, |
||
152 | null, |
||
153 | FunctionScore::MULTI_VALUE_MODE_SUM |
||
154 | ); |
||
155 | $response = $this->_getIndexForTest()->search($query); |
||
0 ignored issues
–
show
$query is of type object<Elastica\Query\FunctionScore> , but the function expects a string|array|object<Elastica\Query> .
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
Loading history...
|
|||
156 | $results = $response->getResults(); |
||
157 | |||
158 | // the document with the sum of distances should be scored highest |
||
159 | $result0 = $results[0]->getData(); |
||
160 | $this->assertEquals("Miller's Field", $result0['name']); |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * @group unit |
||
165 | */ |
||
166 | View Code Duplication | public function testAddWeightFunction(): void |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
167 | { |
||
168 | $filter = new Term(['price' => 4.5]); |
||
169 | $query = new FunctionScore(); |
||
170 | $query->addWeightFunction(5.0, $filter); |
||
171 | |||
172 | $sameFilter = new Term(['price' => 4.5]); |
||
173 | $sameQuery = new FunctionScore(); |
||
174 | $sameQuery->addWeightFunction(5.0, $sameFilter); |
||
175 | |||
176 | $this->assertEquals($query->toArray(), $sameQuery->toArray()); |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * @group unit |
||
181 | */ |
||
182 | View Code Duplication | public function testLegacyFilterAddWeightFunction(): void |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
183 | { |
||
184 | $query = new FunctionScore(); |
||
185 | $filter = new Term(['price' => 4.5]); |
||
186 | $query->addWeightFunction(5.0, $filter); |
||
187 | |||
188 | $sameQuery = new FunctionScore(); |
||
189 | $sameFilter = new Term(['price' => 4.5]); |
||
190 | $sameQuery->addWeightFunction(5.0, $sameFilter); |
||
191 | |||
192 | $this->assertEquals($query->toArray(), $sameQuery->toArray()); |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * @group functional |
||
197 | */ |
||
198 | View Code Duplication | public function testWeight(): void |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
199 | { |
||
200 | $filter = new Term(['price' => 4.5]); |
||
201 | $query = new FunctionScore(); |
||
202 | $query->addWeightFunction(5.0, $filter); |
||
203 | |||
204 | $expected = [ |
||
205 | 'function_score' => [ |
||
206 | 'functions' => [ |
||
207 | [ |
||
208 | 'weight' => 5.0, |
||
209 | 'filter' => [ |
||
210 | 'term' => [ |
||
211 | 'price' => 4.5, |
||
212 | ], |
||
213 | ], |
||
214 | ], |
||
215 | ], |
||
216 | ], |
||
217 | ]; |
||
218 | |||
219 | $this->assertEquals($expected, $query->toArray()); |
||
220 | |||
221 | $response = $this->_getIndexForTest()->search($query); |
||
0 ignored issues
–
show
$query is of type object<Elastica\Query\FunctionScore> , but the function expects a string|array|object<Elastica\Query> .
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
Loading history...
|
|||
222 | $results = $response->getResults(); |
||
223 | |||
224 | // the document with price = 4.5 should be scored highest |
||
225 | $result0 = $results[0]->getData(); |
||
226 | $this->assertEquals("Mr. Frostie's", $result0['name']); |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * @group functional |
||
231 | */ |
||
232 | View Code Duplication | public function testWeightWithLegacyFilter(): void |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
233 | { |
||
234 | $filter = new Term(['price' => 4.5]); |
||
235 | $query = new FunctionScore(); |
||
236 | $query->addWeightFunction(5.0, $filter); |
||
237 | |||
238 | $expected = [ |
||
239 | 'function_score' => [ |
||
240 | 'functions' => [ |
||
241 | [ |
||
242 | 'weight' => 5.0, |
||
243 | 'filter' => [ |
||
244 | 'term' => [ |
||
245 | 'price' => 4.5, |
||
246 | ], |
||
247 | ], |
||
248 | ], |
||
249 | ], |
||
250 | ], |
||
251 | ]; |
||
252 | |||
253 | $this->assertEquals($expected, $query->toArray()); |
||
254 | |||
255 | $response = $this->_getIndexForTest()->search($query); |
||
0 ignored issues
–
show
$query is of type object<Elastica\Query\FunctionScore> , but the function expects a string|array|object<Elastica\Query> .
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
Loading history...
|
|||
256 | $results = $response->getResults(); |
||
257 | |||
258 | // the document with price = 4.5 should be scored highest |
||
259 | $result0 = $results[0]->getData(); |
||
260 | $this->assertEquals("Mr. Frostie's", $result0['name']); |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * @group functional |
||
265 | */ |
||
266 | View Code Duplication | public function testRandomScore(): void |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
267 | { |
||
268 | $filter = new Term(['price' => 4.5]); |
||
269 | $query = new FunctionScore(); |
||
270 | $query->addRandomScoreFunction(2, $filter, null, '_id'); |
||
271 | |||
272 | $expected = [ |
||
273 | 'function_score' => [ |
||
274 | 'functions' => [ |
||
275 | [ |
||
276 | 'random_score' => [ |
||
277 | 'seed' => 2, |
||
278 | 'field' => '_id', |
||
279 | ], |
||
280 | 'filter' => [ |
||
281 | 'term' => [ |
||
282 | 'price' => 4.5, |
||
283 | ], |
||
284 | ], |
||
285 | ], |
||
286 | ], |
||
287 | ], |
||
288 | ]; |
||
289 | |||
290 | $this->assertEquals($expected, $query->toArray()); |
||
291 | |||
292 | $response = $this->_getIndexForTest()->search($query); |
||
0 ignored issues
–
show
$query is of type object<Elastica\Query\FunctionScore> , but the function expects a string|array|object<Elastica\Query> .
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
Loading history...
|
|||
293 | $results = $response->getResults(); |
||
294 | |||
295 | // the document with the random score should have a score > 1, means it is the first result |
||
296 | $result0 = $results[0]->getData(); |
||
297 | |||
298 | $this->assertEquals("Miller's Field", $result0['name']); |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * @group functional |
||
303 | */ |
||
304 | View Code Duplication | public function testRandomScoreWithLegacyFilter(): void |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
305 | { |
||
306 | $filter = new Term(['price' => 4.5]); |
||
307 | $query = new FunctionScore(); |
||
308 | $query->addRandomScoreFunction(2, $filter, null, '_id'); |
||
309 | |||
310 | $expected = [ |
||
311 | 'function_score' => [ |
||
312 | 'functions' => [ |
||
313 | [ |
||
314 | 'random_score' => [ |
||
315 | 'seed' => 2, |
||
316 | 'field' => '_id', |
||
317 | ], |
||
318 | 'filter' => [ |
||
319 | 'term' => [ |
||
320 | 'price' => 4.5, |
||
321 | ], |
||
322 | ], |
||
323 | ], |
||
324 | ], |
||
325 | ], |
||
326 | ]; |
||
327 | |||
328 | $this->assertEquals($expected, $query->toArray()); |
||
329 | |||
330 | $response = $this->_getIndexForTest()->search($query); |
||
0 ignored issues
–
show
$query is of type object<Elastica\Query\FunctionScore> , but the function expects a string|array|object<Elastica\Query> .
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
Loading history...
|
|||
331 | $results = $response->getResults(); |
||
332 | |||
333 | // the document with the random score should have a score > 1, means it is the first result |
||
334 | $result0 = $results[0]->getData(); |
||
335 | |||
336 | $this->assertEquals("Miller's Field", $result0['name']); |
||
337 | } |
||
338 | |||
339 | /** |
||
340 | * @group unit |
||
341 | */ |
||
342 | View Code Duplication | public function testRandomScoreWeight(): void |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
343 | { |
||
344 | $filter = new Term(['price' => 4.5]); |
||
345 | $query = new FunctionScore(); |
||
346 | $query->addRandomScoreFunction(2, $filter, 2, '_id'); |
||
347 | |||
348 | $expected = [ |
||
349 | 'function_score' => [ |
||
350 | 'functions' => [ |
||
351 | [ |
||
352 | 'random_score' => [ |
||
353 | 'seed' => 2, |
||
354 | 'field' => '_id', |
||
355 | ], |
||
356 | 'filter' => [ |
||
357 | 'term' => [ |
||
358 | 'price' => 4.5, |
||
359 | ], |
||
360 | ], |
||
361 | 'weight' => 2, |
||
362 | ], |
||
363 | ], |
||
364 | ], |
||
365 | ]; |
||
366 | |||
367 | $this->assertEquals($expected, $query->toArray()); |
||
368 | } |
||
369 | |||
370 | /** |
||
371 | * @group unit |
||
372 | */ |
||
373 | View Code Duplication | public function testRandomScoreWeightWithLegacyFilter(): void |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
374 | { |
||
375 | $filter = new Term(['price' => 4.5]); |
||
376 | $query = new FunctionScore(); |
||
377 | $query->addRandomScoreFunction(2, $filter, 2, '_id'); |
||
378 | |||
379 | $expected = [ |
||
380 | 'function_score' => [ |
||
381 | 'functions' => [ |
||
382 | [ |
||
383 | 'random_score' => [ |
||
384 | 'seed' => 2, |
||
385 | 'field' => '_id', |
||
386 | ], |
||
387 | 'filter' => [ |
||
388 | 'term' => [ |
||
389 | 'price' => 4.5, |
||
390 | ], |
||
391 | ], |
||
392 | 'weight' => 2, |
||
393 | ], |
||
394 | ], |
||
395 | ], |
||
396 | ]; |
||
397 | |||
398 | $this->assertEquals($expected, $query->toArray()); |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * @group functional |
||
403 | */ |
||
404 | public function testRandomScoreWithoutSeed(): void |
||
405 | { |
||
406 | $query = new FunctionScore(); |
||
407 | $query->setRandomScore(); |
||
408 | |||
409 | $response = $this->_getIndexForTest()->search($query); |
||
0 ignored issues
–
show
$query is of type object<Elastica\Query\FunctionScore> , but the function expects a string|array|object<Elastica\Query> .
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
Loading history...
|
|||
410 | |||
411 | $this->assertEquals(2, $response->count()); |
||
412 | } |
||
413 | |||
414 | /** |
||
415 | * @group functional |
||
416 | */ |
||
417 | View Code Duplication | public function testRandomScoreWithoutField(): void |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
418 | { |
||
419 | $filter = new Term(['price' => 4.5]); |
||
420 | $query = new FunctionScore(); |
||
421 | $query->addRandomScoreFunction(2, $filter); |
||
422 | |||
423 | $expected = [ |
||
424 | 'function_score' => [ |
||
425 | 'functions' => [ |
||
426 | [ |
||
427 | 'random_score' => [ |
||
428 | 'seed' => 2, |
||
429 | ], |
||
430 | 'filter' => [ |
||
431 | 'term' => [ |
||
432 | 'price' => 4.5, |
||
433 | ], |
||
434 | ], |
||
435 | ], |
||
436 | ], |
||
437 | ], |
||
438 | ]; |
||
439 | |||
440 | $this->assertEquals($expected, $query->toArray()); |
||
441 | |||
442 | $response = $this->_getIndexForTest()->search($query); |
||
0 ignored issues
–
show
$query is of type object<Elastica\Query\FunctionScore> , but the function expects a string|array|object<Elastica\Query> .
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
Loading history...
|
|||
443 | $results = $response->getResults(); |
||
444 | |||
445 | // the document with the random score should have a score > 1, means it is the first result |
||
446 | $result0 = $results[0]->getData(); |
||
447 | |||
448 | $this->assertEquals("Miller's Field", $result0['name']); |
||
449 | } |
||
450 | |||
451 | /** |
||
452 | * @group functional |
||
453 | */ |
||
454 | public function testScriptScore(): void |
||
455 | { |
||
456 | $scriptString = "_score * doc['price'].value"; |
||
457 | $script = new Script($scriptString, null, Script::LANG_PAINLESS); |
||
458 | $query = new FunctionScore(); |
||
459 | $query->addScriptScoreFunction($script); |
||
460 | $expected = [ |
||
461 | 'function_score' => [ |
||
462 | 'functions' => [ |
||
463 | [ |
||
464 | 'script_score' => [ |
||
465 | 'script' => [ |
||
466 | 'source' => $scriptString, |
||
467 | 'lang' => Script::LANG_PAINLESS, |
||
468 | ], |
||
469 | ], |
||
470 | ], |
||
471 | ], |
||
472 | ], |
||
473 | ]; |
||
474 | |||
475 | $this->assertEquals($expected, $query->toArray()); |
||
476 | |||
477 | $response = $this->_getIndexForTest()->search($query); |
||
0 ignored issues
–
show
$query is of type object<Elastica\Query\FunctionScore> , but the function expects a string|array|object<Elastica\Query> .
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
Loading history...
|
|||
478 | $results = $response->getResults(); |
||
479 | |||
480 | // the document the highest price should be scored highest |
||
481 | $result0 = $results[0]->getData(); |
||
482 | $this->assertEquals("Miller's Field", $result0['name']); |
||
483 | } |
||
484 | |||
485 | /** |
||
486 | * @group functional |
||
487 | */ |
||
488 | public function testSetMinScore(): void |
||
489 | { |
||
490 | $this->_checkVersion('1.5'); |
||
491 | |||
492 | $expected = [ |
||
493 | 'function_score' => [ |
||
494 | 'min_score' => 0.8, |
||
495 | 'functions' => [ |
||
496 | [ |
||
497 | 'gauss' => [ |
||
498 | 'price' => [ |
||
499 | 'origin' => 0, |
||
500 | 'scale' => 10, |
||
501 | ], |
||
502 | ], |
||
503 | ], |
||
504 | ], |
||
505 | ], |
||
506 | ]; |
||
507 | |||
508 | $query = new FunctionScore(); |
||
509 | $query->addDecayFunction(FunctionScore::DECAY_GAUSS, 'price', 0, 10); |
||
510 | $returnedValue = $query->setMinScore(0.8); |
||
511 | |||
512 | $this->assertEquals($expected, $query->toArray()); |
||
513 | $this->assertInstanceOf(FunctionScore::class, $returnedValue); |
||
514 | |||
515 | $response = $this->_getIndexForTest()->search($query); |
||
0 ignored issues
–
show
$query is of type object<Elastica\Query\FunctionScore> , but the function expects a string|array|object<Elastica\Query> .
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
Loading history...
|
|||
516 | $results = $response->getResults(); |
||
517 | |||
518 | $this->assertCount(1, $results); |
||
519 | $this->assertEquals(1, $results[0]->getId()); |
||
520 | } |
||
521 | |||
522 | /** |
||
523 | * @group functional |
||
524 | */ |
||
525 | public function testFieldValueFactor(): void |
||
526 | { |
||
527 | $this->_checkVersion('1.6'); |
||
528 | |||
529 | $expected = [ |
||
530 | 'function_score' => [ |
||
531 | 'functions' => [ |
||
532 | [ |
||
533 | 'field_value_factor' => [ |
||
534 | 'field' => 'popularity', |
||
535 | 'factor' => 1.2, |
||
536 | 'modifier' => 'sqrt', |
||
537 | 'missing' => 0.1, // available from >=1.6 |
||
538 | ], |
||
539 | ], |
||
540 | ], |
||
541 | ], |
||
542 | ]; |
||
543 | |||
544 | $query = new FunctionScore(); |
||
545 | $query->addFieldValueFactorFunction('popularity', 1.2, FunctionScore::FIELD_VALUE_FACTOR_MODIFIER_SQRT, 0.1); |
||
546 | |||
547 | $this->assertEquals($expected, $query->toArray()); |
||
548 | |||
549 | $response = $this->_getIndexForTest()->search($query); |
||
0 ignored issues
–
show
$query is of type object<Elastica\Query\FunctionScore> , but the function expects a string|array|object<Elastica\Query> .
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
Loading history...
|
|||
550 | $results = $response->getResults(); |
||
551 | |||
552 | $this->assertCount(2, $results); |
||
553 | $this->assertEquals(2, $results[0]->getId()); |
||
554 | } |
||
555 | |||
556 | protected function _getIndexForTest() |
||
557 | { |
||
558 | $index = $this->_createIndex(); |
||
559 | $index->setMapping(new Mapping([ |
||
560 | 'name' => ['type' => 'text', 'index' => 'false'], |
||
561 | 'location' => ['type' => 'geo_point'], |
||
562 | 'price' => ['type' => 'float'], |
||
563 | 'popularity' => ['type' => 'integer'], |
||
564 | ])); |
||
565 | |||
566 | $index->addDocuments([ |
||
567 | new Document(1, [ |
||
568 | 'name' => "Mr. Frostie's", |
||
569 | 'location' => [['lat' => 32.799605, 'lon' => -117.243027], ['lat' => 32.792744, 'lon' => -117.2387341]], |
||
570 | 'price' => 4.5, |
||
571 | 'popularity' => null, |
||
572 | ]), |
||
573 | new Document(2, [ |
||
574 | 'name' => "Miller's Field", |
||
575 | 'location' => ['lat' => 32.795964, 'lon' => -117.255028], |
||
576 | 'price' => 9.5, |
||
577 | 'popularity' => 1, |
||
578 | ]), |
||
579 | ]); |
||
580 | |||
581 | $index->refresh(); |
||
582 | |||
583 | return $index; |
||
584 | } |
||
585 | } |
||
586 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: