1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
<?php
namespace Elastica\Test\Aggregation;
use Elastica\Aggregation\DateHistogram;
use Elastica\Document;
use Elastica\Query;
use Elastica\Type\Mapping;
class DateHistogramTest extends BaseAggregationTest
{
protected function _getIndexForTest()
{
$index = $this->_createIndex();
$type = $index->getType('test');
$type->setMapping(new Mapping(null, array(
'created' => array('type' => 'date'),
)));
$type->addDocuments(array(
new Document(1, array('created' => '2014-01-29T00:20:00')),
new Document(2, array('created' => '2014-01-29T02:20:00')),
new Document(3, array('created' => '2014-01-29T03:20:00')),
));
$index->refresh();
return $index;
}
/**
* @group functional
*/
public function testDateHistogramAggregation()
{
$agg = new DateHistogram('hist', 'created', '1h');
$query = new Query();
$query->addAggregation($agg);
$results = $this->_getIndexForTest()->search($query)->getAggregation('hist');
$this->assertEquals(3, sizeof($results['buckets']));
}
/**
* @group unit
*/
public function testSetOffset()
{
$agg = new DateHistogram('hist', 'created', '1h');
$agg->setOffset('3m');
$expected = array(
'date_histogram' => array(
'field' => 'created',
'interval' => '1h',
'offset' => '3m',
),
);
$this->assertEquals($expected, $agg->toArray());
$this->assertInstanceOf('Elastica\Aggregation\DateHistogram', $agg->setOffset('3m'));
}
/**
* @group functional
*/
public function testSetOffsetWorks()
{
$agg = new DateHistogram('hist', 'created', '1m');
$agg->setOffset('+40s');
$query = new Query();
$query->addAggregation($agg);
$results = $this->_getIndexForTest()->search($query)->getAggregation('hist');
$this->assertEquals('2014-01-29T00:19:40.000Z', $results['buckets'][0]['key_as_string']);
}
/**
* @group unit
*/
public function testSetTimezone()
{
$agg = new DateHistogram('hist', 'created', '1h');
$agg->setTimezone('-02:30');
$expected = array(
'date_histogram' => array(
'field' => 'created',
'interval' => '1h',
'time_zone' => '-02:30',
),
);
$this->assertEquals($expected, $agg->toArray());
$this->assertInstanceOf('Elastica\Aggregation\DateHistogram', $agg->setTimezone('-02:30'));
}
}
|