Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

average aggregation #30

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Aggregation/AvgAggregation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Erichard\ElasticQueryBuilder\Aggregation;

class AvgAggregation extends MetricAggregation
{
protected function getType(): string
{
return 'avg';
}
}
76 changes: 76 additions & 0 deletions tests/Aggregation/AvgAggregationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace Tests\Erichard\ElasticQueryBuilder\Aggregation;

use Erichard\ElasticQueryBuilder\Aggregation\AvgAggregation;
use Erichard\ElasticQueryBuilder\Options\SourceScript;
use PHPUnit\Framework\TestCase;

class AvgAggregationTest extends TestCase
{
public function testBuildWithNameOnly(): void
{
$query = new AvgAggregation('avg_price');

$this->assertEquals([
'avg' => [
'field' => 'avg_price',
],
], $query->build());
}

public function testItBuildTheAggregationUsingAField(): void
{
$query = new AvgAggregation('avg_price');
$query->setField('price');

$this->assertEquals([
'avg' => [
'field' => 'price',
],
], $query->build());
}

public function testItBuildTheAggregationUsingAScript(): void
{
$query = new AvgAggregation('avg_price', new SourceScript('doc.price.value'));

$this->assertEquals([
'avg' => [
'script' => [
'source' => 'doc.price.value',
],
],
], $query->build());
}

public function testItBuildTheAggregationUsingAScriptViaSet(): void
{
$query = new AvgAggregation('avg_price');
$query->setScript('doc.price.value');

$this->assertEquals([
'avg' => [
'script' => [
'source' => 'doc.price.value',
],
],
], $query->build());
}

public function testItBuildTheAggregationWithMissingValue(): void
{
$query = new AvgAggregation('avg_price');
$query->setField('price');
$query->setMissing(10);

$this->assertEquals([
'avg' => [
'field' => 'price',
'missing' => 10,
],
], $query->build());
}
}