diff --git a/src/Aggregation/StatsAggregation.php b/src/Aggregation/StatsAggregation.php index 515e8a37876d3f5c14e441aa60c740023d21095b..425bea55cc22a61a7272206d9a7af79238ff3f48 100644 --- a/src/Aggregation/StatsAggregation.php +++ b/src/Aggregation/StatsAggregation.php @@ -22,6 +22,21 @@ class StatsAggregation extends AbstractAggregation use MetricTrait; use ScriptAwareTrait; + /** + * Inner aggregations container init. + * + * @param string $name + * @param string $field + * @param string $script + */ + public function __construct($name, $field = null, $script = null) + { + parent::__construct($name); + + $this->setField($field); + $this->setScript($script); + } + /** * {@inheritdoc} */ diff --git a/tests/Aggregation/StatsAggregationTest.php b/tests/Aggregation/StatsAggregationTest.php index 621e0cfc4dbf5c243600a57846a3545e3c77c7d3..f826b397310bbb89a08c4cfda0155737f420b8b3 100644 --- a/tests/Aggregation/StatsAggregationTest.php +++ b/tests/Aggregation/StatsAggregationTest.php @@ -31,4 +31,23 @@ class StatsAggregationTest extends \PHPUnit_Framework_TestCase $this->assertEquals($expectedResult, $aggregation->toArray()); } + + /** + * Tests if parameter can be passed to constructor. + */ + public function testConstructor() + { + $aggregation = new StatsAggregation('foo', 'fieldValue', 'scriptValue'); + $this->assertSame( + [ + 'agg_foo' => [ + 'stats' => [ + 'field' => 'fieldValue', + 'script' => 'scriptValue', + ], + ], + ], + $aggregation->toArray() + ); + } }