1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Elastica\Test\Aggregation; |
4
|
|
|
|
5
|
|
|
use Elastica\Aggregation\DateHistogram; |
6
|
|
|
use Elastica\Aggregation\NormalizeAggregation; |
7
|
|
|
use Elastica\Aggregation\Sum; |
8
|
|
|
use Elastica\Document; |
9
|
|
|
use Elastica\Exception\InvalidException; |
10
|
|
|
use Elastica\Index; |
11
|
|
|
use Elastica\Query; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @internal |
15
|
|
|
*/ |
16
|
|
|
class NormalizeAggregationTest extends BaseAggregationTest |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @group unit |
20
|
|
|
*/ |
21
|
|
|
public function testToArray(): void |
22
|
|
|
{ |
23
|
|
|
$expected = [ |
24
|
|
|
'date_histogram' => [ |
25
|
|
|
'field' => 'date', |
26
|
|
|
'interval' => 'day', |
27
|
|
|
], |
28
|
|
|
'aggs' => [ |
29
|
|
|
'sum_agg' => [ |
30
|
|
|
'sum' => [ |
31
|
|
|
'field' => 'value', |
32
|
|
|
], |
33
|
|
|
], |
34
|
|
|
'normalize_agg' => [ |
35
|
|
|
'normalize' => [ |
36
|
|
|
'buckets_path' => 'sum_agg', |
37
|
|
|
'method' => 'percent_of_sum', |
38
|
|
|
'format' => '00.00%', |
39
|
|
|
], |
40
|
|
|
], |
41
|
|
|
], |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
$dateHistogramAgg = new DateHistogram('histogram_agg', 'date', 'day'); |
45
|
|
|
|
46
|
|
|
$sumAgg = new Sum('sum_agg'); |
47
|
|
|
$sumAgg->setField('value'); |
48
|
|
|
$dateHistogramAgg->addAggregation($sumAgg); |
49
|
|
|
|
50
|
|
|
$normalizeAgg = new NormalizeAggregation('normalize_agg', 'sum_agg', 'percent_of_sum'); |
51
|
|
|
$normalizeAgg->setFormat('00.00%'); |
52
|
|
|
$dateHistogramAgg->addAggregation($normalizeAgg); |
53
|
|
|
|
54
|
|
|
$this->assertEquals($expected, $dateHistogramAgg->toArray()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @group unit |
59
|
|
|
*/ |
60
|
|
|
public function testToArrayInvalidBucketsPath(): void |
61
|
|
|
{ |
62
|
|
|
$this->expectException(InvalidException::class); |
63
|
|
|
|
64
|
|
|
$normalizeAgg = new NormalizeAggregation('normalize_agg'); |
65
|
|
|
$normalizeAgg->toArray(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @group unit |
70
|
|
|
*/ |
71
|
|
|
public function testToArrayInvalidMethod(): void |
72
|
|
|
{ |
73
|
|
|
$this->expectException(InvalidException::class); |
74
|
|
|
|
75
|
|
|
$normalizeAgg = new NormalizeAggregation('normalize_agg', 'agg'); |
76
|
|
|
$normalizeAgg->toArray(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @group functional |
81
|
|
|
*/ |
82
|
|
|
public function testNormalizeAggregation(): void |
83
|
|
|
{ |
84
|
|
|
$this->_checkVersion('7.9'); |
85
|
|
|
|
86
|
|
|
$index = $this->_getIndexForTest(); |
87
|
|
|
|
88
|
|
|
$dateHistogramAgg = new DateHistogram('histogram_agg', 'date', 'day'); |
89
|
|
|
$dateHistogramAgg->setFormat('yyyy-MM-dd'); |
90
|
|
|
|
91
|
|
|
$sumAgg = new Sum('sum_agg'); |
92
|
|
|
$sumAgg->setField('value'); |
93
|
|
|
$dateHistogramAgg->addAggregation($sumAgg); |
94
|
|
|
|
95
|
|
|
$normalizeAgg = new NormalizeAggregation('normalize_agg', 'sum_agg', 'percent_of_sum'); |
96
|
|
|
$normalizeAgg->setFormat('00.00%'); |
97
|
|
|
$dateHistogramAgg->addAggregation($normalizeAgg); |
98
|
|
|
|
99
|
|
|
$query = new Query(); |
100
|
|
|
$query->addAggregation($dateHistogramAgg); |
101
|
|
|
|
102
|
|
|
$dateHistogramAggResult = $index->search($query)->getAggregation('histogram_agg')['buckets']; |
103
|
|
|
|
104
|
|
|
$this->assertCount(3, $dateHistogramAggResult); |
105
|
|
|
|
106
|
|
|
$this->assertEquals('14.29%', $dateHistogramAggResult[0]['normalize_agg']['value_as_string']); |
107
|
|
|
$this->assertEquals('57.14%', $dateHistogramAggResult[1]['normalize_agg']['value_as_string']); |
108
|
|
|
$this->assertEquals('28.57%', $dateHistogramAggResult[2]['normalize_agg']['value_as_string']); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
View Code Duplication |
protected function _getIndexForTest(): Index |
|
|
|
|
112
|
|
|
{ |
113
|
|
|
$index = $this->_createIndex(); |
114
|
|
|
|
115
|
|
|
$index->addDocuments([ |
116
|
|
|
new Document(1, ['date' => '2018-12-01T01:00:00', 'value' => 1]), |
117
|
|
|
new Document(2, ['date' => '2018-12-01T10:00:00', 'value' => 2]), |
118
|
|
|
new Document(3, ['date' => '2018-12-02T02:00:00', 'value' => 3]), |
119
|
|
|
new Document(4, ['date' => '2018-12-02T15:00:00', 'value' => 4]), |
120
|
|
|
new Document(5, ['date' => '2018-12-02T20:00:00', 'value' => 5]), |
121
|
|
|
new Document(6, ['date' => '2018-12-03T03:00:00', 'value' => 6]), |
122
|
|
|
]); |
123
|
|
|
|
124
|
|
|
$index->refresh(); |
125
|
|
|
|
126
|
|
|
return $index; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
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.