Newer
Older
<?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;
public function __construct(
private Client $esClient,
private Configuration $configuration,
private LoggerInterface $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' => [
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
'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',
],
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
)
);
$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()));
}
}
}
}