diff --git a/src/Aggregation/FilterAggregation.php b/src/Aggregation/FilterAggregation.php index 58e3deb57598716866f6bc8bd457d0eaab5791f1..3e49b7d14dad8f37ee7776fe3efc0c956e74b8af 100644 --- a/src/Aggregation/FilterAggregation.php +++ b/src/Aggregation/FilterAggregation.php @@ -69,13 +69,7 @@ class FilterAggregation extends AbstractAggregation throw new \LogicException("Filter aggregation `{$this->getName()}` has no filter added"); } - if ($this->filter instanceof BoolFilter && $this->filter->isRelevant() - || !$this->filter instanceof BoolFilter - ) { - $filterData = [$this->filter->getType() => $this->filter->toArray()]; - } else { - $filterData = $this->filter->toArray(); - } + $filterData = [$this->filter->getType() => $this->filter->toArray()]; return $filterData; } diff --git a/src/Query/BoolQuery.php b/src/Query/BoolQuery.php index 924ee6cc5af87eaa81f595639f2ea8f02c584cfa..0c70493f7c02ac18965dd87ab592f4ab2be1795a 100644 --- a/src/Query/BoolQuery.php +++ b/src/Query/BoolQuery.php @@ -30,30 +30,26 @@ class BoolQuery implements BuilderInterface /** * @var array */ - private $container = []; + private $container; /** * Constructor to prepare container. */ public function __construct() { - $this->container = [ - self::MUST => [], - self::MUST_NOT => [], - self::SHOULD => [], - ]; + $this->container = []; } /** * Checks if bool expression is relevant. * * @return bool + * + * @deprecated Will be removed in 2.0. No replacement. Always use full structure. */ public function isRelevant() { - return - (count($this->container[self::MUST_NOT]) + count($this->container[self::SHOULD])) > 0 - || count($this->container[self::MUST]) > 1; + return true; } /** @@ -63,11 +59,13 @@ class BoolQuery implements BuilderInterface public function getQueries($boolType = null) { if ($boolType === null) { - return array_merge( - $this->container[self::MUST], - $this->container[self::MUST_NOT], - $this->container[self::SHOULD] - ); + $queries = []; + + foreach ($this->container as $item) { + $queries = array_merge($queries, $item); + } + + return $queries; } return $this->container[$boolType]; @@ -104,15 +102,7 @@ class BoolQuery implements BuilderInterface */ public function toArray() { - $output = $this->processArray(); - - if (!$this->isRelevant()) { - /** @var BuilderInterface $query */ - $mustContainer = $this->container[self::MUST]; - $query = array_shift($mustContainer); - - return [$query->getType() => $query->toArray()]; - } + $output = []; foreach ($this->container as $boolType => $builders) { /** @var BuilderInterface $builder */ diff --git a/src/SearchEndpoint/FilterEndpoint.php b/src/SearchEndpoint/FilterEndpoint.php index 2340798ab5bf1f3182cd2a9aaa937877fadd43fe..6b9ba4c793804376792902df1a3657c9b81b706e 100644 --- a/src/SearchEndpoint/FilterEndpoint.php +++ b/src/SearchEndpoint/FilterEndpoint.php @@ -35,14 +35,8 @@ class FilterEndpoint extends QueryEndpoint } $query = new FilteredQuery(); - if (!$this->getBool()->isRelevant()) { - $filters = $this->getBool()->getQueries(BoolFilter::MUST); - $filter = array_shift($filters); - } else { - $filter = $this->getBool(); - } + $query->setFilter($this->getBool()); - $query->setFilter($filter); $this->addReference('filtered_query', $query); } diff --git a/src/SearchEndpoint/PostFilterEndpoint.php b/src/SearchEndpoint/PostFilterEndpoint.php index 5a8d719c16ca4c607c811222dd7db2a03ec4d82c..3fadc68ba23db967a76d0b0505527ba5aff417d1 100644 --- a/src/SearchEndpoint/PostFilterEndpoint.php +++ b/src/SearchEndpoint/PostFilterEndpoint.php @@ -33,12 +33,7 @@ class PostFilterEndpoint extends FilterEndpoint return null; } - if (!$this->getBool()->isRelevant()) { - $filters = $this->getBool()->getQueries(BoolFilter::MUST); - $filter = array_shift($filters); - } else { - $filter = $this->getBool(); - } + $filter = $this->getBool(); return [$filter->getType() => $filter->toArray()]; } diff --git a/src/SearchEndpoint/QueryEndpoint.php b/src/SearchEndpoint/QueryEndpoint.php index e51414ffd1f21590a8d7b7fdd3de6a9ef0f24919..9b610b5190e13c440759438d76c0fbc743db3680 100644 --- a/src/SearchEndpoint/QueryEndpoint.php +++ b/src/SearchEndpoint/QueryEndpoint.php @@ -51,10 +51,7 @@ class QueryEndpoint extends AbstractSearchEndpoint implements OrderedNormalizerI } $queryArray = $this->bool->toArray(); - - if ($this->bool->isRelevant()) { - $queryArray = [$this->bool->getType() => $queryArray]; - } + $queryArray = [$this->bool->getType() => $queryArray]; return $queryArray; } diff --git a/tests/Query/BoolQueryTest.php b/tests/Query/BoolQueryTest.php index 4505857c21c346cf57de1b392849c2d16b47f59a..d6fa12491df7722aa0bc0e63c9485abe583358da 100644 --- a/tests/Query/BoolQueryTest.php +++ b/tests/Query/BoolQueryTest.php @@ -23,40 +23,11 @@ class BoolQueryTest extends \PHPUnit_Framework_TestCase /** * Tests isRelevant method. */ - public function testBoolIsRelevantWithOneQuery() + public function testIsRelevant() { $bool = new BoolQuery(); - $this->assertFalse($bool->isRelevant()); - $bool->add(new TermQuery('acme', 'foo')); - - $this->assertFalse($bool->isRelevant()); - } - - /** - * Tests isRelevant method when there is query added to should case. - */ - public function testBoolIsRelevantWithOneShouldQuery() - { - $bool = new BoolQuery(); - $this->assertFalse($bool->isRelevant()); - $bool->add(new TermQuery('acme', 'foo'), BoolQuery::SHOULD); - $this->assertTrue($bool->isRelevant()); } - - /** - * Tests isRelevant method with 2 queries. - */ - public function testBoolIsRelevantWithTwoQuery() - { - $bool = new BoolQuery(); - $this->assertFalse($bool->isRelevant()); - $bool->add(new TermQuery('acme', 'foo')); - $bool->add(new TermQuery('bar', 'go')); - - $this->assertTrue($bool->isRelevant()); - } - /** * Test for addToBool() without setting a correct bool operator. * diff --git a/tests/Query/NestedQueryTest.php b/tests/Query/NestedQueryTest.php index 69583698a2d5ca641676660f42e5dcbf1060af1b..abf9efa7d398cfefb0565c8f01f31d6d4615588a 100644 --- a/tests/Query/NestedQueryTest.php +++ b/tests/Query/NestedQueryTest.php @@ -11,7 +11,9 @@ namespace ONGR\ElasticsearchDSL\Tests\Unit\DSL\Query; +use ONGR\ElasticsearchDSL\Query\BoolQuery; use ONGR\ElasticsearchDSL\Query\NestedQuery; +use ONGR\ElasticsearchDSL\Query\TermQuery; class NestedQueryTest extends \PHPUnit_Framework_TestCase { @@ -57,4 +59,30 @@ class NestedQueryTest extends \PHPUnit_Framework_TestCase $this->assertTrue(method_exists($nestedQuery, 'hasParameter'), 'Nested query must have hasParameter method'); $this->assertTrue(method_exists($nestedQuery, 'getParameter'), 'Nested query must have getParameter method'); } + + /** + * Test for toArray() in case bool with single clause given. + */ + public function testSingleBoolMust() + { + $bool = new BoolQuery(); + $bool->add(new TermQuery('field1', 'value1')); + + $query = new NestedQuery('obj1', $bool); + + $expected = [ + 'path' => 'obj1', + 'query' => [ + 'bool' => [ + 'must' => [ + [ + 'term' => ['field1' => 'value1'], + ], + ], + ], + ], + ]; + + $this->assertEquals($expected, $query->toArray()); + } } diff --git a/tests/SearchEndpoint/FilterEndpointTest.php b/tests/SearchEndpoint/FilterEndpointTest.php index 814fbd17853f45965dd3bbbfeca28bd19ba4d9aa..5e3b4ade0408c7de33f42e8dde97b49b512a2129 100644 --- a/tests/SearchEndpoint/FilterEndpointTest.php +++ b/tests/SearchEndpoint/FilterEndpointTest.php @@ -64,7 +64,15 @@ class FilterEndpointTest extends \PHPUnit_Framework_TestCase /** @var FilteredQuery $reference */ $reference = $instance->getReference('filtered_query'); $this->assertInstanceOf('ONGR\ElasticsearchDSL\Query\FilteredQuery', $reference); - $this->assertSame($matchAllFilter, $reference->getFilter()); + + /** @var \ONGR\ElasticsearchDSL\Query\BoolQuery $bool */ + $bool = $reference->getFilter(); + $this->assertInstanceOf('ONGR\ElasticsearchDSL\Query\BoolQuery', $bool); + + $must = $bool->getQueries('must'); + $realReference = reset($must); + + $this->assertSame($matchAllFilter, $realReference); } /** diff --git a/tests/SearchEndpoint/PostFilterEndpointTest.php b/tests/SearchEndpoint/PostFilterEndpointTest.php index 75236a108edb30d8d6063be693400ede10bd613d..7c22d4acb5edd22616ddddc6e0d0aed1d4d623c5 100644 --- a/tests/SearchEndpoint/PostFilterEndpointTest.php +++ b/tests/SearchEndpoint/PostFilterEndpointTest.php @@ -54,7 +54,7 @@ class PostFilterEndpointTest extends \PHPUnit_Framework_TestCase $instance->add($matchAll); $this->assertEquals( - json_encode([$matchAll->getType() => $matchAll->toArray()]), + json_encode(['bool' => ['must' => [[$matchAll->getType() => $matchAll->toArray()]]]]), json_encode($instance->normalize($normalizerInterface)) ); } diff --git a/tests/SearchEndpoint/QueryEndpointTest.php b/tests/SearchEndpoint/QueryEndpointTest.php index 71dfd090cc94e1d1915b8aca15294597d4c54535..997f82f75c9010fca0a18e4a15074ac8f74f263a 100644 --- a/tests/SearchEndpoint/QueryEndpointTest.php +++ b/tests/SearchEndpoint/QueryEndpointTest.php @@ -55,7 +55,7 @@ class QueryEndpointTest extends \PHPUnit_Framework_TestCase $instance->add($matchAll); $this->assertEquals( - [$matchAll->getType() => $matchAll->toArray()], + ['bool' => ['must' => [[$matchAll->getType() => $matchAll->toArray()]]]], $instance->normalize($normalizerInterface) ); }