|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Elastica\Query; |
|
4
|
|
|
|
|
5
|
|
|
use Elastica\Exception\InvalidException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Terms query. |
|
9
|
|
|
* |
|
10
|
|
|
* @author Nicolas Ruflin <[email protected]> |
|
11
|
|
|
* @author Roberto Nygaard <[email protected]> |
|
12
|
|
|
* |
|
13
|
|
|
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html |
|
14
|
|
|
*/ |
|
15
|
|
|
class Terms extends AbstractQuery |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
private $field; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string[] |
|
24
|
|
|
*/ |
|
25
|
|
|
private $terms; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string[]|null |
|
29
|
|
|
*/ |
|
30
|
|
|
private $lookup; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param string[] $terms Terms list, leave empty if building a terms-lookup query |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(string $field, array $terms = []) |
|
36
|
|
|
{ |
|
37
|
|
|
if (empty($field)) { |
|
38
|
|
|
throw new InvalidException('Terms field name has to be set'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$this->field = $field; |
|
42
|
|
|
$this->terms = $terms; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Commodity function to build a Terms query, preconfigured for terms lookup. |
|
47
|
|
|
*/ |
|
48
|
|
|
public static function buildTermsLookup(string $field, string $index, string $id, string $path): self |
|
49
|
|
|
{ |
|
50
|
|
|
$t = new self($field); |
|
51
|
|
|
|
|
52
|
|
|
return $t->setTermsLookup($index, $id, $path); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Sets terms for the query. |
|
57
|
|
|
* |
|
58
|
|
|
* @param string[] |
|
59
|
|
|
*/ |
|
60
|
|
|
public function setTerms(array $terms): self |
|
61
|
|
|
{ |
|
62
|
|
|
$this->terms = $terms; |
|
63
|
|
|
|
|
64
|
|
|
return $this; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Adds a single term to the list. |
|
69
|
|
|
*/ |
|
70
|
|
|
public function addTerm(string $term): self |
|
71
|
|
|
{ |
|
72
|
|
|
$this->terms[] = $term; |
|
73
|
|
|
|
|
74
|
|
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function setTermsLookup(string $index, string $id, string $path): self |
|
78
|
|
|
{ |
|
79
|
|
|
$this->lookup = [ |
|
|
|
|
|
|
80
|
|
|
'index' => $index, |
|
81
|
|
|
'id' => $id, |
|
82
|
|
|
'path' => $path, |
|
83
|
|
|
]; |
|
84
|
|
|
|
|
85
|
|
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* {@inheritdoc} |
|
90
|
|
|
*/ |
|
91
|
|
|
public function toArray(): array |
|
92
|
|
|
{ |
|
93
|
|
|
if (null !== $this->lookup && \count($this->terms)) { |
|
94
|
|
|
throw new InvalidException('Unable to build Terms query: only one of terms or lookup properties should be set'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
if (null !== $this->lookup) { |
|
98
|
|
|
$this->setParam($this->field, $this->lookup); |
|
99
|
|
|
} else { |
|
100
|
|
|
$this->setParam($this->field, $this->terms); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return parent::toArray(); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..