diff --git a/Aggregation/FiltersAggregation.php b/Aggregation/FiltersAggregation.php new file mode 100644 index 0000000000000000000000000000000000000000..67978ed5364d959b10141ee4d5588bda7d25397d --- /dev/null +++ b/Aggregation/FiltersAggregation.php @@ -0,0 +1,82 @@ +<?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\BucketingTrait; +use ONGR\ElasticsearchBundle\DSL\BuilderInterface; + +/** + * Class representing filters aggregation. + */ +class FiltersAggregation extends AbstractAggregation +{ + use BucketingTrait; + + /** + * @var BuilderInterface[] + */ + private $filters = []; + + /** + * @var bool + */ + private $anonymous = false; + + /** + * @param bool $anonymous + * + * @return FiltersAggregation + */ + public function setAnonymous($anonymous) + { + $this->anonymous = $anonymous; + + return $this; + } + + /** + * @param BuilderInterface $filter + * @param string $name + * + * @throws \LogicException + * + * @return FiltersAggregation + */ + public function addFilter(BuilderInterface $filter, $name = '') + { + if ($this->anonymous === false && empty($name)) { + throw new \LogicException('In not anonymous filters filter name must be set.'); + } elseif ($this->anonymous === false && !empty($name)) { + $this->filters['filters'][$name] = [$filter->getType() => $filter->toArray()]; + } else { + $this->filters['filters'][] = [$filter->getType() => $filter->toArray()]; + } + + return $this; + } + + /** + * {@inheritdoc} + */ + public function getArray() + { + return $this->filters; + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'filters'; + } +} diff --git a/Aggregation/GeoBoundsAggregation.php b/Aggregation/GeoBoundsAggregation.php new file mode 100644 index 0000000000000000000000000000000000000000..0fd8dea147b1d570a76ed5cda20d9314b816f91f --- /dev/null +++ b/Aggregation/GeoBoundsAggregation.php @@ -0,0 +1,68 @@ +<?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\BucketingTrait; + +/** + * Class representing geo bounds aggregation. + */ +class GeoBoundsAggregation extends AbstractAggregation +{ + use BucketingTrait; + + /** + * @var bool + */ + private $wrapLongitude = true; + + /** + * @return bool + */ + public function isWrapLongitude() + { + return $this->wrapLongitude; + } + + /** + * @param bool $wrapLongitude + */ + public function setWrapLongitude($wrapLongitude) + { + $this->wrapLongitude = $wrapLongitude; + } + + /** + * {@inheritdoc} + */ + public function getArray() + { + $data = []; + if ($this->getField()) { + $data['field'] = $this->getField(); + } else { + throw new \LogicException('Geo bounds aggregation must have a field set.'); + } + + $data['wrap_longitude'] = $this->isWrapLongitude(); + + return $data; + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'geo_bounds'; + } +} diff --git a/Aggregation/GeoDistanceAggregation.php b/Aggregation/GeoDistanceAggregation.php new file mode 100644 index 0000000000000000000000000000000000000000..c1e4906a51fa9000beb279c08a5a1d44401d1a74 --- /dev/null +++ b/Aggregation/GeoDistanceAggregation.php @@ -0,0 +1,158 @@ +<?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\BucketingTrait; + +/** + * Class representing geo distance aggregation. + */ +class GeoDistanceAggregation extends AbstractAggregation +{ + use BucketingTrait; + + /** + * @var mixed + */ + private $origin; + + /** + * @var string + */ + private $distanceType; + + /** + * @var string + */ + private $unit; + + /** + * @var array + */ + private $ranges = []; + + /** + * @return string + */ + public function getOrigin() + { + return $this->origin; + } + + /** + * @param mixed $origin + */ + public function setOrigin($origin) + { + $this->origin = $origin; + } + + /** + * @return string + */ + public function getDistanceType() + { + return $this->distanceType; + } + + /** + * @param string $distanceType + */ + public function setDistanceType($distanceType) + { + $this->distanceType = $distanceType; + } + + /** + * @return string + */ + public function getUnit() + { + return $this->unit; + } + + /** + * @param string $unit + */ + public function setUnit($unit) + { + $this->unit = $unit; + } + + /** + * Add range to aggregation. + * + * @param int|float|null $from + * @param int|float|null $to + * + * @throws \LogicException + * + * @return GeoDistanceAggregation + */ + public function addRange($from = null, $to = null) + { + $range = array_filter( + [ + 'from' => $from, + 'to' => $to, + ] + ); + + if (empty($range)) { + throw new \LogicException('Either from or to must be set. Both cannot be null.'); + } + + $this->ranges[] = $range; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function getArray() + { + $data = []; + + if ($this->getField()) { + $data['field'] = $this->getField(); + } else { + throw new \LogicException('Geo distance aggregation must have a field set.'); + } + + if ($this->getOrigin()) { + $data['origin'] = $this->getOrigin(); + } else { + throw new \LogicException('Geo distance aggregation must have an origin set.'); + } + + if ($this->getUnit()) { + $data['unit'] = $this->getUnit(); + } + + if ($this->getDistanceType()) { + $data['distance_type'] = $this->getDistanceType(); + } + + $data['ranges'] = $this->ranges; + + return $data; + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'geo_distance'; + } +} diff --git a/Aggregation/GeoHashGridAggregation.php b/Aggregation/GeoHashGridAggregation.php new file mode 100644 index 0000000000000000000000000000000000000000..e2642a552a248ce0f40fdfde0cc1f67ee34f41aa --- /dev/null +++ b/Aggregation/GeoHashGridAggregation.php @@ -0,0 +1,121 @@ +<?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\BucketingTrait; + +/** + * Class representing geohash grid aggregation. + */ +class GeoHashGridAggregation extends AbstractAggregation +{ + use BucketingTrait; + + /** + * @var int + */ + private $precision; + + /** + * @var int + */ + private $size; + + /** + * @var int + */ + private $shardSize; + + /** + * @return int + */ + public function getPrecision() + { + return $this->precision; + } + + /** + * @param int $precision + */ + public function setPrecision($precision) + { + $this->precision = $precision; + } + + /** + * @return int + */ + public function getSize() + { + return $this->size; + } + + /** + * @param int $size + */ + public function setSize($size) + { + $this->size = $size; + } + + /** + * @return int + */ + public function getShardSize() + { + return $this->shardSize; + } + + /** + * @param int $shardSize + */ + public function setShardSize($shardSize) + { + $this->shardSize = $shardSize; + } + + /** + * {@inheritdoc} + */ + public function getArray() + { + $data = []; + + if ($this->getField()) { + $data['field'] = $this->getField(); + } else { + throw new \LogicException('Geo bounds aggregation must have a field set.'); + } + + if ($this->getPrecision()) { + $data['precision'] = $this->getPrecision(); + } + + if ($this->getSize()) { + $data['size'] = $this->getSize(); + } + + if ($this->getShardSize()) { + $data['shard_size'] = $this->getShardSize(); + } + + return $data; + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'geohash_grid'; + } +}