1
|
|
|
<?php namespace Nord\Lumen\Elasticsearch\Search\Query\Compound; |
2
|
|
|
|
3
|
|
|
use Nord\Lumen\Elasticsearch\Search\Query\QueryDSL; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* A query that matches documents matching boolean combinations of other queries. |
7
|
|
|
* |
8
|
|
|
* The bool query maps to Lucene BooleanQuery. It is built using one or more boolean clauses, |
9
|
|
|
* each clause with a typed occurrence. The occurrence types are: |
10
|
|
|
* |
11
|
|
|
* - "must" |
12
|
|
|
* The clause (query) must appear in matching documents and will contribute to the score. |
13
|
|
|
* |
14
|
|
|
* - "filter" |
15
|
|
|
* The clause (query) must appear in matching documents. However unlike must the score of the query will be ignored. |
16
|
|
|
* |
17
|
|
|
* - "should" |
18
|
|
|
* The clause (query) should appear in the matching document. In a boolean query with no must or filter clauses, one or |
19
|
|
|
* more should clauses must match a document. The minimum number of should clauses to match can be set using the |
20
|
|
|
* minimum_should_match parameter. |
21
|
|
|
* |
22
|
|
|
* - "must_not" |
23
|
|
|
* The clause (query) must not appear in the matching documents. |
24
|
|
|
* |
25
|
|
|
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html |
26
|
|
|
*/ |
27
|
|
|
class BoolQuery extends AbstractQuery |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var QueryDSL[] The clause (query) must appear in matching documents and will contribute to the score. |
31
|
|
|
*/ |
32
|
|
|
private $must = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var QueryDSL[] The clause (query) must appear in matching documents. |
36
|
|
|
* However unlike must the score of the query will be ignored. |
37
|
|
|
*/ |
38
|
|
|
private $filter = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var QueryDSL[] The clause (query) should appear in the matching document. |
42
|
|
|
* In a boolean query with no must or filter clauses, one or more should clauses must match a document. |
43
|
|
|
* The minimum number of should clauses to match can be set using the minimum_should_match parameter. |
44
|
|
|
*/ |
45
|
|
|
private $should = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var QueryDSL[] The clause (query) must not appear in the matching documents. |
49
|
|
|
*/ |
50
|
|
|
private $mustNot = []; |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param QueryDSL $query |
55
|
|
|
* @return BoolQuery |
56
|
|
|
*/ |
57
|
|
|
public function addMust(QueryDSL $query) |
58
|
|
|
{ |
59
|
|
|
$this->must[] = $query; |
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param QueryDSL $query |
66
|
|
|
* @return BoolQuery |
67
|
|
|
*/ |
68
|
|
|
public function addFilter(QueryDSL $query) |
69
|
|
|
{ |
70
|
|
|
$this->filter[] = $query; |
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param QueryDSL $query |
77
|
|
|
* @return BoolQuery |
78
|
|
|
*/ |
79
|
|
|
public function addShould(QueryDSL $query) |
80
|
|
|
{ |
81
|
|
|
$this->should[] = $query; |
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param QueryDSL $query |
88
|
|
|
* @return BoolQuery |
89
|
|
|
*/ |
90
|
|
|
public function addMustNot(QueryDSL $query) |
91
|
|
|
{ |
92
|
|
|
$this->mustNot[] = $query; |
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @inheritdoc |
99
|
|
|
*/ |
100
|
|
|
public function toArray() |
101
|
|
|
{ |
102
|
|
|
$array = []; |
103
|
|
|
|
104
|
|
|
$fields = [ |
105
|
|
|
'must' => 'must', |
106
|
|
|
'filter' => 'filter', |
107
|
|
|
'should' => 'should', |
108
|
|
|
'mustNot' => 'must_not', |
109
|
|
|
]; |
110
|
|
|
|
111
|
|
|
foreach ($fields as $propertyName => $fieldName) { |
112
|
|
|
$property = $this->{$propertyName}; |
113
|
|
|
|
114
|
|
|
if (!empty($property)) { |
115
|
|
|
$array['bool'][$fieldName] = []; |
116
|
|
|
foreach ($property as $query) { |
117
|
|
|
$array['bool'][$fieldName][] = $query->toArray(); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $array; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|