Skip to content

Commit e8b2fcb

Browse files
committed
Elasticsearch 8.0
1 parent 85b43b8 commit e8b2fcb

File tree

8 files changed

+96
-73
lines changed

8 files changed

+96
-73
lines changed

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
],
1818
"require": {
1919
"php": "^8.2",
20-
"laravel/framework": "^11.0",
21-
"ruflin/elastica": "^7.1 || 8.x-dev",
20+
"laravel/framework": "^11.0 || ^12.0",
21+
"ruflin/elastica": "^8.0",
2222
"spatie/laravel-package-tools": "^1.13.0"
2323
},
2424
"require-dev": {
@@ -35,7 +35,7 @@
3535
"psr/log": "^3.0.2",
3636
"rector/rector": "^2.0.9",
3737
"sentry/sentry": "^4.10.0",
38-
"symfony/http-client": "^6.4.19"
38+
"symfony/http-client": "^7.2.4"
3939
},
4040
"autoload": {
4141
"psr-4": {

phpstan-baseline.neon

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,20 @@ parameters:
3131
path: src/Index/AbstractIndex.php
3232

3333
-
34-
message: '#^Casting to string something that''s already string\.$#'
35-
identifier: cast.useless
34+
message: '#^Method Limenet\\LaravelElasticaBridge\\Index\\AbstractIndex\:\:getMapping\(\) return type has no value type specified in iterable type array\.$#'
35+
identifier: missingType.iterableValue
3636
count: 1
3737
path: src/Index/AbstractIndex.php
3838

3939
-
40-
message: '#^Method Limenet\\LaravelElasticaBridge\\Index\\AbstractIndex\:\:getMapping\(\) return type has no value type specified in iterable type array\.$#'
40+
message: '#^Method Limenet\\LaravelElasticaBridge\\Index\\AbstractIndex\:\:getSettings\(\) return type has no value type specified in iterable type array\.$#'
4141
identifier: missingType.iterableValue
4242
count: 1
4343
path: src/Index/AbstractIndex.php
4444

4545
-
46-
message: '#^Method Limenet\\LaravelElasticaBridge\\Index\\AbstractIndex\:\:getSettings\(\) return type has no value type specified in iterable type array\.$#'
47-
identifier: missingType.iterableValue
46+
message: '#^Parameter \#1 \$params of method Elastic\\Elasticsearch\\Endpoints\\Indices\:\:getAlias\(\) expects array\{name\: list, index\: list, ignore_unavailable\: bool, allow_no_indices\: bool, expand_wildcards\: Elastic\\Elasticsearch\\Endpoints\\enum, local\: bool, pretty\: bool, human\: bool, \.\.\.\}, array\{name\: string\} given\.$#'
47+
identifier: argument.type
4848
count: 1
4949
path: src/Index/AbstractIndex.php
5050

@@ -60,6 +60,12 @@ parameters:
6060
count: 1
6161
path: src/Index/IndexInterface.php
6262

63+
-
64+
message: '#^Parameter \#1 \$params of method Elastic\\Elasticsearch\\Endpoints\\Indices\:\:existsAlias\(\) expects array\{name\: list, index\: list, ignore_unavailable\: bool, allow_no_indices\: bool, expand_wildcards\: Elastic\\Elasticsearch\\Endpoints\\enum, local\: bool, pretty\: bool, human\: bool, \.\.\.\}, array\{name\: string\} given\.$#'
65+
identifier: argument.type
66+
count: 1
67+
path: src/Jobs/SetupIndex.php
68+
6369
-
6470
message: '#^Cannot access offset ''events'' on Illuminate\\Contracts\\Foundation\\Application\.$#'
6571
identifier: offsetAccess.nonOffsetAccessible

src/Client/ElasticaClient.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,18 @@ class ElasticaClient
1616

1717
public function __construct()
1818
{
19-
$client = new Client([
20-
'host' => config('elastica-bridge.elasticsearch.host'),
21-
'port' => config('elastica-bridge.elasticsearch.port'),
22-
]);
19+
$logger = null;
2320

2421
if (config('elastica-bridge.logging.sentry_breadcrumbs') === true && class_exists('\Sentry\Breadcrumb')) {
25-
$client->setLogger(new SentryBreadcrumbLogger);
22+
$logger = (new SentryBreadcrumbLogger);
2623
}
2724

25+
$client = new Client(
26+
[
27+
'host' => config('elastica-bridge.elasticsearch.host'),
28+
'port' => config('elastica-bridge.elasticsearch.port'),
29+
], logger: $logger);
30+
2831
$this->client = $client;
2932
}
3033

src/Enum/IndexBlueGreenSuffix.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Limenet\LaravelElasticaBridge\Enum;
6+
7+
enum IndexBlueGreenSuffix: string
8+
{
9+
case BLUE = '--blue';
10+
11+
case GREEN = '--green';
12+
}

src/Index/AbstractIndex.php

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Limenet\LaravelElasticaBridge\Index;
66

7+
use Elastic\Elasticsearch\Exception\ClientResponseException;
78
use Elastica\Document;
89
use Elastica\Exception\InvalidException;
910
use Elastica\Exception\NotFoundException;
@@ -14,8 +15,10 @@
1415
use Illuminate\Database\Eloquent\Model;
1516
use Illuminate\Support\Facades\Cache;
1617
use Limenet\LaravelElasticaBridge\Client\ElasticaClient;
18+
use Limenet\LaravelElasticaBridge\Enum\IndexBlueGreenSuffix;
1719
use Limenet\LaravelElasticaBridge\Exception\Index\BlueGreenIndicesIncorrectlySetupException;
1820
use Limenet\LaravelElasticaBridge\Model\ElasticsearchableInterface;
21+
use Limenet\LaravelElasticaBridge\Util\ElasticsearchResponse;
1922
use RuntimeException;
2023

2124
abstract class AbstractIndex implements IndexInterface
@@ -114,61 +117,54 @@ final public function hasBlueGreenIndices(): bool
114117
{
115118
return array_reduce(
116119
array_map(
117-
fn (string $suffix): bool => $this->client->getIndex($this->getName().$suffix)->exists(),
118-
self::INDEX_SUFFIXES
120+
fn (IndexBlueGreenSuffix $suffix): bool => $this->client->getIndex($this->getName().$suffix->value)->exists(),
121+
IndexBlueGreenSuffix::cases()
119122
),
120123
fn (bool $carry, bool $item): bool => $item && $carry,
121124
true
122125
);
123126
}
124127

125-
final public function getBlueGreenActiveSuffix(): string
128+
final public function getBlueGreenActiveSuffix(): IndexBlueGreenSuffix
126129
{
127130
if (! $this->hasBlueGreenIndices()) {
128131
throw new BlueGreenIndicesIncorrectlySetupException;
129132
}
130133

131-
$aliases = array_filter(
132-
$this->client->getClient()->request('_aliases')->getData(),
133-
fn (array $datum): bool => in_array($this->getName(), array_keys($datum['aliases']), true)
134-
);
135-
136-
if (count($aliases) !== 1) {
134+
try {
135+
$aliases = array_filter(
136+
ElasticsearchResponse::getResponse($this->getElasticaIndex()->getClient()->indices()->getAlias(['name' => $this->getName()]))->asArray(),
137+
fn (array $datum): bool => array_key_exists($this->getName(), $datum['aliases'])
138+
);
139+
} catch (ClientResponseException) {
137140
throw new BlueGreenIndicesIncorrectlySetupException;
138141
}
139142

140-
$suffix = substr((string) array_keys($aliases)[0], strlen($this->getName()));
141-
142-
if (! in_array($suffix, self::INDEX_SUFFIXES, true)) {
143+
if (count($aliases) !== 1) {
143144
throw new BlueGreenIndicesIncorrectlySetupException;
144145
}
145146

146-
return $suffix;
147+
$suffix = substr(array_keys($aliases)[0], strlen($this->getName()));
148+
149+
return IndexBlueGreenSuffix::tryFrom($suffix) ?? throw new BlueGreenIndicesIncorrectlySetupException;
147150
}
148151

149-
final public function getBlueGreenInactiveSuffix(): string
152+
final public function getBlueGreenInactiveSuffix(): IndexBlueGreenSuffix
150153
{
151-
$active = $this->getBlueGreenActiveSuffix();
152-
153-
if ($active === self::INDEX_SUFFIX_BLUE) {
154-
return self::INDEX_SUFFIX_GREEN;
155-
}
156-
157-
if ($active === self::INDEX_SUFFIX_GREEN) {
158-
return self::INDEX_SUFFIX_BLUE;
159-
}
160-
161-
throw new BlueGreenIndicesIncorrectlySetupException;
154+
return match ($this->getBlueGreenActiveSuffix()) {
155+
IndexBlueGreenSuffix::BLUE => IndexBlueGreenSuffix::GREEN,
156+
IndexBlueGreenSuffix::GREEN => IndexBlueGreenSuffix::BLUE,
157+
};
162158
}
163159

164160
final public function getBlueGreenActiveElasticaIndex(): Index
165161
{
166-
return $this->client->getIndex($this->getName().$this->getBlueGreenActiveSuffix());
162+
return $this->client->getIndex($this->getName().$this->getBlueGreenActiveSuffix()->value);
167163
}
168164

169165
final public function getBlueGreenInactiveElasticaIndex(): Index
170166
{
171-
return $this->client->getIndex($this->getName().$this->getBlueGreenInactiveSuffix());
167+
return $this->client->getIndex($this->getName().$this->getBlueGreenInactiveSuffix()->value);
172168
}
173169

174170
final public function indexingLock(): Lock

src/Index/IndexInterface.php

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,12 @@
88
use Elastica\Index;
99
use Illuminate\Contracts\Cache\Lock;
1010
use Illuminate\Database\Eloquent\Model;
11+
use Limenet\LaravelElasticaBridge\Enum\IndexBlueGreenSuffix;
1112
use Limenet\LaravelElasticaBridge\Exception\Index\BlueGreenIndicesIncorrectlySetupException;
1213
use Limenet\LaravelElasticaBridge\Model\ElasticsearchableInterface;
1314

1415
interface IndexInterface
1516
{
16-
/**
17-
* The suffix for the blue index.
18-
*/
19-
public const INDEX_SUFFIX_BLUE = '--blue';
20-
21-
/**
22-
* The suffix for the green index.
23-
*/
24-
public const INDEX_SUFFIX_GREEN = '--green';
25-
26-
/**
27-
* List of valid index suffixes.
28-
*/
29-
public const INDEX_SUFFIXES = [self::INDEX_SUFFIX_BLUE, self::INDEX_SUFFIX_GREEN];
30-
3117
public const DOCUMENT_MODEL_ID = '__id';
3218

3319
public const DOCUMENT_MODEL_CLASS = '__class';
@@ -94,7 +80,7 @@ public function hasBlueGreenIndices(): bool;
9480
*
9581
* @internal
9682
*/
97-
public function getBlueGreenActiveSuffix(): string;
83+
public function getBlueGreenActiveSuffix(): IndexBlueGreenSuffix;
9884

9985
/**
10086
* Returns the currently inactive blue/green suffix.
@@ -103,7 +89,7 @@ public function getBlueGreenActiveSuffix(): string;
10389
*
10490
* @internal
10591
*/
106-
public function getBlueGreenInactiveSuffix(): string;
92+
public function getBlueGreenInactiveSuffix(): IndexBlueGreenSuffix;
10793

10894
/**
10995
* Returns the currently active blue/green Elastica index.

src/Jobs/SetupIndex.php

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
namespace Limenet\LaravelElasticaBridge\Jobs;
66

7-
use Elastica\Exception\ClientException;
8-
use Elastica\Exception\ConnectionException;
9-
use Elastica\Exception\ResponseException;
7+
use Elastica\Exception\ExceptionInterface as ElasticaException;
108
use Illuminate\Bus\Batchable;
119
use Limenet\LaravelElasticaBridge\Client\ElasticaClient;
10+
use Limenet\LaravelElasticaBridge\Enum\IndexBlueGreenSuffix;
1211
use Limenet\LaravelElasticaBridge\Exception\Index\BlueGreenIndicesIncorrectlySetupException;
1312
use Limenet\LaravelElasticaBridge\Index\IndexInterface;
13+
use Limenet\LaravelElasticaBridge\Util\ElasticsearchResponse;
1414

1515
class SetupIndex extends AbstractIndexJob
1616
{
@@ -40,32 +40,24 @@ private function migrate(ElasticaClient $elastica): void
4040
$index = $elastica->getClient()->getIndex($this->indexConfig->getName());
4141

4242
try {
43-
$response = $elastica->getClient()->request(sprintf('_alias/%s', $this->indexConfig->getName()));
44-
} catch (ClientException|ConnectionException|ResponseException) {
43+
$response = ElasticsearchResponse::getResponse($elastica->getClient()->indices()->existsAlias(['name' => $this->indexConfig->getName()]))->asBool();
44+
} catch (ElasticaException) {
4545
if ($index->exists() && count($index->getAliases()) === 0) {
4646
$index->delete();
4747
}
4848

4949
return;
5050
}
5151

52-
if ($response->hasError()) {
53-
return;
54-
}
55-
56-
if (array_keys($response->getData())[0] !== $this->indexConfig->getName()) {
57-
return;
58-
}
59-
6052
if ($index->exists()) {
6153
$index->delete();
6254
}
6355
}
6456

6557
private function cleanup(ElasticaClient $elastica): void
6658
{
67-
foreach (IndexInterface::INDEX_SUFFIXES as $suffix) {
68-
$name = $this->indexConfig->getName().$suffix;
59+
foreach (IndexBlueGreenSuffix::cases() as $suffix) {
60+
$name = $this->indexConfig->getName().$suffix->value;
6961
$aliasIndex = $elastica->getIndex($name);
7062

7163
if ($this->deleteExisting && $aliasIndex->exists()) {
@@ -83,7 +75,7 @@ private function setup(ElasticaClient $elastica): void
8375
try {
8476
$this->indexConfig->getBlueGreenActiveSuffix();
8577
} catch (BlueGreenIndicesIncorrectlySetupException) {
86-
$elastica->getIndex($this->indexConfig->getName().IndexInterface::INDEX_SUFFIX_BLUE)->addAlias($this->indexConfig->getName());
78+
$elastica->getIndex($this->indexConfig->getName().IndexBlueGreenSuffix::BLUE->value)->addAlias($this->indexConfig->getName());
8779
}
8880
}
8981
}

src/Util/ElasticsearchResponse.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Limenet\LaravelElasticaBridge\Util;
6+
7+
use Elastic\Elasticsearch\Response\Elasticsearch;
8+
use Http\Promise\Promise;
9+
10+
class ElasticsearchResponse
11+
{
12+
/**
13+
* This method checks if the response object is an instance of Elasticsearch.
14+
* If it's not an instance of Elasticsearch, it throws a \RuntimeException.
15+
* If it's an instance of Elasticsearch, it returns the response directly.
16+
* This is mainly done for the benefit of PHPStan.
17+
*
18+
* @throws \RuntimeException if the response is not an instance of Elasticsearch
19+
*/
20+
public static function getResponse(Elasticsearch|Promise $response): Elasticsearch
21+
{
22+
if (! $response instanceof Elasticsearch) {
23+
throw new \RuntimeException;
24+
}
25+
26+
return $response;
27+
}
28+
}

0 commit comments

Comments
 (0)