Skip to content

Commit 3873e86

Browse files
authored
Merge pull request #14 from thrashzone13/main
Add extended bounds param to Histogram and DateHistogram
2 parents 65a4387 + 98cf9f1 commit 3873e86

File tree

4 files changed

+91
-6
lines changed

4 files changed

+91
-6
lines changed

src/Aggregation/DateHistogramAggregation.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44

55
namespace Erichard\ElasticQueryBuilder\Aggregation;
66

7+
use Erichard\ElasticQueryBuilder\Features\HasExtendedBounds;
78
use Erichard\ElasticQueryBuilder\Features\HasField;
89

910
/**
1011
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html
1112
*/
1213
class DateHistogramAggregation extends AbstractAggregation
1314
{
14-
use HasField; // TODO enum
15+
use HasField, HasExtendedBounds; // TODO enum
1516

1617
/**
1718
* @param array<AbstractAggregation> $aggregations
@@ -20,10 +21,14 @@ public function __construct(
2021
string $nameAndField,
2122
private string $calendarInterval,
2223
?string $field = null,
23-
array $aggregations = []
24+
array $aggregations = [],
25+
?string $min = null,
26+
?string $max = null,
2427
) {
2528
parent::__construct($nameAndField, $aggregations);
2629
$this->field = $field ?? $nameAndField;
30+
$this->min = $min;
31+
$this->max = $max;
2732
}
2833

2934
public function setCalendarInterval(string $calendarInterval): self
@@ -45,9 +50,16 @@ protected function getType(): string
4550

4651
protected function buildAggregation(): array
4752
{
48-
return [
53+
$build = [
4954
'field' => $this->field,
5055
'calendar_interval' => $this->calendarInterval,
5156
];
57+
58+
if ($this->min !== null && $this->max !== null) {
59+
$build['extended_bounds']['min'] = $this->min;
60+
$build['extended_bounds']['max'] = $this->max;
61+
}
62+
63+
return $build;
5264
}
5365
}

src/Aggregation/HistogramAggregation.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
namespace Erichard\ElasticQueryBuilder\Aggregation;
66

7+
use Erichard\ElasticQueryBuilder\Features\HasExtendedBounds;
78
use Erichard\ElasticQueryBuilder\Features\HasField;
89

910
class HistogramAggregation extends AbstractAggregation
1011
{
11-
use HasField;
12+
use HasField, HasExtendedBounds;
1213

1314
/**
1415
* @param array<AbstractAggregation> $aggregations
@@ -17,10 +18,14 @@ public function __construct(
1718
string $name,
1819
string $field,
1920
private int $interval,
20-
array $aggregations = []
21+
array $aggregations = [],
22+
?string $min = null,
23+
?string $max = null,
2124
) {
2225
parent::__construct($name, $aggregations);
2326
$this->field = $field;
27+
$this->min = $min;
28+
$this->max = $max;
2429
}
2530

2631
public function getInterval(): int
@@ -40,9 +45,16 @@ protected function getType(): string
4045

4146
protected function buildAggregation(): array
4247
{
43-
return [
48+
$build = [
4449
'field' => $this->field,
4550
'interval' => $this->interval,
4651
];
52+
53+
if ($this->min !== null && $this->max !== null) {
54+
$build['extended_bounds']['min'] = $this->min;
55+
$build['extended_bounds']['max'] = $this->max;
56+
}
57+
58+
return $build;
4759
}
4860
}

src/Features/HasExtendedBounds.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Erichard\ElasticQueryBuilder\Features;
6+
7+
/**
8+
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-histogram-aggregation.html#search-aggregations-bucket-histogram-aggregation-extended-bounds
9+
*/
10+
trait HasExtendedBounds
11+
{
12+
protected ?string $min;
13+
14+
protected ?string $max;
15+
16+
public function setMin(?string $min): self
17+
{
18+
$this->min = $min;
19+
20+
return $this;
21+
}
22+
23+
public function getMin(): ?string
24+
{
25+
return $this->min;
26+
}
27+
28+
public function setMax(?string $max): self
29+
{
30+
$this->max = $max;
31+
32+
return $this;
33+
}
34+
35+
public function getMax(): ?string
36+
{
37+
return $this->max;
38+
}
39+
}

tests/Aggregation/DateHistogramAggregationTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,26 @@ public function testItBuildTheAggregationWithSet(): void
4747
],
4848
], $aggregation->build());
4949
}
50+
51+
public function testWithExtendedBounds(): void
52+
{
53+
$nameAndField = 'per_day';
54+
$calendarInterval = 'day';
55+
$field = 'date';
56+
$min = '2022-01-10';
57+
$max = '2022-01-20';
58+
59+
$aggregation = new DateHistogramAggregation($nameAndField, $calendarInterval, $field, [], $min, $max);
60+
61+
$this->assertEquals([
62+
'date_histogram' => [
63+
'field' => $field,
64+
'calendar_interval' => $calendarInterval,
65+
'extended_bounds' => [
66+
'min' => $min,
67+
'max' => $max,
68+
],
69+
],
70+
], $aggregation->build());
71+
}
5072
}

0 commit comments

Comments
 (0)