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
59f9e445
Unverified
Commit
59f9e445
authored
5 years ago
by
Milroy E. Fraser
Committed by
Simonas Šerlinskas
5 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Implemented TermsSetQuery
parent
7ddc361f
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/TermLevel/TermsSetQuery.php
+86
-0
86 additions, 0 deletions
src/Query/TermLevel/TermsSetQuery.php
tests/Unit/Query/TermLevel/TermsSetQueryTest.php
+47
-0
47 additions, 0 deletions
tests/Unit/Query/TermLevel/TermsSetQueryTest.php
with
133 additions
and
0 deletions
src/Query/TermLevel/TermsSetQuery.php
0 → 100644
+
86
−
0
View file @
59f9e445
<?php
/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace
ONGR\ElasticsearchDSL\Query\TermLevel
;
use
ONGR\ElasticsearchDSL\BuilderInterface
;
use
ONGR\ElasticsearchDSL\ParametersTrait
;
/**
* Represents Elasticsearch "terms_set" query.
*
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-set-query.html
*/
class
TermsSetQuery
implements
BuilderInterface
{
use
ParametersTrait
;
const
MINIMUM_SHOULD_MATCH_TYPE_FIELD
=
'minimum_should_match_field'
;
const
MINIMUM_SHOULD_MATCH_TYPE_SCRIPT
=
'minimum_should_match_script'
;
/**
* @var string
*/
private
$field
;
/**
* @var array
*/
private
$terms
;
/**
* Constructor.
*
* @param string $field Field name
* @param array $terms An array of terms
* @param array $parameters Parameters
*/
public
function
__construct
(
$field
,
$terms
,
array
$parameters
)
{
$this
->
field
=
$field
;
$this
->
terms
=
$terms
;
$this
->
validateParameters
(
$parameters
);
$this
->
setParameters
(
$parameters
);
}
/**
* {@inheritdoc}
*/
public
function
getType
()
{
return
'terms_set'
;
}
/**
* {@inheritdoc}
*/
public
function
toArray
()
{
$query
=
[
'terms'
=>
$this
->
terms
,
];
return
[
$this
->
getType
()
=>
[
$this
->
field
=>
$this
->
processArray
(
$query
),
]];
}
private
function
validateParameters
(
array
$parameters
)
{
if
(
!
isset
(
$parameters
[
self
::
MINIMUM_SHOULD_MATCH_TYPE_FIELD
])
&&
!
isset
(
$parameters
[
self
::
MINIMUM_SHOULD_MATCH_TYPE_SCRIPT
])
)
{
$message
=
"Either minimum_should_match_field or minimum_should_match_script must be set."
;
throw
new
\InvalidArgumentException
(
$message
);
}
}
}
This diff is collapsed.
Click to expand it.
tests/Unit/Query/TermLevel/TermsSetQueryTest.php
0 → 100644
+
47
−
0
View file @
59f9e445
<?php
/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace
ONGR\ElasticsearchDSL\Tests\Unit\Query\TermLevel
;
use
ONGR\ElasticsearchDSL\Query\TermLevel\TermsSetQuery
;
class
TermsSetQueryTest
extends
\PHPUnit\Framework\TestCase
{
/**
* Tests toArray().
*/
public
function
testToArray
()
{
$terms
=
[
'php'
,
'c++'
,
'java'
];
$parameters
=
[
'minimum_should_match_field'
=>
'required_matches'
];
$query
=
new
TermsSetQuery
(
'programming_languages'
,
$terms
,
$parameters
);
$expected
=
[
'terms_set'
=>
[
'programming_languages'
=>
[
'terms'
=>
[
'php'
,
'c++'
,
'java'
],
'minimum_should_match_field'
=>
'required_matches'
,
]
],
];
$this
->
assertEquals
(
$expected
,
$query
->
toArray
());
}
public
function
testItThrowsAaExceptionWhenMinimumShouldMatchFieldOrMinimumShouldMatchScriptIsNotGiven
()
{
$message
=
"Either minimum_should_match_field or minimum_should_match_script must be set."
;
$this
->
expectException
(
\InvalidArgumentException
::
class
);
$this
->
expectExceptionMessage
(
$message
);
$terms
=
[
'php'
,
'c++'
,
'java'
];
new
TermsSetQuery
(
'programming_languages'
,
$terms
,
[]);
}
}
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