diff --git a/src/Aggregation/TopHitsAggregation.php b/src/Aggregation/TopHitsAggregation.php index 3ee30986c346887590ea1ad56f24d4a69e7e9e25..70a0ff1c090da0555ce939baec19c64ea0c76bb6 100644 --- a/src/Aggregation/TopHitsAggregation.php +++ b/src/Aggregation/TopHitsAggregation.php @@ -124,27 +124,10 @@ class TopHitsAggregation extends AbstractAggregation * {@inheritdoc} */ public function getArray() - { - $data = new \stdClass(); - - $filteredData = $this->getFilteredData(); - foreach ($filteredData as $key => $value) { - $data->{$key} = $value; - } - - return $data; - } - - /** - * Filters the data. - * - * @return array - */ - private function getFilteredData() { $output = array_filter( [ - 'sort' => $this->getSort() ? $this->getSort()->toArray() : [], + 'sort' => $this->getSort() ? $this->getSort()->toArray() : null, 'size' => $this->getSize(), 'from' => $this->getFrom(), ], @@ -153,6 +136,8 @@ class TopHitsAggregation extends AbstractAggregation } ); + $output = $this->processArray($output); + return $output; } } diff --git a/tests/Aggregation/TopHitsAggregationTest.php b/tests/Aggregation/TopHitsAggregationTest.php index cd63b502d2b0932580cc935b5e917ce02cb467e9..6e4ca2162666252bf2e72bb456b2ccd93a0f428d 100644 --- a/tests/Aggregation/TopHitsAggregationTest.php +++ b/tests/Aggregation/TopHitsAggregationTest.php @@ -26,18 +26,43 @@ class TopHitsAggregationTest extends \PHPUnit_Framework_TestCase public function testToArray() { $sort = new FieldSort('acme'); - $aggregation = new TopHitsAggregation('acme', 0, 1, $sort); + $aggregation = new TopHitsAggregation('acme', 1, 1, $sort); - $expectedAgg = new \stdClass(); - $expectedAgg->size = 0; - $expectedAgg->from = 1; - $expectedAgg->sort = $sort->toArray(); $expected = [ 'acme' => [ - 'top_hits' => $expectedAgg, + 'top_hits' => [ + 'sort' => [ + 'acme' => [], + ], + 'size' => 1, + 'from' => 1, + ], ], ]; - $this->assertEquals($expected, $aggregation->toArray()); + $this->assertSame($expected, $aggregation->toArray()); + } + + /** + * Check if parameters can be set to agg. + */ + public function testParametersAddition() + { + $aggregation = new TopHitsAggregation('acme', 0, 1); + $aggregation->addParameter('_source', ['include' => ['title']]); + + $expected = [ + 'acme' => [ + 'top_hits' => [ + 'size' => 0, + 'from' => 1, + '_source' => [ + 'include' => ['title'], + ] + ], + ], + ]; + + $this->assertSame($expected, $aggregation->toArray()); } }