Newer
Older
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
namespace IQDEV\ElasticSearch\Indexer;
use Elastic\Elasticsearch\Client;
use Elastic\Elasticsearch\Exception\ClientResponseException;
use Elastic\Elasticsearch\Exception\ContentTypeException;
use Elastic\Elasticsearch\Exception\MissingParameterException;
use Elastic\Elasticsearch\Exception\ServerResponseException;
use Elastic\Elasticsearch\Response\Elasticsearch;
use Elastic\Elasticsearch\Traits\EndpointTrait;
use IQDEV\ElasticSearch\Configuration;
use Psr\Log\LoggerInterface;
final class EsHelperEndpoint
{
use EndpointTrait;
private Client $esClient;
private Configuration $configuration;
private LoggerInterface $logger;
public function __construct(
Client $esClient,
Configuration $configuration,
LoggerInterface $logger
)
{
$this->esClient = $esClient;
$this->configuration = $configuration;
$this->logger = $logger;
}
public function isIndexExists(): bool
{
$response = $this->esClient
->indices()
->exists(
[
'index' => $this->configuration->getIndexName(),
]
);
return $response instanceof Elasticsearch && true === $response->asBool();
}
/**
* Создание индекса
*
* @return void
*
* @throws \Elastic\Elasticsearch\Exception\ClientResponseException
* @throws \Elastic\Elasticsearch\Exception\MissingParameterException
* @throws \Elastic\Elasticsearch\Exception\ServerResponseException
*/
public function create(): void
{
if (false === $this->isIndexExists()) {
$this->logger->info(sprintf('Index %s was created', $this->configuration->getIndexName()));
$this->esClient->indices()->create(
[
'index' => $this->configuration->getIndexName(),
'body' => [
'mappings' => $this->configuration->getMapping(),
'settings' => $this->configuration->getSettings(),
],
]
);
}
}
/**
* Обновление конфигурации индекса
*
* @throws ContentTypeException
*/
public function reconfigurate(): void
{
$this->esClient->sendRequest(
$this->createRequest(
'POST',
'/' . $this->encode($this->configuration->getIndexName()) . '/_close',
[
'Accept' => 'application/json',
'Content-Type' => 'application/json',
],
[],
)
);
$this->esClient->sendRequest(
$this->createRequest(
'PUT',
'/' . $this->encode($this->configuration->getIndexName()) . '/_settings',
[
'Accept' => 'application/json',
'Content-Type' => 'application/json',
],
$this->configuration->getSettings(),
)
);
$this->esClient->sendRequest(
$this->createRequest(
'PUT',
'/' . $this->encode($this->configuration->getIndexName()) . '/_mapping',
[
'Accept' => 'application/json',
'Content-Type' => 'application/json',
],
$this->configuration->getMapping(),
)
);
$this->esClient->sendRequest(
$this->createRequest(
'POST',
'/' . $this->encode($this->configuration->getIndexName()) . '/_open',
[
'Accept' => 'application/json',
'Content-Type' => 'application/json',
],
[],
)
);
$this->logger->info(sprintf('Index %s was reconfigurated', $this->configuration->getIndexName()));
}
/**
* Полная очистка документов индекса
*
* @throws ContentTypeException
*/
public function clear(): void
{
if ($this->isIndexExists()) {
$this->esClient->sendRequest(
$this->createRequest(
'POST',
'/' . $this->encode($this->configuration->getIndexName()) . '/_delete_by_query',
[
'Accept' => 'application/json',
'Content-Type' => 'application/json',
],
'{"query": {"match_all": {}}}'
)
);
$this->logger->info(sprintf('Index %s was cleared', $this->configuration->getIndexName()));
} else {
$this->logger->error(sprintf('Index %s does not exists', $this->configuration->getIndexName()));
}
}
/**
* Удаление индекса
*
* @return void
*
* @throws ClientResponseException
* @throws MissingParameterException
* @throws ServerResponseException
*/
public function delete(): void
{
if ($this->isIndexExists()) {
$response = $this->esClient
->indices()
->delete(
[
'index' => $this->configuration->getIndexName(),
]
);
if (($response instanceof Elasticsearch) && false === $response->asBool()) {
$this->logger->info(sprintf('Index %s was deleted', $this->configuration->getIndexName()));
} else {
$this->logger->error(sprintf('Index %s was not deleted', $this->configuration->getIndexName()));
}
}
}
}