|
1
|
|
|
<?php |
|
2
|
|
|
namespace Elastica\Query; |
|
3
|
|
|
|
|
4
|
|
|
use Elastica\Exception\InvalidException; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Bool query. |
|
8
|
|
|
* |
|
9
|
|
|
* @author Nicolas Ruflin <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html |
|
12
|
|
|
*/ |
|
13
|
|
|
class BoolQuery extends AbstractQuery |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Add should part to query. |
|
17
|
|
|
* |
|
18
|
|
|
* @param \Elastica\Query\AbstractQuery|array $args Should query |
|
19
|
|
|
* |
|
20
|
|
|
* @return $this |
|
21
|
|
|
*/ |
|
22
|
|
|
public function addShould($args) |
|
23
|
|
|
{ |
|
24
|
|
|
return $this->_addQuery('should', $args); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Add must part to query. |
|
29
|
|
|
* |
|
30
|
|
|
* @param \Elastica\Query\AbstractQuery|array $args Must query |
|
31
|
|
|
* |
|
32
|
|
|
* @return $this |
|
33
|
|
|
*/ |
|
34
|
|
|
public function addMust($args) |
|
35
|
|
|
{ |
|
36
|
|
|
return $this->_addQuery('must', $args); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Add must not part to query. |
|
41
|
|
|
* |
|
42
|
|
|
* @param \Elastica\Query\AbstractQuery|array $args Must not query |
|
43
|
|
|
* |
|
44
|
|
|
* @return $this |
|
45
|
|
|
*/ |
|
46
|
|
|
public function addMustNot($args) |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->_addQuery('must_not', $args); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Sets the filter. |
|
53
|
|
|
* |
|
54
|
|
|
* @param \Elastica\Query\AbstractQuery $filter Filter object |
|
55
|
|
|
* |
|
56
|
|
|
* @return $this |
|
57
|
|
|
*/ |
|
58
|
|
|
public function addFilter(AbstractQuery $filter) |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->addParam('filter', $filter); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Adds a query to the current object. |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $type Query type |
|
67
|
|
|
* @param \Elastica\Query\AbstractQuery|array $args Query |
|
68
|
|
|
* |
|
69
|
|
|
* @throws \Elastica\Exception\InvalidException If not valid query |
|
70
|
|
|
* |
|
71
|
|
|
* @return $this |
|
72
|
|
|
*/ |
|
73
|
|
View Code Duplication |
protected function _addQuery($type, $args) |
|
|
|
|
|
|
74
|
|
|
{ |
|
75
|
|
|
if (!is_array($args) && !($args instanceof AbstractQuery)) { |
|
76
|
|
|
throw new InvalidException('Invalid parameter. Has to be array or instance of Elastica\Query\AbstractQuery'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $this->addParam($type, $args); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Sets boost value of this query. |
|
84
|
|
|
* |
|
85
|
|
|
* @param float $boost Boost value |
|
86
|
|
|
* |
|
87
|
|
|
* @return $this |
|
88
|
|
|
*/ |
|
89
|
|
|
public function setBoost($boost) |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->setParam('boost', $boost); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Set the minimum number of of should match. |
|
96
|
|
|
* |
|
97
|
|
|
* @param int $minimumNumberShouldMatch Should match minimum |
|
98
|
|
|
* |
|
99
|
|
|
* @return $this |
|
100
|
|
|
* |
|
101
|
|
|
* @deprecated Replaced by setMinimumShouldMatch |
|
102
|
|
|
*/ |
|
103
|
|
|
public function setMinimumNumberShouldMatch($minimumNumberShouldMatch) |
|
104
|
|
|
{ |
|
105
|
|
|
trigger_error('Deprecated: Elastica\Query::setMinimumNumberShouldMatch() is deprecated and will be removed in further Elastica releases. Use Elastica\Query::setMinimumShouldMatch() instead.', E_USER_DEPRECATED); |
|
106
|
|
|
|
|
107
|
|
|
return $this->setParam('minimum_number_should_match', $minimumNumberShouldMatch); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Sets the minimum number of should clauses to match. |
|
112
|
|
|
* |
|
113
|
|
|
* @param int|string $minimum Minimum value |
|
114
|
|
|
* |
|
115
|
|
|
* @return $this |
|
116
|
|
|
*/ |
|
117
|
|
|
public function setMinimumShouldMatch($minimum) |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->setParam('minimum_should_match', $minimum); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Converts array to an object in case no queries are added. |
|
124
|
|
|
* |
|
125
|
|
|
* @return array |
|
126
|
|
|
*/ |
|
127
|
|
|
public function toArray() |
|
128
|
|
|
{ |
|
129
|
|
|
if (empty($this->_params)) { |
|
130
|
|
|
$this->_params = new \stdClass(); |
|
|
|
|
|
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return parent::toArray(); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
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.