|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the ONGR package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace ONGR\ElasticsearchDSL\Aggregation; |
|
13
|
|
|
|
|
14
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\Type\MetricTrait; |
|
15
|
|
|
use ONGR\ElasticsearchDSL\ScriptAwareTrait; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Difference values counter. |
|
19
|
|
|
*/ |
|
20
|
|
|
class CardinalityAggregation extends AbstractAggregation |
|
21
|
|
|
{ |
|
22
|
|
|
use MetricTrait; |
|
23
|
|
|
use ScriptAwareTrait; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param string $name |
|
27
|
|
|
* @param string $field |
|
28
|
|
|
* @param string $script |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct($name, $field = null, $script = null) |
|
31
|
|
|
{ |
|
32
|
|
|
parent::__construct($name); |
|
33
|
|
|
|
|
34
|
|
|
$this->setField($field); |
|
35
|
|
|
$this->setScript($script); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var int |
|
40
|
|
|
*/ |
|
41
|
|
|
private $precisionThreshold; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var bool |
|
45
|
|
|
*/ |
|
46
|
|
|
private $rehash; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getArray() |
|
52
|
|
|
{ |
|
53
|
|
|
$out = array_filter( |
|
54
|
|
|
[ |
|
55
|
|
|
'field' => $this->getField(), |
|
56
|
|
|
'script' => $this->getScript(), |
|
57
|
|
|
'precision_threshold' => $this->getPrecisionThreshold(), |
|
58
|
|
|
'rehash' => $this->isRehash(), |
|
59
|
|
|
], |
|
60
|
|
|
function ($val) { |
|
61
|
|
|
return ($val || is_bool($val)); |
|
62
|
|
|
} |
|
63
|
|
|
); |
|
64
|
|
|
|
|
65
|
|
|
$this->checkRequiredFields($out); |
|
66
|
|
|
|
|
67
|
|
|
return $out; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Precision threshold. |
|
72
|
|
|
* |
|
73
|
|
|
* @param int $precision Precision Threshold. |
|
74
|
|
|
*/ |
|
75
|
|
|
public function setPrecisionThreshold($precision) |
|
76
|
|
|
{ |
|
77
|
|
|
$this->precisionThreshold = $precision; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return int |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getPrecisionThreshold() |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->precisionThreshold; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @return bool |
|
90
|
|
|
*/ |
|
91
|
|
|
public function isRehash() |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->rehash; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param bool $rehash |
|
98
|
|
|
*/ |
|
99
|
|
|
public function setRehash($rehash) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->rehash = $rehash; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* {@inheritdoc} |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getType() |
|
108
|
|
|
{ |
|
109
|
|
|
return 'cardinality'; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Checks if required fields are set. |
|
114
|
|
|
* |
|
115
|
|
|
* @param array $fields |
|
116
|
|
|
* |
|
117
|
|
|
* @throws \LogicException |
|
118
|
|
|
*/ |
|
119
|
|
|
private function checkRequiredFields($fields) |
|
120
|
|
|
{ |
|
121
|
|
|
if (!array_key_exists('field', $fields) && !array_key_exists('script', $fields)) { |
|
122
|
|
|
throw new \LogicException('Cardinality aggregation must have field or script set.'); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|