-
Notifications
You must be signed in to change notification settings - Fork 18
/
SolrQueryBuilder.php
366 lines (300 loc) · 8.05 KB
/
SolrQueryBuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?php
/**
* The default solr / lucene formatted query
*
* @author marcus@silverstripe.com.au
* @license BSD License http://silverstripe.org/bsd-license/
*/
class SolrQueryBuilder {
public $title = 'Default Solr';
protected $userQuery = '';
protected $fields = array('title_as', 'text');
protected $and = array();
protected $params = array();
protected $filters = array();
/**
* Allow "alpha only sort" fields to be wrapped in wildcard characters when queried against.
* @var boolean
*/
protected $enableQueryWildcard = true;
/**
* an array of field => amount to boost
* @var array
*/
protected $boost = array();
/**
* Field:value => boost amount
*
* @var array
*/
protected $boostFieldValues = array();
protected $sort;
/**
*
* @var array
*/
protected $facets = array('fields' => array(), 'queries' => array());
/**
* Per-field facet limits
*
* @var array
*/
protected $facetFieldLimits = array();
/**
* Number of facets to return
*
* @var int
*/
protected $facetLimit = 50;
/**
* Number of items with faces to be included
*
* @var int
*/
protected $facetCount = 1;
public function baseQuery($query) {
$this->userQuery = $query ?: '*:*';
return $this;
}
public function queryFields($fields) {
$this->fields = $fields;
return $this;
}
/**
* Retrieve the current set of fields being queried
*
* @return array
*/
public function currentFields() {
return $this->fields;
}
public function sortBy($field, $direction) {
$this->sort = "$field $direction";
return $this;
}
public function andWith($field, $value) {
$existing = array();
if (isset($this->and[$field])) {
$existing = $this->and[$field];
}
if (is_array($value)) {
$existing = $existing + $value;
} else {
$existing[] = $value;
}
$this->and[$field] = $existing;
return $this;
}
public function setParams($params) {
$this->params = $params;
return $this;
}
public function addParam($name, $value) {
$this->params[$name] = $value;
return $this;
}
public function addFacetFields($fields, $limit = 0) {
$a = array_merge($this->facets['fields'], $fields);
$this->facets['fields'] = array_unique(array_merge($this->facets['fields'], $fields));
$this->facetLimit = $limit;
if ($limit) {
$this->facetLimit = $limit;
}
return $this;
}
public function addFacetQueries($queries, $limit = 0) {
$this->facets['queries'] = array_unique(array_merge($this->facets['queries'], $queries));
if ($limit) {
$this->facetLimit = $limit;
}
return $this;
}
public function addFacetFieldLimit($field, $limit) {
$this->facetFieldLimits[$field] = $limit;
}
public function getParams() {
if (count($this->filters)) {
$this->params['fq'] = array_values($this->filters);
}
if ($this->sort) {
$this->params['sort'] = $this->sort;
}
$this->facetParams();
return $this->params;
}
/**
* Return the base search term
*
* @return string
*/
public function getUserQuery() {
return $this->userQuery;
}
protected function facetParams() {
if (isset($this->facets['fields']) && count($this->facets['fields'])) {
$this->params['facet'] = 'true';
$this->params['facet.field'] = array_values($this->facets['fields']);
}
if (isset($this->facets['queries']) && count($this->facets['queries'])) {
$this->params['facet'] = 'true';
$this->params['facet.query'] = $this->facets['queries'];
}
if ($this->facetLimit) {
$this->params['facet.limit'] = $this->facetLimit;
}
if (count($this->facetFieldLimits)) {
foreach ($this->facetFieldLimits as $field => $limit) {
$this->params['f.' . $field . '.facet.limit'] = $limit;
}
}
$this->params['facet.mincount'] = $this->facetCount ? $this->facetCount : 1;
}
public function parse($string) {
// custom search query entered
if (strpos($string, ':') > 0) {
return $string;
}
$sep = '';
$lucene = '';
foreach ($this->fields as $field) {
$lucene .= $sep . '(' . $field . ':';
// Wrap wildcard characters around the individual terms for any "alpha only sort" fields.
$lucene .= ($this->enableQueryWildcard && (substr($field, -3) === '_as')) ? $this->wildcard($string) : $string;
$lucene .= ')';
if (isset($this->boost[$field])) {
$lucene .= '^' . $this->boost[$field];
}
$sep = ' OR ';
}
return $lucene;
}
/**
* Wrap wildcard characters around individual terms of an input string, useful when dealing with "alpha only sort" fields.
* NOTE: The support for custom query syntax of an input string is currently limited to: * () "" OR || AND && NOT ! + -
* @param string
* @return string
*/
public function wildcard($string) {
// Appropriately handle the input string if it only consists of a single term, where wildcard characters should not be wrapped around quotations.
$single = (strpos($string, ' ') === false);
if($single && (strpos($string, '"') === false)) {
return "*{$string}*";
}
else if($single) {
return $string;
}
// Parse each individual term of the input string.
$string = explode(' ', $string);
$terms = array();
if(is_array($string)) {
$quotation = false;
foreach($string as $term) {
// Parse a "search phrase" by storing the current state, where wildcard characters should no longer be wrapped.
if(($quotations = substr_count($term, '"')) > 0) {
if($quotations === 1) {
$quotation = !$quotation;
}
$terms[] = $term;
continue;
}
// Appropriately handle each individual term depending on the "search phrase" state and any custom query syntax.
if($quotation || ($term === 'OR') || ($term === '||') || ($term === 'AND') || ($term === '&&') || ($term === 'NOT') || ($term === '!') || (strpos($term, '+') === 0) || (strpos($term, '-') === 0)) {
$terms[] = $term;
}
else {
$term = "*{$term}*";
// When dealing with custom grouping, make sure the search terms have been wrapped.
$term = str_replace(array(
'*(',
')*'
), array(
'(*',
'*)'
), $term);
$terms[] = $term;
}
}
}
return implode(' ', $terms);
}
public function boost($boost) {
$this->boost = $boost;
}
public function boostFieldValues($boost) {
$this->boostFieldValues = $boost;
}
public function toString() {
$rawQuery = $this->userQuery ? '(' . $this->parse($this->userQuery).')' : '';
// add in all the clauses;
$sep = '';
if ($rawQuery) {
$sep = ' AND ';
}
foreach ($this->and as $field => $valArray) {
$innerSep = '';
$innerQuery = '';
foreach ($valArray as $value) {
$innerQuery .= $innerSep . $field .':' . $value;
$innerSep = ' OR ';
}
if (strlen($innerQuery)) {
$rawQuery .= $sep . '(' . $innerQuery . ')';
$sep = ' AND ';
}
}
$sep = '';
if ($rawQuery) {
$sep = ' OR ';
}
$boostFieldVals = array();
foreach ($this->boostFieldValues as $field => $boost) {
$boostFieldVals[] = $field . '^' . $boost;
}
if (count($boostFieldVals)) {
$rawQuery .= $sep . ' (' . implode(' OR ', $boostFieldVals) .')';
}
return $rawQuery;
}
/**
* Add a filter query clause.
*
* Filter queries simply restrict the result set without affecting the score of results
*
* @param string $query
*/
public function addFilter($query, $value = null) {
if ($value) {
$query = "$query:$value";
}
$this->filters[$query] = $query;
return $this;
}
/**
* Remove a filter in place on this query
*
* @param string $query
* @param mixed $value
*/
public function removeFilter($query, $value = null) {
if ($value) {
$query = "$query:$value";
}
unset($this->filters[$query]);
return $this;
}
/**
* Apply a geo field restriction around a particular point
*
* @param string $point
* The point in "lat,lon" format
* @param string $field
* @param float $radius
*/
public function restrictNearPoint($point, $field, $radius) {
$this->addFilter("{!geofilt}");
$this->params['sfield'] = $field;
$this->params['pt'] = $point;
$this->params['d'] = $radius;
return $this;
}
}