-
Notifications
You must be signed in to change notification settings - Fork 201
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
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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']) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does not look like intentional changes. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'], | ||
], | ||
|
@@ -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()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.