-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathDateRangeAggregation.php
153 lines (133 loc) · 3.34 KB
/
DateRangeAggregation.php
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing;
use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation;
use ONGR\ElasticsearchDSL\Aggregation\Type\BucketingTrait;
/**
* Class representing date range aggregation.
*
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-daterange-aggregation.html
*/
class DateRangeAggregation extends AbstractAggregation
{
use BucketingTrait;
/**
* @var string
*/
private $format;
/**
* @var array
*/
private $ranges = [];
/**
* @var bool
*/
private $keyed = false;
/**
* @param string $name
* @param string $field
* @param string $format
* @param array $ranges
* @param bool $keyed
*/
public function __construct($name, $field = null, $format = null, array $ranges = [], $keyed = false)
{
parent::__construct($name);
$this->setField($field);
$this->setFormat($format);
$this->setKeyed($keyed);
foreach ($ranges as $range) {
$from = isset($range['from']) ? $range['from'] : null;
$to = isset($range['to']) ? $range['to'] : null;
$key = isset($range['key']) ? $range['key'] : null;
$this->addRange($from, $to, $key);
}
}
/**
* Sets if result buckets should be keyed.
*
* @param bool $keyed
*
* @return DateRangeAggregation
*/
public function setKeyed($keyed)
{
$this->keyed = $keyed;
return $this;
}
/**
* @return string
*/
public function getFormat()
{
return $this->format;
}
/**
* @param string $format
*/
public function setFormat($format)
{
$this->format = $format;
}
/**
* Add range to aggregation.
*
* @param string|null $from
* @param string|null $to
* @param string|null $key
*
* @return $this
*
* @throws \LogicException
*/
public function addRange($from = null, $to = null, $key = null)
{
$range = array_filter(
[
'from' => $from,
'to' => $to,
'key' => $key,
],
function ($v) {
return !is_null($v);
}
);
if (empty($range)) {
throw new \LogicException('Either from or to must be set. Both cannot be null.');
}
$this->ranges[] = $range;
return $this;
}
/**
* {@inheritdoc}
*/
public function getArray()
{
if ($this->getField() && !empty($this->ranges)) {
$data = [
'field' => $this->getField(),
'ranges' => $this->ranges,
'keyed' => $this->keyed,
];
if ($this->getFormat()) {
$data['format'] = $this->getFormat();
}
return $data;
}
throw new \LogicException('Date range aggregation must have field and range added.');
}
/**
* {@inheritdoc}
*/
public function getType()
{
return 'date_range';
}
}