-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |