Skip to content

Commit

Permalink
Merge pull request #253 from Jeroen-G/elastic8
Browse files Browse the repository at this point in the history
Add elastic8 support
  • Loading branch information
Jeroen-G authored Jan 5, 2025
2 parents 2fd73a6 + eb3ac7b commit 975cf7e
Show file tree
Hide file tree
Showing 34 changed files with 368 additions and 211 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

strategy:
matrix:
php: [8.0, 8.1, 8.2, 8.3]
php: [8.2, 8.3, 8.4]
experimental: [ false ]
include:
- php: 8.4
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:

strategy:
matrix:
php: [8.1, 8.2, 8.3]
php: [8.2, 8.3, 8.4]

name: Mutation Testing Code Review Annotations ${{ matrix.php }}

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
/node_modules
/report
.phpunit.result.cache
.phpunit.cache
composer.lock
infection.log
16 changes: 16 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## [4.0.0]

### Added

- Support for ElasticSearch 8.

### Removed

- ElasticSearch 7 support: the elastic 8 client is not backwards compatible. See 'changed' for the breaking changes.
- Support for Laravel 9 and below to make package maintenance easier.
- Support for PHP 8.0 and 8.1, as required by Laravel.

### Changed

- The import path for `selector` has changed from `Elasticsearch\ConnectionPool\Selectors\RoundRobinSelector` to `Elastic\Transport\NodePool\Selector\RoundRobin`

## [3.15.0]

### Fixed
Expand Down
17 changes: 9 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,22 @@
"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",
"illuminate/support": "^9.0||^10.0||^11.0",
"laravel/scout": "^9.0||^10.0||^11.0",
"php": "^8.2",
"elasticsearch/elasticsearch": "^8.0",
"illuminate/support": "^11.0",
"laravel/scout": "^10.0",
"webmozart/assert": "^1.10",
"psr/log": "^1|^2|^3"
"psr/log": "^3"
},
"require-dev": {
"phpunit/phpunit": "~9.0",
"phpunit/phpunit": "^10.0",
"mockery/mockery": "^1.5",
"infection/infection": "^0.26",
"infection/infection": "^0.29",
"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
4 changes: 2 additions & 2 deletions docs/connection.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ To disable TLS verification set it to `false`. **NOT recommended for production*
Elastic can also have multiple possible connections

```php
use Elasticsearch\ConnectionPool\Selectors\RoundRobinSelector;
use Elastic\Transport\NodePool\Selector\RoundRobin;

return [
'connection' => [
Expand All @@ -127,7 +127,7 @@ Elastic can also have multiple possible connections
'ssl' => [
'verify' => false,
],
'selector' => RoundRobinSelector::class
'selector' => RoundRobin::class
],
'additionalConnections' => [
[
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
12 changes: 6 additions & 6 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?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">
<coverage>
<include>
<directory>src/</directory>
</include>
</coverage>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="tests/bootstrap.php" backupGlobals="false" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<testsuites>
<testsuite name="unit">
<directory suffix=".php">./tests/Unit</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>src/</directory>
</include>
</source>
</phpunit>
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: 2 additions & 2 deletions src/Domain/Syntax/RegExp.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
class RegExp implements SyntaxInterface
{
private string $field;

private ?string $value;

private string $flags;

private bool $insensitive;

public function __construct(string $field, string $value = null, string $flags = 'ALL', bool $insensitive = false)
public function __construct(string $field, ?string $value = null, string $flags = 'ALL', bool $insensitive = false)
{
$this->field = $field;
$this->value = $value;
Expand Down
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
30 changes: 27 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,33 @@ public static function fromConfig(Repository $config): ClientBuilder
ARRAY_FILTER_USE_KEY
);

$builder->setHosts([$hostConnectionProperties]);
if (!empty($hostConnectionProperties)) {
$builder->setHosts([
sprintf(
"%s://%s:%s",
$hostConnectionProperties['scheme'],
$hostConnectionProperties['host'],
$hostConnectionProperties['port']
),
]);
}

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
Loading

0 comments on commit 975cf7e

Please sign in to comment.