1
|
|
|
<?php namespace Nord\Lumen\Elasticsearch\Search\Query\Joining; |
2
|
|
|
|
3
|
|
|
use Nord\Lumen\Elasticsearch\Exceptions\InvalidArgument; |
4
|
|
|
use Nord\Lumen\Elasticsearch\Search\Query\Traits\HasType; |
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
|
|
|
use HasType; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var ?int |
18
|
|
|
*/ |
19
|
|
|
private $minChildren; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var ?int |
23
|
|
|
*/ |
24
|
|
|
private $maxChildren; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @inheritdoc |
28
|
|
|
* @throws InvalidArgument |
29
|
|
|
*/ |
30
|
|
|
public function toArray() |
31
|
|
|
{ |
32
|
|
|
$query = $this->getQuery(); |
33
|
|
|
|
34
|
|
|
if ($query === null) { |
35
|
|
|
throw new InvalidArgument('Query must be set'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$hasChild = [ |
39
|
|
|
'type' => $this->getType(), |
40
|
|
|
'query' => $query->toArray(), |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
$scoreMode = $this->getScoreMode(); |
44
|
|
|
if (null !== $scoreMode) { |
45
|
|
|
$hasChild['score_mode'] = $scoreMode; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$minChildren = $this->getMinChildren(); |
49
|
|
|
if (null !== $minChildren) { |
50
|
|
|
$hasChild['min_children'] = $minChildren; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$maxChildren = $this->getMaxChildren(); |
54
|
|
|
if (null !== $maxChildren) { |
55
|
|
|
$hasChild['max_children'] = $maxChildren; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return ['has_child' => $hasChild]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param int $minChildren |
63
|
|
|
* @return HasChildQuery |
64
|
|
|
*/ |
65
|
|
|
public function setMinChildren(int $minChildren) |
66
|
|
|
{ |
67
|
|
|
$this->minChildren = $minChildren; |
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return int|null |
74
|
|
|
*/ |
75
|
|
|
public function getMinChildren(): ?int |
76
|
|
|
{ |
77
|
|
|
return $this->minChildren; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param int $maxChildren |
83
|
|
|
* @return HasChildQuery |
84
|
|
|
*/ |
85
|
|
|
public function setMaxChildren(int $maxChildren) |
86
|
|
|
{ |
87
|
|
|
$this->maxChildren = $maxChildren; |
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return int|null |
94
|
|
|
*/ |
95
|
|
|
public function getMaxChildren(): ?int |
96
|
|
|
{ |
97
|
|
|
return $this->maxChildren; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|