Skip to content

Commit b4c3c89

Browse files
committed
Merge remote-tracking branch 'remotes/origin/7.x'
2 parents 3aa8cbb + e2d8935 commit b4c3c89

File tree

5 files changed

+140
-9
lines changed

5 files changed

+140
-9
lines changed

composer.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@
1111
}
1212
],
1313
"require": {
14-
"php": "^7.1",
14+
"php": "^7.0",
1515
"symfony/serializer": "^3.0|^4.0",
1616
"paragonie/random_compat": "*"
1717
},
1818
"require-dev": {
1919
"elasticsearch/elasticsearch": "^7.0",
2020
"phpunit/phpunit": "~6.0",
21-
"squizlabs/php_codesniffer": "^3.0",
22-
"php-coveralls/php-coveralls": "^2.1"
21+
"squizlabs/php_codesniffer": "^3.0"
2322
},
2423
"suggest": {
2524
"elasticsearch/elasticsearch": "This library is for elasticsearch/elasticsearch client to enhance it with DSL functionality."

src/ParametersTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function removeParameter($name)
5050
*
5151
* @param string $name
5252
*
53-
* @return array|string|int|bool|\stdClass
53+
* @return array|string|int|float|bool|\stdClass
5454
*/
5555
public function getParameter($name)
5656
{
@@ -69,7 +69,7 @@ public function getParameters()
6969

7070
/**
7171
* @param string $name
72-
* @param array|string|int|bool|\stdClass $value
72+
* @param array|string|int|float|bool|\stdClass $value
7373
*/
7474
public function addParameter($name, $value)
7575
{
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 (!isset($parameters[self::MINIMUM_SHOULD_MATCH_TYPE_FIELD]) &&
79+
!isset($parameters[self::MINIMUM_SHOULD_MATCH_TYPE_SCRIPT])
80+
) {
81+
$message = "Either minimum_should_match_field or minimum_should_match_script must be set.";
82+
throw new \InvalidArgumentException($message);
83+
}
84+
}
85+
}

src/Search.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -458,15 +458,15 @@ public function getSuggests()
458458
}
459459

460460
/**
461-
* @return int
461+
* @return null|int
462462
*/
463463
public function getFrom()
464464
{
465465
return $this->from;
466466
}
467467

468468
/**
469-
* @param int $from
469+
* @param null|int $from
470470
*
471471
* @return $this
472472
*/
@@ -498,15 +498,15 @@ public function setTrackTotalHits(bool $trackTotalHits)
498498
}
499499

500500
/**
501-
* @return int
501+
* @return null|int
502502
*/
503503
public function getSize()
504504
{
505505
return $this->size;
506506
}
507507

508508
/**
509-
* @param int $size
509+
* @param null|int $size
510510
*
511511
* @return $this
512512
*/
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)