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\Metric; |
13
|
|
|
|
14
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; |
15
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\Type\MetricTrait; |
16
|
|
|
use ONGR\ElasticsearchDSL\ScriptAwareTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Difference values counter. |
20
|
|
|
* |
21
|
|
|
* @link http://goo.gl/tG7ciG |
22
|
|
|
*/ |
23
|
|
|
class CardinalityAggregation extends AbstractAggregation |
24
|
|
|
{ |
25
|
|
|
use MetricTrait; |
26
|
|
|
use ScriptAwareTrait; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var int |
30
|
|
|
*/ |
31
|
|
|
private $precisionThreshold; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var bool |
35
|
|
|
*/ |
36
|
|
|
private $rehash; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
|
View Code Duplication |
public function getArray() |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$out = array_filter( |
44
|
|
|
[ |
45
|
|
|
'field' => $this->getField(), |
46
|
|
|
'script' => $this->getScript(), |
47
|
|
|
'precision_threshold' => $this->getPrecisionThreshold(), |
48
|
|
|
'rehash' => $this->isRehash(), |
49
|
|
|
], |
50
|
|
|
function ($val) { |
51
|
|
|
return ($val || is_bool($val)); |
52
|
|
|
} |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
$this->checkRequiredFields($out); |
56
|
|
|
|
57
|
|
|
return $out; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param int $precision |
62
|
|
|
* |
63
|
|
|
* @return $this |
64
|
|
|
*/ |
65
|
|
|
public function setPrecisionThreshold($precision) |
66
|
|
|
{ |
67
|
|
|
$this->precisionThreshold = $precision; |
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return int |
74
|
|
|
*/ |
75
|
|
|
public function getPrecisionThreshold() |
76
|
|
|
{ |
77
|
|
|
return $this->precisionThreshold; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
|
|
public function isRehash() |
84
|
|
|
{ |
85
|
|
|
return $this->rehash; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param bool $rehash |
90
|
|
|
* |
91
|
|
|
* @return $this |
92
|
|
|
*/ |
93
|
|
|
public function setRehash($rehash) |
94
|
|
|
{ |
95
|
|
|
$this->rehash = $rehash; |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
|
|
public function getType() |
104
|
|
|
{ |
105
|
|
|
return 'cardinality'; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Checks if required fields are set. |
110
|
|
|
* |
111
|
|
|
* @param array $fields |
112
|
|
|
* |
113
|
|
|
* @throws \LogicException |
114
|
|
|
*/ |
115
|
|
|
private function checkRequiredFields($fields) |
116
|
|
|
{ |
117
|
|
|
if (!array_key_exists('field', $fields) && !array_key_exists('script', $fields)) { |
118
|
|
|
throw new \LogicException('Cardinality aggregation must have field or script set.'); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
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.