Skip to content

Commit 1dd6736

Browse files
authored
Merge pull request #15 from olekjs/reindex-and-config-features
Index and Alias feature
2 parents e771ca3 + 9ccc158 commit 1dd6736

14 files changed

+377
-16
lines changed

src/Alias/Alias.php

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
namespace Olekjs\Elasticsearch\Alias;
4+
5+
use Illuminate\Http\Client\Response;
6+
use Olekjs\Elasticsearch\Client;
7+
use Olekjs\Elasticsearch\Contracts\AliasInterface;
8+
use Olekjs\Elasticsearch\Contracts\ClientInterface;
9+
use Olekjs\Elasticsearch\Exceptions\SearchResponseException;
10+
11+
class Alias implements AliasInterface
12+
{
13+
public function __construct(private readonly ClientInterface $client = new Client())
14+
{
15+
}
16+
17+
/**
18+
* @throws SearchResponseException
19+
*/
20+
public function getIndicesForAlias(string $alias): array
21+
{
22+
$response = $this->client->getBaseClient()->get("$alias/_alias");
23+
24+
if ($response->clientError()) {
25+
$this->client->throwSearchResponseException(
26+
data_get($response, 'error.reason'),
27+
$response->status(),
28+
);
29+
}
30+
31+
$indices = [];
32+
foreach ($response->json() as $index => $aliases) {
33+
$indices[] = $index;
34+
}
35+
36+
return $indices;
37+
}
38+
39+
public function add(string $index, string $alias): bool
40+
{
41+
$response = $this->runActions([
42+
[
43+
'add' => [
44+
'index' => $index,
45+
'alias' => $alias,
46+
]
47+
]
48+
]);
49+
50+
return $response->successful();
51+
}
52+
53+
public function remove(string $index, string $alias): bool
54+
{
55+
$response = $this->runActions([
56+
[
57+
'remove' => [
58+
'index' => $index,
59+
'alias' => $alias,
60+
]
61+
]
62+
]);
63+
64+
return $response->successful();
65+
}
66+
67+
public function runActions(array $actions): Response
68+
{
69+
$response = $this->client->getBaseClient()->post('_aliases', ['actions' => $actions]);
70+
71+
if ($response->clientError()) {
72+
$this->client->throwUpdateResponseException(
73+
json_encode($response->json(), JSON_THROW_ON_ERROR),
74+
$response->status()
75+
);
76+
}
77+
78+
return $response;
79+
}
80+
81+
public function replace(string $alias, string $newIndex, ?string $oldIndex = null): bool
82+
{
83+
if (null === $oldIndex) {
84+
$indices = $this->getIndicesForAlias($alias);
85+
86+
$oldIndex = $indices[0] ?? null;
87+
}
88+
89+
if (null === $oldIndex) {
90+
throw new \LogicException('Old index is not defined.');
91+
}
92+
93+
$response = $this->runActions([
94+
[
95+
'add' => [
96+
'index' => $newIndex,
97+
'alias' => $alias,
98+
]
99+
],
100+
[
101+
'remove' => [
102+
'index' => $oldIndex,
103+
'alias' => $alias,
104+
]
105+
]
106+
]);
107+
108+
return $response->successful();
109+
}
110+
}

src/Client.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function create(string $index, string|int $id, array $data): IndexRespons
103103

104104
if ($response->clientError()) {
105105
$this->throwIndexResponseException(
106-
json_encode($response->json()),
106+
json_encode($response->json(), JSON_THROW_ON_ERROR),
107107
$response->status()
108108
);
109109
}
@@ -132,7 +132,7 @@ public function update(
132132
'if_seq_no' => $sequenceNumber,
133133
]);
134134

135-
$baseUrl = $baseUrl . '?' . $strictUrl;
135+
$baseUrl .= '?' . $strictUrl;
136136
}
137137

138138
$body = match (true) {
@@ -145,7 +145,7 @@ public function update(
145145

146146
if ($response->notFound() && data_get($response, 'status') === SymfonyResponse::HTTP_NOT_FOUND) {
147147
$this->throwNotFoundException(
148-
json_encode($response->json())
148+
json_encode($response->json(), JSON_THROW_ON_ERROR)
149149
);
150150
}
151151

@@ -156,13 +156,13 @@ public function update(
156156
&& data_get($response, 'status') === SymfonyResponse::HTTP_CONFLICT
157157
) {
158158
$this->throwConflictResponseException(
159-
json_encode($response->json())
159+
json_encode($response->json(), JSON_THROW_ON_ERROR)
160160
);
161161
}
162162

163163
if ($response->clientError()) {
164164
$this->throwUpdateResponseException(
165-
json_encode($response->json()),
165+
json_encode($response->json(), JSON_THROW_ON_ERROR),
166166
$response->status()
167167
);
168168
}
@@ -181,13 +181,13 @@ public function delete(string $index, string|int $id): IndexResponseDto
181181

182182
if ($response->notFound() && data_get($response, 'result') === 'not_found') {
183183
$this->throwNotFoundException(
184-
json_encode($response->json())
184+
json_encode($response->json(), JSON_THROW_ON_ERROR)
185185
);
186186
}
187187

188188
if ($response->clientError()) {
189189
$this->throwDeleteResponseException(
190-
json_encode($response->json()),
190+
json_encode($response->json(), JSON_THROW_ON_ERROR),
191191
$response->status()
192192
);
193193
}

src/Contracts/AbstractClient.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
abstract class AbstractClient
1919
{
20-
protected function getBaseClient(): PendingRequest
20+
public function getBaseClient(): PendingRequest
2121
{
2222
$apiKey = config('services.elasticsearch.api_key');
2323
$port = config('services.elasticsearch.port');
@@ -39,7 +39,7 @@ protected function getBaseClient(): PendingRequest
3939
/**
4040
* @throws NotFoundResponseException
4141
*/
42-
protected function throwNotFoundException(string $message, int $code = Response::HTTP_NOT_FOUND): void
42+
public function throwNotFoundException(string $message, int $code = Response::HTTP_NOT_FOUND): void
4343
{
4444
throw new NotFoundResponseException(
4545
$message,
@@ -50,7 +50,7 @@ protected function throwNotFoundException(string $message, int $code = Response:
5050
/**
5151
* @throws IndexNotFoundResponseException
5252
*/
53-
protected function throwIndexNotFoundException(string $message, int $code = Response::HTTP_NOT_FOUND): void
53+
public function throwIndexNotFoundException(string $message, int $code = Response::HTTP_NOT_FOUND): void
5454
{
5555
throw new IndexNotFoundResponseException(
5656
$message,
@@ -61,7 +61,7 @@ protected function throwIndexNotFoundException(string $message, int $code = Resp
6161
/**
6262
* @throws SearchResponseException
6363
*/
64-
protected function throwSearchResponseException(string $message, int $code = Response::HTTP_BAD_REQUEST): void
64+
public function throwSearchResponseException(string $message, int $code = Response::HTTP_BAD_REQUEST): void
6565
{
6666
throw new SearchResponseException(
6767
$message,
@@ -72,7 +72,7 @@ protected function throwSearchResponseException(string $message, int $code = Res
7272
/**
7373
* @throws IndexResponseException
7474
*/
75-
protected function throwIndexResponseException(string $message, int $code = Response::HTTP_BAD_REQUEST): void
75+
public function throwIndexResponseException(string $message, int $code = Response::HTTP_BAD_REQUEST): void
7676
{
7777
throw new IndexResponseException(
7878
$message,
@@ -83,7 +83,7 @@ protected function throwIndexResponseException(string $message, int $code = Resp
8383
/**
8484
* @throws DeleteResponseException
8585
*/
86-
protected function throwDeleteResponseException(string $message, int $code = Response::HTTP_BAD_REQUEST): void
86+
public function throwDeleteResponseException(string $message, int $code = Response::HTTP_BAD_REQUEST): void
8787
{
8888
throw new DeleteResponseException(
8989
$message,
@@ -94,7 +94,7 @@ protected function throwDeleteResponseException(string $message, int $code = Res
9494
/**
9595
* @throws UpdateResponseException
9696
*/
97-
protected function throwUpdateResponseException(string $message, int $code = Response::HTTP_BAD_REQUEST): void
97+
public function throwUpdateResponseException(string $message, int $code = Response::HTTP_BAD_REQUEST): void
9898
{
9999
throw new UpdateResponseException(
100100
$message,
@@ -105,7 +105,7 @@ protected function throwUpdateResponseException(string $message, int $code = Res
105105
/**
106106
* @throws ConflictResponseException
107107
*/
108-
protected function throwConflictResponseException(string $message, int $code = Response::HTTP_CONFLICT): void
108+
public function throwConflictResponseException(string $message, int $code = Response::HTTP_CONFLICT): void
109109
{
110110
throw new ConflictResponseException(
111111
$message,

src/Contracts/AliasInterface.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Olekjs\Elasticsearch\Contracts;
4+
5+
use Illuminate\Http\Client\Response;
6+
7+
interface AliasInterface
8+
{
9+
/**
10+
* @return array<int, string>
11+
*/
12+
public function getIndicesForAlias(string $alias): array;
13+
14+
public function add(string $index, string $alias): bool;
15+
16+
public function remove(string $index, string $alias): bool;
17+
18+
public function runActions(array $actions): Response;
19+
20+
public function replace(string $alias, string $newIndex, ?string $oldIndex = null): bool;
21+
}

src/Contracts/ClientInterface.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Olekjs\Elasticsearch\Contracts;
44

5+
use Illuminate\Http\Client\PendingRequest;
56
use Olekjs\Elasticsearch\Dto\BulkResponseDto;
67
use Olekjs\Elasticsearch\Dto\FindResponseDto;
78
use Olekjs\Elasticsearch\Dto\IndexResponseDto;
@@ -16,6 +17,7 @@
1617
use Olekjs\Elasticsearch\Exceptions\NotFoundResponseException;
1718
use Olekjs\Elasticsearch\Exceptions\SearchResponseException;
1819
use Olekjs\Elasticsearch\Exceptions\UpdateResponseException;
20+
use Symfony\Component\HttpFoundation\Response;
1921

2022
interface ClientInterface
2123
{
@@ -108,4 +110,41 @@ public function paginate(string $index, array $data = [], int $page = 1, int $pe
108110
* @throws CoreException
109111
*/
110112
public function bulk(BulkOperationInterface $bulk): BulkResponseDto;
113+
114+
public function getBaseClient(): PendingRequest;
115+
116+
/**
117+
* @throws NotFoundResponseException
118+
*/
119+
public function throwNotFoundException(string $message, int $code = Response::HTTP_NOT_FOUND): void;
120+
121+
/**
122+
* @throws IndexNotFoundResponseException
123+
*/
124+
public function throwIndexNotFoundException(string $message, int $code = Response::HTTP_NOT_FOUND): void;
125+
126+
/**
127+
* @throws SearchResponseException
128+
*/
129+
public function throwSearchResponseException(string $message, int $code = Response::HTTP_BAD_REQUEST): void;
130+
131+
/**
132+
* @throws IndexResponseException
133+
*/
134+
public function throwIndexResponseException(string $message, int $code = Response::HTTP_BAD_REQUEST): void;
135+
136+
/**
137+
* @throws DeleteResponseException
138+
*/
139+
public function throwDeleteResponseException(string $message, int $code = Response::HTTP_BAD_REQUEST): void;
140+
141+
/**
142+
* @throws UpdateResponseException
143+
*/
144+
public function throwUpdateResponseException(string $message, int $code = Response::HTTP_BAD_REQUEST): void;
145+
146+
/**
147+
* @throws ConflictResponseException
148+
*/
149+
public function throwConflictResponseException(string $message, int $code = Response::HTTP_CONFLICT): void;
111150
}

src/Contracts/IndexInterface.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Olekjs\Elasticsearch\Contracts;
4+
5+
interface IndexInterface
6+
{
7+
public function create(string $name): bool;
8+
9+
public function delete(string $name): bool;
10+
}

src/Index/Index.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Olekjs\Elasticsearch\Index;
4+
5+
use Olekjs\Elasticsearch\Client;
6+
use Olekjs\Elasticsearch\Contracts\ClientInterface;
7+
use Olekjs\Elasticsearch\Contracts\IndexInterface;
8+
use Olekjs\Elasticsearch\Exceptions\UpdateResponseException;
9+
10+
class Index implements IndexInterface
11+
{
12+
public function __construct(private readonly ClientInterface $client = new Client())
13+
{
14+
}
15+
16+
/**
17+
* @throws UpdateResponseException
18+
* @throws \JsonException
19+
*/
20+
public function create(string $name, array $settings = []): bool
21+
{
22+
$response = $this->client->getBaseClient()->put($name, (object) $settings);
23+
24+
if ($response->clientError()) {
25+
$this->client->throwUpdateResponseException(
26+
json_encode($response->json(), JSON_THROW_ON_ERROR),
27+
$response->status()
28+
);
29+
}
30+
31+
return $response->successful();
32+
}
33+
34+
public function delete(string $name): bool
35+
{
36+
$response = $this->client->getBaseClient()->delete($name);
37+
38+
if ($response->clientError()) {
39+
$this->client->throwDeleteResponseException(
40+
json_encode($response->json(), JSON_THROW_ON_ERROR),
41+
$response->status()
42+
);
43+
}
44+
45+
return $response->successful();
46+
}
47+
}

0 commit comments

Comments
 (0)