Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Queries/BoolQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
131 changes: 131 additions & 0 deletions tests/Queries/BoolQueryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

namespace Queries;

use PHPUnit\Framework\TestCase;
use Spatie\ElasticsearchQueryBuilder\Queries\BoolQuery;
use Spatie\ElasticsearchQueryBuilder\Queries\TermQuery;

final class BoolQueryTest extends TestCase
{
public function testCreate(): void
{
$query = BoolQuery::create();

$this->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());
}
}