1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Trait GetterSetterTrait|Firesphere\SolrSearch\Traits\GetterSetterTrait Getters and setters that are duplicate among |
4
|
|
|
* classes like {@link \Firesphere\SolrSearch\Indexes\BaseIndex} and {@link \Firesphere\SolrSearch\Queries\BaseQuery} |
5
|
|
|
* |
6
|
|
|
* @package Firesphere\SolrSearch\Traits |
7
|
|
|
* @author Simon `Firesphere` Erkelens; Marco `Sheepy` Hermo |
8
|
|
|
* @copyright Copyright (c) 2018 - now() Firesphere & Sheepy |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Firesphere\SolrSearch\Traits; |
12
|
|
|
|
13
|
|
|
use SilverStripe\Dev\Deprecation; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Trait GetterSetterTrait for getting and setting data |
17
|
|
|
* |
18
|
|
|
* Getters and setters shared between the Index and Query |
19
|
|
|
* |
20
|
|
|
* @package Firesphere\SolrSearch\Traits |
21
|
|
|
*/ |
22
|
|
|
trait GetterSetterTrait |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var array Classes to use |
26
|
|
|
*/ |
27
|
|
|
protected $class = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Sets boosting at _index_ time or _query_ time. Depending on the usage of this trait |
31
|
|
|
* [ |
32
|
|
|
* 'FieldName' => 2, |
33
|
|
|
* ] |
34
|
|
|
* |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $boostedFields = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Format: |
41
|
|
|
* SiteTree::class => [ |
42
|
|
|
* 'BaseClass' => SiteTree::class, |
43
|
|
|
* 'Field' => 'ChannelID', |
44
|
|
|
* 'Title' => 'Channel' |
45
|
|
|
* ], |
46
|
|
|
* Object::class => [ |
47
|
|
|
* 'BaseClass' => Object::class, |
48
|
|
|
* 'Field' => 'Relation.ID', |
49
|
|
|
* 'Title' => 'Relation' |
50
|
|
|
* ], |
51
|
|
|
* |
52
|
|
|
* The facets will be applied as a single "AND" query. |
53
|
|
|
* e.g. SiteTree_ChannelID:1 with Object_Relation_ID:5 will not be found, |
54
|
|
|
* if the facet filter requires the SiteTree_ChannelID to be 1 AND Object_Relation_ID to be 3 or 6 |
55
|
|
|
* |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
protected $facetFields = []; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Format: |
62
|
|
|
* SiteTree::class => [ |
63
|
|
|
* 'BaseClass' => SiteTree::class, |
64
|
|
|
* 'Field' => 'ChannelID', |
65
|
|
|
* 'Title' => 'Channel' |
66
|
|
|
* ], |
67
|
|
|
* Object::class => [ |
68
|
|
|
* 'BaseClass' => Object::class, |
69
|
|
|
* 'Field' => 'Relation.ID', |
70
|
|
|
* 'Title' => 'Relation' |
71
|
|
|
* ], |
72
|
|
|
* |
73
|
|
|
* The facets will be applied as "OR" separated groups of filters. Compared to default |
74
|
|
|
* facets, that will combine all facets in to a single "AND" query. |
75
|
|
|
* e.g. SiteTree_ChannelID:1 with Object_Relation_ID:5 will be found, |
76
|
|
|
* if the facet filter requires the SiteTree_ChannelID to be 1 OR Object_Relation_ID to be 3 or 6 |
77
|
|
|
* |
78
|
|
|
* @var array |
79
|
|
|
*/ |
80
|
|
|
protected $orFacetFields = []; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Set the classes |
84
|
|
|
* |
85
|
|
|
* @param array $class |
86
|
|
|
* @return $this |
87
|
|
|
*/ |
88
|
40 |
|
public function setClasses($class): self |
89
|
|
|
{ |
90
|
40 |
|
$this->class = $class; |
91
|
|
|
|
92
|
40 |
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Add a class to index or query |
97
|
|
|
* $options is not used anymore, added for backward compatibility |
98
|
|
|
* |
99
|
|
|
* @param $class |
100
|
|
|
* @param array $options unused |
101
|
|
|
* @return $this |
102
|
|
|
*/ |
103
|
52 |
|
public function addClass($class, $options = []): self |
104
|
|
|
{ |
105
|
52 |
|
if (count($options)) { |
106
|
|
|
Deprecation::notice('5.0', 'Options are not used anymore'); |
107
|
|
|
} |
108
|
52 |
|
$this->class[] = $class; |
109
|
|
|
|
110
|
52 |
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get classes |
115
|
|
|
* |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
49 |
|
public function getClasses(): array |
119
|
|
|
{ |
120
|
49 |
|
return $this->class; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Add a boosted field to be boosted at query time |
125
|
|
|
* |
126
|
|
|
* @param string $field |
127
|
|
|
* @param array|int $options |
128
|
|
|
* @param int|null $boost |
129
|
|
|
* @return $this |
130
|
|
|
*/ |
131
|
3 |
|
public function addBoostedField($field, $options = [], $boost = null) |
132
|
|
|
{ |
133
|
3 |
|
if ($boost === null && is_int($options)) { |
134
|
1 |
|
$boost = $options; |
135
|
|
|
} |
136
|
|
|
|
137
|
3 |
|
$this->boostedFields[$field] = $boost; |
138
|
|
|
|
139
|
3 |
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Get the boosted fields |
144
|
|
|
* |
145
|
|
|
* @return array |
146
|
|
|
*/ |
147
|
43 |
|
public function getBoostedFields(): array |
148
|
|
|
{ |
149
|
43 |
|
return $this->boostedFields; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Boosted fields are used at index time, not at query time |
154
|
|
|
* |
155
|
|
|
* @param array $boostedFields |
156
|
|
|
* @return $this |
157
|
|
|
*/ |
158
|
40 |
|
public function setBoostedFields($boostedFields): self |
159
|
|
|
{ |
160
|
40 |
|
$this->boostedFields = $boostedFields; |
161
|
|
|
|
162
|
40 |
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Get the facet fields |
167
|
|
|
* |
168
|
|
|
* @return array |
169
|
|
|
*/ |
170
|
42 |
|
public function getFacetFields(): array |
171
|
|
|
{ |
172
|
42 |
|
return $this->facetFields; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Set the facet fields |
177
|
|
|
* |
178
|
|
|
* @param array $facetFields |
179
|
|
|
* @return $this |
180
|
|
|
*/ |
181
|
39 |
|
public function setFacetFields($facetFields): self |
182
|
|
|
{ |
183
|
39 |
|
$this->facetFields = $facetFields; |
184
|
|
|
|
185
|
39 |
|
return $this; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Get the OR separated facets |
190
|
|
|
* |
191
|
|
|
* @return array |
192
|
|
|
*/ |
193
|
1 |
|
public function getOrFacetFields(): array |
194
|
|
|
{ |
195
|
1 |
|
return $this->orFacetFields; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Add a set of OR facets |
200
|
|
|
* |
201
|
|
|
* @param array $orFacetFields |
202
|
|
|
* @return GetterSetterTrait |
203
|
|
|
*/ |
204
|
1 |
|
public function setOrFacetFields(array $orFacetFields): self |
205
|
|
|
{ |
206
|
1 |
|
$this->orFacetFields = $orFacetFields; |
207
|
|
|
|
208
|
1 |
|
return $this; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|