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

upgrade ruflin/elastica to v8 and do necessary changes #188

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ test: ## Run test suite
./vendor/bin/simple-phpunit

start: ## Start testing tools (Elasticsearch)
docker run --rm -d --name "elastically_es" -p 9999:9200 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch-oss:7.8.0
docker run --rm -d --name "elastically_es" -p 9999:9200 -e "discovery.type=single-node" -e "xpack.security.enabled=false" -e "action.destructive_requires_name=false" -it -m 1GB docker.elastic.co/elasticsearch/elasticsearch:8.14.2

start_opensearch: ## Start testing tools (OpenSearch)
docker run --rm -d --name "elastically_es" -p 9999:9200 -e "discovery.type=single-node" -e "plugins.security.disabled=true" opensearchproject/opensearch:2.3.0
Expand Down
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
"require": {
"php": ">=8.0",
"ext-json": "*",
"nyholm/psr7": "^1.8",
"phpdocumentor/reflection-docblock": "^4.4|^5.0",
"ruflin/elastica": "^7.0",
"ruflin/elastica": "^8.0",
"symfony/deprecation-contracts": "^2.4 || ^3.0",
"symfony/property-access": "^5.4 || ^6.0 || ^7.0",
"symfony/property-info": "^5.4 || ^6.0 || ^7.0",
"symfony/psr-http-message-bridge": "^7.1",
"symfony/serializer": "^5.4 || ^6.0 || ^7.0",
"symfony/yaml": "^5.4 || ^6.0 || ^7.0"
},
Expand Down Expand Up @@ -53,7 +55,8 @@
"config": {
"sort-packages": true,
"allow-plugins": {
"symfony/flex": true
"symfony/flex": true,
"php-http/discovery": true
}
}
}
4 changes: 2 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class Client extends ElasticaClient
private ResultSetBuilder $resultSetBuilder;
private IndexNameMapper $indexNameMapper;

public function __construct($config = [], ?callable $callback = null, ?LoggerInterface $logger = null, ?ResultSetBuilder $resultSetBuilder = null, ?IndexNameMapper $indexNameMapper = null)
public function __construct($config = [], ?LoggerInterface $logger = null, ?ResultSetBuilder $resultSetBuilder = null, ?IndexNameMapper $indexNameMapper = null)
{
parent::__construct($config, $callback, $logger);
parent::__construct($config, $logger);

// BC Layer, to remove in 2.0
$this->factory = new Factory($config);
Expand Down
1 change: 0 additions & 1 deletion src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public function buildClient(): Client
return $this->client ??= new Client(
$this->config,
null,
null,
$this->buildBuilder(),
$this->buildIndexNameMapper()
);
Expand Down
12 changes: 5 additions & 7 deletions src/IndexBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
use Elastica\Exception\ExceptionInterface;
use Elastica\Exception\RuntimeException;
use Elastica\Reindex;
use Elastica\Request;
use Elastica\Response;
use Elastica\Task;
use Elasticsearch\Endpoints\Cluster\State;
use JoliCode\Elastically\Mapping\MappingProviderInterface;

class IndexBuilder
Expand Down Expand Up @@ -64,7 +62,9 @@ public function markAsLive(Index $index, string $indexName): Response
$data['actions'][] = ['remove' => ['index' => $indexPrefixedName . '*', 'alias' => $indexPrefixedName]];
$data['actions'][] = ['add' => ['index' => $index->getName(), 'alias' => $indexPrefixedName]];

return $this->client->request('_aliases', Request::POST, $data);
return $this->client->toElasticaResponse(
$this->client->indices()->updateAliases(['index' => $indexName, 'body' => $data])
);
}

/**
Expand Down Expand Up @@ -119,13 +119,11 @@ public function purgeOldIndices(string $indexName, bool $dryRun = false): array
{
$indexName = $this->indexNameMapper->getPrefixedIndex($indexName);

$stateRequest = new State();
$stateRequest->setParams([
$state = $this->client->cluster()->state([
'filter_path' => 'metadata.indices.*.state,metadata.indices.*.aliases',
]);

$indexes = $this->client->requestEndpoint($stateRequest);
$indexes = $indexes->getData();
$indexes = $this->client->toElasticaResponse($state)->getData();
$indexes = $indexes['metadata']['indices'];

foreach ($indexes as $realIndexName => &$data) {
Expand Down
5 changes: 5 additions & 0 deletions src/Indexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ public function setBulkRequestParams(array $bulkRequestParams): void
$this->refreshBulkRequestParams();
}

public function getClient(): Client
{
return $this->client;
}

protected function getCurrentBulk(): Bulk
{
if (!$this->currentBulk) {
Expand Down
4 changes: 0 additions & 4 deletions src/Messenger/IndexationRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use JoliCode\Elastically\Client;
use JoliCode\Elastically\Indexer;
use JoliCode\Elastically\IndexNameMapper;
use Psr\Log\NullLogger;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
use Symfony\Component\Messenger\MessageBusInterface;
Expand Down Expand Up @@ -50,9 +49,6 @@ public function __construct(Client $client, MessageBusInterface $bus, DocumentEx
$this->exchanger = $exchanger;
$this->indexer = $indexer;
$this->indexNameMapper = $indexNameMapper;

// Disable the logs for memory concerns
$this->client->setLogger(new NullLogger());
}

/**
Expand Down
6 changes: 4 additions & 2 deletions tests/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace JoliCode\Elastically\Tests;

use Elastica\Request;
use Http\Discovery\Psr17Factory;
use JoliCode\Elastically\Client;
use JoliCode\Elastically\Factory;
use PHPUnit\Framework\TestCase;
Expand All @@ -21,15 +23,15 @@ abstract class BaseTestCase extends TestCase
{
protected function setUp(): void
{
$this->getFactory()->buildClient()->request('*', 'DELETE');
$this->getClient()->sendRequest((new Psr17Factory())->createRequest(Request::DELETE, '*'));
}

protected function getFactory(?string $path = null, array $config = []): Factory
{
return new Factory($config + [
Factory::CONFIG_MAPPINGS_DIRECTORY => $path ?? __DIR__ . '/configs',
'log' => false,
'port' => '9999',
'hosts' => ['http://127.0.0.1:9999'],
]);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/IndexBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

namespace JoliCode\Elastically\Tests;

use Elastic\Elasticsearch\Exception\ClientResponseException;
use Elastica\Document as ElasticaDocument;
use Elastica\Exception\InvalidException;
use Elastica\Exception\ResponseException;
use Elastica\Index;
use Elastica\Index\Settings;
use JoliCode\Elastically\Factory;
Expand Down Expand Up @@ -175,7 +175,7 @@ public function testPurgeAndCloseOldIndices(IndexBuilder $indexBuilder): void
try {
$index2->search();
$this->assertFalse(true, 'Search should throw a "closed index" exception.');
} catch (ResponseException $e) {
} catch (ClientResponseException $e) {
$this->assertStringContainsStringIgnoringCase('closed', $e->getMessage());
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/IndexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ public function testRequestParameters(): void
$response = $indexer->flush();

$this->assertInstanceOf(ResponseSet::class, $response);
$transferInfo = $response->getTransferInfo();
$this->assertStringContainsString('_bulk?refresh=wait_for', $transferInfo['url']);
$query = $indexer->getClient()->getLastRequest()->getUri()->getQuery();
$this->assertStringContainsString('refresh=wait_for', $query);

// Test the same with an invalid pipeline
$indexer->setBulkRequestParams([
Expand Down
3 changes: 1 addition & 2 deletions tests/Jane/JaneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ public function testCreateIndexAndSearchWithJaneObject()

// Build Elastically Client
$elastically = new Client(
['port' => '9999'],
null,
['hosts' => ['http://127.0.0.1:9999']],
null,
$resultSetBuilder,
$indexNameMapper
Expand Down