Skip to content

Commit

Permalink
Add elastic8 support
Browse files Browse the repository at this point in the history
  • Loading branch information
blackshadev committed Dec 20, 2024
1 parent 250ef43 commit 7583f71
Show file tree
Hide file tree
Showing 20 changed files with 268 additions and 140 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@ do-test-report:

do-phpstan:
@echo "\n=== Running PHPStan ===\n"
vendor/bin/phpstan analyse
vendor/bin/phpstan --memory-limit=1G analyse
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"homepage": "https://jeroen-g.github.io/Explorer/",
"keywords": ["Laravel", "Scout", "Elasticsearch", "Elastic", "search", "Explorer"],
"require": {
"php": "8.0.*||8.1.*||8.2.*||8.3.*",
"elasticsearch/elasticsearch": "^7.16",
"php": "^8.0",
"elasticsearch/elasticsearch": "^8.0",
"illuminate/support": "^9.0||^10.0||^11.0",
"laravel/scout": "^9.0||^10.0||^11.0",
"webmozart/assert": "^1.10",
Expand All @@ -26,7 +26,8 @@
"symplify/easy-coding-standard": "^9.0",
"nunomaduro/larastan": "^2.5",
"phpstan/phpstan-mockery": "^1.1",
"orchestra/testbench": "*"
"orchestra/testbench": "*",
"dg/bypass-finals": "^1.8"
},
"autoload": {
"psr-4": {
Expand Down
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ parameters:
count: 1
path: src/Infrastructure/Console/ElasticSearch.php

-
message: "#^Call to an undefined method JeroenG\\\\Explorer\\\\Domain\\\\IndexManagement\\\\IndexConfigurationInterface\\:\\:getAliasConfiguration\\(\\)\\.$#"
count: 1
path: src/Infrastructure/Elastic/ElasticIndexAdapter.php

-
message: "#^Call to an undefined method JeroenG\\\\Explorer\\\\Domain\\\\IndexManagement\\\\IndexConfigurationInterface\\:\\:getAliasConfiguration\\(\\)\\.$#"
count: 2
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" backupStaticAttributes="false" colors="true" verbose="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="tests/bootstrap.php" backupGlobals="false" backupStaticAttributes="false" colors="true" verbose="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory>src/</directory>
Expand Down
3 changes: 2 additions & 1 deletion src/Application/IndexAdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace JeroenG\Explorer\Application;

use JeroenG\Explorer\Domain\IndexManagement\AliasedIndexConfigurationInterface;
use JeroenG\Explorer\Domain\IndexManagement\IndexConfigurationInterface;

interface IndexAdapterInterface
Expand All @@ -16,7 +17,7 @@ public function delete(IndexConfigurationInterface $indexConfiguration): void;

public function flush(string $index): void;

public function createNewWriteIndex(IndexConfigurationInterface $indexConfiguration): string;
public function createNewWriteIndex(AliasedIndexConfigurationInterface $indexConfiguration): string;

public function ensureIndex(IndexConfigurationInterface $indexConfiguration): void;
}
2 changes: 1 addition & 1 deletion src/Domain/IndexManagement/AliasedIndexConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace JeroenG\Explorer\Domain\IndexManagement;

final class AliasedIndexConfiguration implements IndexConfigurationInterface
final class AliasedIndexConfiguration implements AliasedIndexConfigurationInterface
{
private function __construct(
private string $name,
Expand Down
10 changes: 10 additions & 0 deletions src/Domain/IndexManagement/AliasedIndexConfigurationInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace JeroenG\Explorer\Domain\IndexManagement;

interface AliasedIndexConfigurationInterface extends IndexConfigurationInterface
{
public function getAliasConfiguration(): IndexAliasConfigurationInterface;
}
4 changes: 1 addition & 3 deletions src/ExplorerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@

namespace JeroenG\Explorer;

use Elasticsearch\Client;
use Elastic\Elasticsearch\Client;
use Illuminate\Foundation\Application;
use Illuminate\Support\ServiceProvider;
use JeroenG\Explorer\Application\DocumentAdapterInterface;
use JeroenG\Explorer\Application\IndexAdapterInterface;
use JeroenG\Explorer\Domain\Aggregations\AggregationSyntaxInterface;
use JeroenG\Explorer\Domain\IndexManagement\IndexConfigurationRepositoryInterface;
use JeroenG\Explorer\Infrastructure\Console\ElasticSearch;
use JeroenG\Explorer\Infrastructure\Console\ElasticUpdate;
use JeroenG\Explorer\Infrastructure\Elastic\ElasticClientFactory;
use JeroenG\Explorer\Infrastructure\Elastic\ElasticClientBuilder;
use JeroenG\Explorer\Infrastructure\Elastic\ElasticDocumentAdapter;
use JeroenG\Explorer\Infrastructure\Elastic\ElasticIndexAdapter;
use JeroenG\Explorer\Infrastructure\IndexManagement\ElasticIndexChangedChecker;
use JeroenG\Explorer\Infrastructure\IndexManagement\ElasticIndexConfigurationRepository;
use JeroenG\Explorer\Infrastructure\Scout\Builder;
use JeroenG\Explorer\Infrastructure\Scout\ElasticEngine;
Expand Down
23 changes: 20 additions & 3 deletions src/Infrastructure/Elastic/ElasticClientBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@

namespace JeroenG\Explorer\Infrastructure\Elastic;

use Elasticsearch\ClientBuilder;
use Elastic\Elasticsearch\ClientBuilder;
use Elastic\Transport\NodePool\Resurrect\ElasticsearchResurrect;
use Elastic\Transport\NodePool\Selector\SelectorInterface;
use Elastic\Transport\NodePool\SimpleNodePool;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Support\Facades\Log;
use InvalidArgumentException;

final class ElasticClientBuilder
{
Expand All @@ -22,13 +26,26 @@ public static function fromConfig(Repository $config): ClientBuilder
ARRAY_FILTER_USE_KEY
);

$builder->setHosts([$hostConnectionProperties]);
if (!empty($hostConnectionProperties)) {
$builder->setHosts([$hostConnectionProperties]);
}

if ($config->has('explorer.additionalConnections')) {
$builder->setHosts([$config->get('explorer.connection'), ...$config->get('explorer.additionalConnections')]);
}
if ($config->has('explorer.connection.selector')) {
$builder->setSelector($config->get('explorer.connection.selector'));
$selectorClass = $config->get('explorer.connection.selector');
if (!is_a($selectorClass, SelectorInterface::class, true)) {
throw new InvalidArgumentException(
'Expect explorer.connection.selector to implement elastic SelectorInterface'
);
}

$nodePool = new SimpleNodePool(
new $selectorClass(),
new ElasticsearchResurrect(),
);
$builder->setNodePool($nodePool);
}

if($config->has('explorer.connection.api')) {
Expand Down
12 changes: 7 additions & 5 deletions src/Infrastructure/Elastic/ElasticClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

namespace JeroenG\Explorer\Infrastructure\Elastic;

use Elasticsearch\Client;
use Elasticsearch\ClientBuilder;
use GuzzleHttp\Ring\Client\MockHandler;
use Elastic\Elasticsearch\Client;
use Elastic\Elasticsearch\ClientBuilder;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Handler\MockHandler;

final class ElasticClientFactory
{
Expand All @@ -24,10 +25,11 @@ public function client(): Client

public static function fake(FakeResponse $response): ElasticClientFactory
{
$handler = new MockHandler($response->toArray());
$handler = new MockHandler([$response->toResponse()]);

$builder = ClientBuilder::create();
$builder->setHosts(['testhost']);
$builder->setHandler($handler);
$builder->setHttpClient(new GuzzleClient(['handler' => $handler]));
return new self($builder->build());
}
}
16 changes: 11 additions & 5 deletions src/Infrastructure/Elastic/ElasticDocumentAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace JeroenG\Explorer\Infrastructure\Elastic;

use Elasticsearch\Client;
use Elasticsearch\Common\Exceptions\Missing404Exception;
use Elastic\Elasticsearch\Client;
use Elastic\Elasticsearch\Exception\ClientResponseException;
use JeroenG\Explorer\Application\DocumentAdapterInterface;
use JeroenG\Explorer\Application\Operations\Bulk\BulkOperationInterface;
use JeroenG\Explorer\Application\Results;
Expand All @@ -22,7 +22,7 @@ public function bulk(BulkOperationInterface $command): callable|array
{
return $this->client->bulk([
'body' => $command->build(),
]);
])->asArray();
}

public function update(string $index, $id, array $data): callable|array
Expand All @@ -31,7 +31,7 @@ public function update(string $index, $id, array $data): callable|array
'index' => $index,
'id' => $id,
'body' => $data,
]);
])->asArray();
}

public function delete(string $index, $id): void
Expand All @@ -41,7 +41,13 @@ public function delete(string $index, $id): void
'index' => $index,
'id' => $id
]);
} catch (Missing404Exception) {}
} catch (ClientResponseException $clientResponseException) {
if ($clientResponseException->getCode() === 404) {
return;
}

throw $clientResponseException;
}
}

public function search(SearchCommandInterface $command): Results
Expand Down
21 changes: 11 additions & 10 deletions src/Infrastructure/Elastic/ElasticIndexAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

namespace JeroenG\Explorer\Infrastructure\Elastic;

use Elasticsearch\Client;
use Elastic\Elasticsearch\Client;
use JeroenG\Explorer\Application\IndexAdapterInterface;
use JeroenG\Explorer\Domain\IndexManagement\AliasedIndexConfiguration;
use JeroenG\Explorer\Domain\IndexManagement\AliasedIndexConfigurationInterface;
use JeroenG\Explorer\Domain\IndexManagement\IndexAliasConfigurationInterface;
use JeroenG\Explorer\Domain\IndexManagement\DirectIndexConfiguration;
use JeroenG\Explorer\Domain\IndexManagement\IndexConfigurationInterface;
Expand Down Expand Up @@ -59,11 +60,11 @@ public function delete(IndexConfigurationInterface $indexConfiguration): void
$aliasConfiguration = $indexConfiguration->getAliasConfiguration();
$aliasName = $aliasConfiguration->getHistoryAliasName();

if (!$this->client->indices()->existsAlias(['name' => $aliasName])) {
if (!$this->client->indices()->existsAlias(['name' => $aliasName])->asBool()) {
return;
}

$indicesForAlias = $this->client->indices()->getAlias(['name' => $aliasName]);
$indicesForAlias = $this->client->indices()->getAlias(['name' => $aliasName])->asArray();

foreach ($indicesForAlias as $index => $data) {
$this->client->indices()->delete(['index' => $index]);
Expand All @@ -81,12 +82,12 @@ public function flush(string $index): void

public function getWriteIndexName(IndexAliasConfigurationInterface $aliasConfiguration): ?string
{
$aliasConfig = $this->client->indices()->getAlias(['name' => $aliasConfiguration->getWriteAliasName() ]);
$aliasConfig = $this->client->indices()->getAlias(['name' => $aliasConfiguration->getWriteAliasName() ])->asArray();

return last(array_keys($aliasConfig));
}

public function createNewWriteIndex(IndexConfigurationInterface $indexConfiguration): string
public function createNewWriteIndex(AliasedIndexConfigurationInterface $indexConfiguration): string
{
$aliasConfig = $indexConfiguration->getAliasConfiguration();
$indexName = $this->getUniqueAliasIndexName($aliasConfig);
Expand All @@ -110,7 +111,7 @@ public function ensureIndex(IndexConfigurationInterface $indexConfiguration): vo
{
$exists = $this->client->indices()->exists([
'index' => $indexConfiguration->getWriteIndexName(),
]);
])->asBool();

if (!$exists) {
$this->create($indexConfiguration);
Expand All @@ -119,7 +120,7 @@ public function ensureIndex(IndexConfigurationInterface $indexConfiguration): vo

private function makeAliasActive(IndexAliasConfigurationInterface $aliasConfiguration): void
{
$exists = $this->client->indices()->existsAlias(['name' => $aliasConfiguration->getAliasName()]);
$exists = $this->client->indices()->existsAlias(['name' => $aliasConfiguration->getAliasName()])->asBool();
$index = $this->getWriteIndexName($aliasConfiguration);
$alias = $aliasConfiguration->getAliasName();

Expand All @@ -145,11 +146,11 @@ private function makeAliasActive(IndexAliasConfigurationInterface $aliasConfigur
private function pruneAliases(IndexAliasConfigurationInterface $indexAliasConfiguration): void
{
$aliasName = $indexAliasConfiguration->getHistoryAliasName();
if (!$this->client->indices()->existsAlias(['name' => $aliasName])) {
if (!$this->client->indices()->existsAlias(['name' => $aliasName])->asBool()) {
return;
}

$indicesForAlias = $this->client->indices()->getAlias(['name' => $aliasName]);
$indicesForAlias = $this->client->indices()->getAlias(['name' => $aliasName])->asArray();
$writeAlias = $this->getWriteIndexName($indexAliasConfiguration);

foreach ($indicesForAlias as $index => $data) {
Expand All @@ -174,7 +175,7 @@ private function getUniqueAliasIndexName(IndexAliasConfigurationInterface $alias
$name = $aliasConfig->getIndexName() . '_' . time();
$iX = 0;

while ($this->client->indices()->exists([ 'index' => $name ])) {
while ($this->client->indices()->exists([ 'index' => $name ])->asBool()) {
$name .= '_' . $iX++;
}

Expand Down
15 changes: 15 additions & 0 deletions src/Infrastructure/Elastic/FakeResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace JeroenG\Explorer\Infrastructure\Elastic;

use Elastic\Elasticsearch\Response\Elasticsearch;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\ResponseInterface;
use Webmozart\Assert\Assert;

class FakeResponse
Expand Down Expand Up @@ -31,4 +34,16 @@ public function toArray(): array
'effective_url' => 'localhost'
];
}

public function toResponse(): ResponseInterface
{
return new Response(
$this->statusCode,
[
Elasticsearch::HEADER_CHECK => Elasticsearch::PRODUCT_NAME,
'Content-Type' => 'application/json',
],
$this->body
);
}
}
10 changes: 7 additions & 3 deletions src/Infrastructure/Elastic/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@

namespace JeroenG\Explorer\Infrastructure\Elastic;

use Elasticsearch\Client;
use Elastic\Elasticsearch\Client;
use Elastic\Elasticsearch\ClientInterface;
use JeroenG\Explorer\Application\Results;
use JeroenG\Explorer\Application\SearchCommandInterface;

class Finder
{
/**
* @param Client $client
*/
public function __construct(
private Client $client,
private ClientInterface $client,
private SearchCommandInterface $builder,
) {
}
Expand All @@ -23,7 +27,7 @@ public function find(): Results
'body' => $this->builder->buildQuery(),
];

$rawResults = $this->client->search($query);
$rawResults = $this->client->search($query)->asArray();

return new Results($rawResults);
}
Expand Down
38 changes: 38 additions & 0 deletions tests/Unit/ClientExpectation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);

namespace JeroenG\Explorer\Tests\Unit;

use Elastic\Elasticsearch\Client;
use Elastic\Elasticsearch\ClientInterface;
use Mockery;
use Mockery\MockInterface;

final class ClientExpectation
{
private MockInterface|Client $mock;

public function __construct(MockInterface|Client $mock)
{
$this->mock = $mock;
}

public static function create(): self
{

return new self(Mockery::mock(Client::class));
}

public function getMock(): Client
{
return $this->mock;
}

public function expectSearch(array $input, FakeElasticResponse $response): void
{
$this->mock
->expects('search')
->with($input)
->andReturn($response);
}
}
Loading

0 comments on commit 7583f71

Please sign in to comment.