1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GroupByInc\API; |
4
|
|
|
|
5
|
|
|
use GroupByInc\API\Model\Bias as MBias; |
6
|
|
|
use GroupByInc\API\Model\Biasing as MBiasing; |
7
|
|
|
use GroupByInc\API\Model\CustomUrlParam; |
8
|
|
|
use GroupByInc\API\Model\MatchStrategy as MMatchStrategy; |
9
|
|
|
use GroupByInc\API\Model\Navigation; |
10
|
|
|
use GroupByInc\API\Model\PartialMatchRule as MPartialMatchRule; |
11
|
|
|
use GroupByInc\API\Model\Refinement; |
12
|
|
|
use GroupByInc\API\Model\Refinement\Type; |
13
|
|
|
use GroupByInc\API\Model\RefinementRange; |
14
|
|
|
use GroupByInc\API\Model\RefinementValue; |
15
|
|
|
use GroupByInc\API\Model\Sort as MSort; |
16
|
|
|
use GroupByInc\API\Request\Bias; |
17
|
|
|
use GroupByInc\API\Request\Biasing; |
18
|
|
|
use GroupByInc\API\Request\MatchStrategy as RMatchStrategy; |
19
|
|
|
use GroupByInc\API\Request\PartialMatchRule as RPartialMatchRule; |
20
|
|
|
use GroupByInc\API\Request\RefinementsRequest; |
21
|
|
|
use GroupByInc\API\Request\Request; |
22
|
|
|
use GroupByInc\API\Request\RestrictNavigation; |
23
|
|
|
use GroupByInc\API\Request\SelectedRefinement; |
24
|
|
|
use GroupByInc\API\Request\SelectedRefinementRange; |
25
|
|
|
use GroupByInc\API\Request\SelectedRefinementValue; |
26
|
|
|
use GroupByInc\API\Request\Sort as RSort; |
27
|
|
|
use GroupByInc\API\Util\SerializerFactory; |
28
|
|
|
use GroupByInc\API\Util\StringBuilder; |
29
|
|
|
use GroupByInc\API\Util\StringUtils; |
30
|
|
|
use JMS\Serializer\Serializer; |
31
|
|
|
use RuntimeException; |
32
|
|
|
|
33
|
|
|
class Symbol |
34
|
|
|
{ |
35
|
|
|
const TILDE = "~"; |
36
|
|
|
const DOT = "."; |
37
|
|
|
const DOUBLE_DOT = ".."; |
38
|
|
|
const EQUAL = "="; |
39
|
|
|
const COLON = ":"; |
40
|
|
|
const AMPERSAND = "&"; |
41
|
|
|
const SLASH = "/"; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
class Query |
45
|
|
|
{ |
46
|
|
|
/** @var string */ |
47
|
|
|
private $userId; |
48
|
|
|
/** @var string */ |
49
|
|
|
private $query; |
50
|
|
|
/** @var int */ |
51
|
|
|
private $skip = 0; |
52
|
|
|
/** @var int */ |
53
|
|
|
private $pageSize = 10; |
54
|
|
|
/** @var string */ |
55
|
|
|
private $collection; |
56
|
|
|
/** @var string */ |
57
|
|
|
private $area; |
58
|
|
|
/** @var string */ |
59
|
|
|
private $biasingProfile; |
60
|
|
|
/** @var string */ |
61
|
|
|
private $language; |
62
|
|
|
/** @var MSort[] */ |
63
|
|
|
private $sort; |
64
|
|
|
/** @var CustomUrlParam[] */ |
65
|
|
|
private $customUrlParams = array(); |
66
|
|
|
/** @var Navigation[] */ |
67
|
|
|
private $navigations = array(); |
68
|
|
|
/** @var string[] */ |
69
|
|
|
private $includedNavigations = array(); |
70
|
|
|
/** @var string[] */ |
71
|
|
|
private $excludedNavigations = array(); |
72
|
|
|
/** @var string[] */ |
73
|
|
|
private $fields = array(); |
74
|
|
|
/** @var string[] */ |
75
|
|
|
private $orFields = array(); |
76
|
|
|
/** @var bool */ |
77
|
|
|
private $pruneRefinements = true; |
78
|
|
|
/** @var bool */ |
79
|
|
|
private $disableAutocorrection = false; |
80
|
|
|
/** @var bool */ |
81
|
|
|
private $wildcardSearchEnabled = false; |
82
|
|
|
// Removed until CBOR support for serialization / de-serialization improves |
83
|
|
|
// /** @var bool */ |
84
|
|
|
// private $returnBinary = false; |
85
|
|
|
/** @var RestrictNavigation */ |
86
|
|
|
private $restrictNavigation; |
87
|
|
|
/** @var MBiasing */ |
88
|
|
|
private $biasing; |
89
|
|
|
|
90
|
|
|
/** @var Serializer */ |
91
|
|
|
private $serializer; |
92
|
|
|
|
93
|
|
|
const TILDE_REGEX = "/~((?=[\\w]*[=:]))/"; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param mixed $request |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
private function requestToJson($request) |
101
|
|
|
{ |
102
|
|
|
$jsonRequest = null; |
103
|
|
|
try { |
104
|
|
|
$jsonRequest = $this->serializer->serialize($request, 'json'); |
105
|
|
|
} catch (RuntimeException $e) { |
106
|
|
|
throw new RuntimeException('Unable to serialize request ' . var_dump($request)); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $jsonRequest; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $clientKey Your client key. |
114
|
|
|
* |
115
|
|
|
* @return string JSON representation of request to Bridge. |
116
|
|
|
*/ |
117
|
|
|
public function getBridgeJson($clientKey) |
118
|
|
|
{ |
119
|
|
|
$data = $this->populateRequest($clientKey); |
120
|
|
|
return $this->requestToJson($data); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param string $clientKey Your client key. |
125
|
|
|
* @param string $navigationName Name of the navigation to get refinements for |
126
|
|
|
* |
127
|
|
|
* @return string JSON representation of request to Bridge. |
128
|
|
|
*/ |
129
|
|
|
public function getBridgeRefinementsJson($clientKey, $navigationName) |
130
|
|
|
{ |
131
|
|
|
$data = new RefinementsRequest(); |
132
|
|
|
$data->originalQuery = $this->populateRequest($clientKey); |
133
|
|
|
$data->navigationName = $navigationName; |
134
|
|
|
return $this->requestToJson($data); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $clientKey |
139
|
|
|
* |
140
|
|
|
* @return Request |
141
|
|
|
*/ |
142
|
|
|
private function populateRequest($clientKey) |
143
|
|
|
{ |
144
|
|
|
$request = new Request(); |
145
|
|
|
$request->clientKey = $clientKey; |
146
|
|
|
$request->area = $this->area; |
147
|
|
|
$request->collection = $this->collection; |
148
|
|
|
$request->userId = $this->userId; |
149
|
|
|
$request->query = $this->query; |
150
|
|
|
$request->fields = $this->fields; |
151
|
|
|
$request->orFields = $this->orFields; |
152
|
|
|
$request->language = $this->language; |
153
|
|
|
$request->biasingProfile = $this->biasingProfile; |
154
|
|
|
$request->pageSize = $this->pageSize; |
155
|
|
|
$request->skip = $this->skip; |
156
|
|
|
$request->customUrlParams = $this->customUrlParams; |
157
|
|
|
$request->refinements = $this->generateSelectedRefinements($this->navigations); |
|
|
|
|
158
|
|
|
$request->restrictNavigation = $this->restrictNavigation; |
159
|
|
|
|
160
|
|
|
if (!empty($this->biasing)) { |
161
|
|
|
$request->biasing = self::convertBiasing($this->biasing); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
if (!empty($this->includedNavigations)) { |
165
|
|
|
$request->includedNavigations = $this->includedNavigations; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
if (!empty($this->excludedNavigations)) { |
169
|
|
|
$request->excludedNavigations = $this->excludedNavigations; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$pruneRefinements = $this->pruneRefinements; |
173
|
|
|
if (isset($pruneRefinements) && $pruneRefinements === false) { |
174
|
|
|
$request->pruneRefinements = false; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$disableAutocorrection = $this->disableAutocorrection; |
178
|
|
|
if (isset($disableAutocorrection) && $disableAutocorrection === true) { |
179
|
|
|
$request->disableAutocorrection = true; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$wildcardSearchEnabled = $this->wildcardSearchEnabled; |
183
|
|
|
if (isset($wildcardSearchEnabled) && $wildcardSearchEnabled === true) { |
184
|
|
|
$request->wildcardSearchEnabled = true; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if (!empty($this->sort)) { |
188
|
|
|
foreach ($this->sort as $s) { |
189
|
|
|
array_push($request->sort, $this->convertSort($s)); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
// $returnBinary = $this->returnBinary; |
194
|
|
|
// if (isset($returnBinary) && $returnBinary === true) { |
195
|
|
|
// $request->returnBinary = true; |
196
|
|
|
// } |
197
|
|
|
|
198
|
|
|
return $request; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param Navigation[] $navigations |
203
|
|
|
* |
204
|
|
|
* @return Refinement[] |
205
|
|
|
*/ |
206
|
|
|
private function generateSelectedRefinements($navigations) |
207
|
|
|
{ |
208
|
|
|
$refinements = []; |
209
|
|
|
foreach ($navigations as $key => $navigation) { |
210
|
|
|
foreach ($navigation->getRefinements() as $refinement) { |
211
|
|
|
switch ($refinement->getType()) { |
212
|
|
|
case Type::Range: { |
213
|
|
|
/** @var RefinementRange $rr */ |
214
|
|
|
$rr = $refinement; |
215
|
|
|
$selectedRefinementRange = new SelectedRefinementRange(); |
216
|
|
|
$selectedRefinementRange |
217
|
|
|
->setNavigationName($navigation->getName()) |
218
|
|
|
->setLow($rr->getLow()) |
219
|
|
|
->setHigh($rr->getHigh()) |
220
|
|
|
->setExclude($rr->isExclude()); |
221
|
|
|
|
222
|
|
|
array_push($refinements, $selectedRefinementRange); |
223
|
|
|
break; |
224
|
|
|
} |
225
|
|
|
case Type::Value: { |
226
|
|
|
/** @var RefinementValue $rv */ |
227
|
|
|
$rv = $refinement; |
228
|
|
|
$selectedRefinementValue = new SelectedRefinementValue(); |
229
|
|
|
$selectedRefinementValue |
230
|
|
|
->setNavigationName($navigation->getName()) |
231
|
|
|
->setValue($rv->getValue()) |
232
|
|
|
->setExclude($rv->isExclude()); |
233
|
|
|
|
234
|
|
|
array_push($refinements, $selectedRefinementValue); |
235
|
|
|
break; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
return $refinements; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @param string $clientKey Your client key. |
245
|
|
|
* |
246
|
|
|
* @return string JSON representation of request to Bridge. |
247
|
|
|
*/ |
248
|
|
|
public function getBridgeJsonRefinementSearch($clientKey) |
249
|
|
|
{ |
250
|
|
|
$data = new Request(); |
251
|
|
|
$data->clientKey = $clientKey; |
252
|
|
|
$data->collection = $this->collection; |
253
|
|
|
$data->area = $this->area; |
254
|
|
|
$data->refinementQuery = $this->query; |
255
|
|
|
|
256
|
|
|
$wildcardSearchEnabled = $this->wildcardSearchEnabled; |
257
|
|
|
if (isset($wildcardSearchEnabled) && $wildcardSearchEnabled === true) { |
258
|
|
|
$data->wildcardSearchEnabled = true; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
return $this->requestToJson($data); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
public function __construct() |
265
|
|
|
{ |
266
|
|
|
$this->serializer = SerializerFactory::build(); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @return string The current search string. |
271
|
|
|
*/ |
272
|
|
|
public function getQuery() |
273
|
|
|
{ |
274
|
|
|
return $this->query; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @param string $query The search string. |
279
|
|
|
*/ |
280
|
|
|
public function setQuery($query) |
281
|
|
|
{ |
282
|
|
|
$this->query = $query; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @return string The data sub-collection. |
287
|
|
|
*/ |
288
|
|
|
public function getCollection() |
289
|
|
|
{ |
290
|
|
|
return $this->collection; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* @param string $collection The string representation of a collection query. |
295
|
|
|
*/ |
296
|
|
|
public function setCollection($collection) |
297
|
|
|
{ |
298
|
|
|
$this->collection = $collection; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* @return string The area name. |
303
|
|
|
*/ |
304
|
|
|
public function getArea() |
305
|
|
|
{ |
306
|
|
|
return $this->area; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* @param string $area The area name. |
311
|
|
|
*/ |
312
|
|
|
public function setArea($area) |
313
|
|
|
{ |
314
|
|
|
$this->area = $area; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* @return string[] A list of metadata fields that will be returned by the search engine. |
319
|
|
|
*/ |
320
|
|
|
public function getFields() |
321
|
|
|
{ |
322
|
|
|
return $this->fields; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* @return string[] A list of the fields that the search service will treat as OR'able. |
327
|
|
|
*/ |
328
|
|
|
public function getOrFields() |
329
|
|
|
{ |
330
|
|
|
return $this->orFields; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* @param string[] $fields A list of case-sensitive names of the attributes to return. |
335
|
|
|
*/ |
336
|
|
|
public function addFields($fields) |
337
|
|
|
{ |
338
|
|
|
$this->fields = array_merge($this->fields, $fields); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* @return string[] A list of which navigations to return from the bridge. |
343
|
|
|
*/ |
344
|
|
|
public function getIncludedNavigations() |
345
|
|
|
{ |
346
|
|
|
return $this->includedNavigations; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* @param string[] $navigations A list of which navigations to return from the bridge. |
351
|
|
|
*/ |
352
|
|
|
public function addIncludedNavigations($navigations) |
353
|
|
|
{ |
354
|
|
|
$this->includedNavigations = array_merge($this->includedNavigations, $navigations); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* @return string[] A list of which navigations to not return from the bridge. |
359
|
|
|
*/ |
360
|
|
|
public function getExcludedNavigations() |
361
|
|
|
{ |
362
|
|
|
return $this->excludedNavigations; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* @param string[] $navigations A list of which navigations to not return from the bridge. |
367
|
|
|
*/ |
368
|
|
|
public function addExcludedNavigations($navigations) |
369
|
|
|
{ |
370
|
|
|
$this->excludedNavigations = array_merge($this->excludedNavigations, $navigations); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* @return Navigation[] |
375
|
|
|
*/ |
376
|
|
|
public function &getNavigations() |
377
|
|
|
{ |
378
|
|
|
return $this->navigations; |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* @param Navigation[] $navigations |
383
|
|
|
*/ |
384
|
|
|
public function setNavigations($navigations) |
385
|
|
|
{ |
386
|
|
|
$this->navigations = $navigations; |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* @param string $name The case-sensitive name of the attribute to return. |
391
|
|
|
*/ |
392
|
|
|
public function addField($name) |
393
|
|
|
{ |
394
|
|
|
array_push($this->fields, $name); |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* @param string $name Field that should be treated as OR. |
399
|
|
|
*/ |
400
|
|
|
public function addOrField($name) |
401
|
|
|
{ |
402
|
|
|
array_push($this->orFields, $name); |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* @param string[] $fields A list of fields that should be treated as OR. |
407
|
|
|
*/ |
408
|
|
|
public function addOrFields($fields) |
409
|
|
|
{ |
410
|
|
|
$this->orFields = array_merge($this->orFields, $fields); |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
/** |
414
|
|
|
* @param string $name The parameter name. |
415
|
|
|
* @param string $value The parameter value. |
416
|
|
|
*/ |
417
|
|
|
public function addCustomUrlParamByName($name, $value) |
418
|
|
|
{ |
419
|
|
|
$param = new CustomUrlParam(); |
420
|
|
|
$this->addCustomUrlParam($param->setKey($name)->setValue($value)); |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
/** |
424
|
|
|
* @param CustomUrlParam $param Set an additional parameter that can be used to trigger rules. |
425
|
|
|
*/ |
426
|
|
|
public function addCustomUrlParam($param) |
427
|
|
|
{ |
428
|
|
|
array_push($this->customUrlParams, $param); |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
public function splitRefinements($refinementString) |
432
|
|
|
{ |
433
|
|
|
if (StringUtils::isNotBlank($refinementString)) { |
434
|
|
|
return preg_split(self::TILDE_REGEX, $refinementString, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); |
435
|
|
|
} |
436
|
|
|
return []; |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
/** |
440
|
|
|
* @param string $refinementString A tilde separated list of refinements. |
441
|
|
|
*/ |
442
|
|
|
public function addRefinementsByString($refinementString) |
443
|
|
|
{ |
444
|
|
|
if ($refinementString == null) { |
445
|
|
|
return; |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
$refinementStrings = self::splitRefinements($refinementString); |
449
|
|
|
foreach ($refinementStrings as $refinementString) { |
450
|
|
|
if (empty($refinementString) || "=" == $refinementString) { |
451
|
|
|
continue; |
452
|
|
|
} |
453
|
|
|
$colon = strpos($refinementString, Symbol::COLON); |
454
|
|
|
$equals = strpos($refinementString, Symbol::EQUAL); |
455
|
|
|
//when === false, it means it did not find the substring in the string |
456
|
|
|
$isRange = !($colon === false) && ($equals === false); |
457
|
|
|
|
458
|
|
|
$refinement = null; |
|
|
|
|
459
|
|
|
if ($isRange) { |
460
|
|
|
$nameValue = explode(Symbol::COLON, $refinementString, 2); |
461
|
|
|
$refinement = new RefinementRange(); |
462
|
|
|
if (StringUtils::endsWith($nameValue[1], Symbol::DOUBLE_DOT)) { |
463
|
|
|
$value = explode(Symbol::DOUBLE_DOT, $nameValue[1]); |
464
|
|
|
$refinement->setLow($value[0]); |
465
|
|
|
$refinement->setHigh(""); |
466
|
|
|
} else if (StringUtils::startsWith($nameValue[1], Symbol::DOUBLE_DOT)) { |
467
|
|
|
$refinement->setLow(""); |
468
|
|
|
$value = explode(Symbol::DOUBLE_DOT, $nameValue[1]); |
469
|
|
|
$refinement->setHigh($value[1]); |
470
|
|
|
} else { |
471
|
|
|
$lowHigh = explode(Symbol::DOUBLE_DOT, $nameValue[1]); |
472
|
|
|
$refinement->setLow($lowHigh[0]); |
473
|
|
|
$refinement->setHigh($lowHigh[1]); |
474
|
|
|
} |
475
|
|
|
} else { |
476
|
|
|
$nameValue = explode(Symbol::EQUAL, $refinementString, 2); |
477
|
|
|
$refinement = new RefinementValue(); |
478
|
|
|
$refinement->setValue($nameValue[1]); |
479
|
|
|
} |
480
|
|
|
if (!empty($nameValue[0])) { |
481
|
|
|
$this->addRefinement($nameValue[0], $refinement); |
482
|
|
|
} |
483
|
|
|
} |
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
/** |
487
|
|
|
* @param string $navigationName The name of the Navigation. |
488
|
|
|
* @param Refinement $refinement A RefinementRange or RefinementValue object. |
489
|
|
|
*/ |
490
|
|
|
public function addRefinement($navigationName, $refinement) |
491
|
|
|
{ |
492
|
|
|
$navigation = null; |
|
|
|
|
493
|
|
|
if (array_key_exists($navigationName, $this->navigations)) { |
494
|
|
|
$navigation = $this->navigations[$navigationName]; |
495
|
|
|
} else { |
496
|
|
|
$navigation = new Navigation(); |
497
|
|
|
$navigation->setName($navigationName)->setRange($refinement instanceof SelectedRefinementRange); |
498
|
|
|
$this->navigations[$navigationName] = $navigation; |
499
|
|
|
} |
500
|
|
|
$refinements = $navigation->getRefinements(); |
501
|
|
|
array_push($refinements, $refinement); |
502
|
|
|
$navigation->setRefinements($refinements); |
503
|
|
|
} |
504
|
|
|
|
505
|
|
|
/** |
506
|
|
|
* @param string $navigationName The name of the refinement. |
507
|
|
|
* @param mixed $low The low value. |
508
|
|
|
* @param mixed $high The high value. |
509
|
|
|
* @param bool $exclude True if the results should exclude this range refinement, false otherwise. |
510
|
|
|
*/ |
511
|
|
|
public function addRangeRefinement($navigationName, $low, $high, $exclude = false) |
512
|
|
|
{ |
513
|
|
|
$refinement = new RefinementRange(); |
514
|
|
|
$this->addRefinement($navigationName, $refinement->setLow($low)->setHigh($high)->setExclude($exclude)); |
515
|
|
|
} |
516
|
|
|
|
517
|
|
|
/** |
518
|
|
|
* @param string $navigationName The name of the refinement. |
519
|
|
|
* @param mixed $value The refinement value. |
520
|
|
|
* @param bool $exclude True if the results should exclude this value refinement, false otherwise. |
521
|
|
|
*/ |
522
|
|
|
public function addValueRefinement($navigationName, $value, $exclude = false) |
523
|
|
|
{ |
524
|
|
|
$refinement = new RefinementValue();; |
525
|
|
|
$this->addRefinement($navigationName, $refinement->setValue($value)->setExclude($exclude)); |
526
|
|
|
} |
527
|
|
|
|
528
|
|
|
/** |
529
|
|
|
* @return bool Are refinements with zero counts being removed. |
530
|
|
|
*/ |
531
|
|
|
public function isPruneRefinements() |
532
|
|
|
{ |
533
|
|
|
return $this->pruneRefinements; |
534
|
|
|
} |
535
|
|
|
|
536
|
|
|
/** |
537
|
|
|
* @param bool $pruneRefinements Specifies whether refinements should be pruned. |
538
|
|
|
*/ |
539
|
|
|
public function setPruneRefinements($pruneRefinements) |
540
|
|
|
{ |
541
|
|
|
$this->pruneRefinements = $pruneRefinements; |
542
|
|
|
} |
543
|
|
|
|
544
|
|
|
/** |
545
|
|
|
* @return MSort[] The current list of sort parameters. |
546
|
|
|
*/ |
547
|
|
|
public function &getSort() |
548
|
|
|
{ |
549
|
|
|
return $this->sort; |
550
|
|
|
} |
551
|
|
|
|
552
|
|
|
/** |
553
|
|
|
* @param MSort[] $sort Any number of sort criteria. |
554
|
|
|
*/ |
555
|
|
|
public function setSort($sort) |
556
|
|
|
{ |
557
|
|
|
$this->sort = $sort; |
558
|
|
|
} |
559
|
|
|
|
560
|
|
|
/** |
561
|
|
|
* @return int The number of documents to skip. |
562
|
|
|
*/ |
563
|
|
|
public function getSkip() |
564
|
|
|
{ |
565
|
|
|
return $this->skip; |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
/** |
569
|
|
|
* @param int $skip The number of documents to skip. |
570
|
|
|
*/ |
571
|
|
|
public function setSkip($skip) |
572
|
|
|
{ |
573
|
|
|
$this->skip = $skip; |
574
|
|
|
} |
575
|
|
|
|
576
|
|
|
/** |
577
|
|
|
* @return CustomUrlParam[] A list of custom url params. |
578
|
|
|
*/ |
579
|
|
|
public function getCustomUrlParams() |
580
|
|
|
{ |
581
|
|
|
return $this->customUrlParams; |
582
|
|
|
} |
583
|
|
|
|
584
|
|
|
/** |
585
|
|
|
* @param CustomUrlParam[] $customUrlParams Set the custom url params. |
586
|
|
|
*/ |
587
|
|
|
public function setCustomUrlParams($customUrlParams) |
588
|
|
|
{ |
589
|
|
|
$this->customUrlParams = $customUrlParams; |
590
|
|
|
} |
591
|
|
|
|
592
|
|
|
// /** |
593
|
|
|
// * @return bool Is return JSON set to true. |
594
|
|
|
// */ |
595
|
|
|
// public function isReturnBinary() |
596
|
|
|
// { |
597
|
|
|
// return $this->returnBinary; |
598
|
|
|
// } |
599
|
|
|
// |
600
|
|
|
// /** |
601
|
|
|
// * @param bool $returnBinary Whether to tell the bridge to return binary data rather than JSON. |
602
|
|
|
// */ |
603
|
|
|
// public function setReturnBinary($returnBinary) |
604
|
|
|
// { |
605
|
|
|
// $this->returnBinary = $returnBinary; |
606
|
|
|
// } |
607
|
|
|
|
608
|
|
|
/** |
609
|
|
|
* @return string The current language restrict value. |
610
|
|
|
*/ |
611
|
|
|
public function getLanguage() |
612
|
|
|
{ |
613
|
|
|
return $this->language; |
614
|
|
|
} |
615
|
|
|
|
616
|
|
|
/** |
617
|
|
|
* @param string $language The value for language restrict. |
618
|
|
|
*/ |
619
|
|
|
public function setLanguage($language) |
620
|
|
|
{ |
621
|
|
|
$this->language = $language; |
622
|
|
|
} |
623
|
|
|
|
624
|
|
|
/** |
625
|
|
|
* @return string The current biasing profile name. |
626
|
|
|
*/ |
627
|
|
|
public function getBiasingProfile() |
628
|
|
|
{ |
629
|
|
|
return $this->biasingProfile; |
630
|
|
|
} |
631
|
|
|
|
632
|
|
|
/** |
633
|
|
|
* @param string $biasingProfile Override the biasing profile used for this query. |
634
|
|
|
*/ |
635
|
|
|
public function setBiasingProfile($biasingProfile) |
636
|
|
|
{ |
637
|
|
|
$this->biasingProfile = $biasingProfile; |
638
|
|
|
} |
639
|
|
|
|
640
|
|
|
/** |
641
|
|
|
* @return int The current page size. |
642
|
|
|
*/ |
643
|
|
|
public function getPageSize() |
644
|
|
|
{ |
645
|
|
|
return $this->pageSize; |
646
|
|
|
} |
647
|
|
|
|
648
|
|
|
/** |
649
|
|
|
* @param int $pageSize The number of records to return with the query. |
650
|
|
|
*/ |
651
|
|
|
public function setPageSize($pageSize) |
652
|
|
|
{ |
653
|
|
|
$this->pageSize = $pageSize; |
654
|
|
|
} |
655
|
|
|
|
656
|
|
|
/** |
657
|
|
|
* @return boolean |
658
|
|
|
*/ |
659
|
|
|
public function isDisableAutocorrection() |
660
|
|
|
{ |
661
|
|
|
return $this->disableAutocorrection; |
662
|
|
|
} |
663
|
|
|
|
664
|
|
|
/** |
665
|
|
|
* @param boolean $disableAutocorrection Specifies whether the auto-correction behavior should be disabled. |
666
|
|
|
* By default, when no results are returned for the given query (and there is |
667
|
|
|
* a did-you-mean available), the first did-you-mean is automatically queried |
668
|
|
|
* instead. |
669
|
|
|
*/ |
670
|
|
|
public function setDisableAutocorrection($disableAutocorrection) |
671
|
|
|
{ |
672
|
|
|
$this->disableAutocorrection = $disableAutocorrection; |
673
|
|
|
} |
674
|
|
|
|
675
|
|
|
/** |
676
|
|
|
* @return boolean |
677
|
|
|
*/ |
678
|
|
|
public function isWildcardSearchEnabled() |
679
|
|
|
{ |
680
|
|
|
return $this->wildcardSearchEnabled; |
681
|
|
|
} |
682
|
|
|
|
683
|
|
|
/** |
684
|
|
|
* @param boolean $wildcardSearchEnabled Indicate if the *(star) character in the search string should be treated |
685
|
|
|
* as a wildcard prefix search. For example, `sta*` will match `star` and |
686
|
|
|
* `start`. |
687
|
|
|
*/ |
688
|
|
|
public function setWildcardSearchEnabled($wildcardSearchEnabled) |
689
|
|
|
{ |
690
|
|
|
$this->wildcardSearchEnabled = $wildcardSearchEnabled; |
691
|
|
|
} |
692
|
|
|
|
693
|
|
|
/** |
694
|
|
|
* <b>Warning</b> This will count as two queries against your search index. |
695
|
|
|
* |
696
|
|
|
* Typically, this feature is used when you have a large number of navigation items that will overwhelm the end |
697
|
|
|
* user. It works by using one of the existing navigation items to decide what the query is about and fires a second |
698
|
|
|
* query to restrict the navigation to the most relevant set of navigation items for this search term. |
699
|
|
|
* |
700
|
|
|
* For example, if you pass in a search of `paper` and a restrict navigation of `category:2` |
701
|
|
|
* |
702
|
|
|
* The bridge will find the category navigation refinements in the first query and fire a second query for the top 2 |
703
|
|
|
* most populous categories. Therefore, a search for something generic like "paper" will bring back top category |
704
|
|
|
* matches like copy paper (1,030), paper pads (567). The bridge will fire off the second query with the search |
705
|
|
|
* term, plus an OR refinement with the most likely categories. The navigation items in the first query are |
706
|
|
|
* entirely replaced with the navigation items in the second query, except for the navigation that was used for the |
707
|
|
|
* restriction so that users still have the ability to navigate by all category types. |
708
|
|
|
* |
709
|
|
|
* @param RestrictNavigation $restrictNavigation Restriction criteria |
710
|
|
|
*/ |
711
|
|
|
public function setRestrictNavigation($restrictNavigation) |
712
|
|
|
{ |
713
|
|
|
$this->restrictNavigation = $restrictNavigation; |
714
|
|
|
} |
715
|
|
|
|
716
|
|
|
/** @return RestrictNavigation */ |
717
|
|
|
public function getRestrictNavigation() |
718
|
|
|
{ |
719
|
|
|
return $this->restrictNavigation; |
720
|
|
|
} |
721
|
|
|
|
722
|
|
|
/** |
723
|
|
|
* @return MBiasing |
724
|
|
|
*/ |
725
|
|
|
public function getBiasing() |
726
|
|
|
{ |
727
|
|
|
return $this->biasing; |
728
|
|
|
} |
729
|
|
|
|
730
|
|
|
/** |
731
|
|
|
* Add a biasing profile, which is defined at query time. |
732
|
|
|
* |
733
|
|
|
* @param MBiasing $biasing |
734
|
|
|
*/ |
735
|
|
|
public function setBiasing($biasing) |
736
|
|
|
{ |
737
|
|
|
$this->biasing = $biasing; |
738
|
|
|
} |
739
|
|
|
|
740
|
|
|
/** |
741
|
|
|
* @param string[] $bringToTop |
742
|
|
|
*/ |
743
|
|
|
public function setBringToTop($bringToTop) { |
744
|
|
|
if (empty($this->biasing)) { |
745
|
|
|
$this->biasing = new MBiasing(); |
746
|
|
|
} |
747
|
|
|
$this->biasing->setBringToTop($bringToTop); |
748
|
|
|
} |
749
|
|
|
|
750
|
|
|
/** |
751
|
|
|
* @param boolean $augment |
752
|
|
|
*/ |
753
|
|
|
public function setBiasingAugment($augment) { |
754
|
|
|
if (empty($this->biasing)) { |
755
|
|
|
$this->biasing = new MBiasing(); |
756
|
|
|
} |
757
|
|
|
$this->biasing->setAugmentbiases($augment); |
758
|
|
|
} |
759
|
|
|
|
760
|
|
|
/** |
761
|
|
|
* @param float $influence |
762
|
|
|
*/ |
763
|
|
|
public function setInfluence($influence) { |
764
|
|
|
if (empty($this->biasing)) { |
765
|
|
|
$this->biasing = new MBiasing(); |
766
|
|
|
} |
767
|
|
|
$this->biasing->setInfluence($influence); |
768
|
|
|
} |
769
|
|
|
|
770
|
|
|
/** |
771
|
|
|
* @return string A string representation of all of the currently set refinements. |
772
|
|
|
*/ |
773
|
|
View Code Duplication |
public function getRefinementString() |
|
|
|
|
774
|
|
|
{ |
775
|
|
|
if (!empty($this->navigations)) { |
776
|
|
|
$builder = new StringBuilder(); |
777
|
|
|
foreach ($this->navigations as $n) { |
778
|
|
|
foreach ($n->getRefinements() as $r) { |
779
|
|
|
$builder->append(Symbol::TILDE)->append($n->getName())->append($r->toTildeString()); |
780
|
|
|
} |
781
|
|
|
} |
782
|
|
|
if ($builder->length() > 0) { |
783
|
|
|
return $builder->__toString(); |
784
|
|
|
} |
785
|
|
|
} |
786
|
|
|
return null; |
787
|
|
|
} |
788
|
|
|
|
789
|
|
|
/** |
790
|
|
|
* @return string A string representation of all of the currently set custom url parameters. |
791
|
|
|
*/ |
792
|
|
View Code Duplication |
public function getCustomUrlParamsString() |
|
|
|
|
793
|
|
|
{ |
794
|
|
|
if (!empty($this->customUrlParams)) { |
795
|
|
|
$builder = new StringBuilder(); |
796
|
|
|
foreach ($this->customUrlParams as $c) { |
797
|
|
|
$builder->append(Symbol::TILDE)->append($c->getKey())->append(Symbol::EQUAL)->append($c->getValue()); |
798
|
|
|
} |
799
|
|
|
if ($builder->length() > 0) { |
800
|
|
|
return $builder->__toString(); |
801
|
|
|
} |
802
|
|
|
} |
803
|
|
|
return null; |
804
|
|
|
} |
805
|
|
|
|
806
|
|
|
/** |
807
|
|
|
* @param MSort $sort |
808
|
|
|
* |
809
|
|
|
* @return RSort |
810
|
|
|
*/ |
811
|
|
|
protected static function convertSort($sort) |
812
|
|
|
{ |
813
|
|
|
/** @var RSort $convertedSort */ |
814
|
|
|
$convertedSort = null; |
815
|
|
|
if (!empty($sort)) { |
816
|
|
|
$convertedSort = new RSort(); |
817
|
|
|
$convertedSort->setField($sort->getField()); |
818
|
|
|
switch ($sort->getOrder()) { |
819
|
|
|
case MSort\Order::Ascending: |
820
|
|
|
$convertedSort->setOrder(RSort\Order::Ascending); |
821
|
|
|
break; |
822
|
|
|
case MSort\Order::Descending: |
823
|
|
|
$convertedSort->setOrder(RSort\Order::Descending); |
824
|
|
|
break; |
825
|
|
|
} |
826
|
|
|
} |
827
|
|
|
return $convertedSort; |
828
|
|
|
} |
829
|
|
|
|
830
|
|
|
/** |
831
|
|
|
* @param MMatchStrategy $strategy |
832
|
|
|
* |
833
|
|
|
* @return RMatchStrategy |
834
|
|
|
*/ |
835
|
|
|
protected static function convertPartialMatchStrategy($strategy) |
836
|
|
|
{ |
837
|
|
|
/** @var RMatchStrategy $convertedStrategy */ |
838
|
|
|
$convertedStrategy = null; |
839
|
|
|
if (!empty($strategy)) { |
840
|
|
|
$rules = $strategy->getRules(); |
841
|
|
|
if (!empty($rules)) { |
842
|
|
|
$convertedStrategy = new RMatchStrategy(); |
843
|
|
|
/** @var MPartialMatchRule $r */ |
844
|
|
|
foreach ($rules as $r) { |
845
|
|
|
array_push($rules, Query::convertPartialMatchRule($r)); |
846
|
|
|
} |
847
|
|
|
$strategy->setRules($rules); |
848
|
|
|
} |
849
|
|
|
} |
850
|
|
|
return $convertedStrategy; |
851
|
|
|
} |
852
|
|
|
|
853
|
|
|
/** |
854
|
|
|
* @param MPartialMatchRule $rule |
855
|
|
|
* |
856
|
|
|
* @return RPartialMatchRule |
857
|
|
|
*/ |
858
|
|
|
protected static function convertPartialMatchRule($rule) |
859
|
|
|
{ |
860
|
|
|
/** @var RPartialMatchRule $convertedRule */ |
861
|
|
|
$convertedRule = null; |
862
|
|
|
if (!empty($rule)) { |
863
|
|
|
$convertedRule = new RPartialMatchRule(); |
864
|
|
|
$convertedRule->setTerms($rule->getTerms()) |
865
|
|
|
->setTermsGreaterThan($rule->getTermsGreaterThan()) |
866
|
|
|
->setMustMatch($rule->getMustMatch()) |
867
|
|
|
->setPercentage($rule->isPercentage()); |
868
|
|
|
} |
869
|
|
|
return $convertedRule; |
870
|
|
|
} |
871
|
|
|
|
872
|
|
|
/** |
873
|
|
|
* @param MBias $bias |
874
|
|
|
* |
875
|
|
|
* @return Bias |
876
|
|
|
*/ |
877
|
|
|
protected static function convertBias($bias) |
878
|
|
|
{ |
879
|
|
|
return (new MBias())->setName($bias->getName())->setContent($bias->getStrength())->setStrength($bias->getStrength()); |
880
|
|
|
} |
881
|
|
|
|
882
|
|
|
/** |
883
|
|
|
* @param MBias[] $biases |
884
|
|
|
* |
885
|
|
|
* @return Bias[] |
886
|
|
|
*/ |
887
|
|
|
protected static function convertBiases($biases) |
888
|
|
|
{ |
889
|
|
|
return array_map('self::convertBias', $biases); |
890
|
|
|
} |
891
|
|
|
|
892
|
|
|
/** |
893
|
|
|
* @param MBiasing $biasing |
894
|
|
|
* |
895
|
|
|
* @return Biasing |
896
|
|
|
*/ |
897
|
|
|
protected static function convertBiasing($biasing) |
898
|
|
|
{ |
899
|
|
|
/** @var Biasing $convertedBiasing */ |
900
|
|
|
$convertedBiasing = new Biasing(); |
901
|
|
|
|
902
|
|
|
/** @var $hasData */ |
903
|
|
|
$hasData = false; |
904
|
|
|
|
905
|
|
|
if (!empty($biasing)) { |
906
|
|
|
// != must be used because empty() only accepts variables in PHP 5.4 |
907
|
|
|
if($biasing->getBringToTop() != array()) { |
908
|
|
|
$convertedBiasing->setBringToTop($biasing->getBringToTop()); |
909
|
|
|
$hasData = true; |
910
|
|
|
} |
911
|
|
|
if($biasing->getBiases() != array()) { |
912
|
|
|
$convertedBiasing->setBiases(self::convertBiases($biasing->getBiases())); |
913
|
|
|
$convertedBiasing->setAugmentBiases($convertedBiasing->isAugmentBiases()); |
914
|
|
|
$hasData = true; |
915
|
|
|
} |
916
|
|
|
if($biasing->getInfluence() !== null) { |
917
|
|
|
$convertedBiasing->setInfluence($biasing->getInfluence()); |
918
|
|
|
$hasData = true; |
919
|
|
|
} |
920
|
|
|
} |
921
|
|
|
return $hasData ? $convertedBiasing : null; |
922
|
|
|
} |
923
|
|
|
|
924
|
|
|
/** |
925
|
|
|
* @return string |
926
|
|
|
*/ |
927
|
|
|
public function getUserId() |
928
|
|
|
{ |
929
|
|
|
return $this->userId; |
930
|
|
|
} |
931
|
|
|
|
932
|
|
|
/** |
933
|
|
|
* @param string $userId |
934
|
|
|
* |
935
|
|
|
* @return Query |
936
|
|
|
*/ |
937
|
|
|
public function setUserId($userId) |
938
|
|
|
{ |
939
|
|
|
$this->userId = $userId; |
940
|
|
|
return $this; |
941
|
|
|
} |
942
|
|
|
|
943
|
|
|
} |
944
|
|
|
|