diff --git a/src/Aggregation/Composite.php b/src/Aggregation/Composite.php index 1b39eece7c..86e81707b1 100644 --- a/src/Aggregation/Composite.php +++ b/src/Aggregation/Composite.php @@ -5,7 +5,6 @@ class Composite extends AbstractAggregation { /** - * @param int $size * @return $this */ public function setSize(int $size): self @@ -14,7 +13,6 @@ public function setSize(int $size): self } /** - * @param AbstractAggregation $aggregation * @return $this */ public function addSource(AbstractAggregation $aggregation): self @@ -23,7 +21,6 @@ public function addSource(AbstractAggregation $aggregation): self } /** - * @param array|null $checkpoint * @return $this */ public function addAfter(?array $checkpoint): self diff --git a/tests/Aggregation/CompositeTest.php b/tests/Aggregation/CompositeTest.php new file mode 100644 index 0000000000..9f5424dfcd --- /dev/null +++ b/tests/Aggregation/CompositeTest.php @@ -0,0 +1,131 @@ +setSize(200); + $this->assertEquals(200, $composite->getParam('size')); + + $expected = [ + 'composite' => [ + 'size' => 200, + ], + ]; + $this->assertEquals($expected, $composite->toArray()); + } + + /** + * @group unit + */ + public function testAddSource(): void + { + $expected = [ + 'composite' => [ + 'sources' => [ + [ + 'product' => [ + 'terms' => [ + 'field' => 'product_id', + ], + ], + ], + ], + ], + ]; + + $composite = new Composite('products'); + $composite->addSource((new Terms('product_id'))->setField('product_id')->setName('product')); + $this->assertEquals($expected, $composite->toArray()); + } + + /** + * @group unit + */ + public function testAddAfter(): void + { + $checkpoint = ['checkpointproduct' => 'checkpoint']; + $expected = [ + 'composite' => [ + 'after' => $checkpoint, + ], + ]; + + $composite = new Composite('products'); + $composite->addAfter($checkpoint); + $this->assertEquals($expected, $composite->toArray()); + } + + /** + * @group functional + */ + public function testCompositeNoAfterAggregation(): void + { + $composite = new Composite('products'); + $composite->setSize(5); + $composite->addSource((new Terms('color'))->setField('color.keyword')->setName('color')); + + $query = new Query(); + $query->addAggregation($composite); + + $results = $this->_getIndexForTest()->search($query)->getAggregation('products'); + $expected = [ + 'after_key' => [ + 'color' => 'red', + ], + 'buckets' => [ + [ + 'key' => [ + 'color' => 'blue', + ], + 'doc_count' => 2, + ], + [ + 'key' => [ + 'color' => 'green', + ], + 'doc_count' => 1, + ], + [ + 'key' => [ + 'color' => 'red', + ], + 'doc_count' => 1, + ], + ], + ]; + + $this->assertEquals($expected, $results); + } + + protected function _getIndexForTest(): Index + { + $index = $this->_createIndex(); + + $index->addDocuments([ + new Document(1, ['price' => 5, 'color' => 'blue']), + new Document(2, ['price' => 5, 'color' => 'blue']), + new Document(3, ['price' => 3, 'color' => 'red']), + new Document(4, ['price' => 3, 'color' => 'green']), + ]); + + $index->refresh(); + + return $index; + } +}