Skip to content
Snippets Groups Projects
Commit b7fcaeca authored by Martynas Sudintas's avatar Martynas Sudintas
Browse files

Implemented BoolQuery, FilterQuery. Deprecated Bool

parent f054de56
No related branches found
No related tags found
No related merge requests found
...@@ -13,33 +13,15 @@ namespace ONGR\ElasticsearchBundle\DSL\Bool; ...@@ -13,33 +13,15 @@ namespace ONGR\ElasticsearchBundle\DSL\Bool;
use ONGR\ElasticsearchBundle\DSL\BuilderInterface; use ONGR\ElasticsearchBundle\DSL\BuilderInterface;
use ONGR\ElasticsearchBundle\DSL\ParametersTrait; use ONGR\ElasticsearchBundle\DSL\ParametersTrait;
use ONGR\ElasticsearchBundle\DSL\Query\BoolQuery;
/** /**
* Bool operator. Can be used for filters and queries. * 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. * Add BuilderInterface object to bool operator.
* *
...@@ -47,40 +29,11 @@ class Bool implements BuilderInterface ...@@ -47,40 +29,11 @@ class Bool implements BuilderInterface
* @param string $type * @param string $type
* *
* @throws \UnexpectedValueException * @throws \UnexpectedValueException
*
* @deprecated Will be removed in 1.0. Use ONGR\ElasticsearchBundle\DSL\Query\BoolQuery::add().
*/ */
public function addToBool(BuilderInterface $bool, $type = self::MUST) public function addToBool(BuilderInterface $bool, $type = BoolQuery::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()
{ {
$output = $this->processArray(); $this->add($bool, $type);
foreach ($this->container as $type => $filters) {
/** @var BuilderInterface $bool */
foreach ($filters as $bool) {
$output[$type][] = [$bool->getType() => $bool->toArray()];
}
}
return $output;
} }
} }
<?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
{
}
<?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';
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment