Skip to content
Snippets Groups Projects
Commit 722a655b authored by Simonas Šerlinskas's avatar Simonas Šerlinskas Committed by GitHub
Browse files

Merge pull request #129 from einorler/pipeline_min_bucket

Added min bucket pipeline aggregation
parents 13aeab92 bd1b80a1
No related branches found
No related tags found
No related merge requests found
# Min Bucket Aggregation
> More info about Min bucket aggregation is in the [official elasticsearch docs][1]
A sibling pipeline aggregation which identifies the bucket(s) with the minimum value of a
specified metric in a sibling aggregation and outputs both the value and the key(s) of the bucket(s).
The specified metric must be numeric and the sibling aggregation must be a multi-bucket aggregation.
## Simple example
```JSON
{
"aggs" : {
"sales_per_month" : {
"date_histogram" : {
"field" : "date",
"interval" : "month"
},
"aggs": {
"sales": {
"sum": {
"field": "price"
}
}
}
},
"min_monthly_sales": {
"min_bucket": {
"buckets_path": "sales_per_month>sales"
}
}
}
}
```
And now the query via DSL:
```php
$dateAggregation = new DateHistogramAggregation('sales_per_month', 'date', 'month');
$sumAggregation = new SumAggregation('sales', 'price');
$minBucketAggregation = new MinBucketAggregation('min_monthly_sales', 'sales_per_month>sales');
$dateAggregation->addAggregation($sumAggregation);
$search = new Search();
$search->addAggregation($dateAggregation);
$search->addAggregation($minBucketAggregation);
$queryArray = $search->toArray();
```
[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-min-bucket-aggregation.html
<?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;
/**
* Class representing Min Bucket Pipeline Aggregation.
*
* @link https://goo.gl/5oo4XH
*/
class MinBucketAggregation extends AbstractPipelineAggregation
{
/**
* {@inheritdoc}
*/
public function getType()
{
return 'min_bucket';
}
}
<?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\Aggregation\Pipeline;
use ONGR\ElasticsearchDSL\Aggregation\Pipeline\MinBucketAggregation;
/**
* Unit test for min bucket aggregation.
*/
class MinBucketAggregationTest extends \PHPUnit_Framework_TestCase
{
/**
* Tests toArray method.
*/
public function testToArray()
{
$aggregation = new MinBucketAggregation('acme', 'test');
$expected = [
'min_bucket' => [
'buckets_path' => 'test',
],
];
$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