1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Elastica\Test\Aggregation; |
4
|
|
|
|
5
|
|
|
use Elastica\Aggregation\Cardinality; |
6
|
|
|
use Elastica\Document; |
7
|
|
|
use Elastica\Index; |
8
|
|
|
use Elastica\Mapping; |
9
|
|
|
use Elastica\Query; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @internal |
13
|
|
|
*/ |
14
|
|
|
class CardinalityTest extends BaseAggregationTest |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @group functional |
18
|
|
|
*/ |
19
|
|
View Code Duplication |
public function testCardinalityAggregation(): void |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
$agg = new Cardinality('cardinality'); |
22
|
|
|
$agg->setField('color'); |
23
|
|
|
|
24
|
|
|
$query = new Query(); |
25
|
|
|
$query->addAggregation($agg); |
26
|
|
|
$results = $this->_getIndexForTest()->search($query)->getAggregation('cardinality'); |
27
|
|
|
|
28
|
|
|
$this->assertEquals(3, $results['value']); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @group functional |
33
|
|
|
*/ |
34
|
|
View Code Duplication |
public function testCardinalityAggregationWithMissing(): void |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$agg = new Cardinality('cardinality'); |
37
|
|
|
$agg->setField('color'); |
38
|
|
|
$agg->setMissing('yellow'); |
39
|
|
|
|
40
|
|
|
$query = new Query(); |
41
|
|
|
$query->addAggregation($agg); |
42
|
|
|
$results = $this->_getIndexForTest()->search($query)->getAggregation('cardinality'); |
43
|
|
|
|
44
|
|
|
$this->assertEquals(4, $results['value']); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function validPrecisionThresholdProvider() |
48
|
|
|
{ |
49
|
|
|
return [ |
50
|
|
|
'negative-int' => [-140], |
51
|
|
|
'zero' => [0], |
52
|
|
|
'positive-int' => [150], |
53
|
|
|
'more-than-max' => [40001], |
54
|
|
|
]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @dataProvider validPrecisionThresholdProvider |
59
|
|
|
* @group unit |
60
|
|
|
*/ |
61
|
|
|
public function testPrecisionThreshold(int $threshold): void |
62
|
|
|
{ |
63
|
|
|
$agg = new Cardinality('threshold'); |
64
|
|
|
$agg->setPrecisionThreshold($threshold); |
65
|
|
|
|
66
|
|
|
$this->assertNotNull($agg->getParam('precision_threshold')); |
67
|
|
|
$this->assertIsInt($agg->getParam('precision_threshold')); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @dataProvider validRehashProvider |
72
|
|
|
* @group unit |
73
|
|
|
* |
74
|
|
|
* @param bool $rehash |
75
|
|
|
*/ |
76
|
|
|
public function testRehash($rehash): void |
77
|
|
|
{ |
78
|
|
|
$agg = new Cardinality('rehash'); |
79
|
|
|
$agg->setRehash($rehash); |
80
|
|
|
|
81
|
|
|
$this->assertNotNull($agg->getParam('rehash')); |
82
|
|
|
$this->assertIsBool($agg->getParam('rehash')); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function validRehashProvider() |
86
|
|
|
{ |
87
|
|
|
return [ |
88
|
|
|
'true' => [true], |
89
|
|
|
'false' => [false], |
90
|
|
|
]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
View Code Duplication |
protected function _getIndexForTest(): Index |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
$index = $this->_createIndex(); |
96
|
|
|
|
97
|
|
|
$mapping = new Mapping([ |
98
|
|
|
'color' => ['type' => 'keyword'], |
99
|
|
|
]); |
100
|
|
|
$index->setMapping($mapping); |
101
|
|
|
|
102
|
|
|
$index->addDocuments([ |
103
|
|
|
new Document(1, ['color' => 'blue']), |
104
|
|
|
new Document(2, ['color' => 'blue']), |
105
|
|
|
new Document(3, ['color' => 'red']), |
106
|
|
|
new Document(4, ['color' => 'green']), |
107
|
|
|
new Document(5, []), |
108
|
|
|
]); |
109
|
|
|
|
110
|
|
|
$index->refresh(); |
111
|
|
|
|
112
|
|
|
return $index; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
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.