1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Solr Client Symfony package. |
7
|
|
|
* |
8
|
|
|
* (c) ingatlan.com Zrt. <[email protected]> |
9
|
|
|
* |
10
|
|
|
* This source file is subject to the MIT license that is bundled |
11
|
|
|
* with this source code in the file LICENSE. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace iCom\SolrClient\Query; |
15
|
|
|
|
16
|
|
|
use iCom\SolrClient\JsonQuery; |
17
|
|
|
use iCom\SolrClient\Query\Command\Add; |
18
|
|
|
use iCom\SolrClient\Query\Command\Commit; |
19
|
|
|
use iCom\SolrClient\Query\Command\Delete; |
20
|
|
|
use iCom\SolrClient\Query\Command\Optimize; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Creates a JSON formatted update query. |
24
|
|
|
* |
25
|
|
|
* Note that: Solr has his own JSON syntax which allows to have multiple keys (multiple commands). |
26
|
|
|
* |
27
|
|
|
* @see https://lucene.apache.org/solr/guide/8_3/uploading-data-with-index-handlers.html#sending-json-update-commands |
28
|
|
|
* |
29
|
|
|
* @psalm-immutable |
30
|
|
|
*/ |
31
|
|
|
final class UpdateQuery implements JsonQuery |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var Command[] |
35
|
|
|
* @psaml-var list<Command> |
36
|
|
|
*/ |
37
|
|
|
private $commands = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @psalm-param list<Command> $commands |
41
|
|
|
*/ |
42
|
|
|
public function __construct(iterable $commands = []) |
43
|
|
|
{ |
44
|
|
|
$this->commands = (static function (Command ...$commands): array { |
45
|
|
|
return $commands; |
46
|
|
|
})(...$commands); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @psalm-param list<Command> $commands |
51
|
|
|
*/ |
52
|
|
|
public static function create(iterable $commands = []): self |
53
|
|
|
{ |
54
|
|
|
return new self($commands); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function add(array $document, ?int $commitWithin = null, ?bool $overwrite = null): self |
58
|
|
|
{ |
59
|
|
|
$add = Add::create($document); |
60
|
|
|
|
61
|
|
|
if (null !== $overwrite) { |
62
|
|
|
$add = $overwrite ? $add->enableOverWrite() : $add->disableOverWrite(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (null !== $commitWithin) { |
66
|
|
|
$add = $add->commitWithin($commitWithin); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $this->withCommand($add); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function commit(?bool $waitSearcher = null, ?bool $expungeDeletes = null): self |
73
|
|
|
{ |
74
|
|
|
$commit = Commit::create(); |
75
|
|
|
|
76
|
|
|
if (null !== $waitSearcher) { |
77
|
|
|
$commit = $waitSearcher ? $commit->enableWaitSearcher() : $commit->disableWaitSearcher(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (null !== $expungeDeletes) { |
81
|
|
|
$commit = $expungeDeletes ? $commit->enableExpungeDeletes() : $commit->disableExpungeDeletes(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this->withCommand($commit); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function optimize(?bool $waitSearcher = null, ?int $maxSegments = null): self |
88
|
|
|
{ |
89
|
|
|
$optimize = Optimize::create(); |
90
|
|
|
|
91
|
|
|
if (null !== $waitSearcher) { |
92
|
|
|
$optimize = $waitSearcher ? $optimize->enableWaitSearcher() : $optimize->disableWaitSearcher(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if (null !== $maxSegments) { |
96
|
|
|
$optimize = $optimize->maxSegments($maxSegments); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $this->withCommand($optimize); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function deleteByIds(array $ids): self |
103
|
|
|
{ |
104
|
|
|
return $this->withCommand(Delete::fromIds($ids)); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function deleteByQuery(SelectQuery $query): self |
108
|
|
|
{ |
109
|
|
|
return $this->withCommand(Delete::fromQuery($query)); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function toJson(): string |
113
|
|
|
{ |
114
|
|
|
$commands = []; |
115
|
|
|
foreach ($this->commands as $command) { |
116
|
|
|
$commands[] = sprintf('"%s":%s', $command->getName(), $command->toJson()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return sprintf('{%s}', implode(',', $commands)); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
private function withCommand(Command $command): self |
123
|
|
|
{ |
124
|
|
|
$self = clone $this; |
125
|
|
|
$self->commands[] = $command; |
126
|
|
|
|
127
|
|
|
return $self; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|