diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 55ed1d6..5178c16 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,20 +15,22 @@ jobs: strategy: fail-fast: false matrix: - php-version: ['8.1'] + php-version: ['8.2'] prefer-lowest: [''] include: - - php-version: '8.1' + - php-version: '8.2' prefer-lowest: 'prefer-lowest' services: elasticsearch: - image: elasticsearch:7.17.8 + image: elasticsearch:8.12.1 ports: - 9200/tcp env: discovery.type: single-node ES_JAVA_OPTS: -Xms500m -Xmx500m + xpack.security.enabled: false + xpack.security.enrollment.enabled: false options: >- --health-cmd "curl http://127.0.0.1:9200/_cluster/health" --health-interval 10s @@ -69,21 +71,21 @@ jobs: fi - name: Setup problem matchers for PHPUnit - if: matrix.php-version == '8.1' + if: matrix.php-version == '8.2' run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" - name: Run PHPUnit env: DB_URL: Cake\ElasticSearch\Datasource\Connection://127.0.0.1:${{ job.services.elasticsearch.ports['9200'] }}?driver=Cake\ElasticSearch\Datasource\Connection run: | - if [[ ${{ matrix.php-version }} == '8.1' ]]; then + if [[ ${{ matrix.php-version }} == '8.2' ]]; then export CODECOVERAGE=1 && vendor/bin/phpunit --display-incomplete --display-skipped --coverage-clover=coverage.xml else vendor/bin/phpunit fi - name: Submit code coverage - if: matrix.php-version == '8.1' + if: matrix.php-version == '8.2' uses: codecov/codecov-action@v3 cs-stan: @@ -92,12 +94,14 @@ jobs: services: elasticsearch: - image: elasticsearch:7.17.8 + image: elasticsearch:8.12.1 ports: - 9200/tcp env: discovery.type: single-node ES_JAVA_OPTS: -Xms500m -Xmx500m + xpack.security.enabled: false + xpack.security.enrollment.enabled: false options: >- --health-cmd "curl http://127.0.0.1:9200/_cluster/health" --health-interval 10s @@ -110,7 +114,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: '8.1' + php-version: '8.2' extensions: mbstring, intl, apcu tools: cs2pr coverage: none diff --git a/composer.json b/composer.json index b4c09b7..fe5e4a3 100644 --- a/composer.json +++ b/composer.json @@ -18,8 +18,8 @@ "source": "https://github.com/cakephp/elastic-search" }, "require": { - "cakephp/cakephp": "^5.0.0", - "ruflin/elastica": "^7.1" + "cakephp/cakephp": "^5.1.0", + "ruflin/elastica": "^8.0" }, "require-dev": { "cakephp/cakephp-codesniffer": "^5.0", @@ -40,7 +40,8 @@ }, "config": { "allow-plugins": { - "dealerdirect/phpcodesniffer-composer-installer": true + "dealerdirect/phpcodesniffer-composer-installer": true, + "php-http/discovery": true } }, "scripts": { diff --git a/docker-compose.yml b/docker-compose.yml index 6c15bf2..0e8a4c9 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,12 +7,14 @@ services: volumes: - .:/code elasticsearch: - image: "elasticsearch:7.17.4" + image: "elasticsearch:8.12.1" ports: - 9200/tcp environment: discovery.type: single-node ES_JAVA_OPTS: -Xms500m -Xmx500m + xpack.security.enabled: false + xpack.security.enrollment.enabled: false healthcheck: test: "curl -f http://127.0.0.1:9200/_cluster/health || exit 1" interval: "10s" diff --git a/src/Datasource/Connection.php b/src/Datasource/Connection.php index 5fa4afe..ba6c82c 100644 --- a/src/Datasource/Connection.php +++ b/src/Datasource/Connection.php @@ -79,7 +79,7 @@ public function __construct(array $config = [], ?callable $callback = null) $this->enableQueryLogging((bool)$config['log']); } - $this->_client = new ElasticaClient($config, $callback, $this->getEsLogger()); + $this->_client = new ElasticaClient($config, $this->getEsLogger()); } /** diff --git a/src/Datasource/Log/ElasticLogger.php b/src/Datasource/Log/ElasticLogger.php index b4f7abd..6985cd0 100644 --- a/src/Datasource/Log/ElasticLogger.php +++ b/src/Datasource/Log/ElasticLogger.php @@ -118,6 +118,9 @@ public function log(mixed $level, Stringable|string $message, array $context = [ protected function _log(string $level, string $message, array $context = []): void { $logData = $context; + if ($level === LogLevel::INFO || $message !== 'Elastica Request') { + return; + } if ($level === LogLevel::DEBUG && isset($context['request'])) { $logData = [ 'method' => $context['request']['method'], diff --git a/src/Query.php b/src/Query.php index 7d7eaf5..92e5dbf 100644 --- a/src/Query.php +++ b/src/Query.php @@ -18,6 +18,7 @@ use Cake\Collection\Iterator\MapReduce; use Cake\Datasource\Exception\RecordNotFoundException; +use Cake\Datasource\FactoryLocator; use Cake\Datasource\QueryCacher; use Cake\Datasource\QueryInterface; use Cake\Datasource\RepositoryInterface; @@ -1199,4 +1200,39 @@ protected function decoratorClass(): string { return ResultSetDecorator::class; } + + /** + * Serialize Query + * + * @return array + */ + public function __serialize(): array + { + return [ + 'dirty' => $this->_dirty, + 'queryParts' => $this->_queryParts, + 'searchOptions' => $this->_searchOptions, + 'options' => $this->_options, + 'repository' => $this->_repository->getAlias(), + ]; + } + + /** + * Unserialize Query + * + * @param array $data Serialized data + * @return void + */ + public function __unserialize(array $data): void + { + $this->_dirty = $data['dirty']; + $this->_queryParts = $data['queryParts']; + $this->_searchOptions = $data['searchOptions']; + $this->_options = $data['options']; + + $this->_repository = FactoryLocator::get('ElasticSearch')->get($data['repository']); + + $this->_elasticQuery = new ElasticaQuery(); + $this->compileQuery(); + } } diff --git a/src/TestSuite/Fixture/DeleteQueryStrategy.php b/src/TestSuite/Fixture/DeleteQueryStrategy.php index 8c5b341..f6498b9 100644 --- a/src/TestSuite/Fixture/DeleteQueryStrategy.php +++ b/src/TestSuite/Fixture/DeleteQueryStrategy.php @@ -75,7 +75,10 @@ public function teardownTest(): void foreach ($fixtures as $fixture) { /** @var \Cake\ElasticSearch\Datasource\Connection $connection */ $esIndex = $connection->getIndex($fixture->getIndex()->getName()); - $esIndex->deleteByQuery(new MatchAll()); + $esIndex->deleteByQuery(new MatchAll(), [ + 'conflicts' => 'proceed', + 'refresh' => true, + ]); $esIndex->refresh(); } }, $this->fixtures); diff --git a/tests/TestCase/Datasource/ConnectionTest.php b/tests/TestCase/Datasource/ConnectionTest.php index 9f294c5..fd4e2b1 100644 --- a/tests/TestCase/Datasource/ConnectionTest.php +++ b/tests/TestCase/Datasource/ConnectionTest.php @@ -21,7 +21,9 @@ use Cake\Datasource\ConnectionManager; use Cake\ElasticSearch\Datasource\Connection; use Cake\ElasticSearch\TestSuite\TestCase; +use Cake\Http\ServerRequest; use Cake\Log\Log; +use Laminas\Diactoros\Uri; use Psr\Log\LoggerInterface; /** @@ -83,7 +85,11 @@ public function testQueryLoggingWithLog() $connection->enableQueryLogging(); $connection->setLogger(Log::engine('elasticsearch')); - $result = $connection->request('_stats'); + $request = new ServerRequest([ + 'method' => 'GET', + 'uri' => new Uri('_stats'), + ]); + $result = $connection->sendRequest($request); $connection->disableQueryLogging(); $this->assertNotEmpty($result); @@ -108,6 +114,7 @@ public function testLoggerQueryLogger() { Log::setConfig('elasticsearch', [ 'engine' => 'Array', + 'levels' => ['debug'], ]); $logger = new QueryLogger(); @@ -128,7 +135,13 @@ public function testLoggerQueryLogger() $connection->setLogger($logger); $connection->enableQueryLogging(); - $connection->request('_stats'); + $request = new ServerRequest([ + 'method' => 'GET', + 'uri' => new Uri('_stats'), + 'data' => [], + 'input' => '', + ]); + $connection->sendRequest($request); $connection->disableQueryLogging(); $logs = Log::engine('elasticsearch')->read();