Skip to content

Commit

Permalink
Add units & functionals test on composite aggregation
Browse files Browse the repository at this point in the history
update CHANGELOG.md
  • Loading branch information
itkg-ppottie committed Oct 7, 2020
1 parent 4603754 commit a0b6755
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Backward Compatibility Breaks
### Added
* Ability to specify the type of authentication manually by the `auth_type` parameter (in the client class config) was added (allowed values are `basic, digest, gssnegotiate, ntlm`)
* Added `Elastica\Aggregation\Composite` aggregation [#1804](https://github.com/ruflin/Elastica/pull/1804)
### Changed
* Allow `string` such as `wait_for` to be passed to `AbstractUpdateAction::setRefresh` [#1791](https://github.com/ruflin/Elastica/pull/1791)
* Changed the return type of `AbstractUpdateAction::getRefresh` to `boolean|string` [#1791](https://github.com/ruflin/Elastica/pull/1791)
Expand Down
3 changes: 0 additions & 3 deletions src/Aggregation/Composite.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
class Composite extends AbstractAggregation
{
/**
* @param int $size
* @return $this
*/
public function setSize(int $size): self
Expand All @@ -14,7 +13,6 @@ public function setSize(int $size): self
}

/**
* @param AbstractAggregation $aggregation
* @return $this
*/
public function addSource(AbstractAggregation $aggregation): self
Expand All @@ -23,7 +21,6 @@ public function addSource(AbstractAggregation $aggregation): self
}

/**
* @param array|null $checkpoint
* @return $this
*/
public function addAfter(?array $checkpoint): self
Expand Down
11 changes: 11 additions & 0 deletions src/QueryBuilder/DSL/Aggregation.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Elastica\Aggregation\AvgBucket;
use Elastica\Aggregation\BucketScript;
use Elastica\Aggregation\Cardinality;
use Elastica\Aggregation\Composite;
use Elastica\Aggregation\DateHistogram;
use Elastica\Aggregation\DateRange;
use Elastica\Aggregation\DiversifiedSampler;
Expand Down Expand Up @@ -428,4 +429,14 @@ public function diversified_sampler(string $name): DiversifiedSampler
{
return new DiversifiedSampler($name);
}

/**
* composite aggregation.
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-composite-aggregation.html
*/
public function composite(string $name): Composite
{
return new Composite($name);
}
}
196 changes: 196 additions & 0 deletions tests/Aggregation/CompositeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
<?php

namespace Elastica\Test\Aggregation;

use Elastica\Aggregation\Composite;
use Elastica\Aggregation\Terms;
use Elastica\Document;
use Elastica\Index;
use Elastica\Query;

/**
* @internal
*/
class CompositeTest extends BaseAggregationTest
{
/**
* @group unit
*/
public function testSize(): void
{
$composite = new Composite('products');
$composite->setSize(200);
$this->assertEquals(200, $composite->getParam('size'));

$expected = [
'composite' => [
'size' => 200,
],
];
$this->assertEquals($expected, $composite->toArray());
}

/**
* @group unit
*/
public function testAddSource(): void
{
$expected = [
'composite' => [
'sources' => [
[
'product' => [
'terms' => [
'field' => 'product_id',
],
],
],
],
],
];

$composite = new Composite('products');
$composite->addSource((new Terms('product'))->setField('product_id'));
$this->assertEquals($expected, $composite->toArray());
}

/**
* @group unit
*/
public function testAddAfter(): void
{
$checkpoint = ['checkpointproduct' => 'checkpoint'];
$expected = [
'composite' => [
'after' => $checkpoint,
],
];

$composite = new Composite('products');
$composite->addAfter($checkpoint);
$this->assertEquals($expected, $composite->toArray());
}

/**
* @group functional
*/
public function testCompositeNoAfterAggregation(): void
{
$composite = new Composite('products');
$composite->addSource((new Terms('color'))->setField('color.keyword'));

$query = new Query();
$query->addAggregation($composite);

$results = $this->_getIndexForTest()->search($query)->getAggregation('products');
$expected = [
'after_key' => [
'color' => 'red',
],
'buckets' => [
[
'key' => [
'color' => 'blue',
],
'doc_count' => 2,
],
[
'key' => [
'color' => 'green',
],
'doc_count' => 1,
],
[
'key' => [
'color' => 'red',
],
'doc_count' => 1,
],
],
];

$this->assertEquals($expected, $results);
}

/**
* @group functional
*/
public function testCompositeWithSizeAggregation(): void
{
$composite = new Composite('products');
$composite->setSize(2);
$composite->addSource((new Terms('color'))->setField('color.keyword'));

$query = new Query();
$query->addAggregation($composite);

$results = $this->_getIndexForTest()->search($query)->getAggregation('products');
$expected = [
'after_key' => [
'color' => 'green',
],
'buckets' => [
[
'key' => [
'color' => 'blue',
],
'doc_count' => 2,
],
[
'key' => [
'color' => 'green',
],
'doc_count' => 1,
],
],
];

$this->assertEquals($expected, $results);
}

/**
* @group functional
*/
public function testCompositeWithAfterAggregation(): void
{
$composite = new Composite('products');
$composite->setSize(2);
$composite->addSource((new Terms('color'))->setField('color.keyword'));
$composite->addAfter(['color' => 'green']);
$query = new Query();
$query->addAggregation($composite);

$results = $this->_getIndexForTest()->search($query)->getAggregation('products');
$expected = [
'after_key' => [
'color' => 'red',
],
'buckets' => [
[
'key' => [
'color' => 'red',
],
'doc_count' => 1,
],
],
];

$this->assertEquals($expected, $results);
}

protected function _getIndexForTest(): Index
{
$index = $this->_createIndex();

$index->addDocuments([
new Document(1, ['price' => 5, 'color' => 'blue']),
new Document(2, ['price' => 5, 'color' => 'blue']),
new Document(3, ['price' => 3, 'color' => 'red']),
new Document(4, ['price' => 3, 'color' => 'green']),
]);

$index->refresh();

return $index;
}
}

0 comments on commit a0b6755

Please sign in to comment.