1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Elastica\Test\Aggregation; |
4
|
|
|
|
5
|
|
|
use Elastica\Aggregation\DateHistogram; |
6
|
|
|
use Elastica\Document; |
7
|
|
|
use Elastica\Index; |
8
|
|
|
use Elastica\Mapping; |
9
|
|
|
use Elastica\Query; |
10
|
|
|
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @internal |
14
|
|
|
*/ |
15
|
|
|
class DateHistogramTest extends BaseAggregationTest |
16
|
|
|
{ |
17
|
|
|
use ExpectDeprecationTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @group functional |
21
|
|
|
*/ |
22
|
|
View Code Duplication |
public function testDateHistogramAggregation(): void |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
$agg = new DateHistogram('hist', 'created'); |
25
|
|
|
|
26
|
|
|
$version = $this->_getVersion(); |
27
|
|
|
|
28
|
|
|
if (\version_compare($version, '7.2') < 0) { |
29
|
|
|
$agg->setParam('interval', '1h'); |
30
|
|
|
} else { |
31
|
|
|
$agg->setFixedInterval('1h'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$query = new Query(); |
35
|
|
|
$query->addAggregation($agg); |
36
|
|
|
$results = $this->_getIndexForTest()->search($query)->getAggregation('hist'); |
37
|
|
|
|
38
|
|
|
$docCount = 0; |
39
|
|
|
$nonDocCount = 0; |
40
|
|
|
foreach ($results['buckets'] as $bucket) { |
41
|
|
|
if (1 == $bucket['doc_count']) { |
42
|
|
|
++$docCount; |
43
|
|
|
} else { |
44
|
|
|
++$nonDocCount; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
// 3 Documents that were added |
48
|
|
|
$this->assertEquals(3, $docCount); |
49
|
|
|
// 1 document that was generated in between for the missing hour |
50
|
|
|
$this->assertEquals(1, $nonDocCount); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @group unit |
55
|
|
|
* @group legacy |
56
|
|
|
*/ |
57
|
|
|
public function testDateHistogramAggregationWithIntervalTriggersADeprecation(): void |
58
|
|
|
{ |
59
|
|
|
$this->expectDeprecation('Since ruflin/elastica 7.1.0: Argument 3 passed to "Elastica\Aggregation\DateHistogram::__construct()" is deprecated, use "setDateInterval()" or "setCalendarInterval()" instead. It will be removed in 8.0.'); |
60
|
|
|
new DateHistogram('hist', 'created', 'day'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @group unit |
65
|
|
|
* @group legacy |
66
|
|
|
*/ |
67
|
|
|
public function testDateHistogramAggregationSetIntervalTriggersADeprecation(): void |
68
|
|
|
{ |
69
|
|
|
$agg = new DateHistogram('hist', 'created'); |
70
|
|
|
|
71
|
|
|
$this->expectDeprecation('Since ruflin/elastica 7.1.0: The "Elastica\Aggregation\DateHistogram::setInterval()" method is deprecated, use "setDateInterval()" or "setCalendarInterval()" instead. It will be removed in 8.0.'); |
72
|
|
|
|
73
|
|
|
$agg->setInterval('day'); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @group functional |
78
|
|
|
*/ |
79
|
|
View Code Duplication |
public function testDateHistogramCalendarAggregation(): void |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$agg = new DateHistogram('hist', 'created'); |
82
|
|
|
|
83
|
|
|
$version = $this->_getVersion(); |
84
|
|
|
|
85
|
|
|
if (\version_compare($version, '7.2') < 0) { |
86
|
|
|
$agg->setParam('interval', '1h'); |
87
|
|
|
} else { |
88
|
|
|
$agg->setCalendarInterval('1h'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$query = new Query(); |
92
|
|
|
$query->addAggregation($agg); |
93
|
|
|
$results = $this->_getIndexForTest()->search($query)->getAggregation('hist'); |
94
|
|
|
|
95
|
|
|
$docCount = 0; |
96
|
|
|
$nonDocCount = 0; |
97
|
|
|
foreach ($results['buckets'] as $bucket) { |
98
|
|
|
if (1 == $bucket['doc_count']) { |
99
|
|
|
++$docCount; |
100
|
|
|
} else { |
101
|
|
|
++$nonDocCount; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
// 3 Documents that were added |
105
|
|
|
$this->assertEquals(3, $docCount); |
106
|
|
|
// 1 document that was generated in between for the missing hour |
107
|
|
|
$this->assertEquals(1, $nonDocCount); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @group unit |
112
|
|
|
*/ |
113
|
|
View Code Duplication |
public function testSetOffset(): void |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
$agg = new DateHistogram('hist', 'created'); |
116
|
|
|
$agg->setFixedInterval('1h'); |
117
|
|
|
|
118
|
|
|
$agg->setOffset('3m'); |
119
|
|
|
|
120
|
|
|
$expected = [ |
121
|
|
|
'date_histogram' => [ |
122
|
|
|
'field' => 'created', |
123
|
|
|
'offset' => '3m', |
124
|
|
|
'fixed_interval' => '1h', |
125
|
|
|
], |
126
|
|
|
]; |
127
|
|
|
|
128
|
|
|
$this->assertEquals($expected, $agg->toArray()); |
129
|
|
|
|
130
|
|
|
$this->assertInstanceOf(DateHistogram::class, $agg->setOffset('3m')); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @group functional |
135
|
|
|
*/ |
136
|
|
|
public function testSetOffsetWorks(): void |
137
|
|
|
{ |
138
|
|
|
$agg = new DateHistogram('hist', 'created'); |
139
|
|
|
$agg->setOffset('+40s'); |
140
|
|
|
|
141
|
|
|
$version = $this->_getVersion(); |
142
|
|
|
|
143
|
|
|
if (\version_compare($version, '7.2') < 0) { |
144
|
|
|
$agg->setParam('interval', '1m'); |
145
|
|
|
} else { |
146
|
|
|
$agg->setFixedInterval('1m'); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$query = new Query(); |
150
|
|
|
$query->addAggregation($agg); |
151
|
|
|
$results = $this->_getIndexForTest()->search($query)->getAggregation('hist'); |
152
|
|
|
|
153
|
|
|
$this->assertEquals('2014-01-29T00:19:40.000Z', $results['buckets'][0]['key_as_string']); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @group unit |
158
|
|
|
*/ |
159
|
|
View Code Duplication |
public function testSetTimezone(): void |
|
|
|
|
160
|
|
|
{ |
161
|
|
|
$agg = new DateHistogram('hist', 'created'); |
162
|
|
|
$agg->setFixedInterval('1h'); |
163
|
|
|
|
164
|
|
|
$agg->setTimezone('-02:30'); |
165
|
|
|
|
166
|
|
|
$expected = [ |
167
|
|
|
'date_histogram' => [ |
168
|
|
|
'field' => 'created', |
169
|
|
|
'time_zone' => '-02:30', |
170
|
|
|
'fixed_interval' => '1h', |
171
|
|
|
], |
172
|
|
|
]; |
173
|
|
|
|
174
|
|
|
$this->assertEquals($expected, $agg->toArray()); |
175
|
|
|
|
176
|
|
|
$this->assertInstanceOf(DateHistogram::class, $agg->setTimezone('-02:30')); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
protected function _getIndexForTest(): Index |
180
|
|
|
{ |
181
|
|
|
$index = $this->_createIndex(); |
182
|
|
|
$index->setMapping(new Mapping([ |
183
|
|
|
'created' => ['type' => 'date'], |
184
|
|
|
])); |
185
|
|
|
|
186
|
|
|
$index->addDocuments([ |
187
|
|
|
new Document(1, ['created' => '2014-01-29T00:20:00']), |
188
|
|
|
new Document(2, ['created' => '2014-01-29T02:20:00']), |
189
|
|
|
new Document(3, ['created' => '2014-01-29T03:20:00']), |
190
|
|
|
]); |
191
|
|
|
|
192
|
|
|
$index->refresh(); |
193
|
|
|
|
194
|
|
|
return $index; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
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.