1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Elastica\Test\Aggregation; |
4
|
|
|
|
5
|
|
|
use Elastica\Aggregation\DateRange; |
6
|
|
|
use Elastica\Document; |
7
|
|
|
use Elastica\Exception\ResponseException; |
8
|
|
|
use Elastica\Index; |
9
|
|
|
use Elastica\Mapping; |
10
|
|
|
use Elastica\Query; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @internal |
14
|
|
|
*/ |
15
|
|
|
class DateRangeTest extends BaseAggregationTest |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @group unit |
19
|
|
|
*/ |
20
|
|
View Code Duplication |
public function testSetTimezone(): void |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
$agg = (new DateRange('name')) |
23
|
|
|
->setField('date') |
24
|
|
|
->setTimezone('CET') |
25
|
|
|
; |
26
|
|
|
|
27
|
|
|
$expected = [ |
28
|
|
|
'date_range' => [ |
29
|
|
|
'field' => 'date', |
30
|
|
|
'time_zone' => 'CET', |
31
|
|
|
], |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
$this->assertEquals($expected, $agg->toArray()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @group functional |
39
|
|
|
*/ |
40
|
|
|
public function testDateRangeAggregation(): void |
41
|
|
|
{ |
42
|
|
|
$agg = new DateRange('date'); |
43
|
|
|
$agg->setField('created'); |
44
|
|
|
$agg->addRange(1390958535000)->addRange(null, 1390958535000); |
45
|
|
|
|
46
|
|
|
$query = new Query(); |
47
|
|
|
$query->addAggregation($agg); |
48
|
|
|
$results = $this->_getIndexForTest()->search($query)->getAggregation('date'); |
49
|
|
|
|
50
|
|
View Code Duplication |
foreach ($results['buckets'] as $bucket) { |
|
|
|
|
51
|
|
|
if (\array_key_exists('to', $bucket)) { |
52
|
|
|
$this->assertEquals(1, $bucket['doc_count']); |
53
|
|
|
} elseif (\array_key_exists('from', $bucket)) { |
54
|
|
|
$this->assertEquals(2, $bucket['doc_count']); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @group functional |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public function testDateRangeKeyedAggregation(): void |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$agg = new DateRange('date'); |
65
|
|
|
$agg->setField('created'); |
66
|
|
|
$agg->setKeyed(); |
67
|
|
|
$agg->addRange(1390958535000)->addRange(null, 1390958535000); |
68
|
|
|
|
69
|
|
|
$query = new Query(); |
70
|
|
|
$query->addAggregation($agg); |
71
|
|
|
$results = $this->_getIndexForTest()->search($query)->getAggregation('date'); |
72
|
|
|
|
73
|
|
|
$expected = [ |
74
|
|
|
'*-1390958535000', |
75
|
|
|
'1390958535000-*', |
76
|
|
|
]; |
77
|
|
|
$this->assertSame($expected, \array_keys($results['buckets'])); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @group functional |
82
|
|
|
*/ |
83
|
|
|
public function testDateRangeSetFormat(): void |
84
|
|
|
{ |
85
|
|
|
$agg = new DateRange('date'); |
86
|
|
|
$agg->setField('created'); |
87
|
|
|
$agg->addRange(1390958535000)->addRange(null, 1390958535000); |
88
|
|
|
$agg->setFormat('epoch_millis'); |
89
|
|
|
|
90
|
|
|
$query = new Query(); |
91
|
|
|
$query->addAggregation($agg); |
92
|
|
|
$results = $this->_getIndexForTest()->search($query)->getAggregation('date'); |
93
|
|
|
|
94
|
|
|
$this->assertEquals('1390958535000', $results['buckets'][0]['to_as_string']); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @group functional |
99
|
|
|
*/ |
100
|
|
|
public function testDateRangeSetFormatAccordingToFormatTargetField(): void |
101
|
|
|
{ |
102
|
|
|
$agg = new DateRange('date'); |
103
|
|
|
$agg->setField('created'); |
104
|
|
|
$agg->addRange(1390958535000)->addRange(null, 1390958535000); |
105
|
|
|
$agg->setFormat('m-d-y'); |
106
|
|
|
|
107
|
|
|
$query = new Query(); |
108
|
|
|
$query->addAggregation($agg); |
109
|
|
|
|
110
|
|
|
try { |
111
|
|
|
$results = $this->_getIndexForTest()->search($query)->getAggregation('date'); |
|
|
|
|
112
|
|
|
$this->fail('Should throw exception to and from parameters in date_range aggregation are interpreted according of the target field'); |
113
|
|
|
} catch (ResponseException $e) { |
114
|
|
|
$error = $e->getResponse()->getFullError(); |
115
|
|
|
|
116
|
|
|
$this->assertSame('search_phase_execution_exception', $error['type']); |
117
|
|
|
$this->assertStringStartsWith('failed to parse date field', $error['root_cause'][0]['reason']); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
View Code Duplication |
protected function _getIndexForTest(): Index |
|
|
|
|
122
|
|
|
{ |
123
|
|
|
$index = $this->_createIndex(); |
124
|
|
|
$index->setMapping(new Mapping([ |
125
|
|
|
'created' => ['type' => 'date', 'format' => 'epoch_millis'], |
126
|
|
|
])); |
127
|
|
|
|
128
|
|
|
$index->addDocuments([ |
129
|
|
|
new Document(1, ['created' => 1390962135000]), |
130
|
|
|
new Document(2, ['created' => 1390965735000]), |
131
|
|
|
new Document(3, ['created' => 1390954935000]), |
132
|
|
|
]); |
133
|
|
|
|
134
|
|
|
$index->refresh(); |
135
|
|
|
|
136
|
|
|
return $index; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
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.