From b7fcaeca9231eb6f57ea81fcf8b2e8b2bcb42eb7 Mon Sep 17 00:00:00 2001 From: Martynas Sudintas <martynas.sudintas@ongr.io> Date: Wed, 29 Apr 2015 13:39:06 +0300 Subject: [PATCH] Implemented BoolQuery, FilterQuery. Deprecated Bool --- Bool/Bool.php | 63 +++++---------------------------- Filter/BoolFilter.php | 23 ++++++++++++ Query/BoolQuery.php | 81 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 55 deletions(-) create mode 100644 Filter/BoolFilter.php create mode 100644 Query/BoolQuery.php diff --git a/Bool/Bool.php b/Bool/Bool.php index 69194fe..320a497 100644 --- a/Bool/Bool.php +++ b/Bool/Bool.php @@ -13,33 +13,15 @@ namespace ONGR\ElasticsearchBundle\DSL\Bool; use ONGR\ElasticsearchBundle\DSL\BuilderInterface; use ONGR\ElasticsearchBundle\DSL\ParametersTrait; +use ONGR\ElasticsearchBundle\DSL\Query\BoolQuery; /** * Bool operator. Can be used for filters and queries. + * + * @deprecated Will be removed in 1.0. Use ONGR\ElasticsearchBundle\DSL\Query\BoolQuery. */ -class Bool implements BuilderInterface +class Bool extends BoolQuery { - use ParametersTrait; - - const MUST = 'must'; - const MUST_NOT = 'must_not'; - const SHOULD = 'should'; - - /** - * @var array - */ - private $container = []; - - /** - * Checks if bool filter is relevant. - * - * @return bool - */ - public function isRelevant() - { - return (bool)count($this->container); - } - /** * Add BuilderInterface object to bool operator. * @@ -47,40 +29,11 @@ class Bool implements BuilderInterface * @param string $type * * @throws \UnexpectedValueException + * + * @deprecated Will be removed in 1.0. Use ONGR\ElasticsearchBundle\DSL\Query\BoolQuery::add(). */ - public function addToBool(BuilderInterface $bool, $type = self::MUST) - { - $constants = (new \ReflectionObject($this))->getConstants(); - - if (!in_array($type, $constants)) { - throw new \UnexpectedValueException(sprintf('The bool operator %s is not supported', $type)); - } - - $this->container[$type][] = $bool; - } - - /** - * {@inheritdoc} - */ - public function getType() - { - return 'bool'; - } - - /** - * {@inheritdoc} - */ - public function toArray() + public function addToBool(BuilderInterface $bool, $type = BoolQuery::MUST) { - $output = $this->processArray(); - - foreach ($this->container as $type => $filters) { - /** @var BuilderInterface $bool */ - foreach ($filters as $bool) { - $output[$type][] = [$bool->getType() => $bool->toArray()]; - } - } - - return $output; + $this->add($bool, $type); } } diff --git a/Filter/BoolFilter.php b/Filter/BoolFilter.php new file mode 100644 index 0000000..2db4e44 --- /dev/null +++ b/Filter/BoolFilter.php @@ -0,0 +1,23 @@ +<?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\Filter; + +use ONGR\ElasticsearchBundle\DSL\Query\BoolQuery; + +/** + * Represents Elasticsearch "bool" filter. + * + * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-filter.html + */ +class BoolFilter extends BoolQuery +{ +} diff --git a/Query/BoolQuery.php b/Query/BoolQuery.php new file mode 100644 index 0000000..5189831 --- /dev/null +++ b/Query/BoolQuery.php @@ -0,0 +1,81 @@ +<?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\BuilderInterface; +use ONGR\ElasticsearchBundle\DSL\ParametersTrait; + +/** + * Represents Elasticsearch "bool" filter. + * + * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html + */ +class BoolQuery implements BuilderInterface +{ + use ParametersTrait; + + const MUST = 'must'; + const MUST_NOT = 'must_not'; + const SHOULD = 'should'; + + /** + * @var array + */ + private $container = []; + + /** + * Checks if bool expression is relevant. + * + * @return bool + */ + public function isRelevant() + { + return (bool)count($this->container); + } + + /** + * Add BuilderInterface object to bool operator. + * + * @param BuilderInterface $builder Query or a filter to add to bool. + * @param string $type Bool type. Available: must, must_not, should. + * + * @return BoolQuery + * + * @throws \UnexpectedValueException + */ + public function add(BuilderInterface $builder, $type = self::MUST) + { + if (!in_array($type, (new \ReflectionObject($this))->getConstants())) { + throw new \UnexpectedValueException(sprintf('The bool operator %s is not supported', $type)); + } + + $this->container[$type][] = [$builder->getType() => $builder->toArray()]; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + return $this->processArray($this->container); + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'bool'; + } +} -- GitLab