1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Firesphere\SolrSearch\Traits; |
5
|
|
|
|
6
|
|
|
use SilverStripe\Dev\Deprecation; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Trait GetterSetterTrait for getting and setting data |
10
|
|
|
* |
11
|
|
|
* Getters and setters shared between the Index and Query |
12
|
|
|
* |
13
|
|
|
* @package Firesphere\SolrSearch\Traits |
14
|
|
|
*/ |
15
|
|
|
trait GetterSetterTrait |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var array Classes to use |
19
|
|
|
*/ |
20
|
|
|
protected $class = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Sets boosting at _index_ time or _query_ time. Depending on the usage of this trait |
24
|
|
|
* [ |
25
|
|
|
* 'FieldName' => 2, |
26
|
|
|
* ] |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $boostedFields = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Format: |
34
|
|
|
* SiteTree::class => [ |
35
|
|
|
* 'Field' => 'SiteTree_ChannelID', |
36
|
|
|
* 'Title' => 'Channel' |
37
|
|
|
* ], |
38
|
|
|
* |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $facetFields = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Set the classes |
45
|
|
|
* |
46
|
|
|
* @param array $class |
47
|
|
|
* @return $this |
48
|
|
|
*/ |
49
|
73 |
|
public function setClasses($class): self |
50
|
|
|
{ |
51
|
73 |
|
$this->class = $class; |
52
|
|
|
|
53
|
73 |
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Add a class to index or query |
58
|
|
|
* $options is not used anymore, added for backward compatibility |
59
|
|
|
* |
60
|
|
|
* @param $class |
61
|
|
|
* @param array $options unused |
62
|
|
|
* @return $this |
63
|
|
|
*/ |
64
|
73 |
|
public function addClass($class, $options = []): self |
65
|
|
|
{ |
66
|
73 |
|
if (count($options)) { |
67
|
|
|
Deprecation::notice('5.0', 'Options are not used anymore'); |
68
|
|
|
} |
69
|
73 |
|
$this->class[] = $class; |
70
|
|
|
|
71
|
73 |
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get classes |
76
|
|
|
* |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
73 |
|
public function getClasses(): array |
80
|
|
|
{ |
81
|
73 |
|
return $this->class; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Add a boosted field to be boosted at query time |
86
|
|
|
* |
87
|
|
|
* @param string $field |
88
|
|
|
* @param array|int $options |
89
|
|
|
* @param int|null $boost |
90
|
|
|
* @return $this |
91
|
|
|
*/ |
92
|
3 |
|
public function addBoostedField($field, $options = [], $boost = null) |
93
|
|
|
{ |
94
|
3 |
|
if ($boost === null && is_int($options)) { |
95
|
1 |
|
$boost = $options; |
96
|
|
|
} |
97
|
|
|
|
98
|
3 |
|
$this->boostedFields[$field] = $boost; |
99
|
|
|
|
100
|
3 |
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get the boosted fields |
105
|
|
|
* |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
35 |
|
public function getBoostedFields(): array |
109
|
|
|
{ |
110
|
35 |
|
return $this->boostedFields; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Boosted fields are used at index time, not at query time |
115
|
|
|
* |
116
|
|
|
* @param array $boostedFields |
117
|
|
|
* @return $this |
118
|
|
|
*/ |
119
|
73 |
|
public function setBoostedFields($boostedFields): self |
120
|
|
|
{ |
121
|
73 |
|
$this->boostedFields = $boostedFields; |
122
|
|
|
|
123
|
73 |
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get the facet fields |
128
|
|
|
* |
129
|
|
|
* @return array |
130
|
|
|
*/ |
131
|
34 |
|
public function getFacetFields(): array |
132
|
|
|
{ |
133
|
34 |
|
return $this->facetFields; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Set the facet fields |
138
|
|
|
* |
139
|
|
|
* @param array $facetFields |
140
|
|
|
* @return $this |
141
|
|
|
*/ |
142
|
73 |
|
public function setFacetFields($facetFields): self |
143
|
|
|
{ |
144
|
73 |
|
$this->facetFields = $facetFields; |
145
|
|
|
|
146
|
73 |
|
return $this; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|