|
1
|
|
|
<?php |
|
2
|
|
|
namespace Elastica\Aggregation; |
|
3
|
|
|
|
|
4
|
|
|
use Elastica\Exception\InvalidException; |
|
5
|
|
|
use Elastica\Query\AbstractQuery; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class Filters. |
|
9
|
|
|
* |
|
10
|
|
|
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filters-aggregation.html |
|
11
|
|
|
*/ |
|
12
|
|
|
class Filters extends AbstractAggregation |
|
13
|
|
|
{ |
|
14
|
|
|
const NAMED_TYPE = 1; |
|
15
|
|
|
const ANONYMOUS_TYPE = 2; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var int Type of bucket keys - named, or anonymous |
|
19
|
|
|
*/ |
|
20
|
|
|
private $_type; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Add a filter. |
|
24
|
|
|
* |
|
25
|
|
|
* If a name is given, it will be added as a key, otherwise considered as an anonymous filter |
|
26
|
|
|
* |
|
27
|
|
|
* @param AbstractQuery $filter |
|
28
|
|
|
* @param string $name |
|
29
|
|
|
* |
|
30
|
|
|
* @return $this |
|
31
|
|
|
*/ |
|
32
|
|
|
public function addFilter(AbstractQuery $filter, $name = null) |
|
33
|
|
|
{ |
|
34
|
|
|
if (null !== $name && !is_string($name)) { |
|
35
|
|
|
throw new InvalidException('Name must be a string'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$filterArray = []; |
|
39
|
|
|
|
|
40
|
|
|
$type = self::NAMED_TYPE; |
|
41
|
|
|
|
|
42
|
|
|
if (null === $name) { |
|
43
|
|
|
$filterArray[] = $filter; |
|
44
|
|
|
$type = self::ANONYMOUS_TYPE; |
|
45
|
|
|
} else { |
|
46
|
|
|
$filterArray[$name] = $filter; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if ($this->hasParam('filters') |
|
50
|
|
|
&& count($this->getParam('filters')) |
|
51
|
|
|
&& $this->_type !== $type |
|
52
|
|
|
) { |
|
53
|
|
|
throw new InvalidException('Mix named and anonymous keys are not allowed'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$this->_type = $type; |
|
57
|
|
|
|
|
58
|
|
|
return $this->addParam('filters', $filterArray); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param bool $otherBucket |
|
63
|
|
|
* |
|
64
|
|
|
* @return $this |
|
65
|
|
|
*/ |
|
66
|
|
|
public function setOtherBucket($otherBucket) |
|
67
|
|
|
{ |
|
68
|
|
|
if (!is_bool($otherBucket)) { |
|
69
|
|
|
throw new \InvalidArgumentException('other_bucket only supports boolean values'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $this->setParam('other_bucket', $otherBucket); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $otherBucketKey |
|
77
|
|
|
* |
|
78
|
|
|
* @return $this |
|
79
|
|
|
*/ |
|
80
|
|
|
public function setOtherBucketKey($otherBucketKey) |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->setParam('other_bucket_key', $otherBucketKey); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return array |
|
87
|
|
|
*/ |
|
88
|
|
|
public function toArray() |
|
89
|
|
|
{ |
|
90
|
|
|
$array = []; |
|
91
|
|
|
$filters = $this->getParam('filters'); |
|
92
|
|
|
|
|
93
|
|
|
foreach ($filters as $filter) { |
|
94
|
|
|
if (self::NAMED_TYPE === $this->_type) { |
|
95
|
|
|
$key = key($filter); |
|
96
|
|
|
$array['filters']['filters'][$key] = current($filter)->toArray(); |
|
97
|
|
|
} else { |
|
98
|
|
|
$array['filters']['filters'][] = current($filter)->toArray(); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
if ($this->hasParam('other_bucket')) { |
|
103
|
|
|
$array['filters']['other_bucket'] = $this->getParam('other_bucket'); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
if ($this->hasParam('other_bucket_key')) { |
|
107
|
|
|
$array['filters']['other_bucket_key'] = $this->getParam('other_bucket_key'); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
if ($this->_aggs) { |
|
|
|
|
|
|
111
|
|
|
$array['aggs'] = $this->_convertArrayable($this->_aggs); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $array; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.