1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
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
|
|
|
} |
86
|
|
|
|