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

cakephp 5.1 elastica 8.x #322

Open
wants to merge 6 commits into
base: 4.x
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
20 changes: 12 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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
Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -40,7 +40,8 @@
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
"dealerdirect/phpcodesniffer-composer-installer": true,
"php-http/discovery": true
}
},
"scripts": {
Expand Down
4 changes: 3 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/Datasource/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/Datasource/Log/ElasticLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Copy link
Member

Choose a reason for hiding this comment

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

What other kinds of logs are we dropping here?

Copy link
Member Author

@skie skie Nov 21, 2024

Choose a reason for hiding this comment

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

It coming from transport layer, so we getting 5 log entries per one request https://github.com/elastic/elastic-transport-php/blob/8be37d679637545e50b1cea9f8ee903888783021/src/Transport.php#L277
4 of it from transport, and last one from elastica client.

ROOT\src\Datasource\Log\ElasticLogger.php (line 121)
########## DEBUG ##########
'Request: GET http://localhost:9200/_stats'
###########################
ROOT\src\Datasource\Log\ElasticLogger.php (line 121)
########## DEBUG ##########
'Headers: {"Host":["localhost:9200"]}
Body: '
###########################
ROOT\src\Datasource\Log\ElasticLogger.php (line 121)
########## DEBUG ##########
'Response (retry 0): 200'
###########################
ROOT\src\Datasource\Log\ElasticLogger.php (line 121)
########## DEBUG ##########
'Headers: {"X-elastic-product":["Elasticsearch"],"content-type":["application\/json"],"Transfer-Encoding":["chunked"]}
Body: {"_shards":{"total":8,"successful":4,"failed":0},"_all":{"primaries":{"docs":{"count":100,"deleted":0,"total_size_in_bytes":1048486},"shard_stats":{"total_count":4},"store":{"size_in_bytes":1049279,"total_data_set_size_in_bytes":1049279,"reserved_in_bytes":0},"indexing":{"index_total":0,...

ROOT\src\Datasource\Log\ElasticLogger.php (line 121)
########## DEBUG ##########
'Elastica Request'
###########################

return;
}
if ($level === LogLevel::DEBUG && isset($context['request'])) {
$logData = [
'method' => $context['request']['method'],
Expand Down
36 changes: 36 additions & 0 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
5 changes: 4 additions & 1 deletion src/TestSuite/Fixture/DeleteQueryStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 15 additions & 2 deletions tests/TestCase/Datasource/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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);
Expand All @@ -108,6 +114,7 @@ public function testLoggerQueryLogger()
{
Log::setConfig('elasticsearch', [
'engine' => 'Array',
'levels' => ['debug'],
]);
$logger = new QueryLogger();

Expand All @@ -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();
Expand Down
Loading