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