-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from olekjs/reindex-and-config-features
Index and Alias feature
- Loading branch information
Showing
14 changed files
with
377 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
|
||
namespace Olekjs\Elasticsearch\Alias; | ||
|
||
use Illuminate\Http\Client\Response; | ||
use Olekjs\Elasticsearch\Client; | ||
use Olekjs\Elasticsearch\Contracts\AliasInterface; | ||
use Olekjs\Elasticsearch\Contracts\ClientInterface; | ||
use Olekjs\Elasticsearch\Exceptions\SearchResponseException; | ||
|
||
class Alias implements AliasInterface | ||
{ | ||
public function __construct(private readonly ClientInterface $client = new Client()) | ||
{ | ||
} | ||
|
||
/** | ||
* @throws SearchResponseException | ||
*/ | ||
public function getIndicesForAlias(string $alias): array | ||
{ | ||
$response = $this->client->getBaseClient()->get("$alias/_alias"); | ||
|
||
if ($response->clientError()) { | ||
$this->client->throwSearchResponseException( | ||
data_get($response, 'error.reason'), | ||
$response->status(), | ||
); | ||
} | ||
|
||
$indices = []; | ||
foreach ($response->json() as $index => $aliases) { | ||
$indices[] = $index; | ||
} | ||
|
||
return $indices; | ||
} | ||
|
||
public function add(string $index, string $alias): bool | ||
{ | ||
$response = $this->runActions([ | ||
[ | ||
'add' => [ | ||
'index' => $index, | ||
'alias' => $alias, | ||
] | ||
] | ||
]); | ||
|
||
return $response->successful(); | ||
} | ||
|
||
public function remove(string $index, string $alias): bool | ||
{ | ||
$response = $this->runActions([ | ||
[ | ||
'remove' => [ | ||
'index' => $index, | ||
'alias' => $alias, | ||
] | ||
] | ||
]); | ||
|
||
return $response->successful(); | ||
} | ||
|
||
public function runActions(array $actions): Response | ||
{ | ||
$response = $this->client->getBaseClient()->post('_aliases', ['actions' => $actions]); | ||
|
||
if ($response->clientError()) { | ||
$this->client->throwUpdateResponseException( | ||
json_encode($response->json(), JSON_THROW_ON_ERROR), | ||
$response->status() | ||
); | ||
} | ||
|
||
return $response; | ||
} | ||
|
||
public function replace(string $alias, string $newIndex, ?string $oldIndex = null): bool | ||
{ | ||
if (null === $oldIndex) { | ||
$indices = $this->getIndicesForAlias($alias); | ||
|
||
$oldIndex = $indices[0] ?? null; | ||
} | ||
|
||
if (null === $oldIndex) { | ||
throw new \LogicException('Old index is not defined.'); | ||
} | ||
|
||
$response = $this->runActions([ | ||
[ | ||
'add' => [ | ||
'index' => $newIndex, | ||
'alias' => $alias, | ||
] | ||
], | ||
[ | ||
'remove' => [ | ||
'index' => $oldIndex, | ||
'alias' => $alias, | ||
] | ||
] | ||
]); | ||
|
||
return $response->successful(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Olekjs\Elasticsearch\Contracts; | ||
|
||
use Illuminate\Http\Client\Response; | ||
|
||
interface AliasInterface | ||
{ | ||
/** | ||
* @return array<int, string> | ||
*/ | ||
public function getIndicesForAlias(string $alias): array; | ||
|
||
public function add(string $index, string $alias): bool; | ||
|
||
public function remove(string $index, string $alias): bool; | ||
|
||
public function runActions(array $actions): Response; | ||
|
||
public function replace(string $alias, string $newIndex, ?string $oldIndex = null): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Olekjs\Elasticsearch\Contracts; | ||
|
||
interface IndexInterface | ||
{ | ||
public function create(string $name): bool; | ||
|
||
public function delete(string $name): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace Olekjs\Elasticsearch\Index; | ||
|
||
use Olekjs\Elasticsearch\Client; | ||
use Olekjs\Elasticsearch\Contracts\ClientInterface; | ||
use Olekjs\Elasticsearch\Contracts\IndexInterface; | ||
use Olekjs\Elasticsearch\Exceptions\UpdateResponseException; | ||
|
||
class Index implements IndexInterface | ||
{ | ||
public function __construct(private readonly ClientInterface $client = new Client()) | ||
{ | ||
} | ||
|
||
/** | ||
* @throws UpdateResponseException | ||
* @throws \JsonException | ||
*/ | ||
public function create(string $name, array $settings = []): bool | ||
{ | ||
$response = $this->client->getBaseClient()->put($name, (object) $settings); | ||
|
||
if ($response->clientError()) { | ||
$this->client->throwUpdateResponseException( | ||
json_encode($response->json(), JSON_THROW_ON_ERROR), | ||
$response->status() | ||
); | ||
} | ||
|
||
return $response->successful(); | ||
} | ||
|
||
public function delete(string $name): bool | ||
{ | ||
$response = $this->client->getBaseClient()->delete($name); | ||
|
||
if ($response->clientError()) { | ||
$this->client->throwDeleteResponseException( | ||
json_encode($response->json(), JSON_THROW_ON_ERROR), | ||
$response->status() | ||
); | ||
} | ||
|
||
return $response->successful(); | ||
} | ||
} |
Oops, something went wrong.