From 1826e52680bb8cd433e449daf82e8708d57e0bc7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mantas=20Jonu=C5=A1as?= <mantas.jonusas@nfq.lt>
Date: Thu, 5 Mar 2015 17:02:38 +0200
Subject: [PATCH] Added filters, geo bound, geohash grid aggregations

---
 Aggregation/FiltersAggregation.php     |  82 +++++++++++++++++
 Aggregation/GeoBoundsAggregation.php   |  68 ++++++++++++++
 Aggregation/GeoHashGridAggregation.php | 121 +++++++++++++++++++++++++
 3 files changed, 271 insertions(+)
 create mode 100644 Aggregation/FiltersAggregation.php
 create mode 100644 Aggregation/GeoBoundsAggregation.php
 create mode 100644 Aggregation/GeoHashGridAggregation.php

diff --git a/Aggregation/FiltersAggregation.php b/Aggregation/FiltersAggregation.php
new file mode 100644
index 0000000..67978ed
--- /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 0000000..0fd8dea
--- /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/GeoHashGridAggregation.php b/Aggregation/GeoHashGridAggregation.php
new file mode 100644
index 0000000..f5fb75c
--- /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->precision) {
+            $data['precision'] = $this->getPrecision();
+        }
+
+        if ($this->size) {
+            $data['size'] = $this->getSize();
+        }
+
+        if ($this->shardSize) {
+            $data['shard_size'] = $this->getShardSize();
+        }
+
+        return $data;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getType()
+    {
+        return 'geohash_grid';
+    }
+}
-- 
GitLab