|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AmaTeam\ElasticSearch\Client; |
|
6
|
|
|
|
|
7
|
|
|
use AmaTeam\ElasticSearch\API\Client\EntityClientInterface; |
|
8
|
|
|
use AmaTeam\ElasticSearch\API\Client\MappingClientInterface; |
|
9
|
|
|
use AmaTeam\ElasticSearch\API\Entity\Indexing\IndexingInterface; |
|
10
|
|
|
use AmaTeam\ElasticSearch\API\Entity\ProviderInterface; |
|
11
|
|
|
use AmaTeam\ElasticSearch\API\Mapping\ManagerInterface; |
|
12
|
|
|
use AmaTeam\ElasticSearch\API\Mapping\MappingInterface; |
|
13
|
|
|
use Elasticsearch\Client; |
|
14
|
|
|
|
|
15
|
|
|
class EntityClient implements EntityClientInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var Client |
|
19
|
|
|
*/ |
|
20
|
|
|
private $client; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var ProviderInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
private $registry; |
|
25
|
|
|
/** |
|
26
|
|
|
* @var ManagerInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $mappingManager; |
|
29
|
|
|
/** |
|
30
|
|
|
* @var MappingClientInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $mappingClient; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param Client $client |
|
36
|
|
|
* @param ProviderInterface $registry |
|
37
|
|
|
* @param ManagerInterface $mappingManager |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(Client $client, ProviderInterface $registry, ManagerInterface $mappingManager) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->client = $client; |
|
42
|
|
|
$this->registry = $registry; |
|
43
|
|
|
$this->mappingManager = $mappingManager; |
|
44
|
|
|
$this->mappingClient = new MappingClient($client); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @inheritDoc |
|
49
|
|
|
*/ |
|
50
|
|
|
public function setUp(string $entity): void |
|
51
|
|
|
{ |
|
52
|
|
|
$descriptor = $this->registry->get($entity); |
|
53
|
|
|
$mapping = $this->mappingManager->get($entity); |
|
54
|
|
|
$type = $descriptor->getIndexing()->getType(); |
|
55
|
|
|
foreach ($descriptor->getIndexing()->getWriteIndices() as $index) { |
|
56
|
|
|
$this->trySetupIndex($index, $type, $descriptor->getIndexing(), $mapping); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
private function trySetupIndex( |
|
61
|
|
|
string $index, |
|
62
|
|
|
string $type, |
|
63
|
|
|
IndexingInterface $indexing, |
|
64
|
|
|
MappingInterface $mapping |
|
65
|
|
|
): void { |
|
66
|
|
|
if (!$this->client->indices()->exists(['index' => $index])) { |
|
67
|
|
|
$settings = $indexing->getOptions(); |
|
68
|
|
|
$parameters = ['index' => $index, 'wait_for_active_shards' => 'all', 'body' => ['settings' => $settings]]; |
|
69
|
|
|
$this->client->indices()->create($parameters); |
|
70
|
|
|
} |
|
71
|
|
|
$this->mappingClient->apply($index, $type, $mapping); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|