1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* class BaseQuery|Firesphere\SolrSearch\Queries\BaseQuery Base of a Solr Query |
4
|
|
|
* |
5
|
|
|
* @package Firesphere\SolrSearch\Queries |
6
|
|
|
* @author Simon `Firesphere` Erkelens; Marco `Sheepy` Hermo |
7
|
|
|
* @copyright Copyright (c) 2018 - now() Firesphere & Sheepy |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Firesphere\SolrSearch\Queries; |
11
|
|
|
|
12
|
|
|
use Firesphere\SolrSearch\Traits\BaseQueryTrait; |
13
|
|
|
use Firesphere\SolrSearch\Traits\GetterSetterTrait; |
14
|
|
|
use SilverStripe\Core\Injector\Injectable; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class BaseQuery is the base of every query executed. |
18
|
|
|
* |
19
|
|
|
* Build a query to execute agains Solr. Uses as simle as possible an interface. |
20
|
|
|
* |
21
|
|
|
* @package Firesphere\SolrSearch\Queries |
22
|
|
|
*/ |
23
|
|
|
class BaseQuery |
24
|
|
|
{ |
25
|
|
|
use GetterSetterTrait; |
26
|
|
|
use BaseQueryTrait; |
27
|
|
|
use Injectable; |
28
|
|
|
/** |
29
|
|
|
* Pagination start |
30
|
|
|
* |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
protected $start = 0; |
34
|
|
|
/** |
35
|
|
|
* Total rows to display |
36
|
|
|
* |
37
|
|
|
* @var int |
38
|
|
|
*/ |
39
|
|
|
protected $rows = 10; |
40
|
|
|
/** |
41
|
|
|
* Always get the ID. If you don't, you need to implement your own solution |
42
|
|
|
* |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $fields = []; |
46
|
|
|
/** |
47
|
|
|
* Sorting |
48
|
|
|
* |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
protected $sort = []; |
52
|
|
|
/** |
53
|
|
|
* Enable spellchecking? |
54
|
|
|
* |
55
|
|
|
* @var bool |
56
|
|
|
*/ |
57
|
|
|
protected $spellcheck = true; |
58
|
|
|
/** |
59
|
|
|
* Follow spellchecking if there are no results |
60
|
|
|
* |
61
|
|
|
* @var bool |
62
|
|
|
*/ |
63
|
|
|
protected $followSpellcheck = false; |
64
|
|
|
/** |
65
|
|
|
* Minimum results a facet query has to have |
66
|
|
|
* |
67
|
|
|
* @var int |
68
|
|
|
*/ |
69
|
|
|
protected $facetsMinCount = 0; |
70
|
|
|
/** |
71
|
|
|
* Search terms |
72
|
|
|
* |
73
|
|
|
* @var array |
74
|
|
|
*/ |
75
|
|
|
protected $terms = []; |
76
|
|
|
/** |
77
|
|
|
* Highlighted items |
78
|
|
|
* |
79
|
|
|
* @var array |
80
|
|
|
*/ |
81
|
|
|
protected $highlight = []; |
82
|
|
|
/** |
83
|
|
|
* @var array Classes to exclude through hierarchy |
84
|
|
|
*/ |
85
|
|
|
protected $excludedSubClasses = []; |
86
|
|
|
|
87
|
|
|
/** |
88
|
8 |
|
* Get the offset to start |
89
|
|
|
* |
90
|
8 |
|
* @return int |
91
|
|
|
*/ |
92
|
|
|
public function getStart(): int |
93
|
|
|
{ |
94
|
|
|
return $this->start; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Set the offset to start |
99
|
1 |
|
* |
100
|
|
|
* @param int $start |
101
|
1 |
|
* @return $this |
102
|
|
|
*/ |
103
|
1 |
|
public function setStart($start): self |
104
|
|
|
{ |
105
|
|
|
$this->start = $start; |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
8 |
|
* Get the rows to return |
112
|
|
|
* |
113
|
8 |
|
* @return int |
114
|
|
|
*/ |
115
|
|
|
public function getRows(): int |
116
|
|
|
{ |
117
|
|
|
return $this->rows; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Set the rows to return |
122
|
1 |
|
* |
123
|
|
|
* @param int $rows |
124
|
1 |
|
* @return $this |
125
|
|
|
*/ |
126
|
1 |
|
public function setRows($rows): self |
127
|
|
|
{ |
128
|
|
|
$this->rows = $rows; |
129
|
|
|
|
130
|
|
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
8 |
|
* Get the fields to return |
135
|
|
|
* |
136
|
8 |
|
* @return array |
137
|
|
|
*/ |
138
|
|
|
public function getFields(): array |
139
|
|
|
{ |
140
|
|
|
return $this->fields; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Set fields to be returned |
145
|
1 |
|
* |
146
|
|
|
* @param array $fields |
147
|
1 |
|
* @return $this |
148
|
|
|
*/ |
149
|
1 |
|
public function setFields($fields): self |
150
|
|
|
{ |
151
|
|
|
$this->fields = $fields; |
152
|
|
|
|
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
1 |
|
* Get the sort fields |
158
|
|
|
* |
159
|
1 |
|
* @return array |
160
|
|
|
*/ |
161
|
|
|
public function getSort(): array |
162
|
|
|
{ |
163
|
|
|
return $this->sort; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Set the sort fields |
168
|
1 |
|
* |
169
|
|
|
* @param array $sort |
170
|
1 |
|
* @return $this |
171
|
|
|
*/ |
172
|
1 |
|
public function setSort($sort): self |
173
|
|
|
{ |
174
|
|
|
$this->sort = $sort; |
175
|
|
|
|
176
|
|
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
8 |
|
* Get the facet count minimum to use |
181
|
|
|
* |
182
|
8 |
|
* @return int |
183
|
|
|
*/ |
184
|
|
|
public function getFacetsMinCount(): int |
185
|
|
|
{ |
186
|
|
|
return $this->facetsMinCount; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Set the minimum count of facets to be returned |
191
|
1 |
|
* |
192
|
|
|
* @param mixed $facetsMinCount |
193
|
1 |
|
* @return $this |
194
|
|
|
*/ |
195
|
1 |
|
public function setFacetsMinCount($facetsMinCount): self |
196
|
|
|
{ |
197
|
|
|
$this->facetsMinCount = $facetsMinCount; |
198
|
|
|
|
199
|
|
|
return $this; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
8 |
|
* Get the search terms |
204
|
|
|
* |
205
|
8 |
|
* @return array |
206
|
|
|
*/ |
207
|
|
|
public function getTerms(): array |
208
|
|
|
{ |
209
|
|
|
return $this->terms; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Set the search tearms |
214
|
3 |
|
* |
215
|
|
|
* @param array $terms |
216
|
3 |
|
* @return $this |
217
|
|
|
*/ |
218
|
3 |
|
public function setTerms($terms): self |
219
|
|
|
{ |
220
|
|
|
$this->terms = $terms; |
221
|
|
|
|
222
|
|
|
return $this; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
8 |
|
* Get the filters |
227
|
|
|
* |
228
|
8 |
|
* @return array |
229
|
|
|
*/ |
230
|
|
|
public function getFilter(): array |
231
|
|
|
{ |
232
|
|
|
return $this->filter; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Set the query filters |
237
|
1 |
|
* |
238
|
|
|
* @param array $filter |
239
|
1 |
|
* @return $this |
240
|
|
|
*/ |
241
|
1 |
|
public function setFilter($filter): self |
242
|
|
|
{ |
243
|
|
|
$this->filter = $filter; |
244
|
|
|
|
245
|
|
|
return $this; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
8 |
|
* Get the excludes |
250
|
|
|
* |
251
|
8 |
|
* @return array |
252
|
|
|
*/ |
253
|
|
|
public function getExclude(): array |
254
|
|
|
{ |
255
|
|
|
return $this->exclude; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Set the query excludes |
260
|
1 |
|
* |
261
|
|
|
* @param array $exclude |
262
|
1 |
|
* @return $this |
263
|
|
|
*/ |
264
|
1 |
|
public function setExclude($exclude): self |
265
|
|
|
{ |
266
|
|
|
$this->exclude = $exclude; |
267
|
|
|
|
268
|
|
|
return $this; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Add a highlight parameter |
273
|
1 |
|
* |
274
|
|
|
* @param $field |
275
|
1 |
|
* @return $this |
276
|
|
|
*/ |
277
|
1 |
|
public function addHighlight($field): self |
278
|
|
|
{ |
279
|
|
|
$this->highlight[] = $field; |
280
|
|
|
|
281
|
|
|
return $this; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
8 |
|
* Get the highlight parameters |
286
|
|
|
* |
287
|
8 |
|
* @return array |
288
|
|
|
*/ |
289
|
|
|
public function getHighlight(): array |
290
|
|
|
{ |
291
|
|
|
return $this->highlight; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Set the highlight parameters |
296
|
2 |
|
* |
297
|
|
|
* @param array $highlight |
298
|
2 |
|
* @return $this |
299
|
|
|
*/ |
300
|
2 |
|
public function setHighlight($highlight): self |
301
|
|
|
{ |
302
|
|
|
$this->highlight = $highlight; |
303
|
|
|
|
304
|
|
|
return $this; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
7 |
|
* Do we have spellchecking |
309
|
|
|
* |
310
|
7 |
|
* @return bool |
311
|
|
|
*/ |
312
|
|
|
public function hasSpellcheck(): bool |
313
|
|
|
{ |
314
|
|
|
return $this->spellcheck; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Set the spellchecking on this query |
319
|
3 |
|
* |
320
|
|
|
* @param bool $spellcheck |
321
|
3 |
|
* @return self |
322
|
|
|
*/ |
323
|
3 |
|
public function setSpellcheck(bool $spellcheck): self |
324
|
|
|
{ |
325
|
|
|
$this->spellcheck = $spellcheck; |
326
|
|
|
|
327
|
|
|
return $this; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* Set if we should follow spellchecking |
332
|
2 |
|
* |
333
|
|
|
* @param bool $followSpellcheck |
334
|
2 |
|
* @return BaseQuery |
335
|
|
|
*/ |
336
|
2 |
|
public function setFollowSpellcheck(bool $followSpellcheck): BaseQuery |
337
|
|
|
{ |
338
|
|
|
$this->followSpellcheck = $followSpellcheck; |
339
|
|
|
|
340
|
|
|
return $this; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
7 |
|
* Should spellcheck suggestions be followed |
345
|
|
|
* |
346
|
7 |
|
* @return bool |
347
|
|
|
*/ |
348
|
|
|
public function shouldFollowSpellcheck(): bool |
349
|
|
|
{ |
350
|
|
|
return $this->followSpellcheck; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
8 |
|
* Get the facet filtering |
355
|
|
|
* |
356
|
8 |
|
* @return array |
357
|
|
|
*/ |
358
|
|
|
public function getFacetFilter(): array |
359
|
|
|
{ |
360
|
|
|
return $this->facetFilter; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* Set the facet filtering |
365
|
1 |
|
* |
366
|
|
|
* @param array $facetFilter |
367
|
1 |
|
* @return BaseQuery |
368
|
|
|
*/ |
369
|
1 |
|
public function setFacetFilter(array $facetFilter): self |
370
|
|
|
{ |
371
|
|
|
$this->facetFilter = $facetFilter; |
372
|
|
|
|
373
|
|
|
return $this; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* @return array |
378
|
|
|
*/ |
379
|
|
|
public function getExcludedSubClasses(): array |
380
|
|
|
{ |
381
|
|
|
return $this->excludedSubClasses; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* @param array $excludedSubClasses |
386
|
|
|
* @return BaseQuery |
387
|
|
|
*/ |
388
|
|
|
public function setExcludedSubClasses(array $excludedSubClasses): self |
389
|
|
|
{ |
390
|
|
|
$this->excludedSubClasses = $excludedSubClasses; |
391
|
|
|
|
392
|
|
|
return $this; |
393
|
|
|
} |
394
|
|
|
} |
395
|
|
|
|