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

add bucket sort aggregation

parent 1b2b4a44
No related branches found
No related tags found
No related merge requests found
<?php
/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ONGR\ElasticsearchDSL\Aggregation\Pipeline;
use ONGR\ElasticsearchDSL\BuilderInterface;
use ONGR\ElasticsearchDSL\Sort\FieldSort;
/**
* Class representing Bucket Script Pipeline Aggregation.
*
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-sort-aggregation.html
*/
class BucketSortAggregation extends AbstractPipelineAggregation
{
/**
* @var array
*/
private $sort = [];
/**
* @param string $name
* @param string $bucketsPath
*/
public function __construct($name, $bucketsPath = null)
{
parent::__construct($name, $bucketsPath);
}
/**
* @return array
*/
public function getSort()
{
return $this->sort;
}
/**
* @return self
*/
public function addSort(FieldSort $sort)
{
$this->sort[] = $sort->toArray();
}
/**
* @param string $sort
* @return self
*/
public function setSort($sort)
{
$this->sort = $sort;
return $this;
}
/**
* {@inheritdoc}
*/
public function getType()
{
return 'bucket_sort';
}
/**
* {@inheritdoc}
*/
public function getArray()
{
$out = array_filter(
[
'buckets_path' => $this->getBucketsPath(),
'sort' => $this->getSort(),
]
);
return $out;
}
}
<?php
/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation\Pipeline;
use ONGR\ElasticsearchDSL\Aggregation\Pipeline\BucketSortAggregation;
use ONGR\ElasticsearchDSL\Aggregation\Pipeline\MovingFunctionAggregation;
use ONGR\ElasticsearchDSL\Sort\FieldSort;
/**
* Unit test for the bucket sort aggregation.
*/
class BucketSortAggregationTest extends \PHPUnit\Framework\TestCase
{
/**
* Tests toArray method.
*/
public function testToArray()
{
$aggregation = new BucketSortAggregation('acme', 'test');
$expected = [
'bucket_sort' => [
'buckets_path' => 'test',
],
];
$this->assertEquals($expected, $aggregation->toArray());
$aggregation = new BucketSortAggregation('acme');
$expected = [
'bucket_sort' => [],
];
$this->assertEquals($expected, $aggregation->toArray());
$aggregation = new BucketSortAggregation('acme');
$sort = new FieldSort('test_field', FieldSort::ASC);
$aggregation->addSort($sort);
$expected = [
'bucket_sort' => [
'sort' => [
[
'test_field' => ['order' => 'asc'],
]
]
],
];
$this->assertEquals($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