Skip to content

Commit 59f9e44

Browse files
milroyfrasersaimaz
authored andcommitted
Implemented TermsSetQuery
1 parent 7ddc361 commit 59f9e44

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the ONGR package.
5+
*
6+
* (c) NFQ Technologies UAB <info@nfq.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace ONGR\ElasticsearchDSL\Query\TermLevel;
13+
14+
use ONGR\ElasticsearchDSL\BuilderInterface;
15+
use ONGR\ElasticsearchDSL\ParametersTrait;
16+
17+
/**
18+
* Represents Elasticsearch "terms_set" query.
19+
*
20+
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-set-query.html
21+
*/
22+
class TermsSetQuery implements BuilderInterface
23+
{
24+
use ParametersTrait;
25+
26+
const MINIMUM_SHOULD_MATCH_TYPE_FIELD = 'minimum_should_match_field';
27+
const MINIMUM_SHOULD_MATCH_TYPE_SCRIPT = 'minimum_should_match_script';
28+
29+
/**
30+
* @var string
31+
*/
32+
private $field;
33+
34+
/**
35+
* @var array
36+
*/
37+
private $terms;
38+
39+
/**
40+
* Constructor.
41+
*
42+
* @param string $field Field name
43+
* @param array $terms An array of terms
44+
* @param array $parameters Parameters
45+
*/
46+
public function __construct($field, $terms, array $parameters)
47+
{
48+
$this->field = $field;
49+
$this->terms = $terms;
50+
$this->validateParameters($parameters);
51+
$this->setParameters($parameters);
52+
}
53+
54+
/**
55+
* {@inheritdoc}
56+
*/
57+
public function getType()
58+
{
59+
return 'terms_set';
60+
}
61+
62+
/**
63+
* {@inheritdoc}
64+
*/
65+
public function toArray()
66+
{
67+
$query = [
68+
'terms' => $this->terms,
69+
];
70+
71+
return [$this->getType() => [
72+
$this->field => $this->processArray($query),
73+
]];
74+
}
75+
76+
private function validateParameters(array $parameters)
77+
{
78+
if (
79+
!isset($parameters[self::MINIMUM_SHOULD_MATCH_TYPE_FIELD]) &&
80+
!isset($parameters[self::MINIMUM_SHOULD_MATCH_TYPE_SCRIPT])
81+
) {
82+
$message = "Either minimum_should_match_field or minimum_should_match_script must be set.";
83+
throw new \InvalidArgumentException($message);
84+
}
85+
}
86+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the ONGR package.
5+
*
6+
* (c) NFQ Technologies UAB <info@nfq.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\TermLevel;
13+
14+
use ONGR\ElasticsearchDSL\Query\TermLevel\TermsSetQuery;
15+
16+
class TermsSetQueryTest extends \PHPUnit\Framework\TestCase
17+
{
18+
/**
19+
* Tests toArray().
20+
*/
21+
public function testToArray()
22+
{
23+
$terms = ['php', 'c++', 'java'];
24+
$parameters = ['minimum_should_match_field' => 'required_matches'];
25+
$query = new TermsSetQuery('programming_languages', $terms, $parameters);
26+
$expected = [
27+
'terms_set' => [
28+
'programming_languages' => [
29+
'terms' => ['php', 'c++', 'java'],
30+
'minimum_should_match_field' => 'required_matches',
31+
]
32+
],
33+
];
34+
35+
$this->assertEquals($expected, $query->toArray());
36+
}
37+
38+
public function testItThrowsAaExceptionWhenMinimumShouldMatchFieldOrMinimumShouldMatchScriptIsNotGiven()
39+
{
40+
$message = "Either minimum_should_match_field or minimum_should_match_script must be set.";
41+
$this->expectException(\InvalidArgumentException::class);
42+
$this->expectExceptionMessage($message);
43+
44+
$terms = ['php', 'c++', 'java'];
45+
new TermsSetQuery('programming_languages', $terms, []);
46+
}
47+
}

0 commit comments

Comments
 (0)