|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This software package is licensed under `AGPL-3.0-only, proprietary` license[s]. |
|
5
|
|
|
* |
|
6
|
|
|
* @package maslosoft/manganel |
|
7
|
|
|
* @license AGPL-3.0-only, proprietary |
|
8
|
|
|
* |
|
9
|
|
|
* @copyright Copyright (c) Peter Maselkowski <[email protected]> |
|
10
|
|
|
* @link https://maslosoft.com/manganel/ |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Maslosoft\Manganel\Decorators\QueryBuilder\QueryString; |
|
14
|
|
|
|
|
15
|
|
|
use Maslosoft\Mangan\Criteria\ConditionDecorator; |
|
16
|
|
|
use Maslosoft\Manganel\Interfaces\QueryBuilder\QueryStringDecoratorInterface; |
|
17
|
|
|
use Maslosoft\Manganel\Meta\ManganelMeta; |
|
18
|
|
|
use Maslosoft\Manganel\SearchCriteria; |
|
19
|
|
|
use UnexpectedValueException; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* BoostDecorator |
|
23
|
|
|
* |
|
24
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
|
25
|
|
|
*/ |
|
26
|
|
|
class BoostDecorator implements QueryStringDecoratorInterface |
|
27
|
|
|
{ |
|
28
|
|
|
|
|
29
|
24 |
|
public function decorate(&$queryStringParams, SearchCriteria $criteria) |
|
30
|
|
|
{ |
|
31
|
24 |
|
$fields = []; |
|
32
|
24 |
|
$model = $criteria->getModel(); |
|
33
|
|
|
// when no model provided could not determine boosting |
|
34
|
24 |
|
if (empty($model)) |
|
35
|
|
|
{ |
|
36
|
10 |
|
return; |
|
37
|
|
|
} |
|
38
|
15 |
|
$meta = ManganelMeta::create($model); |
|
39
|
15 |
|
$boosted = $meta->properties('searchBoost'); |
|
40
|
|
|
/* @var $boosted float[] */ |
|
41
|
15 |
|
foreach ($boosted as $fieldName => $boost) |
|
42
|
|
|
{ |
|
43
|
15 |
|
if ($boost !== 1.0) |
|
44
|
|
|
{ |
|
45
|
|
|
// Decorate fields, so for instance i18n fields |
|
46
|
|
|
// will map boosting to `title.en` etc. |
|
47
|
12 |
|
$cd = new ConditionDecorator($model); |
|
48
|
12 |
|
$name = key($cd->decorate($fieldName)); |
|
49
|
12 |
|
$fields[$name] = $this->unify($name, $boost, $fields); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
// No custom boost, ignore |
|
54
|
15 |
|
if (empty($fields)) |
|
55
|
|
|
{ |
|
56
|
4 |
|
return; |
|
57
|
|
|
} |
|
58
|
12 |
|
$boosts = []; |
|
59
|
12 |
|
foreach ($fields as $name => $boost) |
|
60
|
|
|
{ |
|
61
|
12 |
|
$boosts[] = sprintf('%s^%f', $name, $boost); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
// Add also _all or it would search only boosted fields |
|
65
|
12 |
|
$boosts[] = '_all'; |
|
66
|
|
|
|
|
67
|
12 |
|
$queryStringParams['fields'] = $boosts; |
|
68
|
12 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Unify boost value if using multi model search with different boost |
|
72
|
|
|
* values for field with same name. |
|
73
|
|
|
* @param string $field |
|
74
|
|
|
* @param float $boost |
|
75
|
|
|
* @param float[] $fields |
|
76
|
|
|
* @return float |
|
77
|
|
|
*/ |
|
78
|
12 |
|
private function unify($field, $boost, $fields) |
|
79
|
|
|
{ |
|
80
|
12 |
|
if (!array_key_exists($field, $fields)) |
|
81
|
|
|
{ |
|
82
|
|
|
// No boost set yet, simply set value |
|
83
|
12 |
|
return $boost; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
// There are already some boost set, decide whether to use maximum |
|
87
|
|
|
// or minimum value |
|
88
|
|
|
$current = $fields[$field]; |
|
89
|
|
|
switch (true) |
|
90
|
|
|
{ |
|
91
|
|
|
// Choose minimal boost out of all |
|
92
|
|
|
case $current < 1 && $boost < 1: |
|
93
|
|
|
$boost = min($current, $boost); |
|
94
|
|
|
break; |
|
95
|
|
|
// Choose maximum boost out of all |
|
96
|
|
|
case $current > 1 && $boost > 1: |
|
97
|
|
|
$boost = max($current, $boost); |
|
98
|
|
|
break; |
|
99
|
|
|
// Choose average, as there is no way to decide whether boost should |
|
100
|
|
|
// be above one or below |
|
101
|
|
|
default: |
|
102
|
|
|
$boost = ($current + $boost) / 2; |
|
103
|
|
|
} |
|
104
|
|
|
return $boost; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|