1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Firesphere\SolrSearch\Queries; |
5
|
|
|
|
6
|
|
|
use Firesphere\SolrSearch\Traits\GetterSetterTrait; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class BaseQuery |
10
|
|
|
* @package Firesphere\SolrSearch\Queries |
11
|
|
|
*/ |
12
|
|
|
class BaseQuery |
13
|
|
|
{ |
14
|
|
|
use GetterSetterTrait; |
15
|
|
|
/** |
16
|
|
|
* @todo add user search history through the Query |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $history = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Key-value pairs of fields and what to filter against |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $filter = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $exclude = []; |
32
|
|
|
/** |
33
|
|
|
* @var int |
34
|
|
|
*/ |
35
|
|
|
protected $start = 0; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var int |
39
|
|
|
*/ |
40
|
|
|
protected $rows = 10; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Always get the ID. If you don't, you need to implement your own solution |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
protected $fields = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
protected $sort = []; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Enable spellchecking? |
55
|
|
|
* @var bool |
56
|
|
|
*/ |
57
|
|
|
protected $spellcheck = true; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Follow spellchecking if there are no results |
61
|
|
|
* @var bool |
62
|
|
|
*/ |
63
|
|
|
protected $followSpellcheck = false; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var int |
67
|
|
|
*/ |
68
|
|
|
protected $facetsMinCount = 0; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var array |
72
|
|
|
*/ |
73
|
|
|
protected $terms = []; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @var array |
77
|
5 |
|
*/ |
78
|
|
|
protected $highlight = []; |
79
|
5 |
|
|
80
|
|
|
/** |
81
|
|
|
* @return int |
82
|
|
|
*/ |
83
|
|
|
public function getStart(): int |
84
|
|
|
{ |
85
|
|
|
return $this->start; |
86
|
1 |
|
} |
87
|
|
|
|
88
|
1 |
|
/** |
89
|
|
|
* @param int $start |
90
|
1 |
|
* @return $this |
91
|
|
|
*/ |
92
|
|
|
public function setStart($start): self |
93
|
|
|
{ |
94
|
|
|
$this->start = $start; |
95
|
|
|
|
96
|
5 |
|
return $this; |
97
|
|
|
} |
98
|
5 |
|
|
99
|
|
|
/** |
100
|
|
|
* @return int |
101
|
|
|
*/ |
102
|
|
|
public function getRows(): int |
103
|
|
|
{ |
104
|
|
|
return $this->rows; |
105
|
1 |
|
} |
106
|
|
|
|
107
|
1 |
|
/** |
108
|
|
|
* @param int $rows |
109
|
1 |
|
* @return $this |
110
|
|
|
*/ |
111
|
|
|
public function setRows($rows): self |
112
|
|
|
{ |
113
|
|
|
$this->rows = $rows; |
114
|
|
|
|
115
|
5 |
|
return $this; |
116
|
|
|
} |
117
|
5 |
|
|
118
|
|
|
/** |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
|
|
public function getFields(): array |
122
|
|
|
{ |
123
|
|
|
return $this->fields; |
124
|
|
|
} |
125
|
1 |
|
|
126
|
|
|
/** |
127
|
1 |
|
* Set fields to be returned |
128
|
|
|
* @param array $fields |
129
|
1 |
|
* @return $this |
130
|
|
|
*/ |
131
|
|
|
public function setFields($fields): self |
132
|
|
|
{ |
133
|
|
|
$this->fields = $fields; |
134
|
|
|
|
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
2 |
|
|
138
|
|
|
/** |
139
|
2 |
|
* Add a field to be returned |
140
|
|
|
* @param string $field fieldname |
141
|
2 |
|
* @return $this |
142
|
|
|
*/ |
143
|
|
|
public function addField($field): self |
144
|
|
|
{ |
145
|
|
|
$this->fields[] = $field; |
146
|
|
|
|
147
|
1 |
|
return $this; |
148
|
|
|
} |
149
|
1 |
|
|
150
|
|
|
/** |
151
|
|
|
* @return array |
152
|
|
|
*/ |
153
|
|
|
public function getSort(): array |
154
|
|
|
{ |
155
|
|
|
return $this->sort; |
156
|
1 |
|
} |
157
|
|
|
|
158
|
1 |
|
/** |
159
|
|
|
* @param array $sort |
160
|
1 |
|
* @return $this |
161
|
|
|
*/ |
162
|
|
|
public function setSort($sort): self |
163
|
|
|
{ |
164
|
|
|
$this->sort = $sort; |
165
|
|
|
|
166
|
5 |
|
return $this; |
167
|
|
|
} |
168
|
5 |
|
|
169
|
|
|
/** |
170
|
|
|
* @return int |
171
|
|
|
*/ |
172
|
|
|
public function getFacetsMinCount(): int |
173
|
|
|
{ |
174
|
|
|
return $this->facetsMinCount; |
175
|
1 |
|
} |
176
|
|
|
|
177
|
1 |
|
/** |
178
|
|
|
* @param mixed $facetsMinCount |
179
|
1 |
|
* @return $this |
180
|
|
|
*/ |
181
|
|
|
public function setFacetsMinCount($facetsMinCount): self |
182
|
|
|
{ |
183
|
|
|
$this->facetsMinCount = $facetsMinCount; |
184
|
|
|
|
185
|
5 |
|
return $this; |
186
|
|
|
} |
187
|
5 |
|
|
188
|
|
|
/** |
189
|
|
|
* @return array |
190
|
|
|
*/ |
191
|
|
|
public function getTerms(): array |
192
|
|
|
{ |
193
|
|
|
return $this->terms; |
194
|
1 |
|
} |
195
|
|
|
|
196
|
1 |
|
/** |
197
|
|
|
* @param array $terms |
198
|
1 |
|
* @return $this |
199
|
|
|
*/ |
200
|
|
|
public function setTerms($terms): self |
201
|
|
|
{ |
202
|
|
|
$this->terms = $terms; |
203
|
|
|
|
204
|
|
|
return $this; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Each boosted query needs a separate addition! |
209
|
|
|
* e.g. $this->addTerm('test', ['MyField', 'MyOtherField'], 3) |
210
|
|
|
* followed by |
211
|
|
|
* $this->addTerm('otherTest', ['Title'], 5); |
212
|
|
|
* |
213
|
|
|
* If you want a generic boost on all terms, use addTerm only once, but boost on each field |
214
|
|
|
* |
215
|
|
|
* The fields parameter is used to boost on |
216
|
|
|
* |
217
|
|
|
* For generic boosting, use @addBoostedField($field, $boost), this will add the boost at Index time |
218
|
4 |
|
* @param string $term Term to search for |
219
|
|
|
* @param array $fields fields to boost on |
220
|
4 |
|
* @param int $boost Boost value |
221
|
4 |
|
* @param bool|float $fuzzy True or a value to the maximum amount of iterations |
222
|
4 |
|
* @return $this |
223
|
4 |
|
*/ |
224
|
4 |
|
public function addTerm($term, $fields = [], $boost = 0, $fuzzy = null): self |
225
|
|
|
{ |
226
|
|
|
$this->terms[] = [ |
227
|
4 |
|
'text' => $term, |
228
|
|
|
'fields' => $fields, |
229
|
|
|
'boost' => $boost, |
230
|
|
|
'fuzzy' => $fuzzy |
231
|
|
|
]; |
232
|
|
|
|
233
|
|
|
return $this; |
234
|
|
|
} |
235
|
2 |
|
|
236
|
|
|
/** |
237
|
2 |
|
* @param string $field |
238
|
|
|
* @param string $value |
239
|
2 |
|
* @return $this |
240
|
|
|
*/ |
241
|
|
|
public function addFilter($field, $value): self |
242
|
|
|
{ |
243
|
|
|
$this->filter[$field] = $value; |
244
|
|
|
|
245
|
5 |
|
return $this; |
246
|
|
|
} |
247
|
5 |
|
|
248
|
|
|
/** |
249
|
|
|
* @return array |
250
|
|
|
*/ |
251
|
|
|
public function getFilter(): array |
252
|
|
|
{ |
253
|
|
|
return $this->filter; |
254
|
1 |
|
} |
255
|
|
|
|
256
|
1 |
|
/** |
257
|
|
|
* @param array $filter |
258
|
1 |
|
* @return $this |
259
|
|
|
*/ |
260
|
|
|
public function setFilter($filter): self |
261
|
|
|
{ |
262
|
|
|
$this->filter = $filter; |
263
|
|
|
|
264
|
|
|
return $this; |
265
|
|
|
} |
266
|
2 |
|
|
267
|
|
|
/** |
268
|
2 |
|
* @param $field |
269
|
|
|
* @param $value |
270
|
2 |
|
* @return $this |
271
|
|
|
*/ |
272
|
|
|
public function addExclude($field, $value): self |
273
|
|
|
{ |
274
|
|
|
$this->exclude[$field] = $value; |
275
|
|
|
|
276
|
5 |
|
return $this; |
277
|
|
|
} |
278
|
5 |
|
|
279
|
|
|
/** |
280
|
|
|
* @return array |
281
|
|
|
*/ |
282
|
|
|
public function getExclude(): array |
283
|
|
|
{ |
284
|
|
|
return $this->exclude; |
285
|
1 |
|
} |
286
|
|
|
|
287
|
1 |
|
/** |
288
|
|
|
* @param array $exclude |
289
|
1 |
|
* @return $this |
290
|
|
|
*/ |
291
|
|
|
public function setExclude($exclude): self |
292
|
|
|
{ |
293
|
|
|
$this->exclude = $exclude; |
294
|
|
|
|
295
|
|
|
return $this; |
296
|
1 |
|
} |
297
|
|
|
|
298
|
1 |
|
/** |
299
|
|
|
* @param $field |
300
|
1 |
|
* @return $this |
301
|
|
|
*/ |
302
|
|
|
public function addHighlight($field): self |
303
|
|
|
{ |
304
|
|
|
$this->highlight[] = $field; |
305
|
|
|
|
306
|
5 |
|
return $this; |
307
|
|
|
} |
308
|
5 |
|
|
309
|
|
|
/** |
310
|
|
|
* @return array |
311
|
|
|
*/ |
312
|
|
|
public function getHighlight(): array |
313
|
|
|
{ |
314
|
|
|
return $this->highlight; |
315
|
1 |
|
} |
316
|
|
|
|
317
|
1 |
|
/** |
318
|
|
|
* @param array $highlight |
319
|
1 |
|
* @return $this |
320
|
|
|
*/ |
321
|
|
|
public function setHighlight($highlight): self |
322
|
|
|
{ |
323
|
|
|
$this->highlight = $highlight; |
324
|
|
|
|
325
|
4 |
|
return $this; |
326
|
|
|
} |
327
|
4 |
|
|
328
|
|
|
/** |
329
|
|
|
* @return bool |
330
|
|
|
*/ |
331
|
|
|
public function hasSpellcheck(): bool |
332
|
|
|
{ |
333
|
|
|
return $this->spellcheck; |
334
|
1 |
|
} |
335
|
|
|
|
336
|
1 |
|
/** |
337
|
|
|
* @param bool $spellcheck |
338
|
1 |
|
* @return self |
339
|
|
|
*/ |
340
|
|
|
public function setSpellcheck(bool $spellcheck): self |
341
|
|
|
{ |
342
|
|
|
$this->spellcheck = $spellcheck; |
343
|
|
|
|
344
|
|
|
return $this; |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* @param bool $followSpellcheck |
349
|
|
|
* @return BaseQuery |
350
|
|
|
*/ |
351
|
|
|
public function setFollowSpellcheck(bool $followSpellcheck): BaseQuery |
352
|
|
|
{ |
353
|
|
|
$this->followSpellcheck = $followSpellcheck; |
354
|
|
|
|
355
|
|
|
return $this; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* @return bool |
360
|
|
|
*/ |
361
|
|
|
public function shouldFollowSpellcheck(): bool |
362
|
|
|
{ |
363
|
|
|
return $this->followSpellcheck; |
364
|
|
|
} |
365
|
|
|
} |
366
|
|
|
|