1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Firesphere\SolrSearch\Traits; |
5
|
|
|
|
6
|
|
|
use Firesphere\SolrSearch\Factories\QueryComponentFactory; |
7
|
|
|
use Firesphere\SolrSearch\Queries\BaseQuery; |
8
|
|
|
use Minimalcode\Search\Criteria; |
9
|
|
|
|
10
|
|
|
trait QueryComponentBoostTrait |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var BaseQuery |
14
|
|
|
*/ |
15
|
|
|
protected $query; |
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $boostTerms = []; |
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $queryArray = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return array |
27
|
|
|
*/ |
28
|
4 |
|
public function getBoostTerms(): array |
29
|
|
|
{ |
30
|
4 |
|
return $this->boostTerms; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param array $boostTerms |
35
|
|
|
* @return QueryComponentFactory |
36
|
|
|
*/ |
37
|
4 |
|
public function setBoostTerms(array $boostTerms): self |
38
|
|
|
{ |
39
|
4 |
|
$this->boostTerms = $boostTerms; |
40
|
|
|
|
41
|
4 |
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Add the index-time boosting to the query |
46
|
|
|
*/ |
47
|
4 |
|
protected function buildBoosts(): void |
48
|
|
|
{ |
49
|
4 |
|
$boosts = $this->query->getBoostedFields(); |
50
|
4 |
|
$queries = $this->getQueryArray(); |
|
|
|
|
51
|
4 |
|
foreach ($boosts as $field => $boost) { |
52
|
1 |
|
foreach ($queries as $term) { |
53
|
1 |
|
$booster = Criteria::where($field) |
54
|
1 |
|
->is($term) |
55
|
1 |
|
->boost($boost); |
56
|
1 |
|
$this->queryArray[] = $booster->getQuery(); |
57
|
|
|
} |
58
|
|
|
} |
59
|
4 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Set boosting at Query time |
63
|
|
|
* |
64
|
|
|
* @param array $search |
65
|
|
|
* @param string $term |
66
|
|
|
* @param array $boostTerms |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
1 |
|
protected function buildQueryBoost($search, string $term, array &$boostTerms): array |
70
|
|
|
{ |
71
|
1 |
|
foreach ($search['fields'] as $boostField) { |
72
|
1 |
|
$boostField = str_replace('.', '_', $boostField); |
73
|
1 |
|
$criteria = Criteria::where($boostField) |
74
|
1 |
|
->is($term) |
75
|
1 |
|
->boost($search['boost']); |
76
|
1 |
|
$boostTerms[] = $criteria->getQuery(); |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
return $boostTerms; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|