From 23f7a398f37ace924e56218a2067795fd09758d6 Mon Sep 17 00:00:00 2001 From: Mantas Varatiejus <mantas.varatiejus@nfq.com> Date: Thu, 21 Jan 2016 16:15:08 +0200 Subject: [PATCH] Remove filtered query --- src/Query/FilteredQuery.php | 120 -------------------- src/SearchEndpoint/FilterEndpoint.php | 6 +- src/SearchEndpoint/QueryEndpoint.php | 32 ++---- tests/SearchEndpoint/FilterEndpointTest.php | 11 +- tests/SearchTest.php | 84 ++++++++++++++ 5 files changed, 95 insertions(+), 158 deletions(-) delete mode 100644 src/Query/FilteredQuery.php diff --git a/src/Query/FilteredQuery.php b/src/Query/FilteredQuery.php deleted file mode 100644 index 0d5504f..0000000 --- a/src/Query/FilteredQuery.php +++ /dev/null @@ -1,120 +0,0 @@ -<?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\ElasticsearchDSL\Query; - -use ONGR\ElasticsearchDSL\BuilderInterface; -use ONGR\ElasticsearchDSL\ParametersTrait; - -/** - * Represents Elasticsearch "bool" filter. - * - * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html - * - * @deprecated Will be removed in 2.0. Use the `bool` query instead with a `filter` clause. - */ -class FilteredQuery implements BuilderInterface -{ - use ParametersTrait; - - /** - * @var BuilderInterface Used query inside filtered query. - */ - private $query; - - /** - * @var BuilderInterface Used filter inside filtered query. - */ - private $filter; - - /** - * @param BuilderInterface $query - * @param BuilderInterface $filter - */ - public function __construct($query = null, $filter = null) - { - @trigger_error( - 'The FilteredQuery class is deprecated and will be removed in 2.0. ' . - 'Use the "bool" query instead with a "filter" clause.', - E_USER_DEPRECATED - ); - - if ($query !== null) { - $this->setQuery($query); - } - - if ($filter !== null) { - $this->setFilter($filter); - } - } - - /** - * Sets query. - * - * @param BuilderInterface $query - */ - public function setQuery(BuilderInterface $query) - { - $this->query = $query; - } - - /** - * @return BuilderInterface - */ - public function getQuery() - { - return $this->query; - } - - /** - * Sets filter. - * - * @param BuilderInterface $filter - */ - public function setFilter(BuilderInterface $filter) - { - $this->filter = $filter; - } - - /** - * @return BuilderInterface - */ - public function getFilter() - { - return $this->filter; - } - - /** - * {@inheritdoc} - */ - public function getType() - { - return 'filtered'; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - $output = []; - - if ($this->getFilter()) { - $output['filter'][$this->getFilter()->getType()] = $this->getFilter()->toArray(); - } - - if ($this->getQuery()) { - $output['query'][$this->getQuery()->getType()] = $this->getQuery()->toArray(); - } - - return count($output) > 0 ? $this->processArray($output) : []; - } -} diff --git a/src/SearchEndpoint/FilterEndpoint.php b/src/SearchEndpoint/FilterEndpoint.php index e9c3d8f..bc2a0d4 100644 --- a/src/SearchEndpoint/FilterEndpoint.php +++ b/src/SearchEndpoint/FilterEndpoint.php @@ -11,7 +11,6 @@ namespace ONGR\ElasticsearchDSL\SearchEndpoint; -use ONGR\ElasticsearchDSL\Query\FilteredQuery; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; /** @@ -33,10 +32,7 @@ class FilterEndpoint extends QueryEndpoint return null; } - $query = new FilteredQuery(); - $query->setFilter($this->getBool()); - - $this->addReference('filtered_query', $query); + $this->addReference('filter_query', $this->getBool()); } /** diff --git a/src/SearchEndpoint/QueryEndpoint.php b/src/SearchEndpoint/QueryEndpoint.php index 9f14df5..e3b4c08 100644 --- a/src/SearchEndpoint/QueryEndpoint.php +++ b/src/SearchEndpoint/QueryEndpoint.php @@ -13,7 +13,6 @@ namespace ONGR\ElasticsearchDSL\SearchEndpoint; use ONGR\ElasticsearchDSL\BuilderInterface; use ONGR\ElasticsearchDSL\Query\BoolQuery; -use ONGR\ElasticsearchDSL\Query\FilteredQuery; use ONGR\ElasticsearchDSL\Serializer\Normalizer\OrderedNormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; @@ -37,24 +36,17 @@ class QueryEndpoint extends AbstractSearchEndpoint implements OrderedNormalizerI */ public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []) { - $query = $this->getBool(); - - if ($this->hasReference('filtered_query')) { - /** @var FilteredQuery $filteredQuery */ - $filteredQuery = $this->getReference('filtered_query'); - - if ($query) { - $filteredQuery->setQuery($query); - } - - $query = $filteredQuery; + if ($this->hasReference('filter_query')) { + /** @var BuilderInterface $filter */ + $filter = $this->getReference('filter_query'); + $this->addToBool($filter, BoolQuery::FILTER); } - if (!$query) { + if (!$this->bool) { return null; } - return [$query->getType() => $query->toArray()]; + return [$this->bool->getType() => $this->bool->toArray()]; } /** @@ -71,7 +63,7 @@ class QueryEndpoint extends AbstractSearchEndpoint implements OrderedNormalizerI public function addToBool(BuilderInterface $builder, $boolType = null, $key = null) { if (!$this->bool) { - $this->bool = $this->getBoolInstance(); + $this->bool = new BoolQuery(); } return $this->bool->add($builder, $boolType, $key); @@ -93,16 +85,6 @@ class QueryEndpoint extends AbstractSearchEndpoint implements OrderedNormalizerI return $this->bool; } - /** - * Returns new bool instance for the endpoint. - * - * @return BoolQuery - */ - protected function getBoolInstance() - { - return new BoolQuery(); - } - /** * {@inheritdoc} */ diff --git a/tests/SearchEndpoint/FilterEndpointTest.php b/tests/SearchEndpoint/FilterEndpointTest.php index c8c8eb8..86425f6 100644 --- a/tests/SearchEndpoint/FilterEndpointTest.php +++ b/tests/SearchEndpoint/FilterEndpointTest.php @@ -11,7 +11,6 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\SearchEndpoint; -use ONGR\ElasticsearchDSL\Query\FilteredQuery; use ONGR\ElasticsearchDSL\Query\MatchAllQuery; use ONGR\ElasticsearchDSL\SearchEndpoint\FilterEndpoint; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; @@ -53,20 +52,16 @@ class FilterEndpointTest extends \PHPUnit_Framework_TestCase 'Symfony\Component\Serializer\Normalizer\NormalizerInterface' ); $this->assertNull($instance->normalize($normalizerInterface)); - $this->assertFalse($instance->hasReference('filtered_query')); + $this->assertFalse($instance->hasReference('filter_query')); $matchAllFilter = new MatchAllQuery(); $instance->add($matchAllFilter); $this->assertNull($instance->normalize($normalizerInterface)); - $this->assertTrue($instance->hasReference('filtered_query')); - - /** @var FilteredQuery $reference */ - $reference = $instance->getReference('filtered_query'); - $this->assertInstanceOf('ONGR\ElasticsearchDSL\Query\FilteredQuery', $reference); + $this->assertTrue($instance->hasReference('filter_query')); /** @var \ONGR\ElasticsearchDSL\Query\BoolQuery $bool */ - $bool = $reference->getFilter(); + $bool = $instance->getReference('filter_query'); $this->assertInstanceOf('ONGR\ElasticsearchDSL\Query\BoolQuery', $bool); $must = $bool->getQueries('must'); diff --git a/tests/SearchTest.php b/tests/SearchTest.php index 1b72a39..be97ad9 100644 --- a/tests/SearchTest.php +++ b/tests/SearchTest.php @@ -11,6 +11,8 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\DSL; +use ONGR\ElasticsearchDSL\Query\MissingQuery; +use ONGR\ElasticsearchDSL\Query\TermQuery; use ONGR\ElasticsearchDSL\Search; /** @@ -183,4 +185,86 @@ class SearchTest extends \PHPUnit_Framework_TestCase $search->getQueryParams() ); } + + /** + * Data provider for testToArray(). + * + * @return array + */ + public function getTestToArrayData() + { + $cases = []; + + $cases['empty_search'] = [ + [], + new Search(), + ]; + + $cases['single_term_query'] = [ + [ + 'query' => [ + 'bool' => [ + 'must' => [ + ['term' => ['foo' => 'bar']], + ], + ], + ], + ], + (new Search())->addQuery(new TermQuery('foo', 'bar')), + ]; + + $cases['single_term_filter'] = [ + [ + 'query' => [ + 'bool' => [ + 'filter' => [ + [ + 'bool' => [ + 'must' => [ + ['term' => ['foo' => 'bar']], + ], + ], + ], + ], + ], + ], + ], + (new Search())->addFilter(new TermQuery('foo', 'bar')), + ]; + + $cases['single_query_query_and_filter'] = [ + [ + 'query' => [ + 'bool' => [ + 'must' => [ + ['term' => ['foo' => 'bar']], + ], + 'filter' => [ + [ + 'bool' => [ + 'must' => [ + ['missing' => ['field' => 'baz']], + ], + ], + ], + ], + ], + ], + ], + (new Search())->addQuery(new TermQuery('foo', 'bar'))->addFilter(new MissingQuery('baz')), + ]; + + return $cases; + } + + /** + * @param array $expected + * @param Search $search + * + * @dataProvider getTestToArrayData() + */ + public function testToArray($expected, $search) + { + $this->assertEquals($expected, $search->toArray()); + } } -- GitLab