From bda39b5b99539892696b4abdc8f3f1115f5c7272 Mon Sep 17 00:00:00 2001 From: Floris ten Hove Date: Thu, 5 Jun 2025 14:03:47 +0200 Subject: [PATCH] Add isEmpty method to BoolQuery --- src/Queries/BoolQuery.php | 6 +- tests/Queries/BoolQueryTest.php | 131 ++++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 tests/Queries/BoolQueryTest.php diff --git a/src/Queries/BoolQuery.php b/src/Queries/BoolQuery.php index 1e24ea4..d4afe9f 100644 --- a/src/Queries/BoolQuery.php +++ b/src/Queries/BoolQuery.php @@ -49,9 +49,13 @@ public function toArray(): array $bool["minimum_should_match"] = $this->minimumShouldMatch; } - return [ 'bool' => array_filter($bool), ]; } + + public function isEmpty(): bool + { + return empty($this->must) && empty($this->filter) && empty($this->should) && empty($this->must_not); + } } diff --git a/tests/Queries/BoolQueryTest.php b/tests/Queries/BoolQueryTest.php new file mode 100644 index 0000000..cc1a52a --- /dev/null +++ b/tests/Queries/BoolQueryTest.php @@ -0,0 +1,131 @@ +assertInstanceOf(BoolQuery::class, $query); + } + + public function testAddMustQuery(): void + { + $query = BoolQuery::create() + ->add(new TermQuery('field', 'value')); + + $this->assertEquals([ + 'bool' => [ + 'must' => [ + ['term' => ['field' => 'value']], + ], + ], + ], $query->toArray()); + } + + public function testAddFilterQuery(): void + { + $query = BoolQuery::create() + ->add(new TermQuery('field', 'value'), 'filter'); + + $this->assertEquals([ + 'bool' => [ + 'filter' => [ + ['term' => ['field' => 'value']], + ], + ], + ], $query->toArray()); + } + + public function testAddShouldQuery(): void + { + $query = BoolQuery::create() + ->add(new TermQuery('field', 'value'), 'should'); + + $this->assertEquals([ + 'bool' => [ + 'should' => [ + ['term' => ['field' => 'value']], + ], + ], + ], $query->toArray()); + } + + public function testAddMustNotQuery(): void + { + $query = BoolQuery::create() + ->add(new TermQuery('field', 'value'), 'must_not'); + + $this->assertEquals([ + 'bool' => [ + 'must_not' => [ + ['term' => ['field' => 'value']], + ], + ], + ], $query->toArray()); + } + + public function testAddMultipleQueries(): void + { + $query = BoolQuery::create() + ->add(new TermQuery('field1', 'value1')) + ->add(new TermQuery('field2', 'value2'), 'filter') + ->add(new TermQuery('field3', 'value3'), 'should') + ->add(new TermQuery('field4', 'value4'), 'must_not'); + + $this->assertEquals([ + 'bool' => [ + 'must' => [ + ['term' => ['field1' => 'value1']], + ], + 'filter' => [ + ['term' => ['field2' => 'value2']], + ], + 'should' => [ + ['term' => ['field3' => 'value3']], + ], + 'must_not' => [ + ['term' => ['field4' => 'value4']], + ], + ], + ], $query->toArray()); + } + + + public function testMinimumShouldMatch(): void + { + $query = BoolQuery::create() + ->add(new TermQuery('field1', 'value1'), 'should') + ->add(new TermQuery('field2', 'value2'), 'should') + ->minimumShouldMatch('1'); + + $this->assertEquals([ + 'bool' => [ + 'should' => [ + ['term' => ['field1' => 'value1']], + ['term' => ['field2' => 'value2']], + ], + 'minimum_should_match' => '1', + ], + ], $query->toArray()); + + $this->assertFalse($query->isEmpty()); + } + + public function testIsEmpty(): void + { + $query = BoolQuery::create(); + + $this->assertTrue($query->isEmpty()); + + $query->add(new TermQuery('field', 'value')); + + $this->assertFalse($query->isEmpty()); + } +}