1
|
|
|
<?php namespace Nord\Lumen\Elasticsearch\Search\Query\Joining; |
2
|
|
|
|
3
|
|
|
use Nord\Lumen\Elasticsearch\Exceptions\InvalidArgument; |
4
|
|
|
use Nord\Lumen\Elasticsearch\Search\Query\ScoreMode; |
5
|
|
|
use Nord\Lumen\Elasticsearch\Search\Query\Traits\HasType; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* The has_child filter accepts a query and the child type to run against, and results in parent documents that have |
9
|
|
|
* child docs matching the query. |
10
|
|
|
* |
11
|
|
|
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-child-query.html |
12
|
|
|
*/ |
13
|
|
|
class HasChildQuery extends AbstractQuery |
14
|
|
|
{ |
15
|
|
|
use HasType; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var int |
19
|
|
|
*/ |
20
|
|
|
private $minChildren; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var int |
24
|
|
|
*/ |
25
|
|
|
private $maxChildren; |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @inheritdoc |
30
|
|
|
*/ |
31
|
|
|
public function toArray() |
32
|
|
|
{ |
33
|
|
|
$hasChild = [ |
34
|
|
|
'type' => $this->getType(), |
35
|
|
|
'query' => $this->getQuery()->toArray(), |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
$scoreMode = $this->getScoreMode(); |
39
|
|
|
if (!is_null($scoreMode)) { |
40
|
|
|
$hasChild['score_mode'] = $scoreMode; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$minChildren = $this->getMinChildren(); |
44
|
|
|
if (!is_null($minChildren)) { |
45
|
|
|
$hasChild['min_children'] = $minChildren; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$maxChildren = $this->getMaxChildren(); |
49
|
|
|
if (!is_null($maxChildren)) { |
50
|
|
|
$hasChild['max_children'] = $maxChildren; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return ['has_child' => $hasChild]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param int $minChildren |
58
|
|
|
* @return HasChildQuery |
59
|
|
|
*/ |
60
|
|
|
public function setMinChildren($minChildren) |
61
|
|
|
{ |
62
|
|
|
$this->assertMinChildren($minChildren); |
63
|
|
|
$this->minChildren = $minChildren; |
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return int |
70
|
|
|
*/ |
71
|
|
|
public function getMinChildren() |
72
|
|
|
{ |
73
|
|
|
return $this->minChildren; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param int $maxChildren |
79
|
|
|
* @return HasChildQuery |
80
|
|
|
*/ |
81
|
|
|
public function setMaxChildren($maxChildren) |
82
|
|
|
{ |
83
|
|
|
$this->assertMaxChildren($maxChildren); |
84
|
|
|
$this->maxChildren = $maxChildren; |
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return int |
91
|
|
|
*/ |
92
|
|
|
public function getMaxChildren() |
93
|
|
|
{ |
94
|
|
|
return $this->maxChildren; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param int $minChildren |
100
|
|
|
* @throws InvalidArgument |
101
|
|
|
*/ |
102
|
|
|
protected function assertMinChildren($minChildren) |
103
|
|
|
{ |
104
|
|
|
if (!is_int($minChildren)) { |
105
|
|
|
throw new InvalidArgument(sprintf( |
106
|
|
|
'HasChild Query `min_children` must be an integer, "%s" given.', |
107
|
|
|
gettype($minChildren) |
108
|
|
|
)); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param int $maxChildren |
115
|
|
|
* @throws InvalidArgument |
116
|
|
|
*/ |
117
|
|
|
protected function assertMaxChildren($maxChildren) |
118
|
|
|
{ |
119
|
|
|
if (!is_int($maxChildren)) { |
120
|
|
|
throw new InvalidArgument(sprintf( |
121
|
|
|
'HasChild Query `max_children` must be an integer, "%s" given.', |
122
|
|
|
gettype($maxChildren) |
123
|
|
|
)); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|