Skip to content
Closed
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
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"require": {
"php": "~8.0.0 || ~8.1.0 || ~8.2.0",
"ext-json": "*",
"elasticsearch/elasticsearch": "^7.10",
"elasticsearch/elasticsearch": "^8.8",
"nyholm/dsn": "^2.0.0",
"psr/log": "^1.0 || ^2.0 || ^3.0",
"symfony/deprecation-contracts": "^3.0"
Expand Down Expand Up @@ -49,5 +49,10 @@
"branch-alias": {
"dev-master": "7.0.x-dev"
}
},
"config": {
"allow-plugins": {
"php-http/discovery": true
}
}
}
4 changes: 4 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
verbose="true"
bootstrap="tests/bootstrap.php"
>
<php>
<env name="ES_HOST" value="https://localhost"/>
<env name="ES_PASSWORD" value=""/>
</php>
<coverage>
<include>
<directory>src/</directory>
Expand Down
20 changes: 20 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Elastica;

use Elastic\Elasticsearch\ClientBuilder;
use Elastic\Elasticsearch\Exception\AuthenticationException;
use Elastica\Bulk\Action;
use Elastica\Bulk\ResponseSet;
use Elastica\Exception\Bulk\ResponseException as BulkResponseException;
Expand Down Expand Up @@ -117,6 +119,24 @@ public function setConfig(array $config): self
return $this;
}

/**
* @throws AuthenticationException
*/
public function getElasticsearchClient(): \Elastic\Elasticsearch\Client
{
$clientBuilder = ClientBuilder::create();
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any concern around recreating the builder each time or should we store in somewhere in the Client under `_elasticsearchClient?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, not really. If you look at the create() function, it just returns a new instance of the class, without any arguments or state. We could store it somewhere in a property as well. Would it make it sense to store in a property if there's no real state attached to the object?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is no state, no need to store it. One thing I want to make sure, we don't open a new connection to Elasticsearch if we send multiple requests.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think (expect) all the connections the Elasticsearch Client makes will be opened and closed within the function call. The ClientBuilder - afaik - doesn't manage some sort of connection pool, if that's the sort of thing you were looking for?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found the part I was referring to here: https://github.com/ruflin/Elastica/blob/8.x/src/Transport/Http.php#L80 I don't think this is impacted by this change at all.


// TODO: make sure the entire list of configuration properties is passed onto the ElasticSearch Client
$clientBuilder->setHosts([$this->getConfig('host') . ':' . $this->getConfig('port')]);
$clientBuilder->setCABundle('path/to/elasticsearch/folder/config/certs/http_ca.crt');
// TODO: check if the alternative is just to default the ClientConfiguration::username/password to empty strings
if (!empty($this->getConfig('username')) && !empty($this->getConfig('password'))) {
$clientBuilder->setBasicAuthentication($this->getConfig('username'), $this->getConfig('password'));
}

return $clientBuilder->build();
}

/**
* Returns a specific config key or the whole config array if not set.
*
Expand Down
36 changes: 30 additions & 6 deletions src/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Elastica;

use Elastic\Elasticsearch\Endpoints\Indices;
use Elastica\Bulk\ResponseSet;
use Elastica\Exception\Bulk\ResponseException as BulkResponseException;
use Elastica\Exception\ClientException;
Expand Down Expand Up @@ -468,9 +469,10 @@ public function refresh(): Response
*
* @return Response Server response
*/
public function create(array $args = [], array $options = []): Response
public function create(array $args = [], array $options = [])
{
$endpoint = new Create();
// Leaving this here to easily compare the 7.x compatible elasticsearch-php library
/*$endpoint = new Create();
$invalidOptions = \array_diff(\array_keys($options), $allowedOptions = \array_merge($endpoint->getParamWhitelist(), [
'recreate',
]));
Expand All @@ -496,21 +498,43 @@ public function create(array $args = [], array $options = []): Response
$endpoint->setParams($options);
$endpoint->setBody($args);

return $this->requestEndpoint($endpoint);
return $this->requestEndpoint($endpoint);*/

// (Pseudo) Code using the 8.x elasticsearch-php library
// NOTE: do we really need to validate the arguments on the Elastica side, or do we leave that up to the elasticsearch-php / ElasticSearch engine to take care of?
// NOTE: not taking into account the recreate option for this refactor example

// The Indices::create() function only takes in 1 $params parameter, containing:
// - the index
// - the body
// - the url query parameters
$params = $options;
$params['body'] = $args;
$params['index'] = $this->_name;

$elasticSearchResponse = (new Indices($this->_client->getElasticSearchClient()))->create($params);

//return transformElasticSearchResponseToElasticaResponse($elasticSearchPPHPLibraryResponse);
}

/**
* Checks if the given index exists ans is created.
* Checks if the given index exists and is created.
*
* @throws ClientException
* @throws ConnectionException
* @throws ResponseException
*/
public function exists(): bool
{
$response = $this->requestEndpoint(new Exists());
// Leaving this here to easily compare the 7.x compatible elasticsearch-php library
/*$response = $this->requestEndpoint(new Exists());

return 200 === $response->getStatus();*/

// This would elasticsearch-php 8.x compatible code
$elasticSearchResponse = (new Indices)->exists(['index' => $this->_name]);

return 200 === $response->getStatus();
return 200 === $elasticSearchResponse->getStatusCode();
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/JSON.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Elastica;

use JsonException;

/**
* Elastica JSON tools.
*/
Expand Down
2 changes: 0 additions & 2 deletions src/Reindex.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ class Reindex extends Param
public const SCROLL = 'scroll';
public const REQUESTS_PER_SECOND = 'requests_per_second';
public const PIPELINE = 'pipeline';
public const SLICES = 'slices';
public const SLICES_AUTO = 'auto';

/**
* @var Index
Expand Down
5 changes: 5 additions & 0 deletions tests/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ protected function _getClient(array $params = [], ?callable $callback = null, ?L

$config = \array_merge($config, $params);

// TODO: either find a way to automatically fetch username/pw for ES, or find a way to disable the security
// In ES8, security is enabled by default
$config['username'] = 'elastic';
$config['password'] = \getenv('ES_PASSWORD');
Comment thread
coreation marked this conversation as resolved.

return new Client($config, $callback, $logger);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ public function testLimitDefaultIndex(): void
/**
* @group functional
*/
public function testCreate(): void
public function testCreateNewIndex(): void
{
$client = $this->_getClient();
$indexName = 'test';
Expand Down
1 change: 1 addition & 0 deletions tests/JSONTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Elastica\Test;

use Elastica\JSON;
use JsonException;
use PHPUnit\Framework\TestCase;

/**
Expand Down