Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Elasticsearch ^8.0 support #239

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"keywords": ["Laravel", "Scout", "Elasticsearch", "Elastic", "search", "Explorer"],
"require": {
"php": "8.0.*||8.1.*||8.2.*||8.3.*",
"elasticsearch/elasticsearch": "^7.16",
"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 Down Expand Up @@ -47,7 +47,8 @@
},
"config": {
"allow-plugins": {
"infection/extension-installer": false
"infection/extension-installer": false,
"php-http/discovery": true
}
}
}
15 changes: 8 additions & 7 deletions config/explorer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
declare(strict_types=1);

return [
/*
* There are different options for the connection. Since Explorer uses the Elasticsearch PHP SDK
* under the hood, all the host configuration options of the SDK are applicable here. See
* https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/configuration.html
/**
* This now follows the same updated configuration options as the official Elasticsearch PHP SDK.
* https://www.elastic.co/guide/en/elasticsearch/client/php-api/8.13/node_pool.html#config-hash
*/
'connection' => [
'host' => 'localhost',
'port' => '9200',
'scheme' => 'http',
'ElasticCloudId' => 'YOUR_ELASTIC_CLOUD_ID',
'ApiKey' => [
'apiKey' => 'YOUR_API_KEY',
'id' => 'YOUR_API_KEY_ID',
],
],

/**
Expand Down
96 changes: 40 additions & 56 deletions docs/connection.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
Explorer connects to ElasticSearch through the PHP ElasticSearch client and has several options to configure a connection.
The connection configuration is defined `config/explorer.php`.

See * https://www.elastic.co/guide/en/elasticsearch/client/php-api/8.13/node_pool.html#config-hash

## Basic

The most basic connection is with http without authorization.
```php
return [
'connection' => [
'host' => 'localhost',
'port' => '9200',
'scheme' => 'http',
'Hosts' => ['localhost:9200'],
],
];
```
Expand All @@ -22,7 +22,22 @@ Another connection option is to use an elastic cloud id as shown below
```php
return [
'connection' => [
'elasticCloudId' => 'staging:dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyRjZWM2ZjI2MWE3NGJmMjRjZTMzYmI4ODExYjg0Mjk0ZiRjNmMyY2E2ZDA0MjI0OWFmMGNjN2Q3YTllOTYyNTc0Mw',
'ElasticCloudId' => 'staging:dXMtZWFzdC03LmF3cy5mb3VuZC5pbyRjZWM2ZjI2MWE3SGJmMjRjZvMzYmI4ODExYjg0Mjk0ZiRjNmMyY2E2ZDA0MjI0OWFmMGNjN2Q3YTllOTYyNTc0Mw',
],
];
```

## * *Preferred Method* - Encoded API key and id

Replace the auth part with API and give it your encoded id and key.

```php
return [
'connection' => [
'ElasticCloudId' => 'staging:dXMtZWFzdC03LmF3cy5mb3VuZC5pbyRjZWM2ZjI2MWE3SGJmMjRjZvMzYmI4ODExYjg0Mjk0ZiRjNmMyY2E2ZDA0MjI0OWFmMGNjN2Q3YTllOTYyNTc0Mw',
'ApiKey' => [
'apiKey' => 'myEncodedKeyAndID'
],
],
];
```
Expand All @@ -34,34 +49,27 @@ To specify a username and password use the `auth` key with a `username` and a `p
```php
return [
'connection' => [
'host' => 'localhost',
'port' => '9200',
'scheme' => 'http',
'auth' => [
'Hosts' => ['localhost:9200'],
'BasicAuthentication' => [
'username' => 'myName',
'password' => 'myPassword'
],
],
];
```

## API key

Replace the auth part with API and give it your key and id.
## API key and id

```php
return [
'connection' => [
'host' => 'localhost',
'port' => '9200',
'scheme' => 'http',
'api' => [
'Hosts' => ['localhost:9200'],
'ApiKey' => [
'id' => 'myId',
'key' => 'myKey'
'apiKey' => 'myKey'
],
],
];
```

## Verify TLS with CA

Expand All @@ -73,12 +81,8 @@ From Elastic 8 and upwards TLS is becoming the default, even in development. Thi
```php
return [
'connection' => [
'host' => 'localhost',
'port' => '9200',
'scheme' => 'http',
'ssl' => [
'verify' => './path/to/ca.crt',
],
'Hosts' => ['localhost:9200'],
'CABundle' => './path/to/ca.crt',
],
];
```
Expand All @@ -87,12 +91,8 @@ To disable TLS verification set it to `false`. **NOT recommended for production*
```php
return [
'connection' => [
'host' => 'localhost',
'port' => '9200',
'scheme' => 'http',
'ssl' => [
'verify' => false,
],
'Hosts' => ['localhost:9200'],
'SSLVerification' => false,
],
];
```
Expand All @@ -101,12 +101,14 @@ To disable TLS verification set it to `false`. **NOT recommended for production*
```
return [
'connection' => [
'host' => 'localhost',
'port' => '9200',
'scheme' => 'https',
'ssl' => [
'cert' => ['path/to/cert.pem', 'passphrase'],
'key' => ['path/to/key.pem', 'passphrase'],
'Hosts' => ['localhost:9200'],
'SSLCert' => [
'cert' => './path/to/cert.crt',
'password' => null,
],
'SSLKey' => [
'key' => './path/to/key.key',
password' => null,
],
],
];
Expand All @@ -117,29 +119,11 @@ 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;


return [
'connection' => [
'host' => 'localhost',
'port' => '9200',
'scheme' => 'http',
'ssl' => [
'verify' => false,
],
'selector' => RoundRobinSelector::class
],
'additionalConnections' => [
[
'host' => 'localhost',
'port' => '9201',
'scheme' => 'http',
],
[
'host' => 'localhost',
'port' => '9202',
'scheme' => 'http',
]
'Hosts' => ['host1:9200', 'host2:9200'],
'SSLVerification' => true,
],
];
```
5 changes: 3 additions & 2 deletions src/Application/Results.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
namespace JeroenG\Explorer\Application;

use Countable;
use Elastic\Elasticsearch\Response\Elasticsearch;

class Results implements Countable
{
private array $rawResults;

public function __construct(array $rawResults)
public function __construct(array|Elasticsearch $rawResults)
{
$this->rawResults = $rawResults;
$this->rawResults = $rawResults instanceof Elasticsearch ? $rawResults->asArray() : $rawResults;

Check warning on line 16 in src/Application/Results.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.1

Escaped Mutant for Mutator "InstanceOf_": --- Original +++ New @@ @@ private array $rawResults; public function __construct(array|Elasticsearch $rawResults) { - $this->rawResults = $rawResults instanceof Elasticsearch ? $rawResults->asArray() : $rawResults; + $this->rawResults = false ? $rawResults->asArray() : $rawResults; } public function hits() : array {

Check warning on line 16 in src/Application/Results.php

View workflow job for this annotation

GitHub Actions / PHP8

Escaped Mutant for Mutator "InstanceOf_": --- Original +++ New @@ @@ private array $rawResults; public function __construct(array|Elasticsearch $rawResults) { - $this->rawResults = $rawResults instanceof Elasticsearch ? $rawResults->asArray() : $rawResults; + $this->rawResults = false ? $rawResults->asArray() : $rawResults; } public function hits() : array {

Check warning on line 16 in src/Application/Results.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.2

Escaped Mutant for Mutator "InstanceOf_": --- Original +++ New @@ @@ private array $rawResults; public function __construct(array|Elasticsearch $rawResults) { - $this->rawResults = $rawResults instanceof Elasticsearch ? $rawResults->asArray() : $rawResults; + $this->rawResults = false ? $rawResults->asArray() : $rawResults; } public function hits() : array {

Check warning on line 16 in src/Application/Results.php

View workflow job for this annotation

GitHub Actions / PHP8.1

Escaped Mutant for Mutator "InstanceOf_": --- Original +++ New @@ @@ private array $rawResults; public function __construct(array|Elasticsearch $rawResults) { - $this->rawResults = $rawResults instanceof Elasticsearch ? $rawResults->asArray() : $rawResults; + $this->rawResults = false ? $rawResults->asArray() : $rawResults; } public function hits() : array {

Check warning on line 16 in src/Application/Results.php

View workflow job for this annotation

GitHub Actions / PHP8.2

Escaped Mutant for Mutator "InstanceOf_": --- Original +++ New @@ @@ private array $rawResults; public function __construct(array|Elasticsearch $rawResults) { - $this->rawResults = $rawResults instanceof Elasticsearch ? $rawResults->asArray() : $rawResults; + $this->rawResults = false ? $rawResults->asArray() : $rawResults; } public function hits() : array {

Check warning on line 16 in src/Application/Results.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.3

Escaped Mutant for Mutator "InstanceOf_": --- Original +++ New @@ @@ private array $rawResults; public function __construct(array|Elasticsearch $rawResults) { - $this->rawResults = $rawResults instanceof Elasticsearch ? $rawResults->asArray() : $rawResults; + $this->rawResults = false ? $rawResults->asArray() : $rawResults; } public function hits() : array {

Check warning on line 16 in src/Application/Results.php

View workflow job for this annotation

GitHub Actions / PHP8.3

Escaped Mutant for Mutator "InstanceOf_": --- Original +++ New @@ @@ private array $rawResults; public function __construct(array|Elasticsearch $rawResults) { - $this->rawResults = $rawResults instanceof Elasticsearch ? $rawResults->asArray() : $rawResults; + $this->rawResults = false ? $rawResults->asArray() : $rawResults; } public function hits() : array {

Check warning on line 16 in src/Application/Results.php

View workflow job for this annotation

GitHub Actions / PHP8.4

Escaped Mutant for Mutator "InstanceOf_": --- Original +++ New @@ @@ private array $rawResults; public function __construct(array|Elasticsearch $rawResults) { - $this->rawResults = $rawResults instanceof Elasticsearch ? $rawResults->asArray() : $rawResults; + $this->rawResults = false ? $rawResults->asArray() : $rawResults; } public function hits() : array {
}

public function hits(): array
Expand Down
19 changes: 7 additions & 12 deletions src/ExplorerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@

namespace JeroenG\Explorer;

use Elasticsearch\Client;
use Elastic\Elasticsearch\ClientBuilder;
use Elastic\Elasticsearch\ClientInterface;
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 Expand Up @@ -48,13 +45,11 @@ public function boot(): void
$this->bootForConsole();
}

$this->app->when(ElasticClientFactory::class)
->needs(Client::class)
->give(static fn () => ElasticClientBuilder::fromConfig(config())->build());

$this->app->when(ElasticDocumentAdapter::class)
->needs(Client::class)
->give(static fn (Application $app) => $app->make(ElasticClientFactory::class)->client());
// Removed the old client builder and factory in favor of the native elastic client builder for simplicity
// This if statement allows the user to define their own client implementation outside of this package
if(!$this->app->has(ClientInterface::class)){
$this->app->singleton(ClientInterface::class, fn (Application $app) => ClientBuilder::fromConfig(config('explorer.connection')));
}

$this->app->when(ElasticIndexConfigurationRepository::class)
->needs('$indexConfigurations')
Expand Down
88 changes: 0 additions & 88 deletions src/Infrastructure/Elastic/ElasticClientBuilder.php

This file was deleted.

33 changes: 0 additions & 33 deletions src/Infrastructure/Elastic/ElasticClientFactory.php

This file was deleted.

Loading
Loading