Skip to content

Commit ac6a757

Browse files
authored
Merge pull request #66 from Crmteam-IVG/main
Add DateHistogramAggregation
2 parents 2f36288 + c3c104c commit ac6a757

File tree

3 files changed

+200
-0
lines changed

3 files changed

+200
-0
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,19 @@ The following query types are available:
358358
);
359359
```
360360

361+
#### `DateHistogramAggregation`
362+
363+
[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html](hhttps://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html)
364+
365+
```php
366+
\Spatie\ElasticsearchQueryBuilder\Aggregations\DateHistogramAggregation::create(
367+
'name',
368+
'@timestamp'
369+
'1h',
370+
)
371+
->aggregation(/* $subAggregation */);
372+
```
373+
361374
## Adding sorts
362375

363376
The `Builder` (and some aggregations) has a `addSort()` method that takes a `Sort` instance to sort the results. You can read more about how sorting works in [the ElasticSearch docs](https://www.elastic.co/guide/en/elasticsearch/reference/current/sort-search-results.html).
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Spatie\ElasticsearchQueryBuilder\Aggregations;
4+
5+
use Spatie\ElasticsearchQueryBuilder\AggregationCollection;
6+
use Spatie\ElasticsearchQueryBuilder\Aggregations\Concerns\WithAggregations;
7+
8+
class DateHistogramAggregation extends Aggregation
9+
{
10+
use WithAggregations;
11+
12+
protected string $field;
13+
14+
protected string $calendarInterval;
15+
16+
public static function create(string $name, string $field, string $calendarInterval, Aggregation ...$aggregations): self
17+
{
18+
return new self($name, $field, $calendarInterval, ...$aggregations);
19+
}
20+
21+
public function __construct(
22+
string $name,
23+
string $field,
24+
string $calendarInterval,
25+
Aggregation ...$aggregations
26+
) {
27+
$this->name = $name;
28+
$this->field = $field;
29+
$this->calendarInterval = $calendarInterval;
30+
$this->aggregations = new AggregationCollection(...$aggregations);
31+
}
32+
33+
public function payload(): array
34+
{
35+
$payload = [
36+
'date_histogram' => [
37+
'field' => $this->field,
38+
'calendar_interval' => $this->calendarInterval,
39+
],
40+
];
41+
42+
if (! $this->aggregations->isEmpty()) {
43+
$payload['aggs'] = $this->aggregations->toArray();
44+
}
45+
46+
return $payload;
47+
}
48+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
3+
namespace Spatie\ElasticsearchQueryBuilder\Tests\Aggregations;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Spatie\ElasticsearchQueryBuilder\Aggregations\DateHistogramAggregation;
7+
use Spatie\ElasticsearchQueryBuilder\Aggregations\TermsAggregation;
8+
9+
class DateHistogramAggregationTest extends TestCase
10+
{
11+
public function testCreateReturnsNewInstance(): void
12+
{
13+
$aggregation = DateHistogramAggregation::create('test_name', 'test_field', '1h');
14+
15+
self::assertInstanceOf(DateHistogramAggregation::class, $aggregation);
16+
}
17+
18+
public function testDefaultSetup(): void
19+
{
20+
$aggregation = new DateHistogramAggregation('test_name', 'test_field', '1h');
21+
22+
self::assertEquals([
23+
'date_histogram' => [
24+
'field' => 'test_field',
25+
'calendar_interval' => '1h',
26+
],
27+
], $aggregation->toArray());
28+
}
29+
30+
public function testDefaultSetupWithAdditionalAggregation(): void
31+
{
32+
$aggregation = (new DateHistogramAggregation('test_name', 'test_field', '1h'))->aggregation(new TermsAggregation('test_agg_name_1', 'test_agg_field_1'));
33+
34+
self::assertEquals([
35+
'date_histogram' => [
36+
'field' => 'test_field',
37+
'calendar_interval' => '1h',
38+
],
39+
'aggs' => [
40+
'test_agg_name_1' => [
41+
'terms' => [
42+
'field' => 'test_agg_field_1',
43+
],
44+
],
45+
],
46+
], $aggregation->toArray());
47+
}
48+
49+
public function testFullSetup(): void
50+
{
51+
$aggregation = new DateHistogramAggregation(
52+
'test_name',
53+
'test_field',
54+
'1h',
55+
new TermsAggregation('test_agg_name_1', 'test_agg_field_1'),
56+
new TermsAggregation('test_agg_name_2', 'test_agg_field_2')
57+
);
58+
59+
self::assertEquals([
60+
'date_histogram' => [
61+
'field' => 'test_field',
62+
'calendar_interval' => '1h',
63+
],
64+
'aggs' => [
65+
'test_agg_name_1' => [
66+
'terms' => [
67+
'field' => 'test_agg_field_1',
68+
],
69+
],
70+
'test_agg_name_2' => [
71+
'terms' => [
72+
'field' => 'test_agg_field_2',
73+
],
74+
],
75+
],
76+
], $aggregation->toArray());
77+
}
78+
79+
public function testDefaultSetupWithStaticCreateFunction(): void
80+
{
81+
$aggregation = DateHistogramAggregation::create('test_name', 'test_field', '1h');
82+
83+
self::assertEquals([
84+
'date_histogram' => [
85+
'field' => 'test_field',
86+
'calendar_interval' => '1h',
87+
],
88+
], $aggregation->toArray());
89+
}
90+
91+
public function testDefaultSetupWithAdditionalAggregationWithStaticCreateFunction(): void
92+
{
93+
$aggregation = DateHistogramAggregation::create('test_name', 'test_field', '1h')->aggregation(TermsAggregation::create('test_agg_name_1', 'test_agg_field_1'));
94+
95+
self::assertEquals([
96+
'date_histogram' => [
97+
'field' => 'test_field',
98+
'calendar_interval' => '1h',
99+
],
100+
'aggs' => [
101+
'test_agg_name_1' => [
102+
'terms' => [
103+
'field' => 'test_agg_field_1',
104+
],
105+
],
106+
],
107+
], $aggregation->toArray());
108+
}
109+
110+
public function testFullSetupWithStaticCreateFunction(): void
111+
{
112+
$aggregation = DateHistogramAggregation::create(
113+
'test_name',
114+
'test_field',
115+
'1h',
116+
TermsAggregation::create('test_agg_name_1', 'test_agg_field_1'),
117+
TermsAggregation::create('test_agg_name_2', 'test_agg_field_2')
118+
);
119+
120+
self::assertEquals([
121+
'date_histogram' => [
122+
'field' => 'test_field',
123+
'calendar_interval' => '1h',
124+
],
125+
'aggs' => [
126+
'test_agg_name_1' => [
127+
'terms' => [
128+
'field' => 'test_agg_field_1',
129+
],
130+
],
131+
'test_agg_name_2' => [
132+
'terms' => [
133+
'field' => 'test_agg_field_2',
134+
],
135+
],
136+
],
137+
], $aggregation->toArray());
138+
}
139+
}

0 commit comments

Comments
 (0)