Skip to content

Commit

Permalink
Add stats aggregation
Browse files Browse the repository at this point in the history
  • Loading branch information
pionl committed Apr 5, 2022
1 parent 5a7032f commit 6da5cae
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Aggregation/StatsAggregation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Erichard\ElasticQueryBuilder\Aggregation;

/**
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-stats-aggregation.html
*/
class StatsAggregation extends MetricAggregation
{
protected function getType(): string
{
return 'stats';
}
}
61 changes: 61 additions & 0 deletions tests/Aggregation/StatsAggregationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace Tests\Erichard\ElasticQueryBuilder\Aggregation;

use Erichard\ElasticQueryBuilder\Aggregation\StatsAggregation;
use PHPUnit\Framework\TestCase;

class StatsAggregationTest extends TestCase
{
public function testItBuildTheAggregationUsingAField(): void
{
$query = new StatsAggregation('price');
$query->setField('price');

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

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

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

public function testWithFieldName(): void
{
$query = new StatsAggregation('price');

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

public function testItBuildTheAggregationWithMissingValue(): void
{
$query = new StatsAggregation('price', 'price');
$query->setMissing(10);

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

0 comments on commit 6da5cae

Please sign in to comment.