1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace olvlvl\ElasticsearchDSL\Query; |
4
|
|
|
|
5
|
|
|
use olvlvl\ElasticsearchDSL\Helpers; |
6
|
|
|
use olvlvl\ElasticsearchDSL\Query\Compound\BoolQuery; |
7
|
|
|
use olvlvl\ElasticsearchDSL\Query\Compound\BoostingQuery; |
8
|
|
|
use olvlvl\ElasticsearchDSL\Query\Compound\ConstantScoreQuery; |
9
|
|
|
use olvlvl\ElasticsearchDSL\Query\Compound\DisMaxQuery; |
10
|
|
|
|
11
|
|
|
trait CompoundQueries |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var BoolQuery[] |
15
|
|
|
*/ |
16
|
|
|
private $bool_queries = []; |
17
|
|
|
|
18
|
|
|
protected function get_bool() |
19
|
|
|
{ |
20
|
|
|
$query = reset($this->bool_queries); |
21
|
|
|
|
22
|
|
|
return $query ? $query : $this->bool(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function bool(): BoolQuery |
26
|
|
|
{ |
27
|
|
|
$this->bool_queries[] = $query = new BoolQuery(); |
28
|
|
|
|
29
|
|
|
return $query; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var BoostingQuery[] |
34
|
|
|
*/ |
35
|
|
|
private $boosting_queries = []; |
36
|
|
|
|
37
|
|
|
protected function get_boosting() |
38
|
|
|
{ |
39
|
|
|
$query = reset($this->boosting_queries); |
40
|
|
|
|
41
|
|
|
return $query ? $query : $this->boosting(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function boosting(float $negative_boost = .5): BoostingQuery |
45
|
|
|
{ |
46
|
|
|
$this->boosting_queries[] = $query = new BoostingQuery($negative_boost); |
47
|
|
|
|
48
|
|
|
return $query; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var ConstantScoreQuery[] |
53
|
|
|
*/ |
54
|
|
|
private $constant_score_queries = []; |
55
|
|
|
|
56
|
|
|
protected function get_constant_score() |
57
|
|
|
{ |
58
|
|
|
$query = reset($this->constant_score_queries); |
59
|
|
|
|
60
|
|
|
return $query ? $query : $this->constant_score(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function constant_score(float $score = 1.0): ConstantScoreQuery |
64
|
|
|
{ |
65
|
|
|
$this->constant_score_queries[] = $query = new ConstantScoreQuery($score); |
66
|
|
|
|
67
|
|
|
return $query; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var DisMaxQuery[] |
72
|
|
|
*/ |
73
|
|
|
private $dis_max_queries = []; |
74
|
|
|
|
75
|
|
|
protected function get_dis_max() |
76
|
|
|
{ |
77
|
|
|
$query = reset($this->dis_max_queries); |
78
|
|
|
|
79
|
|
|
return $query ? $query : $this->dis_max(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function dis_max(array $options = []): DisMaxQuery |
83
|
|
|
{ |
84
|
|
|
$this->dis_max_queries[] = $query = new DisMaxQuery($options); |
85
|
|
|
|
86
|
|
|
return $query; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function jsonSerializeCompoundQueries(): array |
90
|
|
|
{ |
91
|
|
|
return Helpers::filter_merge( |
92
|
|
|
$this->bool_queries, |
93
|
|
|
$this->boosting_queries, |
94
|
|
|
$this->constant_score_queries, |
95
|
|
|
$this->dis_max_queries |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|