diff --git a/Bool/Bool.php b/Bool/Bool.php index 249c41c1cff7eac1563fa5cab6aaaa5dbdb236cf..589be164b1bb2f6aec8845d9d477e005691ed336 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 26988200ae7f717121f744d4e058298eaadfa9ae..d4930bd3804cc52f6e1f0b023a6d6ef079f497a4 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 0000000000000000000000000000000000000000..6fabc7eddfb03842022f412d260c26dc19603335 --- /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 52d301bbcb8b1abd94aa15632fc46bec787c5111..5e9bd482cd94b80d724d0cbe93b0ee23b071c3e6 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();