Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed | Nested query with bool doesn't work when there is only one MU… #42

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 7 additions & 1 deletion src/Query/BoolQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,13 @@ public function toArray()
$mustContainer = $this->container[self::MUST];
$query = array_shift($mustContainer);

return [$query->getType() => $query->toArray()];
if ($query->getType() == 'match') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't fix the bug. The match query is just like an example of any query. This bug occurs on all other queries also.

$output = [self::MUST => [$query->getType() => $query->toArray()]];
} else {
$output = [$query->getType() => $query->toArray()];
}

return $output;
}

foreach ($this->container as $boolType => $builders) {
Expand Down
102 changes: 92 additions & 10 deletions tests/Query/NestedQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,32 @@

namespace ONGR\ElasticsearchDSL\Tests\Unit\DSL\Query;

use ONGR\ElasticsearchDSL\Query\BoolQuery;
use ONGR\ElasticsearchDSL\Query\MatchQuery;
use ONGR\ElasticsearchDSL\Query\NestedQuery;
use ONGR\ElasticsearchDSL\Query\RangeQuery;
use ONGR\ElasticsearchDSL\Search;

class NestedQueryTest extends \PHPUnit_Framework_TestCase
{

/**
* Tests toArray method.
*/
public function testToArray()
{
$missingFilterMock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Filter\MissingFilter')
->setConstructorArgs(['test_field'])
->getMock();
->setConstructorArgs(['test_field'])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does not look like intentional changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automatic reformatting code in my IDE. I turned off.

->getMock();
$missingFilterMock->expects($this->any())
->method('getType')
->willReturn('test_type');
->method('getType')
->willReturn('test_type');
$missingFilterMock->expects($this->any())
->method('toArray')
->willReturn(['testKey' => 'testValue']);
->method('toArray')
->willReturn(['testKey' => 'testValue']);

$result = [
'path' => 'test_path',
'path' => 'test_path',
'query' => [
'test_type' => ['testKey' => 'testValue'],
],
Expand All @@ -47,14 +52,91 @@ public function testToArray()
public function testParameters()
{
$nestedQuery = $this->getMockBuilder('ONGR\ElasticsearchDSL\Query\NestedQuery')
->disableOriginalConstructor()
->setMethods(null)
->getMock();
->disableOriginalConstructor()
->setMethods(null)
->getMock();

$this->assertTrue(method_exists($nestedQuery, 'addParameter'), 'Nested query must have addParameter method');
$this->assertTrue(method_exists($nestedQuery, 'setParameters'), 'Nested query must have setParameters method');
$this->assertTrue(method_exists($nestedQuery, 'getParameters'), 'Nested query must have getParameters method');
$this->assertTrue(method_exists($nestedQuery, 'hasParameter'), 'Nested query must have hasParameter method');
$this->assertTrue(method_exists($nestedQuery, 'getParameter'), 'Nested query must have getParameter method');
}

/**
* Tests if Nested Query has 1 Boolean query.
*/
public function testWith1Boolean()
{

// Case 1: With one Bool Query
$matchQuery = new MatchQuery('some.field', 'someValue');

$boolQuery = new BoolQuery();
$boolQuery->add($matchQuery, BoolQuery::MUST);

$nestedQuery = new NestedQuery('urls', $boolQuery);

$search = new Search();
$search->addQuery($nestedQuery);

$expected = [
'query' => [
'nested' => [
'path' => 'urls',
'query' => [
'bool' => [
'must' => [
'match' => ['some.field' => ['query' => 'someValue']]
]
]
]
]
]
];

$this->assertEquals($expected, $search->toArray());
}

/**
* Tests if Nested Query has 2 Boolean queries.
*/
public function testWith2Boolean()
{
$matchQuery = new MatchQuery('obj1.name', 'blue');
$rangeQuery = new RangeQuery('obj1.count', ['gt' => 5]);

$boolQuery = new BoolQuery();
$boolQuery->add($matchQuery);
$boolQuery->add($rangeQuery);

$nestedQuery = new NestedQuery('obj1', $boolQuery);
$nestedQuery->addParameter('score_mode', 'avg');

$search = new Search();
$search->addQuery($nestedQuery);

$expected = [
'query' => [
'nested' => [
'path' => 'obj1',
'score_mode' => 'avg',
'query' => [
'bool' => [
'must' => [
[
'match' => ['obj1.name' => ['query' => 'blue']]
],
[
'range' => ['obj1.count' => ['gt' => 5]]
]
]
]
]
]
]
];

$this->assertEquals($expected, $search->toArray());
}
}