Skip to content
Snippets Groups Projects
Commit 23f7a398 authored by Mantas Varatiejus's avatar Mantas Varatiejus
Browse files

Remove filtered query

parent 034fde02
No related branches found
No related tags found
No related merge requests found
<?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) : [];
}
}
...@@ -11,7 +11,6 @@ ...@@ -11,7 +11,6 @@
namespace ONGR\ElasticsearchDSL\SearchEndpoint; namespace ONGR\ElasticsearchDSL\SearchEndpoint;
use ONGR\ElasticsearchDSL\Query\FilteredQuery;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/** /**
...@@ -33,10 +32,7 @@ class FilterEndpoint extends QueryEndpoint ...@@ -33,10 +32,7 @@ class FilterEndpoint extends QueryEndpoint
return null; return null;
} }
$query = new FilteredQuery(); $this->addReference('filter_query', $this->getBool());
$query->setFilter($this->getBool());
$this->addReference('filtered_query', $query);
} }
/** /**
......
...@@ -13,7 +13,6 @@ namespace ONGR\ElasticsearchDSL\SearchEndpoint; ...@@ -13,7 +13,6 @@ namespace ONGR\ElasticsearchDSL\SearchEndpoint;
use ONGR\ElasticsearchDSL\BuilderInterface; use ONGR\ElasticsearchDSL\BuilderInterface;
use ONGR\ElasticsearchDSL\Query\BoolQuery; use ONGR\ElasticsearchDSL\Query\BoolQuery;
use ONGR\ElasticsearchDSL\Query\FilteredQuery;
use ONGR\ElasticsearchDSL\Serializer\Normalizer\OrderedNormalizerInterface; use ONGR\ElasticsearchDSL\Serializer\Normalizer\OrderedNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
...@@ -37,24 +36,17 @@ class QueryEndpoint extends AbstractSearchEndpoint implements OrderedNormalizerI ...@@ -37,24 +36,17 @@ class QueryEndpoint extends AbstractSearchEndpoint implements OrderedNormalizerI
*/ */
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []) public function normalize(NormalizerInterface $normalizer, $format = null, array $context = [])
{ {
$query = $this->getBool(); if ($this->hasReference('filter_query')) {
/** @var BuilderInterface $filter */
if ($this->hasReference('filtered_query')) { $filter = $this->getReference('filter_query');
/** @var FilteredQuery $filteredQuery */ $this->addToBool($filter, BoolQuery::FILTER);
$filteredQuery = $this->getReference('filtered_query');
if ($query) {
$filteredQuery->setQuery($query);
}
$query = $filteredQuery;
} }
if (!$query) { if (!$this->bool) {
return null; return null;
} }
return [$query->getType() => $query->toArray()]; return [$this->bool->getType() => $this->bool->toArray()];
} }
/** /**
...@@ -71,7 +63,7 @@ class QueryEndpoint extends AbstractSearchEndpoint implements OrderedNormalizerI ...@@ -71,7 +63,7 @@ class QueryEndpoint extends AbstractSearchEndpoint implements OrderedNormalizerI
public function addToBool(BuilderInterface $builder, $boolType = null, $key = null) public function addToBool(BuilderInterface $builder, $boolType = null, $key = null)
{ {
if (!$this->bool) { if (!$this->bool) {
$this->bool = $this->getBoolInstance(); $this->bool = new BoolQuery();
} }
return $this->bool->add($builder, $boolType, $key); return $this->bool->add($builder, $boolType, $key);
...@@ -93,16 +85,6 @@ class QueryEndpoint extends AbstractSearchEndpoint implements OrderedNormalizerI ...@@ -93,16 +85,6 @@ class QueryEndpoint extends AbstractSearchEndpoint implements OrderedNormalizerI
return $this->bool; return $this->bool;
} }
/**
* Returns new bool instance for the endpoint.
*
* @return BoolQuery
*/
protected function getBoolInstance()
{
return new BoolQuery();
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
......
...@@ -11,7 +11,6 @@ ...@@ -11,7 +11,6 @@
namespace ONGR\ElasticsearchDSL\Tests\Unit\SearchEndpoint; namespace ONGR\ElasticsearchDSL\Tests\Unit\SearchEndpoint;
use ONGR\ElasticsearchDSL\Query\FilteredQuery;
use ONGR\ElasticsearchDSL\Query\MatchAllQuery; use ONGR\ElasticsearchDSL\Query\MatchAllQuery;
use ONGR\ElasticsearchDSL\SearchEndpoint\FilterEndpoint; use ONGR\ElasticsearchDSL\SearchEndpoint\FilterEndpoint;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
...@@ -53,20 +52,16 @@ class FilterEndpointTest extends \PHPUnit_Framework_TestCase ...@@ -53,20 +52,16 @@ class FilterEndpointTest extends \PHPUnit_Framework_TestCase
'Symfony\Component\Serializer\Normalizer\NormalizerInterface' 'Symfony\Component\Serializer\Normalizer\NormalizerInterface'
); );
$this->assertNull($instance->normalize($normalizerInterface)); $this->assertNull($instance->normalize($normalizerInterface));
$this->assertFalse($instance->hasReference('filtered_query')); $this->assertFalse($instance->hasReference('filter_query'));
$matchAllFilter = new MatchAllQuery(); $matchAllFilter = new MatchAllQuery();
$instance->add($matchAllFilter); $instance->add($matchAllFilter);
$this->assertNull($instance->normalize($normalizerInterface)); $this->assertNull($instance->normalize($normalizerInterface));
$this->assertTrue($instance->hasReference('filtered_query')); $this->assertTrue($instance->hasReference('filter_query'));
/** @var FilteredQuery $reference */
$reference = $instance->getReference('filtered_query');
$this->assertInstanceOf('ONGR\ElasticsearchDSL\Query\FilteredQuery', $reference);
/** @var \ONGR\ElasticsearchDSL\Query\BoolQuery $bool */ /** @var \ONGR\ElasticsearchDSL\Query\BoolQuery $bool */
$bool = $reference->getFilter(); $bool = $instance->getReference('filter_query');
$this->assertInstanceOf('ONGR\ElasticsearchDSL\Query\BoolQuery', $bool); $this->assertInstanceOf('ONGR\ElasticsearchDSL\Query\BoolQuery', $bool);
$must = $bool->getQueries('must'); $must = $bool->getQueries('must');
......
...@@ -11,6 +11,8 @@ ...@@ -11,6 +11,8 @@
namespace ONGR\ElasticsearchDSL\Tests\Unit\DSL; namespace ONGR\ElasticsearchDSL\Tests\Unit\DSL;
use ONGR\ElasticsearchDSL\Query\MissingQuery;
use ONGR\ElasticsearchDSL\Query\TermQuery;
use ONGR\ElasticsearchDSL\Search; use ONGR\ElasticsearchDSL\Search;
/** /**
...@@ -183,4 +185,86 @@ class SearchTest extends \PHPUnit_Framework_TestCase ...@@ -183,4 +185,86 @@ class SearchTest extends \PHPUnit_Framework_TestCase
$search->getQueryParams() $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());
}
} }
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