/
Query.php
507 lines (451 loc) · 14.1 KB
/
Query.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*
* @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
*
* @link http://www.solarium-project.org/
*/
/**
* @namespace
*/
namespace Solarium\QueryType\Update\Query;
use Solarium\Core\Client\Client;
use Solarium\Core\Query\AbstractQuery as BaseQuery;
use Solarium\QueryType\Update\RequestBuilder;
use Solarium\QueryType\Update\ResponseParser;
use Solarium\Exception\RuntimeException;
use Solarium\Exception\InvalidArgumentException;
use Solarium\QueryType\Update\Query\Command\AbstractCommand;
use Solarium\QueryType\Update\Query\Command\Add as AddCommand;
use Solarium\QueryType\Update\Query\Command\Commit as CommitCommand;
use Solarium\QueryType\Update\Query\Command\Delete as DeleteCommand;
use Solarium\QueryType\Update\Query\Command\Optimize as OptimizeCommand;
use Solarium\QueryType\Update\Query\Command\Rollback as RollbackCommand;
use Solarium\QueryType\Update\Query\Document\DocumentInterface;
/**
* Update query.
*
* Can be used to send multiple update commands to solr, e.g. add, delete,
* rollback, commit, optimize.
* Multiple commands of any type can be combined into a single update query.
*/
class Query extends BaseQuery
{
/**
* Update command add.
*/
const COMMAND_ADD = 'add';
/**
* Update command delete.
*/
const COMMAND_DELETE = 'delete';
/**
* Update command commit.
*/
const COMMAND_COMMIT = 'commit';
/**
* Update command rollback.
*/
const COMMAND_ROLLBACK = 'rollback';
/**
* Update command optimize.
*/
const COMMAND_OPTIMIZE = 'optimize';
/**
* Update command types.
*
* @var array
*/
protected $commandTypes = array(
self::COMMAND_ADD => 'Solarium\QueryType\Update\Query\Command\Add',
self::COMMAND_DELETE => 'Solarium\QueryType\Update\Query\Command\Delete',
self::COMMAND_COMMIT => 'Solarium\QueryType\Update\Query\Command\Commit',
self::COMMAND_OPTIMIZE => 'Solarium\QueryType\Update\Query\Command\Optimize',
self::COMMAND_ROLLBACK => 'Solarium\QueryType\Update\Query\Command\Rollback',
);
/**
* Default options.
*
* @var array
*/
protected $options = array(
'handler' => 'update',
'resultclass' => 'Solarium\QueryType\Update\Result',
'documentclass' => 'Solarium\QueryType\Update\Query\Document\Document',
'omitheader' => false,
);
/**
* Array of commands.
*
* The commands will be executed in the order of this array, this can be
* important in some cases. For instance a rollback.
*
* @var AbstractCommand[]
*/
protected $commands = array();
/**
* Get type for this query.
*
* @return string
*/
public function getType()
{
return Client::QUERY_UPDATE;
}
/**
* Get a requestbuilder for this query.
*
* @return RequestBuilder
*/
public function getRequestBuilder()
{
return new RequestBuilder();
}
/**
* Get a response parser for this query.
*
* @return ResponseParser
*/
public function getResponseParser()
{
return new ResponseParser();
}
/**
* Create a command instance.
*
* @throws InvalidArgumentException
*
* @param string $type
* @param mixed $options
*
* @return AbstractCommand
*/
public function createCommand($type, $options = null)
{
$type = strtolower($type);
if (!isset($this->commandTypes[$type])) {
throw new InvalidArgumentException("Update commandtype unknown: ".$type);
}
$class = $this->commandTypes[$type];
return new $class($options);
}
/**
* Get all commands for this update query.
*
* @return AbstractCommand[]
*/
public function getCommands()
{
return $this->commands;
}
/**
* Add a command to this update query.
*
* The command must be an instance of one of the Solarium\QueryType\Update_*
* classes.
*
* @param string $key
* @param object $command
*
* @return self Provides fluent interface
*/
public function add($key, $command)
{
if (0 !== strlen($key)) {
$this->commands[$key] = $command;
} else {
$this->commands[] = $command;
}
return $this;
}
/**
* Remove a command.
*
* You can remove a command by passing its key or by passing the command instance.
*
* @param string|\Solarium\QueryType\Update\Query\Command\AbstractCommand $command
*
* @return self Provides fluent interface
*/
public function remove($command)
{
if (is_object($command)) {
foreach ($this->commands as $key => $instance) {
if ($instance === $command) {
unset($this->commands[$key]);
break;
}
}
} else {
if (isset($this->commands[$command])) {
unset($this->commands[$command]);
}
}
return $this;
}
/**
* Convenience method for adding a rollback command.
*
* If you need more control, like choosing a key for the command you need to
* create you own command instance and use the add method.
*
* @return self Provides fluent interface
*/
public function addRollback()
{
return $this->add(null, new RollbackCommand());
}
/**
* Convenience method for adding a delete query command.
*
* If you need more control, like choosing a key for the command you need to
* create you own command instance and use the add method.
*
* @param string $query
* @param array $bind Bind values for placeholders in the query string
*
* @return self Provides fluent interface
*/
public function addDeleteQuery($query, $bind = null)
{
if (!is_null($bind)) {
$query = $this->getHelper()->assemble($query, $bind);
}
$delete = new DeleteCommand();
$delete->addQuery($query);
return $this->add(null, $delete);
}
/**
* Convenience method to add a multi delete query command.
*
* If you need more control, like choosing a key for the command you need to
* create you own command instance and use the add method.
*
* @param array $queries
*
* @return self Provides fluent interface
*/
public function addDeleteQueries($queries)
{
$delete = new DeleteCommand();
$delete->addQueries($queries);
return $this->add(null, $delete);
}
/**
* Convenience method to add a delete by ID command.
*
* If you need more control, like choosing a key for the command you need to
* create you own command instance and use the add method.
*
* @param int|string $id
*
* @return self Provides fluent interface
*/
public function addDeleteById($id)
{
$delete = new DeleteCommand();
$delete->addId($id);
return $this->add(null, $delete);
}
/**
* Convenience method to add a delete by IDs command.
*
* If you need more control, like choosing a key for the command you need to
* create you own command instance and use the add method.
*
* @param array $ids
*
* @return self Provides fluent interface
*/
public function addDeleteByIds($ids)
{
$delete = new DeleteCommand();
$delete->addIds($ids);
return $this->add(null, $delete);
}
/**
* Convenience method to add a 'add document' command.
*
* If you need more control, like choosing a key for the command you need to
* create you own command instance and use the add method.
*
* @param DocumentInterface $document
* @param boolean $overwrite
* @param int $commitWithin
*
* @return self Provides fluent interface
*/
public function addDocument(DocumentInterface $document, $overwrite = null, $commitWithin = null)
{
return $this->addDocuments(array($document), $overwrite, $commitWithin);
}
/**
* Convenience method to add a 'add documents' command.
*
* If you need more control, like choosing a key for the command you need to
* create you own command instance and use the add method.
*
* @param array $documents
* @param boolean $overwrite
* @param int $commitWithin
*
* @return self Provides fluent interface
*/
public function addDocuments($documents, $overwrite = null, $commitWithin = null)
{
$add = new AddCommand();
if (null !== $overwrite) {
$add->setOverwrite($overwrite);
}
if (null !== $commitWithin) {
$add->setCommitWithin($commitWithin);
}
$add->addDocuments($documents);
return $this->add(null, $add);
}
/**
* Convenience method to add a commit command.
*
* If you need more control, like choosing a key for the command you need to
* create you own command instance and use the add method.
*
* @param boolean $softCommit
* @param boolean $waitSearcher
* @param boolean $expungeDeletes
*
* @return self Provides fluent interface
*/
public function addCommit($softCommit = null, $waitSearcher = null, $expungeDeletes = null)
{
$commit = new CommitCommand();
if (null !== $softCommit) {
$commit->setSoftCommit($softCommit);
}
if (null !== $waitSearcher) {
$commit->setWaitSearcher($waitSearcher);
}
if (null !== $expungeDeletes) {
$commit->setExpungeDeletes($expungeDeletes);
}
return $this->add(null, $commit);
}
/**
* Convenience method to add an optimize command.
*
* If you need more control, like choosing a key for the command you need to
* create you own command instance and use the add method.
*
* @param boolean $softCommit
* @param boolean $waitSearcher
* @param int $maxSegments
*
* @return self Provides fluent interface
*/
public function addOptimize($softCommit = null, $waitSearcher = null, $maxSegments = null)
{
$optimize = new OptimizeCommand();
if (null !== $softCommit) {
$optimize->setSoftCommit($softCommit);
}
if (null !== $waitSearcher) {
$optimize->setWaitSearcher($waitSearcher);
}
if (null !== $maxSegments) {
$optimize->setMaxSegments($maxSegments);
}
return $this->add(null, $optimize);
}
/**
* Set a custom document class for use in the createDocument method.
*
* This class should implement the document interface
*
* @param string $value classname
*
* @return self Provides fluent interface
*/
public function setDocumentClass($value)
{
return $this->setOption('documentclass', $value);
}
/**
* Get the current documentclass option.
*
* The value is a classname, not an instance
*
* @return string
*/
public function getDocumentClass()
{
return $this->getOption('documentclass');
}
/**
* Create a document object instance.
*
* You can optionally directly supply the fields and boosts
* to get a ready-made document instance for direct use in an add command
*
* @since 2.1.0
*
* @param array $fields
* @param array $boosts
* @param array $modifiers
*
* @return DocumentInterface
*/
public function createDocument($fields = array(), $boosts = array(), $modifiers = array())
{
$class = $this->getDocumentClass();
return new $class($fields, $boosts, $modifiers);
}
/**
* Initialize options.
*
* Several options need some extra checks or setup work, for these options
* the setters are called.
*
* @throws RuntimeException
*/
protected function init()
{
if (isset($this->options['command'])) {
foreach ($this->options['command'] as $key => $value) {
$type = $value['type'];
if ($type == self::COMMAND_ADD) {
throw new RuntimeException(
"Adding documents is not supported in configuration, use the API for this"
);
}
$this->add($key, $this->createCommand($type, $value));
}
}
}
}