|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Firesphere\SolrSearch\Traits; |
|
5
|
|
|
|
|
6
|
|
|
use Firesphere\SolrSearch\Indexes\BaseIndex; |
|
7
|
|
|
use SilverStripe\Core\Config\Config; |
|
8
|
|
|
use SilverStripe\Core\Injector\Injector; |
|
9
|
|
|
use SilverStripe\Dev\Deprecation; |
|
10
|
|
|
use SilverStripe\ORM\DataObject; |
|
11
|
|
|
use SilverStripe\ORM\FieldType\DBDate; |
|
12
|
|
|
use SilverStripe\ORM\FieldType\DBString; |
|
13
|
|
|
use Solarium\Core\Client\Client; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* This is slightly cheating, but it works and also makes things more readable. |
|
17
|
|
|
* |
|
18
|
|
|
* Trait BaseIndexTrait |
|
19
|
|
|
* @package Firesphere\SolrSearch\Traits |
|
20
|
|
|
*/ |
|
21
|
|
|
trait BaseIndexTrait |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var Client |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $client; |
|
27
|
|
|
/** |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $facetFields = []; |
|
31
|
|
|
/** |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $fulltextFields = []; |
|
35
|
|
|
/** |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $filterFields = []; |
|
39
|
|
|
/** |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $sortFields = []; |
|
43
|
|
|
/** |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $defaultField = '_text'; |
|
47
|
|
|
/** |
|
48
|
|
|
* @var array |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $storedFields = []; |
|
51
|
|
|
/** |
|
52
|
|
|
* @var array |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $copyFields = [ |
|
55
|
|
|
'_text' => [ |
|
56
|
|
|
'*' |
|
57
|
|
|
], |
|
58
|
|
|
]; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return array |
|
62
|
|
|
*/ |
|
63
|
29 |
|
public function getCopyFields(): array |
|
64
|
|
|
{ |
|
65
|
29 |
|
return $this->copyFields; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param array $copyField |
|
70
|
|
|
* @return $this |
|
71
|
|
|
*/ |
|
72
|
66 |
|
public function setCopyFields($copyField): self |
|
73
|
|
|
{ |
|
74
|
66 |
|
$this->copyFields = $copyField; |
|
75
|
|
|
|
|
76
|
66 |
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return string |
|
81
|
|
|
*/ |
|
82
|
30 |
|
public function getDefaultField(): string |
|
83
|
|
|
{ |
|
84
|
30 |
|
return $this->defaultField; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param string $defaultField |
|
89
|
|
|
* @return $this |
|
90
|
|
|
*/ |
|
91
|
66 |
|
public function setDefaultField($defaultField): self |
|
92
|
|
|
{ |
|
93
|
66 |
|
$this->defaultField = $defaultField; |
|
94
|
|
|
|
|
95
|
66 |
|
return $this; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param $sortField |
|
100
|
|
|
* @return $this |
|
101
|
|
|
*/ |
|
102
|
67 |
|
public function addSortField($sortField): self |
|
103
|
|
|
{ |
|
104
|
67 |
|
if (!in_array($sortField, $this->getFulltextFields(), true) && |
|
105
|
67 |
|
!in_array($sortField, $this->getFilterFields(), true) |
|
106
|
|
|
) { |
|
107
|
1 |
|
$this->addFulltextField($sortField); |
|
108
|
1 |
|
$this->sortFields[] = $sortField; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
67 |
|
$this->setSortFields(array_unique($this->getSortFields())); |
|
112
|
|
|
|
|
113
|
67 |
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return array |
|
118
|
|
|
*/ |
|
119
|
67 |
|
public function getFulltextFields(): array |
|
120
|
|
|
{ |
|
121
|
67 |
|
return array_values( |
|
122
|
67 |
|
array_unique( |
|
123
|
67 |
|
$this->fulltextFields |
|
124
|
|
|
) |
|
125
|
|
|
); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param array $fulltextFields |
|
130
|
|
|
* @return $this |
|
131
|
|
|
*/ |
|
132
|
66 |
|
public function setFulltextFields($fulltextFields): self |
|
133
|
|
|
{ |
|
134
|
66 |
|
$this->fulltextFields = $fulltextFields; |
|
135
|
|
|
|
|
136
|
66 |
|
return $this; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @return array |
|
141
|
|
|
*/ |
|
142
|
67 |
|
public function getFilterFields(): array |
|
143
|
|
|
{ |
|
144
|
67 |
|
return $this->filterFields; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param array $filterFields |
|
149
|
|
|
* @return $this |
|
150
|
|
|
*/ |
|
151
|
66 |
|
public function setFilterFields($filterFields): self |
|
152
|
|
|
{ |
|
153
|
66 |
|
$this->filterFields = $filterFields; |
|
154
|
|
|
|
|
155
|
66 |
|
return $this; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param string $fulltextField |
|
160
|
|
|
* @param null|string $forceType |
|
161
|
|
|
* @param array $options |
|
162
|
|
|
* @return $this |
|
163
|
|
|
*/ |
|
164
|
67 |
|
public function addFulltextField($fulltextField, $forceType = null, $options = []): self |
|
165
|
|
|
{ |
|
166
|
67 |
|
if ($forceType) { |
|
167
|
|
|
Deprecation::notice('5.0', 'ForceType should be handled through casting'); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
67 |
|
$key = array_search($fulltextField, $this->getFilterFields(), true); |
|
171
|
|
|
|
|
172
|
67 |
|
if (!$key) { |
|
173
|
67 |
|
$this->fulltextFields[] = $fulltextField; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
67 |
|
if (isset($options['boost'])) { |
|
177
|
1 |
|
$this->addBoostedField($fulltextField, [], $options['boost']); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
67 |
|
if (isset($options['stored'])) { |
|
181
|
1 |
|
$this->storedFields[] = $fulltextField; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
67 |
|
return $this; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Add an abstract for the add Boosted Field to keep things consistent |
|
189
|
|
|
* @param string $field |
|
190
|
|
|
* @param array|int $options |
|
191
|
|
|
* @param null|int $boost |
|
192
|
|
|
* @return mixed |
|
193
|
|
|
*/ |
|
194
|
|
|
abstract public function addBoostedField($field, $options = [], $boost = null); |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* This trait requires classes to be set, so getClasses can be called. |
|
198
|
|
|
* @return array |
|
199
|
|
|
*/ |
|
200
|
|
|
abstract public function getClasses(): array; |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @return array |
|
204
|
|
|
*/ |
|
205
|
67 |
|
public function getSortFields(): array |
|
206
|
|
|
{ |
|
207
|
67 |
|
return $this->sortFields; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @param array $sortFields |
|
212
|
|
|
* @return $this |
|
213
|
|
|
*/ |
|
214
|
67 |
|
public function setSortFields($sortFields): self |
|
215
|
|
|
{ |
|
216
|
67 |
|
$this->sortFields = $sortFields; |
|
217
|
|
|
|
|
218
|
67 |
|
return $this; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* @param bool $includeSubclasses |
|
223
|
|
|
* @throws \ReflectionException |
|
224
|
|
|
* @deprecated Please use addAllFulltextFields(). IncludeSubClasses is not used anymore |
|
225
|
|
|
*/ |
|
226
|
1 |
|
public function addFulltextFields($includeSubclasses = true) |
|
|
|
|
|
|
227
|
|
|
{ |
|
228
|
1 |
|
$this->addAllFulltextFields(); |
|
229
|
1 |
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Add all database-backed text fields as fulltext searchable fields. |
|
233
|
|
|
* |
|
234
|
|
|
* For every class included in the index, examines those classes and all parent looking for "DBText" database |
|
235
|
|
|
* fields (Varchar, Text, HTMLText, etc) and adds them all as fulltext searchable fields. |
|
236
|
|
|
* |
|
237
|
|
|
* Note, there is no check on boosting etc. That needs to be done manually. |
|
238
|
|
|
* @param string $dbType |
|
239
|
|
|
* @throws \ReflectionException |
|
240
|
|
|
*/ |
|
241
|
1 |
|
protected function addAllFieldsByType($dbType = DBString::class): void |
|
242
|
|
|
{ |
|
243
|
1 |
|
$classes = $this->getClasses(); |
|
244
|
1 |
|
foreach ($classes as $key => $class) { |
|
245
|
1 |
|
$fields = DataObject::getSchema()->databaseFields($class, true); |
|
246
|
|
|
|
|
247
|
1 |
|
$this->addFulltextFieldsForClass($fields, $dbType); |
|
248
|
|
|
} |
|
249
|
1 |
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* Add all text-type fields to the given index |
|
253
|
|
|
* @throws \ReflectionException |
|
254
|
|
|
*/ |
|
255
|
1 |
|
public function addAllFulltextFields() |
|
256
|
|
|
{ |
|
257
|
1 |
|
$this->addAllFieldsByType(DBString::class); |
|
258
|
1 |
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* Add all date-type fields to the given index |
|
262
|
|
|
* @throws \ReflectionException |
|
263
|
|
|
*/ |
|
264
|
1 |
|
public function addAllDateFields() |
|
265
|
|
|
{ |
|
266
|
1 |
|
$this->addAllFieldsByType(DBDate::class); |
|
267
|
1 |
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Add all fields of a given type to the index |
|
271
|
|
|
* @param array $fields The fields on the DataObject |
|
272
|
|
|
* @param string $dbType Class type the reflection should extend |
|
273
|
|
|
* @throws \ReflectionException |
|
274
|
|
|
*/ |
|
275
|
1 |
|
protected function addFulltextFieldsForClass(array $fields, $dbType = DBString::class): void |
|
276
|
|
|
{ |
|
277
|
1 |
|
foreach ($fields as $field => $type) { |
|
278
|
1 |
|
$pos = strpos($type, '('); |
|
279
|
1 |
|
if ($pos !== false) { |
|
280
|
1 |
|
$type = substr($type, 0, $pos); |
|
281
|
|
|
} |
|
282
|
1 |
|
$conf = Config::inst()->get(Injector::class, $type); |
|
283
|
1 |
|
$ref = new \ReflectionClass($conf['class']); |
|
284
|
1 |
|
if ($ref->isSubclassOf($dbType)) { |
|
285
|
1 |
|
$this->addFulltextField($field); |
|
286
|
|
|
} |
|
287
|
|
|
} |
|
288
|
1 |
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* @param $field |
|
292
|
|
|
* @param array $options |
|
293
|
|
|
* @return $this |
|
294
|
|
|
*/ |
|
295
|
1 |
|
public function addFacetField($field, $options): self |
|
296
|
|
|
{ |
|
297
|
1 |
|
$this->facetFields[$field] = $options; |
|
298
|
|
|
|
|
299
|
1 |
|
if (!in_array($field, $this->getFilterFields(), true)) { |
|
300
|
1 |
|
$this->addFilterField($field); |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
1 |
|
return $this; |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
/** |
|
307
|
|
|
* @param $filterField |
|
308
|
|
|
* @return $this |
|
309
|
|
|
*/ |
|
310
|
67 |
|
public function addFilterField($filterField): self |
|
311
|
|
|
{ |
|
312
|
67 |
|
$key = array_search($filterField, $this->getFulltextFields(), true); |
|
313
|
67 |
|
if ($key === false) { |
|
314
|
67 |
|
$this->filterFields[] = $filterField; |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
67 |
|
return $this; |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
/** |
|
321
|
|
|
* @param string $field Name of the copyfield |
|
322
|
|
|
* @param array $options Array of all fields that should be copied to this copyfield |
|
323
|
|
|
* @return $this |
|
324
|
|
|
*/ |
|
325
|
1 |
|
public function addCopyField($field, $options): self |
|
326
|
|
|
{ |
|
327
|
1 |
|
$this->copyFields[$field] = $options; |
|
328
|
|
|
|
|
329
|
1 |
|
return $this; |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
/** |
|
333
|
|
|
* @param string $field |
|
334
|
|
|
* @param null|string $forceType |
|
335
|
|
|
* @param array $extraOptions |
|
336
|
|
|
* @return BaseIndex |
|
337
|
|
|
*/ |
|
338
|
1 |
|
public function addStoredField($field, $forceType = null, $extraOptions = []): self |
|
339
|
|
|
{ |
|
340
|
1 |
|
$options = array_merge($extraOptions, ['stored' => 'true']); |
|
341
|
1 |
|
$this->addFulltextField($field, $forceType, $options); |
|
342
|
|
|
|
|
343
|
1 |
|
return $this; |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
|
|
/** |
|
347
|
|
|
* @return Client |
|
348
|
|
|
*/ |
|
349
|
18 |
|
public function getClient() |
|
350
|
|
|
{ |
|
351
|
18 |
|
return $this->client; |
|
352
|
|
|
} |
|
353
|
|
|
|
|
354
|
|
|
/** |
|
355
|
|
|
* @param Client $client |
|
356
|
|
|
* @return $this |
|
357
|
|
|
*/ |
|
358
|
1 |
|
public function setClient($client): self |
|
359
|
|
|
{ |
|
360
|
1 |
|
$this->client = $client; |
|
361
|
|
|
|
|
362
|
1 |
|
return $this; |
|
363
|
|
|
} |
|
364
|
|
|
|
|
365
|
|
|
/** |
|
366
|
|
|
* @return array |
|
367
|
|
|
*/ |
|
368
|
29 |
|
public function getStoredFields(): array |
|
369
|
|
|
{ |
|
370
|
29 |
|
return $this->storedFields; |
|
371
|
|
|
} |
|
372
|
|
|
|
|
373
|
|
|
/** |
|
374
|
|
|
* @param array $storedFields |
|
375
|
|
|
* @return BaseIndex |
|
376
|
|
|
*/ |
|
377
|
1 |
|
public function setStoredFields(array $storedFields): self |
|
378
|
|
|
{ |
|
379
|
1 |
|
$this->storedFields = $storedFields; |
|
380
|
|
|
|
|
381
|
1 |
|
return $this; |
|
382
|
|
|
} |
|
383
|
|
|
} |
|
384
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.