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 #246

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
3a83c4c
Update composer.json
jayjfletcher Apr 6, 2024
ac5a338
Update ElasticClientFactory.php
jayjfletcher Apr 6, 2024
146f7e1
Update ElasticIndexAdapter.php
jayjfletcher Apr 6, 2024
c7fe551
Update ElasticDocumentAdapter.php
jayjfletcher Apr 6, 2024
042f212
Update ElasticClientBuilder.php
jayjfletcher Apr 6, 2024
a08523f
Update ExplorerServiceProvider.php
jayjfletcher Apr 6, 2024
4dee0c0
Update composer.json
jayjfletcher Apr 6, 2024
dea9c91
Update ElasticDocumentAdapter.php
jayjfletcher Apr 6, 2024
7b564b5
Update Finder.php
jayjfletcher Apr 6, 2024
6f7f0fa
Update Finder.php
jayjfletcher Apr 6, 2024
d769d6e
removed the client builder and factory to simplify configuration
jfletcher-seclock Apr 7, 2024
32694b0
removed the client builder and factory to simplify configuration
jfletcher-seclock Apr 7, 2024
5f069db
removed the client builder and factory to simplify configuration
jfletcher-seclock Apr 7, 2024
ca9436e
removed the client builder and factory to simplify configuration
jfletcher-seclock Apr 7, 2024
c06966b
removed the client builder and factory to simplify configuration
jfletcher-seclock Apr 7, 2024
7481e2c
removed the client builder and factory to simplify configuration
jfletcher-seclock Apr 7, 2024
51ea063
cleanup
jfletcher-seclock Apr 7, 2024
bdc2787
cleanup
jfletcher-seclock Apr 7, 2024
9b9b223
cleanup
jfletcher-seclock Apr 11, 2024
5e52a30
Merge remote-tracking branch 'jay/master'
sporchia Jul 18, 2024
fb4ac1e
fix failing parts of CI
sporchia Jul 18, 2024
e1e595b
fix: adjust to using Client in code
sporchia Jul 18, 2024
e52832d
working on correcting tests to mock the upstream HTTP response
sporchia Jul 18, 2024
6493ac1
Update FinderTest.php
sporchia Jul 23, 2024
8bad2fc
Update FinderTest.php
sporchia Jul 23, 2024
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: 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
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\AliasedIndexConfiguration;
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(AliasedIndexConfiguration $indexConfiguration): string;

public function ensureIndex(IndexConfigurationInterface $indexConfiguration): void;
}
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 = is_array($rawResults) ? $rawResults : $rawResults->asArray();
}

public function hits(): array
Expand Down
25 changes: 10 additions & 15 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\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 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(Client::class)) {
$this->app->singleton(Client::class, fn (Application $app) => ClientBuilder::fromConfig(config('explorer.connection')));
}

$this->app->when(ElasticIndexConfigurationRepository::class)
->needs('$indexConfigurations')
Expand Down Expand Up @@ -94,8 +89,8 @@ protected function bootForConsole(): void
], 'explorer.config');

$this->commands([
ElasticSearch::class,
ElasticUpdate::class,
]);
ElasticSearch::class,
ElasticUpdate::class,
]);
}
}
Loading
Loading