From ef4ebafb72fff3f20903ca5596cd71baa027a244 Mon Sep 17 00:00:00 2001 From: Haydar KULEKCI Date: Wed, 16 Feb 2022 11:02:06 +0300 Subject: [PATCH 01/15] composer update and readme changes for elasticsearch 8.0 --- README.md | 1 + composer.json | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index aa8be09b..009b957d 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ With some basic changes to support wider range of Symfony Versions. | Version | Supported Elasticsearch Version | Supported Symfony Version | |---------|---------------------------------|---------------------------| +| 8.x | ^8.0 | ^5.0, ^4.0, ^3.4, ^2.8 | | 7.x | ^7.0 | ^5.0, ^4.0, ^3.4, ^2.8 | | 6.x | ^6.0 | ^5.0, ^4.0, ^3.4, ^2.8 | | 5.x | ^5.0 | ^5.0, ^4.0, ^3.4, ^2.8 | diff --git a/composer.json b/composer.json index 366176d8..a4c2de8d 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ "require": { "php": "^7.1|^8.0", "symfony/serializer": "^2.8 || ^3.4 || ^4.0 || ^5.0", - "elasticsearch/elasticsearch": "^7.0" + "elasticsearch/elasticsearch": "^8.0" }, "require-dev": { "phpunit/phpunit": "^5.7.26 || ^7.5.20 || ^8.0", From 6a7af66c2dfbcd52942de19f44b09da64a841962 Mon Sep 17 00:00:00 2001 From: Haydar KULEKCI Date: Wed, 16 Feb 2022 11:03:15 +0300 Subject: [PATCH 02/15] MovingAverage pipeline aggregation removed for ES8.x --- .../Metric/PercentilesAggregation.php | 10 +++---- .../Pipeline/MovingAverageAggregation.php | 28 ------------------- 2 files changed, 5 insertions(+), 33 deletions(-) delete mode 100644 src/Aggregation/Pipeline/MovingAverageAggregation.php diff --git a/src/Aggregation/Metric/PercentilesAggregation.php b/src/Aggregation/Metric/PercentilesAggregation.php index 627bb568..d7aabdae 100644 --- a/src/Aggregation/Metric/PercentilesAggregation.php +++ b/src/Aggregation/Metric/PercentilesAggregation.php @@ -39,12 +39,12 @@ class PercentilesAggregation extends AbstractAggregation * Inner aggregations container init. * * @param string $name - * @param string $field - * @param array $percents - * @param string $script - * @param int $compression + * @param string|null $field + * @param array|null $percents + * @param string|null $script + * @param int|null $compression */ - public function __construct($name, $field = null, $percents = null, $script = null, $compression = null) + public function __construct(string $name, string $field = null, array $percents = null, string $script = null, int $compression = null) { parent::__construct($name); diff --git a/src/Aggregation/Pipeline/MovingAverageAggregation.php b/src/Aggregation/Pipeline/MovingAverageAggregation.php deleted file mode 100644 index e93fdc5d..00000000 --- a/src/Aggregation/Pipeline/MovingAverageAggregation.php +++ /dev/null @@ -1,28 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace ONGR\ElasticsearchDSL\Aggregation\Pipeline; - -/** - * Class representing Max Bucket Pipeline Aggregation. - * - * @link https://goo.gl/8gIfok - */ -class MovingAverageAggregation extends AbstractPipelineAggregation -{ - /** - * {@inheritdoc} - */ - public function getType() - { - return 'moving_avg'; - } -} From 0675b1308a4517468c63ff6bfc0bde75952b9037 Mon Sep 17 00:00:00 2001 From: Haydar KULEKCI Date: Wed, 16 Feb 2022 11:03:40 +0300 Subject: [PATCH 03/15] Remove deprecated _time and _term sort orders --- tests/Unit/Aggregation/Bucketing/TermsAggregationTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Unit/Aggregation/Bucketing/TermsAggregationTest.php b/tests/Unit/Aggregation/Bucketing/TermsAggregationTest.php index 47924419..eb647d6c 100644 --- a/tests/Unit/Aggregation/Bucketing/TermsAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/TermsAggregationTest.php @@ -178,12 +178,12 @@ public function testTermsAggregationSetOrderDESC() // Case #7 terms aggregation with order term mode, desc direction. $aggregation = new TermsAggregation('test_agg'); $aggregation->setField('test_field'); - $aggregation->addParameter('order', ['_term' => 'desc']); + $aggregation->addParameter('order', ['_key' => 'desc']); $result = [ 'terms' => [ 'field' => 'test_field', - 'order' => ['_term' => 'desc'], + 'order' => ['_key' => 'desc'], ], ]; From 3e5759f58bf86b9d6660e6f05d50ef62266f973f Mon Sep 17 00:00:00 2001 From: Haydar KULEKCI Date: Wed, 16 Feb 2022 11:04:18 +0300 Subject: [PATCH 04/15] Remove deprecated date histogram interval and changed as calendar_interval and fixed interval --- docs/Aggregation/Bucketing/DateHistogram.md | 6 +- .../Bucketing/DateHistogramAggregation.php | 75 ++++++++++++++++--- .../DateHistogramAggregationTest.php | 43 ++++++++++- 3 files changed, 109 insertions(+), 15 deletions(-) diff --git a/docs/Aggregation/Bucketing/DateHistogram.md b/docs/Aggregation/Bucketing/DateHistogram.md index d4a4736a..2d710c4d 100644 --- a/docs/Aggregation/Bucketing/DateHistogram.md +++ b/docs/Aggregation/Bucketing/DateHistogram.md @@ -13,7 +13,7 @@ Example of expressions for interval: `year`, `quarter`, `month`, `week`, `day`, "articles_over_time" : { "date_histogram" : { "field" : "date", - "interval" : "month" + "calendar_interval" : "month" } } } @@ -42,7 +42,7 @@ to provide a custom format to the results of the query: "articles_over_time" : { "date_histogram" : { "field" : "date", - "interval" : "1M", + "calendar_interval" : "1M", "format" : "yyyy-MM-dd" } } @@ -63,4 +63,4 @@ $search->addAggregation($dateHistogramAggregation); $queryArray = $search->toArray(); ``` -[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html \ No newline at end of file +[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html diff --git a/src/Aggregation/Bucketing/DateHistogramAggregation.php b/src/Aggregation/Bucketing/DateHistogramAggregation.php index 352b8167..55e18d16 100644 --- a/src/Aggregation/Bucketing/DateHistogramAggregation.php +++ b/src/Aggregation/Bucketing/DateHistogramAggregation.php @@ -28,6 +28,16 @@ class DateHistogramAggregation extends AbstractAggregation */ protected $interval; + /** + * @var string + */ + protected $calendarInterval; + + /** + * @var string + */ + protected $fixedInterval; + /** * @var string */ @@ -37,34 +47,76 @@ class DateHistogramAggregation extends AbstractAggregation * Inner aggregations container init. * * @param string $name - * @param string $field - * @param string $interval + * @param string|null $field + * @param string|null $interval + * @param string|null $format */ - public function __construct($name, $field = null, $interval = null, $format = null) + public function __construct($name, string $field = null, string $interval = null, string $format = null) { parent::__construct($name); $this->setField($field); - $this->setInterval($interval); + $this->setCalendarInterval($interval); $this->setFormat($format); } /** - * @return int + * @return string + * @deprecated use getCalendarInterval instead */ public function getInterval() { - return $this->interval; + return $this->calendarInterval; } /** * @param string $interval + * @deprecated use setCalendarInterval instead * * @return $this */ public function setInterval($interval) { - $this->interval = $interval; + $this->setCalendarInterval($interval); + + return $this; + } + + + /** + * @return string + */ + public function getFixedInterval() + { + return $this->fixedInterval; + } + + /** + * @param string $interval + * @return $this + */ + public function setFixedInterval($interval) + { + $this->fixedInterval = $interval; + + return $this; + } + + /** + * @return string + */ + public function getCalendarInterval() + { + return $this->calendarInterval; + } + + /** + * @param string $interval + * @return $this + */ + public function setCalendarInterval($interval) + { + $this->calendarInterval = $interval; return $this; } @@ -94,15 +146,20 @@ public function getType() */ public function getArray() { - if (!$this->getField() || !$this->getInterval()) { + if (!$this->getField() || !($this->getCalendarInterval() || $this->getFixedInterval())) { throw new \LogicException('Date histogram aggregation must have field and interval set.'); } $out = [ 'field' => $this->getField(), - 'interval' => $this->getInterval(), ]; + if ($this->getCalendarInterval()) { + $out['calendar_interval'] = $this->getCalendarInterval(); + } else if ($this->getFixedInterval()) { + $out['fixed_interval'] = $this->getFixedInterval(); + } + if (!empty($this->format)) { $out['format'] = $this->format; } diff --git a/tests/Unit/Aggregation/Bucketing/DateHistogramAggregationTest.php b/tests/Unit/Aggregation/Bucketing/DateHistogramAggregationTest.php index 0a48a869..b9d7088f 100644 --- a/tests/Unit/Aggregation/Bucketing/DateHistogramAggregationTest.php +++ b/tests/Unit/Aggregation/Bucketing/DateHistogramAggregationTest.php @@ -12,6 +12,7 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\Bucketing\Aggregation; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\DateHistogramAggregation; +use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; /** * Unit test for children aggregation. @@ -28,6 +29,16 @@ public function testGetArrayException() $aggregation->getArray(); } + /** + * Tests if ChildrenAggregation#getArray throws exception when expected. + */ + public function testGetArrayExceptionWhenDontSendInterval() + { + $this->expectException(\LogicException::class); + $aggregation = new DateHistogramAggregation('foo', 'date'); + $aggregation->getArray(); + } + /** * Tests getType method. */ @@ -43,15 +54,41 @@ public function testDateHistogramAggregationGetType() */ public function testChildrenAggregationGetArray() { - $mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation') + $mock = $this->getMockBuilder(AbstractAggregation::class) ->disableOriginalConstructor() ->getMockForAbstractClass(); $aggregation = new DateHistogramAggregation('foo'); $aggregation->addAggregation($mock); $aggregation->setField('date'); - $aggregation->setInterval('month'); + $aggregation->setCalendarInterval('month'); + $result = $aggregation->getArray(); + $expected = ['field' => 'date', 'calendar_interval' => 'month']; + $this->assertEquals($expected, $result); + } + + /** + * Tests getArray method. + */ + public function testCalendarIntervalGetArray() + { + $aggregation = new DateHistogramAggregation('foo'); + $aggregation->setField('date'); + $aggregation->setCalendarInterval('month'); + $result = $aggregation->getArray(); + $expected = ['field' => 'date', 'calendar_interval' => 'month']; + $this->assertEquals($expected, $result); + } + + /** + * Tests getArray method. + */ + public function testFixedIntervalGetArray() + { + $aggregation = new DateHistogramAggregation('foo'); + $aggregation->setField('date'); + $aggregation->setFixedInterval('month'); $result = $aggregation->getArray(); - $expected = ['field' => 'date', 'interval' => 'month']; + $expected = ['field' => 'date', 'fixed_interval' => 'month']; $this->assertEquals($expected, $result); } } From fce85323d2442e7f95c47442ae2ae738fc638b5e Mon Sep 17 00:00:00 2001 From: Haydar KULEKCI Date: Wed, 16 Feb 2022 11:10:01 +0300 Subject: [PATCH 05/15] php 7.1 support droped and the version increased to ^7.4 --- composer.json | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index a4c2de8d..75046b79 100644 --- a/composer.json +++ b/composer.json @@ -12,10 +12,14 @@ { "name": "Handcrafted in the Alps Team", "homepage": "https://github.com/handcraftedinthealps/ElasticsearchDSL/graphs/contributors" + }, + { + "name": "Haydar KULEKCI", + "homepage": "https://github.com/hkulekci/ElasticsearchDSL/graphs/contributors" } ], "require": { - "php": "^7.1|^8.0", + "php": "^7.4|^8.0", "symfony/serializer": "^2.8 || ^3.4 || ^4.0 || ^5.0", "elasticsearch/elasticsearch": "^8.0" }, From c17732445f1f6e642788618f3901789b3794c99b Mon Sep 17 00:00:00 2001 From: Haydar KULEKCI Date: Wed, 16 Feb 2022 11:31:48 +0300 Subject: [PATCH 06/15] php 7.1, 7.2 and 7.3 remove d from the github ci --- .github/workflows/test-application.yaml | 12 ------------ README.md | 2 +- 2 files changed, 1 insertion(+), 13 deletions(-) diff --git a/.github/workflows/test-application.yaml b/.github/workflows/test-application.yaml index 028ad1ea..ea414f8a 100644 --- a/.github/workflows/test-application.yaml +++ b/.github/workflows/test-application.yaml @@ -16,18 +16,6 @@ jobs: fail-fast: false matrix: include: - - php-version: '7.1' - lint: false - symfony-version: '^2.8' - - - php-version: '7.2' - lint: false - symfony-version: '^3.4' - - - php-version: '7.3' - lint: false - symfony-version: '^4.4' - - php-version: '7.4' lint: true symfony-version: '^5.0' diff --git a/README.md b/README.md index 009b957d..4549516a 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ With some basic changes to support wider range of Symfony Versions. | Version | Supported Elasticsearch Version | Supported Symfony Version | |---------|---------------------------------|---------------------------| -| 8.x | ^8.0 | ^5.0, ^4.0, ^3.4, ^2.8 | +| 8.x | ^8.0 | ^5.0, | | 7.x | ^7.0 | ^5.0, ^4.0, ^3.4, ^2.8 | | 6.x | ^6.0 | ^5.0, ^4.0, ^3.4, ^2.8 | | 5.x | ^5.0 | ^5.0, ^4.0, ^3.4, ^2.8 | From 08883b44d60228ed84bf78a5ad7420c31a6f02d3 Mon Sep 17 00:00:00 2001 From: Haydar KULEKCI Date: Wed, 16 Feb 2022 22:31:12 +0300 Subject: [PATCH 07/15] phpcs fix for the changes --- src/Aggregation/Bucketing/DateHistogramAggregation.php | 2 +- src/Aggregation/Metric/PercentilesAggregation.php | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Aggregation/Bucketing/DateHistogramAggregation.php b/src/Aggregation/Bucketing/DateHistogramAggregation.php index 55e18d16..854b79ba 100644 --- a/src/Aggregation/Bucketing/DateHistogramAggregation.php +++ b/src/Aggregation/Bucketing/DateHistogramAggregation.php @@ -156,7 +156,7 @@ public function getArray() if ($this->getCalendarInterval()) { $out['calendar_interval'] = $this->getCalendarInterval(); - } else if ($this->getFixedInterval()) { + } elseif ($this->getFixedInterval()) { $out['fixed_interval'] = $this->getFixedInterval(); } diff --git a/src/Aggregation/Metric/PercentilesAggregation.php b/src/Aggregation/Metric/PercentilesAggregation.php index d7aabdae..90297e8c 100644 --- a/src/Aggregation/Metric/PercentilesAggregation.php +++ b/src/Aggregation/Metric/PercentilesAggregation.php @@ -44,8 +44,13 @@ class PercentilesAggregation extends AbstractAggregation * @param string|null $script * @param int|null $compression */ - public function __construct(string $name, string $field = null, array $percents = null, string $script = null, int $compression = null) - { + public function __construct( + string $name, + string $field = null, + array $percents = null, + string $script = null, + int $compression = null + ) { parent::__construct($name); $this->setField($field); From 3fba68bc0c52c1609822cc6a89bc31ae98a47f63 Mon Sep 17 00:00:00 2001 From: Haydar KULEKCI Date: Thu, 17 Feb 2022 17:14:40 +0300 Subject: [PATCH 08/15] phpunit upgraged to 9.x --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 75046b79..8752c0a9 100644 --- a/composer.json +++ b/composer.json @@ -19,12 +19,12 @@ } ], "require": { - "php": "^7.4|^8.0", + "php": "^7.4 || ^8.0", "symfony/serializer": "^2.8 || ^3.4 || ^4.0 || ^5.0", "elasticsearch/elasticsearch": "^8.0" }, "require-dev": { - "phpunit/phpunit": "^5.7.26 || ^7.5.20 || ^8.0", + "phpunit/phpunit": "^5.7.26 || ^7.5.20 || ^8.0 || ^9.0", "squizlabs/php_codesniffer": "^2.0 || ^3.0" }, "suggest": { From 19b05eb07937a6f8561a14f1d40345dd05ee949b Mon Sep 17 00:00:00 2001 From: Haydar KULEKCI Date: Sun, 20 Feb 2022 08:04:23 +0300 Subject: [PATCH 09/15] ::class used instead of text --- .../Unit/SearchEndpoint/AggregationsEndpointTest.php | 4 ++-- tests/Unit/SearchEndpoint/HighlightEndpointTest.php | 2 +- tests/Unit/SearchEndpoint/InnerHitsEndpointTest.php | 12 +++++++----- tests/Unit/SearchEndpoint/PostFilterEndpointTest.php | 6 +++--- tests/Unit/SearchEndpoint/QueryEndpointTest.php | 6 +++--- .../SearchEndpoint/SearchEndpointFactoryTest.php | 2 +- tests/Unit/SearchEndpoint/SortEndpointTest.php | 6 +++--- tests/Unit/SearchEndpoint/SuggestEndpointTest.php | 6 +++--- tests/Unit/SearchTest.php | 2 +- 9 files changed, 24 insertions(+), 22 deletions(-) diff --git a/tests/Unit/SearchEndpoint/AggregationsEndpointTest.php b/tests/Unit/SearchEndpoint/AggregationsEndpointTest.php index 016c1ff7..1ed46094 100644 --- a/tests/Unit/SearchEndpoint/AggregationsEndpointTest.php +++ b/tests/Unit/SearchEndpoint/AggregationsEndpointTest.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace ONGR\ElasticsearchDSL\Tests\Unit\Unit\SearchEndpoint; +namespace ONGR\ElasticsearchDSL\Tests\Unit\SearchEndpoint; use ONGR\ElasticsearchDSL\Aggregation\Bucketing\MissingAggregation; use ONGR\ElasticsearchDSL\SearchEndpoint\AggregationsEndpoint; @@ -25,7 +25,7 @@ class AggregationsEndpointTest extends \PHPUnit\Framework\TestCase public function testItCanBeInstantiated() { $this->assertInstanceOf( - 'ONGR\ElasticsearchDSL\SearchEndpoint\AggregationsEndpoint', + AggregationsEndpoint::class, new AggregationsEndpoint() ); } diff --git a/tests/Unit/SearchEndpoint/HighlightEndpointTest.php b/tests/Unit/SearchEndpoint/HighlightEndpointTest.php index 55d6080f..45a48bc7 100644 --- a/tests/Unit/SearchEndpoint/HighlightEndpointTest.php +++ b/tests/Unit/SearchEndpoint/HighlightEndpointTest.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace ONGR\ElasticsearchDSL\Tests\Unit\Unit\SearchEndpoint; +namespace ONGR\ElasticsearchDSL\Tests\Unit\SearchEndpoint; use ONGR\ElasticsearchDSL\Highlight\Highlight; use ONGR\ElasticsearchDSL\SearchEndpoint\HighlightEndpoint; diff --git a/tests/Unit/SearchEndpoint/InnerHitsEndpointTest.php b/tests/Unit/SearchEndpoint/InnerHitsEndpointTest.php index 26e2a7a3..d61f4257 100644 --- a/tests/Unit/SearchEndpoint/InnerHitsEndpointTest.php +++ b/tests/Unit/SearchEndpoint/InnerHitsEndpointTest.php @@ -9,9 +9,11 @@ * file that was distributed with this source code. */ -namespace ONGR\ElasticsearchDSL\Tests\Unit\Unit\SearchEndpoint; +namespace ONGR\ElasticsearchDSL\Tests\Unit\SearchEndpoint; use ONGR\ElasticsearchDSL\SearchEndpoint\InnerHitsEndpoint; +use ONGR\ElasticsearchDSL\BuilderInterface; +use Symfony\Component\Serializer\Normalizer\NormalizerInterface; /** * Class AggregationsEndpointTest. @@ -24,7 +26,7 @@ class InnerHitsEndpointTest extends \PHPUnit\Framework\TestCase public function testItCanBeInstantiated() { $this->assertInstanceOf( - 'ONGR\ElasticsearchDSL\SearchEndpoint\InnerHitsEndpoint', + InnerHitsEndpoint::class, new InnerHitsEndpoint() ); } @@ -35,7 +37,7 @@ public function testItCanBeInstantiated() public function testEndpointGetter() { $hitName = 'foo'; - $innerHit = $this->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface')->getMock(); + $innerHit = $this->getMockBuilder(BuilderInterface::class)->getMock(); $endpoint = new InnerHitsEndpoint(); $endpoint->add($innerHit, $hitName); $builders = $endpoint->getAll(); @@ -50,10 +52,10 @@ public function testEndpointGetter() public function testNormalization() { $normalizer = $this - ->getMockBuilder('Symfony\Component\Serializer\Normalizer\NormalizerInterface') + ->getMockBuilder(NormalizerInterface::class) ->getMock(); $innerHit = $this - ->getMockBuilder('ONGR\ElasticsearchDSL\BuilderInterface') + ->getMockBuilder(BuilderInterface::class) ->setMethods(['getName', 'toArray', 'getType']) ->getMock(); $innerHit->expects($this->any())->method('getName')->willReturn('foo'); diff --git a/tests/Unit/SearchEndpoint/PostFilterEndpointTest.php b/tests/Unit/SearchEndpoint/PostFilterEndpointTest.php index 36adfc3e..3234e14f 100644 --- a/tests/Unit/SearchEndpoint/PostFilterEndpointTest.php +++ b/tests/Unit/SearchEndpoint/PostFilterEndpointTest.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace ONGR\ElasticsearchDSL\Tests\Unit\Unit\SearchEndpoint; +namespace ONGR\ElasticsearchDSL\Tests\Unit\SearchEndpoint; use ONGR\ElasticsearchDSL\Query\MatchAllQuery; use ONGR\ElasticsearchDSL\SearchEndpoint\PostFilterEndpoint; @@ -26,7 +26,7 @@ class PostFilterEndpointTest extends \PHPUnit\Framework\TestCase */ public function testItCanBeInstantiated() { - $this->assertInstanceOf('ONGR\ElasticsearchDSL\SearchEndpoint\PostFilterEndpoint', new PostFilterEndpoint()); + $this->assertInstanceOf(PostFilterEndpoint::class, new PostFilterEndpoint()); } /** @@ -46,7 +46,7 @@ public function testNormalization() $instance = new PostFilterEndpoint(); /** @var NormalizerInterface|MockObject $normalizerInterface */ $normalizerInterface = $this->getMockForAbstractClass( - 'Symfony\Component\Serializer\Normalizer\NormalizerInterface' + NormalizerInterface::class ); $this->assertNull($instance->normalize($normalizerInterface)); diff --git a/tests/Unit/SearchEndpoint/QueryEndpointTest.php b/tests/Unit/SearchEndpoint/QueryEndpointTest.php index 5d28e152..7e5e73e8 100644 --- a/tests/Unit/SearchEndpoint/QueryEndpointTest.php +++ b/tests/Unit/SearchEndpoint/QueryEndpointTest.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace ONGR\ElasticsearchDSL\Tests\Unit\Unit\SearchEndpoint; +namespace ONGR\ElasticsearchDSL\Tests\Unit\SearchEndpoint; use ONGR\ElasticsearchDSL\Query\MatchAllQuery; use ONGR\ElasticsearchDSL\SearchEndpoint\QueryEndpoint; @@ -26,7 +26,7 @@ class QueryEndpointTest extends \PHPUnit\Framework\TestCase */ public function testItCanBeInstantiated() { - $this->assertInstanceOf('ONGR\ElasticsearchDSL\SearchEndpoint\QueryEndpoint', new QueryEndpoint()); + $this->assertInstanceOf(QueryEndpoint::class, new QueryEndpoint()); } /** @@ -46,7 +46,7 @@ public function testEndpoint() $instance = new QueryEndpoint(); /** @var NormalizerInterface|MockObject $normalizerInterface */ $normalizerInterface = $this->getMockForAbstractClass( - 'Symfony\Component\Serializer\Normalizer\NormalizerInterface' + NormalizerInterface::class ); $this->assertNull($instance->normalize($normalizerInterface)); diff --git a/tests/Unit/SearchEndpoint/SearchEndpointFactoryTest.php b/tests/Unit/SearchEndpoint/SearchEndpointFactoryTest.php index 63716a22..c928f19c 100644 --- a/tests/Unit/SearchEndpoint/SearchEndpointFactoryTest.php +++ b/tests/Unit/SearchEndpoint/SearchEndpointFactoryTest.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace ONGR\ElasticsearchDSL\Tests\Unit\Unit\SearchEndpoint; +namespace ONGR\ElasticsearchDSL\Tests\Unit\SearchEndpoint; use ONGR\ElasticsearchDSL\SearchEndpoint\AggregationsEndpoint; use ONGR\ElasticsearchDSL\SearchEndpoint\SearchEndpointFactory; diff --git a/tests/Unit/SearchEndpoint/SortEndpointTest.php b/tests/Unit/SearchEndpoint/SortEndpointTest.php index 7f825bcf..9397ead8 100644 --- a/tests/Unit/SearchEndpoint/SortEndpointTest.php +++ b/tests/Unit/SearchEndpoint/SortEndpointTest.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace ONGR\ElasticsearchDSL\Tests\Unit\Unit\SearchEndpoint; +namespace ONGR\ElasticsearchDSL\Tests\Unit\SearchEndpoint; use ONGR\ElasticsearchDSL\SearchEndpoint\SortEndpoint; use ONGR\ElasticsearchDSL\Sort\FieldSort; @@ -26,7 +26,7 @@ class SortEndpointTest extends \PHPUnit\Framework\TestCase */ public function testItCanBeInstantiated() { - $this->assertInstanceOf('ONGR\ElasticsearchDSL\SearchEndpoint\SortEndpoint', new SortEndpoint()); + $this->assertInstanceOf(SortEndpoint::class, new SortEndpoint()); } /** @@ -38,7 +38,7 @@ public function testNormalize() /** @var NormalizerInterface|MockObject $normalizerInterface */ $normalizerInterface = $this->getMockForAbstractClass( - 'Symfony\Component\Serializer\Normalizer\NormalizerInterface' + NormalizerInterface::class ); $sort = new FieldSort('acme', ['order' => FieldSort::ASC]); diff --git a/tests/Unit/SearchEndpoint/SuggestEndpointTest.php b/tests/Unit/SearchEndpoint/SuggestEndpointTest.php index 34c8704b..6c1bc5de 100644 --- a/tests/Unit/SearchEndpoint/SuggestEndpointTest.php +++ b/tests/Unit/SearchEndpoint/SuggestEndpointTest.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace ONGR\ElasticsearchDSL\Tests\Unit\Unit\SearchEndpoint; +namespace ONGR\ElasticsearchDSL\Tests\Unit\SearchEndpoint; use ONGR\ElasticsearchDSL\SearchEndpoint\SuggestEndpoint; use ONGR\ElasticsearchDSL\Suggest\Suggest; @@ -23,7 +23,7 @@ class SuggestEndpointTest extends \PHPUnit\Framework\TestCase */ public function testItCanBeInstantiated() { - $this->assertInstanceOf('ONGR\ElasticsearchDSL\SearchEndpoint\SuggestEndpoint', new SuggestEndpoint()); + $this->assertInstanceOf(SuggestEndpoint::class, new SuggestEndpoint()); } /** @@ -51,7 +51,7 @@ public function testNormalize() /** @var NormalizerInterface|MockObject $normalizerInterface */ $normalizerInterface = $this->getMockForAbstractClass( - 'Symfony\Component\Serializer\Normalizer\NormalizerInterface' + NormalizerInterface::class ); $suggest = new Suggest('foo', 'bar', 'acme', 'foo'); diff --git a/tests/Unit/SearchTest.php b/tests/Unit/SearchTest.php index bfb2f4d3..3454a8e8 100644 --- a/tests/Unit/SearchTest.php +++ b/tests/Unit/SearchTest.php @@ -27,7 +27,7 @@ class SearchTest extends \PHPUnit\Framework\TestCase */ public function testItCanBeInstantiated() { - $this->assertInstanceOf('ONGR\ElasticsearchDSL\Search', new Search()); + $this->assertInstanceOf(Search::class, new Search()); } public function testScrollUriParameter() From 6d9a3e2f641384cc7f8686b37a389841e9cd056a Mon Sep 17 00:00:00 2001 From: Haydar KULEKCI Date: Sun, 20 Feb 2022 08:31:04 +0300 Subject: [PATCH 10/15] Removes typed endpoint from search on tests --- src/BuilderBag.php | 6 +++--- tests/Functional/AbstractElasticsearchTestCase.php | 2 -- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/BuilderBag.php b/src/BuilderBag.php index 997667fc..eb79fe2b 100644 --- a/src/BuilderBag.php +++ b/src/BuilderBag.php @@ -100,13 +100,13 @@ public function get($name) * * @return BuilderInterface[] */ - public function all($type = null) + public function all(string $type = null) { return array_filter( $this->bag, /** @var BuilderInterface $builder */ - function (BuilderInterface $builder) use ($type) { - return $type === null || $builder->getType() == $type; + static function (BuilderInterface $builder) use ($type) { + return $type === null || $builder->getType() === $type; } ); } diff --git a/tests/Functional/AbstractElasticsearchTestCase.php b/tests/Functional/AbstractElasticsearchTestCase.php index 4765f7c6..42d3f765 100644 --- a/tests/Functional/AbstractElasticsearchTestCase.php +++ b/tests/Functional/AbstractElasticsearchTestCase.php @@ -54,7 +54,6 @@ protected function setUp(): void $bulkBody[] = [ 'index' => [ '_index' => self::INDEX_NAME, - '_type' => $type, '_id' => $id, ] ]; @@ -128,7 +127,6 @@ protected function executeSearch(Search $search, $type = null, $returnRaw = fals $response = $this->client->search( array_filter([ 'index' => self::INDEX_NAME, - 'type' => $type, 'body' => $search->toArray(), ]) ); From 41b790a67472270b641a09de9c25afc90d174f3f Mon Sep 17 00:00:00 2001 From: Haydar KULEKCI Date: Sun, 20 Feb 2022 11:49:10 +0300 Subject: [PATCH 11/15] cutoff_frequency sample removed for CommonTerms query --- docs/Query/FullText/CommonTerms.md | 2 -- tests/Unit/Query/FullText/CommonTermsQueryTest.php | 12 ++++++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/docs/Query/FullText/CommonTerms.md b/docs/Query/FullText/CommonTerms.md index a2307a3a..fcfeb349 100644 --- a/docs/Query/FullText/CommonTerms.md +++ b/docs/Query/FullText/CommonTerms.md @@ -11,7 +11,6 @@ Lets take first example to write easy `Common query` with Elasticsearch DSL. "common": { "name": { "query": "this is bonsai cool", - "cutoff_frequency": 0.001, "minimum_should_match": { "low_freq" : 2, "high_freq" : 3 @@ -28,7 +27,6 @@ $commonTermsQuery = new CommonTermsQuery( "field_name", "this is bonsai cool", [ - "cutoff_frequency" => 0.001, "minimum_should_match" => [ "low_freq" => 2, "high_freq" => 3, diff --git a/tests/Unit/Query/FullText/CommonTermsQueryTest.php b/tests/Unit/Query/FullText/CommonTermsQueryTest.php index 8a387132..8689249e 100644 --- a/tests/Unit/Query/FullText/CommonTermsQueryTest.php +++ b/tests/Unit/Query/FullText/CommonTermsQueryTest.php @@ -20,12 +20,20 @@ class CommonTermsQueryTest extends \PHPUnit\Framework\TestCase */ public function testToArray() { - $query = new CommonTermsQuery('body', 'this is bonsai cool', ['cutoff_frequency' => 0.01]); + $query = new CommonTermsQuery('body', 'this is bonsai cool', [ + "minimum_should_match" => [ + "low_freq" => 2, + "high_freq" => 3 + ] + ]); $expected = [ 'common' => [ 'body' => [ 'query' => 'this is bonsai cool', - 'cutoff_frequency' => 0.01, + "minimum_should_match" => [ + "low_freq" => 2, + "high_freq" => 3 + ] ], ], ]; From edc8829ba8cb025fd6560713a08b284b758624d6 Mon Sep 17 00:00:00 2001 From: Haydar KULEKCI Date: Sun, 20 Feb 2022 13:06:31 +0300 Subject: [PATCH 12/15] Remove CommonTermsQuery and cutoff_frequency params --- docs/Query/FullText/CommonTerms.md | 44 ------------ src/Query/FullText/CommonTermsQuery.php | 71 ------------------- .../Query/FullText/CommonTermsQueryTest.php | 43 ----------- 3 files changed, 158 deletions(-) delete mode 100644 docs/Query/FullText/CommonTerms.md delete mode 100644 src/Query/FullText/CommonTermsQuery.php delete mode 100644 tests/Unit/Query/FullText/CommonTermsQueryTest.php diff --git a/docs/Query/FullText/CommonTerms.md b/docs/Query/FullText/CommonTerms.md deleted file mode 100644 index fcfeb349..00000000 --- a/docs/Query/FullText/CommonTerms.md +++ /dev/null @@ -1,44 +0,0 @@ -# Common terms query - -> More info about Common terms query is in the [official elasticsearch docs][1] - -There are so many use cases with `Common Terms` query. We highly recommend to take a look at the [official docs][1] before continuing. - -Lets take first example to write easy `Common query` with Elasticsearch DSL. - -```JSON -{ - "common": { - "name": { - "query": "this is bonsai cool", - "minimum_should_match": { - "low_freq" : 2, - "high_freq" : 3 - } - } - } -} -``` - -And now the query via DSL: - -```php -$commonTermsQuery = new CommonTermsQuery( - "field_name", - "this is bonsai cool", - [ - "minimum_should_match" => [ - "low_freq" => 2, - "high_freq" => 3, - ], - ] -); - -$search = new Search(); -$search->addQuery($commonTermsQuery); - -$queryArray = $search->toArray(); -``` - - -[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-common-terms-query.html diff --git a/src/Query/FullText/CommonTermsQuery.php b/src/Query/FullText/CommonTermsQuery.php deleted file mode 100644 index 2e31b13b..00000000 --- a/src/Query/FullText/CommonTermsQuery.php +++ /dev/null @@ -1,71 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace ONGR\ElasticsearchDSL\Query\FullText; - -use ONGR\ElasticsearchDSL\BuilderInterface; -use ONGR\ElasticsearchDSL\ParametersTrait; - -/** - * Represents Elasticsearch "common" query. - * - * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-common-terms-query.html - */ -class CommonTermsQuery implements BuilderInterface -{ - use ParametersTrait; - - /** - * @var string - */ - private $field; - - /** - * @var string - */ - private $query; - - /** - * @param string $field - * @param string $query - * @param array $parameters - */ - public function __construct($field, $query, array $parameters = []) - { - $this->field = $field; - $this->query = $query; - $this->setParameters($parameters); - } - - /** - * {@inheritdoc} - */ - public function getType() - { - return 'common'; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - $query = [ - 'query' => $this->query, - ]; - - $output = [ - $this->field => $this->processArray($query), - ]; - - return [$this->getType() => $output]; - } -} diff --git a/tests/Unit/Query/FullText/CommonTermsQueryTest.php b/tests/Unit/Query/FullText/CommonTermsQueryTest.php deleted file mode 100644 index 8689249e..00000000 --- a/tests/Unit/Query/FullText/CommonTermsQueryTest.php +++ /dev/null @@ -1,43 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\FullText; - -use ONGR\ElasticsearchDSL\Query\FullText\CommonTermsQuery; - -class CommonTermsQueryTest extends \PHPUnit\Framework\TestCase -{ - /** - * Tests toArray(). - */ - public function testToArray() - { - $query = new CommonTermsQuery('body', 'this is bonsai cool', [ - "minimum_should_match" => [ - "low_freq" => 2, - "high_freq" => 3 - ] - ]); - $expected = [ - 'common' => [ - 'body' => [ - 'query' => 'this is bonsai cool', - "minimum_should_match" => [ - "low_freq" => 2, - "high_freq" => 3 - ] - ], - ], - ]; - - $this->assertEquals($expected, $query->toArray()); - } -} From 46c8b52e928f973aa3d1e81ab6ddd5b25d67fbce Mon Sep 17 00:00:00 2001 From: Haydar KULEKCI Date: Sun, 20 Feb 2022 18:10:04 +0300 Subject: [PATCH 13/15] functional test for date histogram --- .../AbstractElasticsearchTestCase.php | 3 +- .../DateHistogramAggregationTest.php | 82 +++++++++++++++++++ 2 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 tests/Functional/Aggregation/DateHistogramAggregationTest.php diff --git a/tests/Functional/AbstractElasticsearchTestCase.php b/tests/Functional/AbstractElasticsearchTestCase.php index 42d3f765..2f544485 100644 --- a/tests/Functional/AbstractElasticsearchTestCase.php +++ b/tests/Functional/AbstractElasticsearchTestCase.php @@ -118,11 +118,10 @@ protected function tearDown(): void * Execute search to the elasticsearch and handle results. * * @param Search $search Search object. - * @param null $type Types to search. Can be several types split by comma. * @param bool $returnRaw Return raw response from the client. * @return array */ - protected function executeSearch(Search $search, $type = null, $returnRaw = false) + protected function executeSearch(Search $search, bool $returnRaw = false): array { $response = $this->client->search( array_filter([ diff --git a/tests/Functional/Aggregation/DateHistogramAggregationTest.php b/tests/Functional/Aggregation/DateHistogramAggregationTest.php new file mode 100644 index 00000000..2f36df66 --- /dev/null +++ b/tests/Functional/Aggregation/DateHistogramAggregationTest.php @@ -0,0 +1,82 @@ + + */ + +namespace ONGR\ElasticsearchDSL\Tests\Functional\Aggregation; + +use ONGR\ElasticsearchDSL\Aggregation\Bucketing\DateHistogramAggregation; +use ONGR\ElasticsearchDSL\Search; +use ONGR\ElasticsearchDSL\Tests\Functional\AbstractElasticsearchTestCase; + +class DateHistogramAggregationTest extends AbstractElasticsearchTestCase +{ + /** + * {@inheritdoc} + */ + protected function getDataArray(): array + { + return [ + 'products' => [ + [ + 'title' => 'acme', + 'price' => 10, + 'created_at' => '2022-01-01T00:02:00Z', + ], + [ + 'title' => 'foo', + 'price' => 20, + 'created_at' => '2022-01-01T00:01:00Z', + ], + [ + 'title' => 'bar', + 'price' => 10, + 'created_at' => '2022-01-01T00:03:00Z', + ], + ] + ]; + } + + /** + * Match all test + */ + public function testDateHistogramWithMinuteCalendarInterval(): void + { + $histogram = new DateHistogramAggregation('dates', 'created_at'); + $histogram->setCalendarInterval('minute'); + + $search = new Search(); + $search->addAggregation($histogram); + $results = $this->executeSearch($search, true); + $this->assertCount(count($this->getDataArray()['products']), $results['aggregations']['dates']['buckets']); + } + + /** + * Match all test + */ + public function testDateHistogramWithMonthCalendarInterval(): void + { + $histogram = new DateHistogramAggregation('dates', 'created_at'); + $histogram->setCalendarInterval('month'); + + $search = new Search(); + $search->addAggregation($histogram); + $results = $this->executeSearch($search, true); + $this->assertCount(1, $results['aggregations']['dates']['buckets']); + } + + /** + * Match all test + */ + public function testDateHistogramWitMinuteFixedInterval(): void + { + $histogram = new DateHistogramAggregation('dates', 'created_at'); + $histogram->setFixedInterval('2m'); + + $search = new Search(); + $search->addAggregation($histogram); + $results = $this->executeSearch($search, true); + $this->assertCount(2, $results['aggregations']['dates']['buckets']); + } +} From b3274226affe48b351eb21a9e0516353ee273a30 Mon Sep 17 00:00:00 2001 From: Haydar KULEKCI Date: Sun, 20 Feb 2022 18:10:53 +0300 Subject: [PATCH 14/15] elasticsearch version increased for the test node --- .github/workflows/test-application.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test-application.yaml b/.github/workflows/test-application.yaml index ea414f8a..55ed4e62 100644 --- a/.github/workflows/test-application.yaml +++ b/.github/workflows/test-application.yaml @@ -26,7 +26,7 @@ jobs: services: elasticsearch: - image: elasticsearch:7.5.2 + image: elasticsearch:8.0.0 ports: - 9200:9200 env: From e3464675e5ec8de1c352bd1577162124d28c732b Mon Sep 17 00:00:00 2001 From: Haydar KULEKCI Date: Sun, 20 Feb 2022 18:30:02 +0300 Subject: [PATCH 15/15] removed e_user_deprecated exceptions for GeoShape query --- src/Query/Geo/GeoShapeQuery.php | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/src/Query/Geo/GeoShapeQuery.php b/src/Query/Geo/GeoShapeQuery.php index a2a5893a..3ded13ff 100644 --- a/src/Query/Geo/GeoShapeQuery.php +++ b/src/Query/Geo/GeoShapeQuery.php @@ -23,10 +23,10 @@ class GeoShapeQuery implements BuilderInterface { use ParametersTrait; - const INTERSECTS = 'intersects'; - const DISJOINT = 'disjoint'; - const WITHIN = 'within'; - const CONTAINS = 'contains'; + public const INTERSECTS = 'intersects'; + public const DISJOINT = 'disjoint'; + public const WITHIN = 'within'; + public const CONTAINS = 'contains'; /** * @var array @@ -60,13 +60,6 @@ public function getType() */ public function addShape($field, $type, array $coordinates, $relation = self::INTERSECTS, array $parameters = []) { - // TODO: remove this in the next major version - if (is_array($relation)) { - $parameters = $relation; - $relation = self::INTERSECTS; - trigger_error('$parameters as parameter 4 in addShape is deprecated', E_USER_DEPRECATED); - } - $filter = array_merge( $parameters, [ @@ -101,13 +94,6 @@ public function addPreIndexedShape( $relation = self::INTERSECTS, array $parameters = [] ) { - // TODO: remove this in the next major version - if (is_array($relation)) { - $parameters = $relation; - $relation = self::INTERSECTS; - trigger_error('$parameters as parameter 6 in addShape is deprecated', E_USER_DEPRECATED); - } - $filter = array_merge( $parameters, [