Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
E
Elasticsearch DSL
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Public
Elasticsearch DSL
Commits
37709576
Commit
37709576
authored
5 years ago
by
Timothy Winters
Committed by
Simonas Šerlinskas
5 years ago
Browse files
Options
Downloads
Patches
Plain Diff
BoolQuery constructor allows building container queries (#293)
parent
01f8fe81
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/Query/Compound/BoolQuery.php
+10
-2
10 additions, 2 deletions
src/Query/Compound/BoolQuery.php
tests/Unit/Query/Compound/BoolQueryTest.php
+60
-0
60 additions, 0 deletions
tests/Unit/Query/Compound/BoolQueryTest.php
with
70 additions
and
2 deletions
src/Query/Compound/BoolQuery.php
+
10
−
2
View file @
37709576
...
...
@@ -35,10 +35,18 @@ class BoolQuery implements BuilderInterface
/**
* Constructor to prepare container.
*
* @param array $container
*/
public
function
__construct
()
public
function
__construct
(
array
$container
=
[]
)
{
$this
->
container
=
[];
foreach
(
$container
as
$type
=>
$queries
)
{
$queries
=
is_array
(
$queries
)
?
$queries
:
[
$queries
];
array_walk
(
$queries
,
function
(
$query
)
use
(
$type
)
{
$this
->
add
(
$query
,
$type
);
});
}
}
/**
...
...
This diff is collapsed.
Click to expand it.
tests/Unit/Query/Compound/BoolQueryTest.php
+
60
−
0
View file @
37709576
...
...
@@ -32,6 +32,66 @@ class BoolQueryTest extends \PHPUnit\Framework\TestCase
$bool
->
add
(
new
MatchAllQuery
(),
'acme'
);
}
/**
* Tests constructor builds container
*/
public
function
testBoolConstructor
()
{
$bool
=
new
BoolQuery
([
BoolQuery
::
SHOULD
=>
[
new
TermQuery
(
'key1'
,
'value1'
)],
BoolQuery
::
MUST
=>
[
new
TermQuery
(
'key2'
,
'value2'
),
new
TermQuery
(
'key3'
,
'value3'
),
],
BoolQuery
::
MUST_NOT
=>
new
TermQuery
(
'key4'
,
'value4'
)
]);
$expected
=
[
'bool'
=>
[
'should'
=>
[
[
'term'
=>
[
'key1'
=>
'value1'
,
],
],
],
'must'
=>
[
[
'term'
=>
[
'key2'
=>
'value2'
,
],
],
[
'term'
=>
[
'key3'
=>
'value3'
,
],
],
],
'must_not'
=>
[
[
'term'
=>
[
'key4'
=>
'value4'
,
],
],
],
],
];
$this
->
assertEquals
(
$expected
,
$bool
->
toArray
());
}
/**
* Tests exception thrown if invalid BoolQuery type key is specified
*
* @expectedException \UnexpectedValueException
* @expectedExceptionMessage The bool operator acme is not supported
*/
public
function
testBoolConstructorException
()
{
new
BoolQuery
([
'acme'
=>
[
new
TermQuery
(
'key1'
,
'value1'
)],
]);
}
/**
* Tests toArray() method.
*/
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment