@@ -22,6 +22,7 @@ class SphinxSE {
22
22
protected $ maxmatches ;
23
23
24
24
private $ selects = [];
25
+ private $ queries = [];
25
26
26
27
public function __construct ($ config = [])
27
28
{
@@ -48,23 +49,31 @@ public function setServer($host, $port = 0)
48
49
}
49
50
50
51
/**
51
- * Set offset and count into result set, optionally set max-matches and cutoff limits
52
+ * Set a limit for the returned results
52
53
*
53
- * @param integer $offset
54
54
* @param integer $limit
55
+ *
56
+ */
57
+ public function setLimits ($ limit )
58
+ {
59
+ $ this ->limit = $ limit ;
60
+ }
61
+
62
+ /**
63
+ * Set offset, optionally set max-matches
64
+ *
65
+ * @param integer $offset
55
66
* @param integer $max
56
- * @param integer $cutoff
57
67
*
58
68
*/
59
- public function setLimits ($ offset , $ limit , $ max = 1000 , $ cutoff = 0 )
69
+ public function setOffset ($ offset , $ max = 1000 )
60
70
{
61
71
if ($ offset >= $ max )
62
72
{
63
73
throw new SphinxSEException ('Offset out of bounds exception. ' );
64
74
}
65
75
66
76
$ this ->offset = $ offset ;
67
- $ this ->limit = $ limit ;
68
77
$ this ->maxmatches = $ max ;
69
78
}
70
79
@@ -142,14 +151,16 @@ public function setIndex($index)
142
151
}
143
152
144
153
/**
145
- * Add select statement to the query
154
+ * Add select statement to the query. It can be a string or an array.
155
+ * In the case where data is stored in an array, you can set the special key for that.
156
+ * And also resetting the special select can be done by assigning null to its key.
146
157
*
147
158
* @param $select
148
159
*
149
160
*/
150
161
public function setSelect ($ select )
151
162
{
152
- $ this ->selects [] = $ select ;
163
+ $ this ->selects = array_merge ( $ this -> selects , static :: arrayWrap ( $ select)) ;
153
164
}
154
165
155
166
/**
@@ -172,7 +183,7 @@ public function fieldQuery($fields, $value, $quorum = 0.8, $operator = '/')
172
183
$ field_string = '@ ' . $ fields ;
173
184
}
174
185
175
- $ this ->query . = $ field_string . ' " ' . $ this ->escapeString ($ value ) . '" ' . ($ quorum ? $ operator . $ quorum : '' ) . ' ' ;
186
+ $ this ->queries [ $ field_string ] = $ field_string . ' " ' . $ this ->escapeString ($ value ) . '" ' . ($ quorum ? $ operator . $ quorum : '' );
176
187
}
177
188
178
189
/**
@@ -218,7 +229,18 @@ public function setFilterRange($attribute, $min, $max, $exclude = false)
218
229
*/
219
230
public function setFilterFloatRange ($ attribute , $ min , $ max , $ exclude = false )
220
231
{
221
- $ this ->floatrange [] = ($ exclude ? '! ' : '' ) . $ attribute . ", {$ min }, {$ max }" ;
232
+ $ this ->floatrange [$ attribute ] = ($ exclude ? '! ' : '' ) . $ attribute . ", {$ min }, {$ max }" ;
233
+ }
234
+
235
+ /**
236
+ * Reset float range filter;
237
+ *
238
+ * @param string $attribute attribute name
239
+ *
240
+ */
241
+ public function resetFilterFloatRange ($ attribute )
242
+ {
243
+ $ this ->floatrange [$ attribute ] = null ;
222
244
}
223
245
224
246
/**
@@ -258,7 +280,7 @@ public function setGroupDistinct($attribute)
258
280
*/
259
281
public function setGeoAnchor ($ attrlat , $ attrlong , $ lat , $ long , $ alias = 'geodist ' )
260
282
{
261
- $ this ->setSelect ('GEODIST( ' . $ attrlat . ', ' . $ attrlong . ', ' . $ lat . ', ' . $ long . ') AS ' . $ alias );
283
+ $ this ->setSelect ([ $ alias => 'GEODIST( ' . $ attrlat . ', ' . $ attrlong . ', ' . $ lat . ', ' . $ long . ') AS ' . $ alias] );
262
284
}
263
285
264
286
public function toQuery ()
@@ -267,18 +289,19 @@ public function toQuery()
267
289
268
290
$ properties = $ reflection ->getProperties ();
269
291
270
- $ query = empty ($ this ->selects ) ? '' : 'select= ' . implode (', ' , $ this ->selects ) . '; ' ;
292
+ $ this ->query = $ this ->getQuery ();
293
+
294
+ $ this ->selects = array_filter ($ this ->selects );
295
+
296
+ $ query = empty ($ this ->selects ) ? '' : 'select= ' . implode (', ' , $ this ->selects ) . '; ' ;
271
297
272
298
foreach ($ properties as $ property )
273
299
{
274
300
$ element = $ this ->{$ property ->name };
275
301
276
302
if ($ property ->isProtected () && ! empty ($ element ))
277
303
{
278
- if (! is_array ($ element ))
279
- {
280
- $ element = [$ element ];
281
- }
304
+ $ element = array_filter (static ::arrayWrap ($ element ));
282
305
283
306
foreach ($ element as $ value )
284
307
{
@@ -293,12 +316,12 @@ public function toQuery()
293
316
$ exclude = false ;
294
317
}
295
318
296
- $ query .= ($ exclude ? '! ' : '' ) . $ property ->name . '= ' . $ value . '; ' ;
319
+ $ query .= ($ exclude ? '! ' : '' ) . $ property ->name . '= ' . $ value . '; ' ;
297
320
}
298
321
}
299
322
}
300
323
301
- return $ query ;
324
+ return trim ( $ query) ;
302
325
}
303
326
304
327
/**
@@ -325,23 +348,23 @@ public function escapeString($string)
325
348
*/
326
349
public function getQuery ()
327
350
{
328
- return $ this ->query ;
351
+ return implode ( ' ' , $ this ->queries ) ;
329
352
}
330
353
331
354
/**
332
355
* @param mixed $query
333
356
*/
334
357
public function setQuery ($ query )
335
358
{
336
- $ this ->query = str_replace ('; ' , '' , $ query );
359
+ $ this ->queries = [ str_replace ('; ' , '' , $ query )] ;
337
360
}
338
361
339
362
/**
340
363
* @param mixed $query
341
364
*/
342
365
public function appendQuery ($ query )
343
366
{
344
- $ this ->query . = str_replace ('; ' , '' , $ query );
367
+ $ this ->queries [] = str_replace ('; ' , '' , $ query );
345
368
}
346
369
347
370
public function __call ($ name , $ arguments )
@@ -353,4 +376,21 @@ public function __call($name, $arguments)
353
376
354
377
$ this ->fieldQuery ($ name , $ arguments [0 ]);
355
378
}
379
+
380
+ /**
381
+ * If the given value is not an array and not null, wrap it in one.
382
+ *
383
+ * @param mixed $value
384
+ *
385
+ * @return array
386
+ */
387
+ private static function arrayWrap ($ value )
388
+ {
389
+ if (is_null ($ value ))
390
+ {
391
+ return [];
392
+ }
393
+
394
+ return is_array ($ value ) ? $ value : [$ value ];
395
+ }
356
396
}
0 commit comments