diff --git a/Aggregation/AvgAggregation.php b/Aggregation/AvgAggregation.php new file mode 100644 index 0000000000000000000000000000000000000000..2d79579efdf864ba1a2c27aaa749c6f4295eaeb9 --- /dev/null +++ b/Aggregation/AvgAggregation.php @@ -0,0 +1,26 @@ +<?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'; + } +} diff --git a/Aggregation/ExtendedStatsAggregation.php b/Aggregation/ExtendedStatsAggregation.php new file mode 100644 index 0000000000000000000000000000000000000000..4307db4ddea6b617aaf3ea59f3bc82a9ca47adba --- /dev/null +++ b/Aggregation/ExtendedStatsAggregation.php @@ -0,0 +1,75 @@ +<?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; + } +} diff --git a/Aggregation/MaxAggregation.php b/Aggregation/MaxAggregation.php new file mode 100644 index 0000000000000000000000000000000000000000..c198923f9c8d444d048c9b617607683efd2da962 --- /dev/null +++ b/Aggregation/MaxAggregation.php @@ -0,0 +1,26 @@ +<?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'; + } +} diff --git a/Aggregation/MinAggregation.php b/Aggregation/MinAggregation.php new file mode 100644 index 0000000000000000000000000000000000000000..83ed3ba030bedd4a4ad7f8491ac95a6bd33da223 --- /dev/null +++ b/Aggregation/MinAggregation.php @@ -0,0 +1,26 @@ +<?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'; + } +} diff --git a/Aggregation/PercentileRanksAggregation.php b/Aggregation/PercentileRanksAggregation.php new file mode 100644 index 0000000000000000000000000000000000000000..5bc5c12a3631afe0bdb36a2c1eac989c52dda8ac --- /dev/null +++ b/Aggregation/PercentileRanksAggregation.php @@ -0,0 +1,113 @@ +<?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.'); + } +} diff --git a/Aggregation/PercentilesAggregation.php b/Aggregation/PercentilesAggregation.php new file mode 100644 index 0000000000000000000000000000000000000000..8aa9e1e847bc910bc476267c91e2b7f73fb5a6fb --- /dev/null +++ b/Aggregation/PercentilesAggregation.php @@ -0,0 +1,108 @@ +<?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.'); + } + } +} diff --git a/Aggregation/StatsAggregation.php b/Aggregation/StatsAggregation.php index 73c305218d8e30fad2613a3181d18c77d1dc992b..4952837431f10725d6f31dd6e60a1783b863dfdf 100644 --- a/Aggregation/StatsAggregation.php +++ b/Aggregation/StatsAggregation.php @@ -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; } } diff --git a/Aggregation/SumAggregation.php b/Aggregation/SumAggregation.php new file mode 100644 index 0000000000000000000000000000000000000000..28b07f2a3e2873684dbd3818e5ed466df92ccc9b --- /dev/null +++ b/Aggregation/SumAggregation.php @@ -0,0 +1,26 @@ +<?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'; + } +} diff --git a/Aggregation/ValueCountAggregation.php b/Aggregation/ValueCountAggregation.php new file mode 100644 index 0000000000000000000000000000000000000000..afcab235abdb49a97b8311fa23f32591fdd62e56 --- /dev/null +++ b/Aggregation/ValueCountAggregation.php @@ -0,0 +1,26 @@ +<?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'; + } +} diff --git a/ScriptAwareTrait.php b/ScriptAwareTrait.php new file mode 100644 index 0000000000000000000000000000000000000000..d9a40fe02262efa17c0eff81f767c06881252b23 --- /dev/null +++ b/ScriptAwareTrait.php @@ -0,0 +1,39 @@ +<?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; + } +}