diff --git a/Aggregation/TopHitsAggregation.php b/Aggregation/TopHitsAggregation.php index 17ad36e9f17b4b2f973b6348929b0e2ab52fe859..125516c15d08a165541b8857381f2c21b88a8870 100644 --- a/Aggregation/TopHitsAggregation.php +++ b/Aggregation/TopHitsAggregation.php @@ -49,9 +49,7 @@ class TopHitsAggregation extends AbstractAggregation parent::__construct($name); $this->setFrom($from); $this->setSize($size); - if (!empty($sort)) { - $this->setSort($sort); - } + $this->setSort($sort); } /** @@ -129,18 +127,32 @@ class TopHitsAggregation extends AbstractAggregation { $data = new \stdClass(); - if ($this->getSort()) { - $data->sort = $this->getSort()->toArray(); - } - - if ($this->getSize()) { - $data->size = $this->getSize(); - } - - if ($this->getFrom()) { - $data->from = $this->getFrom(); + $filteredData = $this->getFilteredData(); + foreach ($filteredData as $key => $value) { + $data->{$key} = $value; } return $data; } + + /** + * Filters the data. + * + * @return array + */ + private function getFilteredData() + { + $fd = array_filter( + [ + 'sort' => $this->getSort() ? $this->getSort()->toArray() : [], + 'size' => $this->getSize(), + 'from' => $this->getFrom(), + ], + function ($val) { + return (($val || is_array($val) || ($val || is_numeric($val)))); + } + ); + + return $fd; + } }