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 22, 2020
1 parent a7fc5d5 commit b59dced
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Added `weighted_avg` to aggregations DSL [#1814](https://github.com/ruflin/Elastica/pull/1814)
* Supported PHP 8.0 [#1794](https://github.com/ruflin/Elastica/pull/1794)
* Added support for defining a connection pool with DSN. Example: `pool(http://127.0.0.1 http://127.0.0.2/bar?timeout=4)` [#1808](https://github.com/ruflin/Elastica/pull/1808)
* 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 @@ -452,4 +453,14 @@ public function weighted_avg(string $name): WeightedAvg
{
return new WeightedAvg($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);
}
}
1 change: 1 addition & 0 deletions src/QueryBuilder/Version/Version700.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class Version700 extends Version
'bucket_script',
'serial_diff',
'weighted_avg',
'composite',
];

protected $suggesters = [
Expand Down
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 b59dced

Please sign in to comment.