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