Skip to content

Commit 65ecc26

Browse files
authored
Merge pull request #30 from mahdi-masa/main
average aggregation
2 parents 773e4cf + a1e34a9 commit 65ecc26

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

src/Aggregation/AvgAggregation.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Erichard\ElasticQueryBuilder\Aggregation;
6+
7+
class AvgAggregation extends MetricAggregation
8+
{
9+
protected function getType(): string
10+
{
11+
return 'avg';
12+
}
13+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Erichard\ElasticQueryBuilder\Aggregation;
6+
7+
use Erichard\ElasticQueryBuilder\Aggregation\AvgAggregation;
8+
use Erichard\ElasticQueryBuilder\Options\SourceScript;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class AvgAggregationTest extends TestCase
12+
{
13+
public function testBuildWithNameOnly(): void
14+
{
15+
$query = new AvgAggregation('avg_price');
16+
17+
$this->assertEquals([
18+
'avg' => [
19+
'field' => 'avg_price',
20+
],
21+
], $query->build());
22+
}
23+
24+
public function testItBuildTheAggregationUsingAField(): void
25+
{
26+
$query = new AvgAggregation('avg_price');
27+
$query->setField('price');
28+
29+
$this->assertEquals([
30+
'avg' => [
31+
'field' => 'price',
32+
],
33+
], $query->build());
34+
}
35+
36+
public function testItBuildTheAggregationUsingAScript(): void
37+
{
38+
$query = new AvgAggregation('avg_price', new SourceScript('doc.price.value'));
39+
40+
$this->assertEquals([
41+
'avg' => [
42+
'script' => [
43+
'source' => 'doc.price.value',
44+
],
45+
],
46+
], $query->build());
47+
}
48+
49+
public function testItBuildTheAggregationUsingAScriptViaSet(): void
50+
{
51+
$query = new AvgAggregation('avg_price');
52+
$query->setScript('doc.price.value');
53+
54+
$this->assertEquals([
55+
'avg' => [
56+
'script' => [
57+
'source' => 'doc.price.value',
58+
],
59+
],
60+
], $query->build());
61+
}
62+
63+
public function testItBuildTheAggregationWithMissingValue(): void
64+
{
65+
$query = new AvgAggregation('avg_price');
66+
$query->setField('price');
67+
$query->setMissing(10);
68+
69+
$this->assertEquals([
70+
'avg' => [
71+
'field' => 'price',
72+
'missing' => 10,
73+
],
74+
], $query->build());
75+
}
76+
}

0 commit comments

Comments
 (0)