1 | <?php |
||
2 | namespace Triadev\Es; |
||
3 | |||
4 | use Elasticsearch\Client; |
||
5 | use Elasticsearch\ClientBuilder; |
||
6 | use Triadev\Es\Contract\ElasticsearchContract; |
||
7 | |||
8 | class Elasticsearch implements ElasticsearchContract |
||
9 | { |
||
10 | /** @var Client */ |
||
11 | private $client; |
||
12 | |||
13 | /** |
||
14 | * Get client |
||
15 | * |
||
16 | * @return Client |
||
17 | */ |
||
18 | public function getClient(): Client |
||
19 | { |
||
20 | if (!$this->client) { |
||
21 | $this->client = $this->buildElasticsearchClient(); |
||
22 | } |
||
23 | |||
24 | return $this->client; |
||
25 | } |
||
26 | |||
27 | private function buildElasticsearchClient() : Client |
||
28 | { |
||
29 | $config = config('triadev-elasticsearch'); |
||
30 | |||
31 | $clientBuilder = ClientBuilder::create(); |
||
32 | |||
33 | $clientBuilder->setHosts(explode('|', $config, 'hosts')); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
34 | $clientBuilder->setRetries(array_get($config, 'retries')); |
||
35 | |||
36 | if ($logger = array_get($config, 'logger')) { |
||
37 | $clientBuilder->setLogger($logger); |
||
38 | } |
||
39 | |||
40 | if ($connectionPool = array_get($config, 'connection.pool')) { |
||
41 | $clientBuilder->setConnectionPool($connectionPool); |
||
42 | } |
||
43 | |||
44 | if ($connectionSelector = array_get($config, 'connection.selector')) { |
||
45 | $clientBuilder->setSelector($connectionSelector); |
||
46 | } |
||
47 | |||
48 | if ($serializer = array_get($config, 'serializer')) { |
||
49 | $clientBuilder->setSerializer($serializer); |
||
50 | } |
||
51 | |||
52 | return $clientBuilder->build(); |
||
53 | } |
||
54 | } |
||
55 |