|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\FullTextSearch\Search\Queries; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Dev\Deprecation; |
|
6
|
|
|
use SilverStripe\View\ViewableData; |
|
7
|
|
|
use stdClass; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Represents a search query |
|
11
|
|
|
* |
|
12
|
|
|
* API very much still in flux. |
|
13
|
|
|
*/ |
|
14
|
|
|
class SearchQuery extends ViewableData |
|
15
|
|
|
{ |
|
16
|
|
|
public static $missing = null; |
|
17
|
|
|
public static $present = null; |
|
18
|
|
|
|
|
19
|
|
|
public static $default_page_size = 10; |
|
20
|
|
|
|
|
21
|
|
|
/** These are public, but only for index & variant access - API users should not manually access these */ |
|
22
|
|
|
|
|
23
|
|
|
public $search = []; |
|
24
|
|
|
|
|
25
|
|
|
public $classes = []; |
|
26
|
|
|
|
|
27
|
|
|
public $require = []; |
|
28
|
|
|
public $exclude = []; |
|
29
|
|
|
|
|
30
|
|
|
protected $start = 0; |
|
31
|
|
|
protected $limit = -1; |
|
32
|
|
|
|
|
33
|
|
|
/** These are the API functions */ |
|
34
|
|
|
|
|
35
|
|
|
public function __construct() |
|
36
|
|
|
{ |
|
37
|
|
|
if (self::$missing === null) { |
|
38
|
|
|
self::$missing = new stdClass(); |
|
39
|
|
|
} |
|
40
|
|
|
if (self::$present === null) { |
|
41
|
|
|
self::$present = new stdClass(); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string $text Search terms. Exact format (grouping, boolean expressions, etc.) depends on |
|
47
|
|
|
* the search implementation. |
|
48
|
|
|
* @param array $fields Limits the search to specific fields (using composite field names) |
|
49
|
|
|
* @param array $boost Map of composite field names to float values. The higher the value, |
|
50
|
|
|
* the more important the field gets for relevancy. |
|
51
|
|
|
*/ |
|
52
|
|
|
public function addSearchTerm($text, $fields = null, $boost = []) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->search[] = [ |
|
55
|
|
|
'text' => $text, |
|
56
|
|
|
'fields' => $fields ? (array) $fields : null, |
|
57
|
|
|
'boost' => $boost, |
|
58
|
|
|
'fuzzy' => false |
|
59
|
|
|
]; |
|
60
|
|
|
return $this; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Similar to {@link addSearchTerm()}, but uses stemming and other similarity algorithms |
|
65
|
|
|
* to find the searched terms. For example, a term "fishing" would also likely find results |
|
66
|
|
|
* containing "fish" or "fisher". Depends on search implementation. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $text See {@link addSearchTerm()} |
|
69
|
|
|
* @param array $fields See {@link addSearchTerm()} |
|
70
|
|
|
* @param array $boost See {@link addSearchTerm()} |
|
71
|
|
|
*/ |
|
72
|
|
|
public function addFuzzySearchTerm($text, $fields = null, $boost = []) |
|
73
|
|
|
{ |
|
74
|
|
|
$this->search[] = [ |
|
75
|
|
|
'text' => $text, |
|
76
|
|
|
'fields' => $fields ? (array) $fields : null, |
|
77
|
|
|
'boost' => $boost, |
|
78
|
|
|
'fuzzy' => true |
|
79
|
|
|
]; |
|
80
|
|
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return array |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getSearchTerms() |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->search; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param string $class |
|
93
|
|
|
* @param bool $includeSubclasses |
|
94
|
|
|
* @return $this |
|
95
|
|
|
*/ |
|
96
|
|
|
public function addClassFilter($class, $includeSubclasses = true) |
|
97
|
|
|
{ |
|
98
|
|
|
$this->classes[] = [ |
|
99
|
|
|
'class' => $class, |
|
100
|
|
|
'includeSubclasses' => $includeSubclasses |
|
101
|
|
|
]; |
|
102
|
|
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @return array |
|
107
|
|
|
*/ |
|
108
|
|
|
public function getClassFilters() |
|
109
|
|
|
{ |
|
110
|
|
|
return $this->classes; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Similar to {@link addSearchTerm()}, but typically used to further narrow down |
|
115
|
|
|
* based on other facets which don't influence the field relevancy. |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $field Composite name of the field |
|
118
|
|
|
* @param mixed $values Scalar value, array of values, or an instance of SearchQuery_Range |
|
119
|
|
|
*/ |
|
120
|
|
|
public function addFilter($field, $values) |
|
121
|
|
|
{ |
|
122
|
|
|
$requires = isset($this->require[$field]) ? $this->require[$field] : []; |
|
123
|
|
|
$values = is_array($values) ? $values : [$values]; |
|
124
|
|
|
$this->require[$field] = array_merge($requires, $values); |
|
125
|
|
|
return $this; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @return array |
|
130
|
|
|
*/ |
|
131
|
|
|
public function getFilters() |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->require; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Excludes results which match these criteria, inverse of {@link addFilter()}. |
|
138
|
|
|
* |
|
139
|
|
|
* @param string $field |
|
140
|
|
|
* @param mixed $values |
|
141
|
|
|
*/ |
|
142
|
|
|
public function addExclude($field, $values) |
|
143
|
|
|
{ |
|
144
|
|
|
$excludes = isset($this->exclude[$field]) ? $this->exclude[$field] : []; |
|
145
|
|
|
$values = is_array($values) ? $values : [$values]; |
|
146
|
|
|
$this->exclude[$field] = array_merge($excludes, $values); |
|
147
|
|
|
return $this; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @return array |
|
152
|
|
|
*/ |
|
153
|
|
|
public function getExcludes() |
|
154
|
|
|
{ |
|
155
|
|
|
return $this->exclude; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
public function setStart($start) |
|
159
|
|
|
{ |
|
160
|
|
|
$this->start = $start; |
|
161
|
|
|
return $this; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @return int |
|
166
|
|
|
*/ |
|
167
|
|
|
public function getStart() |
|
168
|
|
|
{ |
|
169
|
|
|
return $this->start; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
public function setLimit($limit) |
|
173
|
|
|
{ |
|
174
|
|
|
$this->limit = $limit; |
|
175
|
|
|
return $this; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* @return int |
|
180
|
|
|
*/ |
|
181
|
|
|
public function getLimit() |
|
182
|
|
|
{ |
|
183
|
|
|
return $this->limit; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
public function setPageSize($page) |
|
187
|
|
|
{ |
|
188
|
|
|
$this->setStart($page * self::$default_page_size); |
|
189
|
|
|
$this->setLimit(self::$default_page_size); |
|
190
|
|
|
return $this; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @return int |
|
195
|
|
|
*/ |
|
196
|
|
|
public function getPageSize() |
|
197
|
|
|
{ |
|
198
|
|
|
return (int) ($this->getLimit() / $this->getStart()); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @return bool |
|
203
|
|
|
*/ |
|
204
|
|
|
public function isFiltered() |
|
205
|
|
|
{ |
|
206
|
|
|
return $this->search || $this->classes || $this->require || $this->exclude; |
|
|
|
|
|
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
public function __toString() |
|
210
|
|
|
{ |
|
211
|
|
|
return "Search Query\n"; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* @codeCoverageIgnore |
|
216
|
|
|
* @deprecated |
|
217
|
|
|
*/ |
|
218
|
|
|
public function search($text, $fields = null, $boost = []) |
|
219
|
|
|
{ |
|
220
|
|
|
Deprecation::notice('4.0', 'Use addSearchTerm() instead'); |
|
221
|
|
|
return $this->addSearchTerm($text, $fields, $boost); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* @codeCoverageIgnore |
|
226
|
|
|
* @deprecated |
|
227
|
|
|
*/ |
|
228
|
|
|
public function fuzzysearch($text, $fields = null, $boost = []) |
|
229
|
|
|
{ |
|
230
|
|
|
Deprecation::notice('4.0', 'Use addFuzzySearchTerm() instead'); |
|
231
|
|
|
return $this->addFuzzySearchTerm($text, $fields, $boost); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* @codeCoverageIgnore |
|
236
|
|
|
* @deprecated |
|
237
|
|
|
*/ |
|
238
|
|
|
public function inClass($class, $includeSubclasses = true) |
|
239
|
|
|
{ |
|
240
|
|
|
Deprecation::notice('4.0', 'Use addClassFilter() instead'); |
|
241
|
|
|
return $this->addClassFilter($class, $includeSubclasses); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* @codeCoverageIgnore |
|
246
|
|
|
* @deprecated |
|
247
|
|
|
*/ |
|
248
|
|
|
public function filter($field, $values) |
|
249
|
|
|
{ |
|
250
|
|
|
Deprecation::notice('4.0', 'Use addFilter() instead'); |
|
251
|
|
|
return $this->addFilter($field, $values); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* @codeCoverageIgnore |
|
256
|
|
|
* @deprecated |
|
257
|
|
|
*/ |
|
258
|
|
|
public function exclude($field, $values) |
|
259
|
|
|
{ |
|
260
|
|
|
Deprecation::notice('4.0', 'Use addExclude() instead'); |
|
261
|
|
|
return $this->addExclude($field, $values); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* @codeCoverageIgnore |
|
266
|
|
|
* @deprecated |
|
267
|
|
|
*/ |
|
268
|
|
|
public function start($start) |
|
269
|
|
|
{ |
|
270
|
|
|
Deprecation::notice('4.0', 'Use setStart() instead'); |
|
271
|
|
|
return $this->setStart($start); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* @codeCoverageIgnore |
|
276
|
|
|
* @deprecated |
|
277
|
|
|
*/ |
|
278
|
|
|
public function limit($limit) |
|
279
|
|
|
{ |
|
280
|
|
|
Deprecation::notice('4.0', 'Use setLimit() instead'); |
|
281
|
|
|
return $this->setLimit($limit); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* @codeCoverageIgnore |
|
286
|
|
|
* @deprecated |
|
287
|
|
|
*/ |
|
288
|
|
|
public function page($page) |
|
289
|
|
|
{ |
|
290
|
|
|
Deprecation::notice('4.0', 'Use setPageSize() instead'); |
|
291
|
|
|
return $this->setPageSize($page); |
|
292
|
|
|
} |
|
293
|
|
|
} |
|
294
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.