-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathRediSearchIndexer.php
143 lines (113 loc) · 4.3 KB
/
RediSearchIndexer.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
<?php
declare(strict_types=1);
/*
* This file is part of the CMS-IG SEAL project.
*
* (c) Alexander Schranz <alexander@sulu.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CmsIg\Seal\Adapter\RediSearch;
use CmsIg\Seal\Adapter\BulkHelper;
use CmsIg\Seal\Adapter\IndexerInterface;
use CmsIg\Seal\Marshaller\Marshaller;
use CmsIg\Seal\Schema\Index;
use CmsIg\Seal\Task\SyncTask;
use CmsIg\Seal\Task\TaskInterface;
final class RediSearchIndexer implements IndexerInterface
{
private readonly Marshaller $marshaller;
public function __construct(
private readonly \Redis $client,
) {
$this->marshaller = new Marshaller(
addRawFilterTextField: true,
geoPointFieldConfig: [
'latitude' => 1,
'longitude' => 0,
'separator' => ',',
'multiple' => true,
],
);
}
public function save(Index $index, array $document, array $options = []): TaskInterface|null
{
$identifierField = $index->getIdentifierField();
/** @var string|int|null $identifier */
$identifier = $document[$identifierField->name] ?? null;
$marshalledDocument = $this->marshaller->marshall($index->fields, $document);
$jsonSet = $this->client->rawCommand(
'JSON.SET',
$index->name . ':' . ((string) $identifier),
'$',
\json_encode($marshalledDocument, \JSON_THROW_ON_ERROR),
);
if (false === $jsonSet) {
throw $this->createRedisLastErrorException();
}
if (!($options['return_slow_promise_result'] ?? false)) {
return null;
}
return new SyncTask($document);
}
public function delete(Index $index, string $identifier, array $options = []): TaskInterface|null
{
$jsonDel = $this->client->rawCommand(
'JSON.DEL',
$index->name . ':' . $identifier,
);
if (false === $jsonDel) {
throw $this->createRedisLastErrorException();
}
if (!($options['return_slow_promise_result'] ?? false)) {
return null;
}
return new SyncTask(null);
}
private function createRedisLastErrorException(): \RuntimeException
{
$lastError = $this->client->getLastError();
$this->client->clearLastError();
return new \RuntimeException('Redis: ' . $lastError);
}
public function bulk(Index $index, iterable $saveDocuments, iterable $deleteDocumentIdentifiers, int $bulkSize = 100, array $options = []): TaskInterface|null
{
$identifierField = $index->getIdentifierField();
foreach (BulkHelper::splitBulk($saveDocuments, $bulkSize) as $bulkSaveDocuments) {
$multiClient = $this->client->multi();
foreach ($bulkSaveDocuments as $document) {
/** @var string|int|null $identifier */
$identifier = $document[$identifierField->name] ?? null;
$marshalledDocument = $this->marshaller->marshall($index->fields, $document);
$multiClient->rawCommand(
'JSON.SET',
$index->name . ':' . ((string) $identifier),
'$',
\json_encode($marshalledDocument, \JSON_THROW_ON_ERROR),
);
}
$multiExec = $multiClient->exec();
if (false === $multiExec) {
throw $this->createRedisLastErrorException();
}
}
foreach (BulkHelper::splitBulk($deleteDocumentIdentifiers, $bulkSize) as $bulkDeleteDocumentIdentifiers) {
$multiClient = $this->client->multi();
foreach ($bulkDeleteDocumentIdentifiers as $deleteDocumentIdentifier) {
$multiClient->rawCommand(
'JSON.DEL',
$index->name . ':' . $deleteDocumentIdentifier,
);
}
$multiExec = $multiClient->exec();
if (false === $multiExec) {
throw $this->createRedisLastErrorException();
}
}
if (!($options['return_slow_promise_result'] ?? false)) {
return null;
}
return new SyncTask(null);
}
}