Skip to content

Commit 0ed554e

Browse files
authored
Merge pull request #26 from webbaard/feature/IB-1280-sum-aggregation
IB-1280 added sum aggregation
2 parents c501baa + fd75a9f commit 0ed554e

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,14 @@ The following query types are available:
206206
\Spatie\ElasticsearchQueryBuilder\Aggregations\MinAggregation::create('min_price', 'price');
207207
```
208208

209+
#### `SumAggregation`
210+
211+
[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-sum-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-sum-aggregation.html)
212+
213+
```php
214+
\Spatie\ElasticsearchQueryBuilder\Aggregations\SumAggregation::create('sum_price', 'price');
215+
```
216+
209217
#### `NestedAggregation`
210218

211219
[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-nested-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-nested-aggregation.html)

src/Aggregations/SumAggregation.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Spatie\ElasticsearchQueryBuilder\Aggregations;
4+
5+
use Spatie\ElasticsearchQueryBuilder\Aggregations\Concerns\WithMissing;
6+
7+
class SumAggregation extends Aggregation
8+
{
9+
use WithMissing;
10+
11+
protected string $field;
12+
13+
public static function create(string $name, string $field): self
14+
{
15+
return new self($name, $field);
16+
}
17+
18+
public function __construct(string $name, string $field)
19+
{
20+
$this->name = $name;
21+
$this->field = $field;
22+
}
23+
24+
public function payload(): array
25+
{
26+
$parameters = [
27+
'field' => $this->field,
28+
];
29+
30+
if ($this->missing) {
31+
$parameters['missing'] = $this->missing;
32+
}
33+
34+
return [
35+
'sum' => $parameters,
36+
];
37+
}
38+
}

0 commit comments

Comments
 (0)