From c137b2c7b0acdb3ce6290e7a2a264ff89e25e169 Mon Sep 17 00:00:00 2001 From: Romain Neutron Date: Mon, 23 Nov 2020 19:38:22 +0100 Subject: [PATCH] Fix Composite::addAfter(null) --- src/Aggregation/Composite.php | 4 ++++ tests/Aggregation/CompositeTest.php | 37 +++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/Aggregation/Composite.php b/src/Aggregation/Composite.php index 86e81707b1..cd0a328f60 100644 --- a/src/Aggregation/Composite.php +++ b/src/Aggregation/Composite.php @@ -25,6 +25,10 @@ public function addSource(AbstractAggregation $aggregation): self */ public function addAfter(?array $checkpoint): self { + if (null === $checkpoint) { + return $this; + } + return $this->setParam('after', $checkpoint); } } diff --git a/tests/Aggregation/CompositeTest.php b/tests/Aggregation/CompositeTest.php index 1d97e34f64..9bb86693f8 100644 --- a/tests/Aggregation/CompositeTest.php +++ b/tests/Aggregation/CompositeTest.php @@ -178,6 +178,43 @@ public function testCompositeWithAfterAggregation(): void $this->assertEquals($expected, $results); } + /** + * @group functional + */ + public function testCompositeWithNullAfter(): void + { + $composite = new Composite('products'); + $composite->setSize(2); + $composite->addSource((new Terms('color'))->setField('color.keyword')); + $composite->addAfter(null); + + $query = new Query(); + $query->addAggregation($composite); + + $results = $this->_getIndexForTest()->search($query)->getAggregation('products'); + $expected = [ + 'after_key' => [ + 'color' => 'green', + ], + 'buckets' => [ + [ + 'key' => [ + 'color' => 'blue', + ], + 'doc_count' => 2, + ], + [ + 'key' => [ + 'color' => 'green', + ], + 'doc_count' => 1, + ], + ], + ]; + + $this->assertEquals($expected, $results); + } + protected function _getIndexForTest(): Index { $index = $this->_createIndex();