Skip to content
Snippets Groups Projects
Commit ac9a2d0a authored by Simonas Šerlinskas's avatar Simonas Šerlinskas
Browse files

fixed top hits aggregation creation

parent 1d795a53
No related branches found
No related tags found
No related merge requests found
......@@ -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;
}
}
......@@ -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());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment