1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing; |
13
|
|
|
|
14
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; |
15
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\Type\BucketingTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class representing Histogram aggregation. |
19
|
|
|
* |
20
|
|
|
* @link https://goo.gl/hGCdDd |
21
|
|
|
*/ |
22
|
|
|
class DateHistogramAggregation extends AbstractAggregation |
23
|
|
|
{ |
24
|
|
|
use BucketingTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $interval; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $format; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Inner aggregations container init. |
38
|
|
|
* |
39
|
|
|
* @param string $name |
40
|
|
|
* @param string $field |
41
|
|
|
* @param string $interval |
42
|
|
|
*/ |
43
|
|
|
public function __construct($name, $field = null, $interval = null, $format = null) |
44
|
|
|
{ |
45
|
|
|
parent::__construct($name); |
46
|
|
|
|
47
|
|
|
$this->setField($field); |
48
|
|
|
$this->setInterval($interval); |
49
|
|
|
$this->setFormat($format); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return int |
54
|
|
|
*/ |
55
|
|
|
public function getInterval() |
56
|
|
|
{ |
57
|
|
|
return $this->interval; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $interval |
62
|
|
|
* |
63
|
|
|
* @return $this |
64
|
|
|
*/ |
65
|
|
|
public function setInterval($interval) |
66
|
|
|
{ |
67
|
|
|
$this->interval = $interval; |
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $format |
74
|
|
|
* |
75
|
|
|
* @return $this |
76
|
|
|
*/ |
77
|
|
|
public function setFormat($format) |
78
|
|
|
{ |
79
|
|
|
$this->format = $format; |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
|
|
public function getType() |
88
|
|
|
{ |
89
|
|
|
return 'date_histogram'; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
|
|
public function getArray() |
96
|
|
|
{ |
97
|
|
|
if (!$this->getField() || !$this->getInterval()) { |
98
|
|
|
throw new \LogicException('Date histogram aggregation must have field and interval set.'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$out = [ |
102
|
|
|
'field' => $this->getField(), |
103
|
|
|
'interval' => $this->getInterval(), |
104
|
|
|
]; |
105
|
|
|
|
106
|
|
|
if (!empty($this->format)) { |
107
|
|
|
$out['format'] = $this->format; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $out; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|