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

Merge pull request #289 from chyzas/patch-dsl-scripts-aggregations

Added more aggregations
parents 92c88b73 e092e9ac
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\ElasticsearchBundle\DSL\Aggregation;
/**
* Class representing Avg Aggregation.
*/
class AvgAggregation extends StatsAggregation
{
/**
* {@inheritdoc}
*/
public function getType()
{
return 'avg';
}
}
<?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\ElasticsearchBundle\DSL\Aggregation;
use ONGR\ElasticsearchBundle\DSL\Aggregation\Type\MetricTrait;
use ONGR\ElasticsearchBundle\DSL\ScriptAwareTrait;
/**
* Class representing Extended stats aggregation.
*/
class ExtendedStatsAggregation extends AbstractAggregation
{
use MetricTrait;
use ScriptAwareTrait;
/**
* @var int
*/
private $sigma;
/**
* @return int
*/
public function getSigma()
{
return $this->sigma;
}
/**
* @param int $sigma
*/
public function setSigma($sigma)
{
$this->sigma = $sigma;
}
/**
* {@inheritdoc}
*/
public function getType()
{
return 'extended_stats';
}
/**
* {@inheritdoc}
*/
public function getArray()
{
$out = [];
if ($this->getField()) {
$out['field'] = $this->getField();
} elseif ($this->getScript()) {
$out['script'] = $this->getScript();
} else {
throw new \LogicException('Extended stats aggregation must have field or script set.');
}
if ($this->getSigma()) {
$out['sigma'] = $this->getSigma();
}
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\ElasticsearchBundle\DSL\Aggregation;
/**
* Class representing Max Aggregation.
*/
class MaxAggregation extends StatsAggregation
{
/**
* {@inheritdoc}
*/
public function getType()
{
return 'max';
}
}
<?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\ElasticsearchBundle\DSL\Aggregation;
/**
* Class representing Min Aggregation.
*/
class MinAggregation extends StatsAggregation
{
/**
* {@inheritdoc}
*/
public function getType()
{
return 'min';
}
}
<?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\ElasticsearchBundle\DSL\Aggregation;
use ONGR\ElasticsearchBundle\DSL\Aggregation\Type\MetricTrait;
use ONGR\ElasticsearchBundle\DSL\ScriptAwareTrait;
/**
* Class representing Percentile Ranks Aggregation.
*/
class PercentileRanksAggregation extends AbstractAggregation
{
use MetricTrait;
use ScriptAwareTrait;
/**
* @var array
*/
private $values;
/**
* @var int
*/
private $compression;
/**
* @return array
*/
public function getValues()
{
return $this->values;
}
/**
* @param array $values
*/
public function setValues($values)
{
$this->values = $values;
}
/**
* @return int
*/
public function getCompression()
{
return $this->compression;
}
/**
* @param int $compression
*/
public function setCompression($compression)
{
$this->compression = $compression;
}
/**
* {@inheritdoc}
*/
public function getType()
{
return 'percentile_ranks';
}
/**
* {@inheritdoc}
*/
public function getArray()
{
$out = array_filter(
[
'field' => $this->getField(),
'script' => $this->getScript(),
'values' => $this->getValues(),
'compression' => $this->getCompression(),
],
function ($val) {
return ($val || is_numeric($val));
}
);
$this->isRequiredParametersSet($out);
return $out;
}
/**
* @param array $a
*
* @return bool
*
* @throws \LogicException
*/
private function isRequiredParametersSet($a)
{
if (array_key_exists('field', $a) && array_key_exists('values', $a)
|| (array_key_exists('script', $a) && array_key_exists('values', $a))
) {
return true;
}
throw new \LogicException('Percentile ranks aggregation must have field and values or script and values set.');
}
}
<?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\ElasticsearchBundle\DSL\Aggregation;
use ONGR\ElasticsearchBundle\DSL\Aggregation\Type\MetricTrait;
use ONGR\ElasticsearchBundle\DSL\ScriptAwareTrait;
/**
* Class representing PercentilesAggregation.
*/
class PercentilesAggregation extends AbstractAggregation
{
use MetricTrait;
use ScriptAwareTrait;
/**
* @var array
*/
private $percents;
/**
* @var int
*/
private $compression;
/**
* @return array
*/
public function getPercents()
{
return $this->percents;
}
/**
* @param array $percents
*/
public function setPercents($percents)
{
$this->percents = $percents;
}
/**
* @return int
*/
public function getCompression()
{
return $this->compression;
}
/**
* @param int $compression
*/
public function setCompression($compression)
{
$this->compression = $compression;
}
/**
* {@inheritdoc}
*/
public function getType()
{
return 'percentiles';
}
/**
* {@inheritdoc}
*/
public function getArray()
{
$out = array_filter(
[
'compression' => $this->getCompression(),
'percents' => $this->getPercents(),
'field' => $this->getField(),
'script' => $this->getScript(),
],
function ($val) {
return ($val || is_numeric($val));
}
);
$this->isRequiredParametersSet($out);
return $out;
}
/**
* @param array $a
*
* @throws \LogicException
*/
private function isRequiredParametersSet($a)
{
if (!array_key_exists('field', $a) && !array_key_exists('script', $a)) {
throw new \LogicException('Percentiles aggregation must have field or script set.');
}
}
}
......@@ -12,6 +12,7 @@
namespace ONGR\ElasticsearchBundle\DSL\Aggregation;
use ONGR\ElasticsearchBundle\DSL\Aggregation\Type\MetricTrait;
use ONGR\ElasticsearchBundle\DSL\ScriptAwareTrait;
/**
* Class representing StatsAggregation.
......@@ -19,6 +20,7 @@ use ONGR\ElasticsearchBundle\DSL\Aggregation\Type\MetricTrait;
class StatsAggregation extends AbstractAggregation
{
use MetricTrait;
use ScriptAwareTrait;
/**
* {@inheritdoc}
......@@ -33,6 +35,14 @@ class StatsAggregation extends AbstractAggregation
*/
public function getArray()
{
return $this->getField() ? ['field' => $this->getField()] : new \stdClass();
$out = [];
if ($this->getField()) {
$out['field'] = $this->getField();
}
if ($this->getScript()) {
$out['script'] = $this->getScript();
}
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\ElasticsearchBundle\DSL\Aggregation;
/**
* Class representing Sum Aggregation.
*/
class SumAggregation extends StatsAggregation
{
/**
* {@inheritdoc}
*/
public function getType()
{
return 'sum';
}
}
<?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\ElasticsearchBundle\DSL\Aggregation;
/**
* Class representing Value Count Aggregation.
*/
class ValueCountAggregation extends StatsAggregation
{
/**
* {@inheritdoc}
*/
public function getType()
{
return 'value_count';
}
}
<?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\ElasticsearchBundle\DSL;
/**
* A trait which handles elasticsearch aggregation script.
*/
trait ScriptAwareTrait
{
/**
* @var string
*/
private $script;
/**
* @return string
*/
public function getScript()
{
return $this->script;
}
/**
* @param string $script
*/
public function setScript($script)
{
$this->script = $script;
}
}
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