From 9a1f18899e01c16bf8c9db7a53f92f2925de1a07 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Simonas=20=C5=A0erlinskas?= <simonas.serlinskas@nfq.com>
Date: Tue, 10 Mar 2015 23:53:48 +0100
Subject: [PATCH] introduced query aware trait, simplified query building

from now, if there is only one query set, there will be no more generated bool query. Also Query class from now on is absolutely not necessary. There is no BC breaks.
---
 Bool/Bool.php             |  18 +++--
 Query/Query.php           |   2 +
 Query/QueryAwareTrait.php | 139 ++++++++++++++++++++++++++++++++++++++
 Search.php                |  76 +++------------------
 4 files changed, 166 insertions(+), 69 deletions(-)
 create mode 100644 Query/QueryAwareTrait.php

diff --git a/Bool/Bool.php b/Bool/Bool.php
index 249c41c..589be16 100644
--- a/Bool/Bool.php
+++ b/Bool/Bool.php
@@ -15,12 +15,16 @@ use ONGR\ElasticsearchBundle\DSL\BuilderInterface;
 use ONGR\ElasticsearchBundle\DSL\ParametersTrait;
 
 /**
- * Class Bool.
+ * Bool operator. Can be used for filters and queries.
  */
 class Bool implements BuilderInterface
 {
     use ParametersTrait;
 
+    const MUST = 'must';
+    const MUST_NOT = 'must_not';
+    const SHOULD = 'should';
+
     /**
      * @var array
      */
@@ -37,14 +41,20 @@ class Bool implements BuilderInterface
     }
 
     /**
-     * Add BuilderInterface object ot bool filter.
+     * Add BuilderInterface object to bool operator.
      *
      * @param BuilderInterface $bool
      * @param string           $type
+     *
+     * @throws \UnexpectedValueException
      */
-    public function addToBool(BuilderInterface $bool, $type = 'must')
+    public function addToBool(BuilderInterface $bool, $type = self::MUST)
     {
-        $this->container[$type][] = $bool;
+        if (in_array($type, [ self::MUST, self::MUST_NOT, self::SHOULD ])) {
+            $this->container[$type][] = $bool;
+        } else {
+            throw new \UnexpectedValueException(sprintf('The bool operator %s is not supported', $type));
+        }
     }
 
     /**
diff --git a/Query/Query.php b/Query/Query.php
index 2698820..d4930bd 100644
--- a/Query/Query.php
+++ b/Query/Query.php
@@ -16,6 +16,8 @@ use ONGR\ElasticsearchBundle\DSL\BuilderInterface;
 
 /**
  * Query class.
+ *
+ * @deprecated will remove in 1.0 Query container now is implemented directly in search.
  */
 class Query implements BuilderInterface
 {
diff --git a/Query/QueryAwareTrait.php b/Query/QueryAwareTrait.php
new file mode 100644
index 0000000..6fabc7e
--- /dev/null
+++ b/Query/QueryAwareTrait.php
@@ -0,0 +1,139 @@
+<?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\Query;
+
+use ONGR\ElasticsearchBundle\DSL\Bool\Bool;
+use ONGR\ElasticsearchBundle\DSL\BuilderInterface;
+
+/**
+ * Provides query container functionality to any class.
+ */
+trait QueryAwareTrait
+{
+    /**
+     * @var BuilderInterface[]
+     */
+    private $queries = [];
+
+    /**
+     * @var \ONGR\ElasticsearchBundle\DSL\Bool\Bool
+     */
+    private $boolQuery;
+
+    /**
+     * @param BuilderInterface $query
+     * @param string           $boolType
+     *
+     * @return $this
+     */
+    public function addQuery(BuilderInterface $query, $boolType = Bool::MUST)
+    {
+        if ($boolType !== Bool::MUST || $this->boolQuery !== null) {
+            $this->getBoolQuery()->addToBool($query, $boolType);
+        } else {
+            $this->queries[$query->getType()] = $query;
+        }
+
+        return $this;
+    }
+
+    /**
+     * Returns Bool query. Creates new instance if there is not initiated.
+     *
+     * @return \ONGR\ElasticsearchBundle\DSL\Bool\Bool
+     */
+    public function getBoolQuery()
+    {
+        if (!$this->boolQuery) {
+            $this->boolQuery = new Bool();
+        }
+
+        return $this->boolQuery;
+    }
+
+    /**
+     * @param array $params Example values:
+     *                      - minimum_should_match => 1
+     *                      - boost => 1.
+     */
+    public function setBoolQueryParameters(array $params)
+    {
+        $this->getBoolQuery()->setParameters($params);
+    }
+
+    /**
+     * Checks if there is added specific query.
+     *
+     * @param string $key
+     * @param string $boolType
+     *
+     * @return bool
+     */
+    public function hasQuery($key, $boolType = Bool::MUST)
+    {
+        return array_key_exists($key, $this->queries[$boolType]);
+    }
+
+    /**
+     * Removes specific query.
+     *
+     * @param string $key
+     */
+    public function removeQuery($key)
+    {
+        if ($this->hasQuery($key)) {
+            unset($this->queries[$key]);
+        }
+    }
+
+    /**
+     * Completely resets query.
+     */
+    public function destroyQuery()
+    {
+        $this->queries = [];
+        $this->boolQuery = null;
+    }
+
+    /**
+     * Return all queries.
+     *
+     * @return array
+     */
+    public function getQueries()
+    {
+        return $this->queries;
+    }
+
+    /**
+     * Aggregates all queries to array.
+     *
+     * @return array
+     */
+    public function processQueries()
+    {
+        if ($this->boolQuery || count($this->getQueries()) > 1) {
+            $bool = $this->getBoolQuery();
+            foreach ($this->getQueries() as $query) {
+                $bool->addToBool($query);
+            }
+
+            return ['query' => [$bool->getType() => $bool->toArray()]];
+        } elseif (count($this->getQueries()) == 1) {
+            $query = array_values($this->getQueries())[0];
+
+            return ['query' => [$query->getType() => $query->toArray()]];
+        }
+
+        return [];
+    }
+}
diff --git a/Search.php b/Search.php
index 52d301b..5e9bd48 100644
--- a/Search.php
+++ b/Search.php
@@ -17,6 +17,7 @@ use ONGR\ElasticsearchBundle\DSL\Filter\PostFilter;
 use ONGR\ElasticsearchBundle\DSL\Highlight\Highlight;
 use ONGR\ElasticsearchBundle\DSL\Query\FilteredQuery;
 use ONGR\ElasticsearchBundle\DSL\Query\Query;
+use ONGR\ElasticsearchBundle\DSL\Query\QueryAwareTrait;
 use ONGR\ElasticsearchBundle\DSL\Sort\AbstractSort;
 use ONGR\ElasticsearchBundle\DSL\Sort\Sorts;
 use ONGR\ElasticsearchBundle\DSL\Suggester\AbstractSuggester;
@@ -26,12 +27,9 @@ use ONGR\ElasticsearchBundle\DSL\Suggester\AbstractSuggester;
  */
 class Search
 {
-    const SCROLL_DURATION = '5m';
+    use QueryAwareTrait;
 
-    /**
-     * @var Query
-     */
-    private $query;
+    const SCROLL_DURATION = '5m';
 
     /**
      * @var array
@@ -439,44 +437,7 @@ class Search
     }
 
     /**
-     * @param BuilderInterface $query    Query.
-     * @param string           $boolType Possible boolType values:
-     *                                   - must
-     *                                   - must_not
-     *                                   - should.
-     *
-     * @return $this
-     */
-    public function addQuery(BuilderInterface $query, $boolType = 'must')
-    {
-        if ($this->query === null) {
-            $this->query = new Query;
-        }
-        $this->query->addQuery($query, $boolType);
-
-        return $this;
-    }
-
-    /**
-     * Unset the query.
-     */
-    public function destroyQuery()
-    {
-        $this->query = null;
-    }
-
-    /**
-     * @param array $params Example values:
-     *                      - minimum_should_match => 1
-     *                      - boost => 1.
-     */
-    public function setBoolQueryParameters(array $params)
-    {
-        $this->query->setBoolParameters($params);
-    }
-
-    /**
-     * Returns query string parameters.
+     * Returns query url parameters.
      *
      * @return array
      */
@@ -571,14 +532,6 @@ class Search
         return $this->postFilters;
     }
 
-    /**
-     * @return BuilderInterface[]
-     */
-    public function getQueries()
-    {
-        return $this->query;
-    }
-
     /**
      * @return array
      */
@@ -648,25 +601,18 @@ class Search
      */
     public function toArray()
     {
-        $output = [];
-
-        $query = $this->query;
+        /*
+         * First we check if there are some filter to add to filtered query.
+         * For now we use match all, but its gonna be changed.
+         */
 
         if ($this->filters !== null) {
-            $filteredQuery = new FilteredQuery($query === null ? null : $query);
+            $filteredQuery = new FilteredQuery();
             $filteredQuery->setFilter($this->filters);
-
-            if ($this->boolFilterParams) {
-                $filteredQuery->setBoolParameters($this->boolFilterParams);
-            }
-
-            $query = new Query();
-            $query->addQuery($filteredQuery);
+            $this->addQuery($filteredQuery);
         }
 
-        if ($query !== null) {
-            $output[$query->getType()] = $query->toArray();
-        }
+        $output = $this->processQueries();
 
         if ($this->postFilters !== null) {
             $postFilter = new PostFilter();
-- 
GitLab