-
Notifications
You must be signed in to change notification settings - Fork 62
/
ElasticDocumentAdapter.php
51 lines (43 loc) · 1.3 KB
/
ElasticDocumentAdapter.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
<?php
declare(strict_types=1);
namespace JeroenG\Explorer\Infrastructure\Elastic;
use Elasticsearch\Client;
use Elasticsearch\Common\Exceptions\Missing404Exception;
use JeroenG\Explorer\Application\DocumentAdapterInterface;
use JeroenG\Explorer\Application\Operations\Bulk\BulkOperationInterface;
use JeroenG\Explorer\Application\Results;
use JeroenG\Explorer\Application\SearchCommandInterface;
final class ElasticDocumentAdapter implements DocumentAdapterInterface
{
public function __construct(
private Client $client,
) {
}
public function bulk(BulkOperationInterface $command): callable|array
{
return $this->client->bulk([
'body' => $command->build(),
]);
}
public function update(string $index, $id, array $data): callable|array
{
return $this->client->index([
'index' => $index,
'id' => $id,
'body' => $data,
]);
}
public function delete(string $index, $id): void
{
try {
$this->client->delete([
'index' => $index,
'id' => $id
]);
} catch (Missing404Exception) {}
}
public function search(SearchCommandInterface $command): Results
{
return (new Finder($this->client, $command))->find();
}
}