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
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @internal |
13
|
|
|
*/ |
14
|
|
|
class DateHistogramTest extends BaseAggregationTest |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @group functional |
18
|
|
|
*/ |
19
|
|
View Code Duplication |
public function testDateHistogramAggregation(): void |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
$agg = new DateHistogram('hist', 'created', '1h'); |
22
|
|
|
|
23
|
|
|
$query = new Query(); |
24
|
|
|
$query->addAggregation($agg); |
25
|
|
|
$results = $this->_getIndexForTest()->search($query)->getAggregation('hist'); |
26
|
|
|
|
27
|
|
|
$docCount = 0; |
28
|
|
|
$nonDocCount = 0; |
29
|
|
|
foreach ($results['buckets'] as $bucket) { |
30
|
|
|
if (1 == $bucket['doc_count']) { |
31
|
|
|
++$docCount; |
32
|
|
|
} else { |
33
|
|
|
++$nonDocCount; |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
// 3 Documents that were added |
37
|
|
|
$this->assertEquals(3, $docCount); |
38
|
|
|
// 1 document that was generated in between for the missing hour |
39
|
|
|
$this->assertEquals(1, $nonDocCount); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @group functional |
44
|
|
|
*/ |
45
|
|
View Code Duplication |
public function testDateHistogramAggregationWithMissing(): void |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
$agg = new DateHistogram('hist', 'created', '1h'); |
48
|
|
|
$agg->setMissing('2014-01-29T04:20:00'); |
49
|
|
|
|
50
|
|
|
$query = new Query(); |
51
|
|
|
$query->addAggregation($agg); |
52
|
|
|
$results = $this->_getIndexForTest()->search($query)->getAggregation('hist'); |
53
|
|
|
|
54
|
|
|
$docCount = 0; |
55
|
|
|
$nonDocCount = 0; |
56
|
|
|
foreach ($results['buckets'] as $bucket) { |
57
|
|
|
if (1 == $bucket['doc_count']) { |
58
|
|
|
++$docCount; |
59
|
|
|
} else { |
60
|
|
|
++$nonDocCount; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
// 3 Documents that were added |
64
|
|
|
$this->assertEquals(4, $docCount); |
65
|
|
|
// 1 document that was generated in between for the missing hour |
66
|
|
|
$this->assertEquals(1, $nonDocCount); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @group unit |
71
|
|
|
*/ |
72
|
|
|
public function testSetOffset(): void |
73
|
|
|
{ |
74
|
|
|
$agg = new DateHistogram('hist', 'created', '1h'); |
75
|
|
|
|
76
|
|
|
$agg->setOffset('3m'); |
77
|
|
|
|
78
|
|
|
$expected = [ |
79
|
|
|
'date_histogram' => [ |
80
|
|
|
'field' => 'created', |
81
|
|
|
'interval' => '1h', |
82
|
|
|
'offset' => '3m', |
83
|
|
|
], |
84
|
|
|
]; |
85
|
|
|
|
86
|
|
|
$this->assertEquals($expected, $agg->toArray()); |
87
|
|
|
|
88
|
|
|
$this->assertInstanceOf(DateHistogram::class, $agg->setOffset('3m')); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @group functional |
93
|
|
|
*/ |
94
|
|
|
public function testSetOffsetWorks(): void |
95
|
|
|
{ |
96
|
|
|
$this->_checkVersion('1.5'); |
97
|
|
|
|
98
|
|
|
$agg = new DateHistogram('hist', 'created', '1m'); |
99
|
|
|
$agg->setOffset('+40s'); |
100
|
|
|
|
101
|
|
|
$query = new Query(); |
102
|
|
|
$query->addAggregation($agg); |
103
|
|
|
$results = $this->_getIndexForTest()->search($query)->getAggregation('hist'); |
104
|
|
|
|
105
|
|
|
$this->assertEquals('2014-01-29T00:19:40.000Z', $results['buckets'][0]['key_as_string']); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @group unit |
110
|
|
|
*/ |
111
|
|
|
public function testSetTimezone(): void |
112
|
|
|
{ |
113
|
|
|
$agg = new DateHistogram('hist', 'created', '1h'); |
114
|
|
|
|
115
|
|
|
$agg->setTimezone('-02:30'); |
116
|
|
|
|
117
|
|
|
$expected = [ |
118
|
|
|
'date_histogram' => [ |
119
|
|
|
'field' => 'created', |
120
|
|
|
'interval' => '1h', |
121
|
|
|
'time_zone' => '-02:30', |
122
|
|
|
], |
123
|
|
|
]; |
124
|
|
|
|
125
|
|
|
$this->assertEquals($expected, $agg->toArray()); |
126
|
|
|
|
127
|
|
|
$this->assertInstanceOf(DateHistogram::class, $agg->setTimezone('-02:30')); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
View Code Duplication |
protected function _getIndexForTest(): Index |
|
|
|
|
131
|
|
|
{ |
132
|
|
|
$index = $this->_createIndex(); |
133
|
|
|
$index->setMapping(new Mapping([ |
134
|
|
|
'created' => ['type' => 'date'], |
135
|
|
|
])); |
136
|
|
|
|
137
|
|
|
$index->addDocuments([ |
138
|
|
|
new Document(1, ['created' => '2014-01-29T00:20:00']), |
139
|
|
|
new Document(2, ['created' => '2014-01-29T02:20:00']), |
140
|
|
|
new Document(3, ['created' => '2014-01-29T03:20:00']), |
141
|
|
|
new Document(4, []), |
142
|
|
|
]); |
143
|
|
|
|
144
|
|
|
$index->refresh(); |
145
|
|
|
|
146
|
|
|
return $index; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
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.