1
|
|
|
<?php namespace Nord\Lumen\Elasticsearch\Search\Query\Joining; |
2
|
|
|
|
3
|
|
|
use Nord\Lumen\Elasticsearch\Exceptions\InvalidArgument; |
4
|
|
|
use Nord\Lumen\Elasticsearch\Search\Query\QueryDSL; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* The has_child filter accepts a query and the child type to run against, and results in parent documents that have |
8
|
|
|
* child docs matching the query. |
9
|
|
|
* |
10
|
|
|
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-child-query.html |
11
|
|
|
*/ |
12
|
|
|
class HasChildQuery extends AbstractQuery |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $type; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var int |
22
|
|
|
*/ |
23
|
|
|
private $minChildren; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var int |
27
|
|
|
*/ |
28
|
|
|
private $maxChildren; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var QueryDSL |
32
|
|
|
*/ |
33
|
|
|
private $query; |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @inheritdoc |
38
|
|
|
*/ |
39
|
|
|
public function toArray() |
40
|
|
|
{ |
41
|
|
|
$hasChild = [ |
42
|
|
|
'type' => $this->getType(), |
43
|
|
|
'query' => $this->getQuery()->toArray(), |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
$scoreMode = $this->getScoreMode(); |
47
|
|
|
if (!is_null($scoreMode)) { |
48
|
|
|
$hasChild['score_mode'] = $scoreMode; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$minChildren = $this->getMinChildren(); |
52
|
|
|
if (!is_null($minChildren)) { |
53
|
|
|
$hasChild['min_children'] = $minChildren; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$maxChildren = $this->getMaxChildren(); |
57
|
|
|
if (!is_null($maxChildren)) { |
58
|
|
|
$hasChild['max_children'] = $maxChildren; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return ['has_child' => $hasChild]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @inheritdoc |
66
|
|
|
*/ |
67
|
|
View Code Duplication |
protected function getValidScoreModes() |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
return [ |
70
|
|
|
self::SCORE_MODE_AVG, |
71
|
|
|
self::SCORE_MODE_SUM, |
72
|
|
|
self::SCORE_MODE_MIN, |
73
|
|
|
self::SCORE_MODE_MAX, |
74
|
|
|
self::SCORE_MODE_NONE, |
75
|
|
|
]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $type |
80
|
|
|
* @return HasChildQuery |
81
|
|
|
*/ |
82
|
|
|
public function setType($type) |
83
|
|
|
{ |
84
|
|
|
$this->type = $type; |
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public function getType() |
93
|
|
|
{ |
94
|
|
|
return $this->type; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param int $minChildren |
100
|
|
|
* @return HasChildQuery |
101
|
|
|
*/ |
102
|
|
|
public function setMinChildren($minChildren) |
103
|
|
|
{ |
104
|
|
|
$this->assertMinChildren($minChildren); |
105
|
|
|
$this->minChildren = $minChildren; |
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return int |
112
|
|
|
*/ |
113
|
|
|
public function getMinChildren() |
114
|
|
|
{ |
115
|
|
|
return $this->minChildren; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param int $maxChildren |
121
|
|
|
* @return HasChildQuery |
122
|
|
|
*/ |
123
|
|
|
public function setMaxChildren($maxChildren) |
124
|
|
|
{ |
125
|
|
|
$this->assertMaxChildren($maxChildren); |
126
|
|
|
$this->maxChildren = $maxChildren; |
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return int |
133
|
|
|
*/ |
134
|
|
|
public function getMaxChildren() |
135
|
|
|
{ |
136
|
|
|
return $this->maxChildren; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param QueryDSL $query |
142
|
|
|
* @return HasChildQuery |
143
|
|
|
*/ |
144
|
|
|
public function setQuery(QueryDSL $query) |
145
|
|
|
{ |
146
|
|
|
$this->query = $query; |
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return QueryDSL |
153
|
|
|
*/ |
154
|
|
|
public function getQuery() |
155
|
|
|
{ |
156
|
|
|
return $this->query; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param int $minChildren |
162
|
|
|
* @throws InvalidArgument |
163
|
|
|
*/ |
164
|
|
|
protected function assertMinChildren($minChildren) |
165
|
|
|
{ |
166
|
|
|
if (!is_int($minChildren)) { |
167
|
|
|
throw new InvalidArgument(sprintf( |
168
|
|
|
'HasChild Query `min_children` must be an integer, "%s" given.', |
169
|
|
|
gettype($minChildren) |
170
|
|
|
)); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param int $maxChildren |
177
|
|
|
* @throws InvalidArgument |
178
|
|
|
*/ |
179
|
|
|
protected function assertMaxChildren($maxChildren) |
180
|
|
|
{ |
181
|
|
|
if (!is_int($maxChildren)) { |
182
|
|
|
throw new InvalidArgument(sprintf( |
183
|
|
|
'HasChild Query `max_children` must be an integer, "%s" given.', |
184
|
|
|
gettype($maxChildren) |
185
|
|
|
)); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.