diff --git a/src/Query/BoolQuery.php b/src/Query/BoolQuery.php index 0c70493f7c02ac18965dd87ab592f4ab2be1795a..ed65f902a42796bb186b833b3e8877b2a15e4272 100644 --- a/src/Query/BoolQuery.php +++ b/src/Query/BoolQuery.php @@ -17,7 +17,7 @@ use ONGR\ElasticsearchDSL\ParametersTrait; /** * Represents Elasticsearch "bool" query. * - * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html */ class BoolQuery implements BuilderInterface { @@ -26,11 +26,12 @@ class BoolQuery implements BuilderInterface const MUST = 'must'; const MUST_NOT = 'must_not'; const SHOULD = 'should'; + const FILTER = 'filter'; /** * @var array */ - private $container; + private $container = []; /** * Constructor to prepare container. @@ -84,7 +85,7 @@ class BoolQuery implements BuilderInterface */ public function add(BuilderInterface $query, $type = self::MUST, $key = null) { - if (!in_array($type, (new \ReflectionObject($this))->getConstants())) { + if (!in_array($type, [self::MUST, self::MUST_NOT, self::SHOULD, self::FILTER])) { throw new \UnexpectedValueException(sprintf('The bool operator %s is not supported', $type)); } diff --git a/tests/Query/BoolQueryTest.php b/tests/Query/BoolQueryTest.php index d6fa12491df7722aa0bc0e63c9485abe583358da..dfe985196653b35c5a4ea08994e3cbaf4aeba2b2 100644 --- a/tests/Query/BoolQueryTest.php +++ b/tests/Query/BoolQueryTest.php @@ -84,4 +84,31 @@ class BoolQueryTest extends \PHPUnit_Framework_TestCase $result = $bool->getType(); $this->assertEquals('bool', $result); } + + /** + * Tests bool query in filter context. + */ + public function testBoolInFilterContext() + { + $bool = new BoolQuery(); + $bool->add(new TermQuery('key1', 'value1'), BoolQuery::FILTER); + $bool->add(new TermQuery('key2', 'value2'), BoolQuery::MUST); + $expected = [ + 'filter' => [ + [ + 'term' => [ + 'key1' => 'value1', + ], + ], + ], + 'must' => [ + [ + 'term' => [ + 'key2' => 'value2', + ], + ], + ], + ]; + $this->assertEquals($expected, $bool->toArray()); + } }