diff --git a/src/Aggregation/TopHitsAggregation.php b/src/Aggregation/TopHitsAggregation.php index d315bcd1819a6a196cebd934962fbf5a49bf61a1..e88e99660e4f8a035c6089e38dd7c09e4c04cf72 100644 --- a/src/Aggregation/TopHitsAggregation.php +++ b/src/Aggregation/TopHitsAggregation.php @@ -125,15 +125,12 @@ class TopHitsAggregation extends AbstractAggregation */ public function getArray() { - $output = $this->getSort() ? $this->getSort()->toArray() : []; $output = array_filter( - array_merge( - $output, - [ - 'size' => $this->getSize(), - 'from' => $this->getFrom(), - ] - ), + [ + 'sort' => $this->getSort() ? $this->getSort()->toArray() : null, + 'size' => $this->getSize(), + 'from' => $this->getFrom(), + ], function ($val) { return (($val || is_array($val) || ($val || is_numeric($val)))); } diff --git a/src/Sort/FieldSort.php b/src/Sort/FieldSort.php index 2f3eeeb3090ac77e5384fb849d6a70386a7c99be..7389976b03b724f980699fe00201104918062e3e 100644 --- a/src/Sort/FieldSort.php +++ b/src/Sort/FieldSort.php @@ -103,6 +103,6 @@ class FieldSort implements BuilderInterface $this->field => empty($fieldValues) ? new \stdClass() : $fieldValues, ]; - return [$this->getType() => $output]; + return $output; } } diff --git a/tests/SearchTest.php b/tests/SearchTest.php index 23ed4c2d0e097a183597f2d5004f0edd17053409..17bebd58e5cbf30775462ba6e28c914022a165a3 100644 --- a/tests/SearchTest.php +++ b/tests/SearchTest.php @@ -14,6 +14,7 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\DSL; use ONGR\ElasticsearchDSL\Query\MissingQuery; use ONGR\ElasticsearchDSL\Query\TermQuery; use ONGR\ElasticsearchDSL\Search; +use ONGR\ElasticsearchDSL\Sort\FieldSort; /** * Test for Search. @@ -242,6 +243,19 @@ class SearchTest extends \PHPUnit_Framework_TestCase (new Search())->addQuery(new TermQuery('foo', 'bar'))->addFilter(new MissingQuery('baz')), ]; + $cases['sort_by_price'] = [ + [ + 'sort' => [ + [ + 'price' => [ + 'order' => 'asc', + ], + ], + ], + ], + (new Search())->addSort(new FieldSort('price', 'asc')), + ]; + return $cases; }