Skip to content
Snippets Groups Projects
Commit a644572a authored by Simonas Šerlinskas's avatar Simonas Šerlinskas
Browse files

Merge branch 'mvar-bool_filter'

parents 738f7a36 f120fecf
No related branches found
No related tags found
No related merge requests found
...@@ -17,7 +17,7 @@ use ONGR\ElasticsearchDSL\ParametersTrait; ...@@ -17,7 +17,7 @@ use ONGR\ElasticsearchDSL\ParametersTrait;
/** /**
* Represents Elasticsearch "bool" query. * 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 class BoolQuery implements BuilderInterface
{ {
...@@ -26,11 +26,12 @@ class BoolQuery implements BuilderInterface ...@@ -26,11 +26,12 @@ class BoolQuery implements BuilderInterface
const MUST = 'must'; const MUST = 'must';
const MUST_NOT = 'must_not'; const MUST_NOT = 'must_not';
const SHOULD = 'should'; const SHOULD = 'should';
const FILTER = 'filter';
/** /**
* @var array * @var array
*/ */
private $container; private $container = [];
/** /**
* Constructor to prepare container. * Constructor to prepare container.
...@@ -84,7 +85,7 @@ class BoolQuery implements BuilderInterface ...@@ -84,7 +85,7 @@ class BoolQuery implements BuilderInterface
*/ */
public function add(BuilderInterface $query, $type = self::MUST, $key = null) 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)); throw new \UnexpectedValueException(sprintf('The bool operator %s is not supported', $type));
} }
......
...@@ -84,4 +84,31 @@ class BoolQueryTest extends \PHPUnit_Framework_TestCase ...@@ -84,4 +84,31 @@ class BoolQueryTest extends \PHPUnit_Framework_TestCase
$result = $bool->getType(); $result = $bool->getType();
$this->assertEquals('bool', $result); $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());
}
} }
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