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

(aggs): Add adjacency matrix aggregation #1642

Merged
merged 3 commits into from
Aug 14, 2019
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ All notable changes to this project will be documented in this file based on the
* support for elasticsearch-php ^7.0
* Added `ParentAggregation` [#1616](https://github.com/ruflin/Elastica/pull/1616)
* Elastica\Reindex missing options (script, remote, wait_for_completion, scroll...)
* Added `AdjacencyMatrix` aggregation [#1642](https://github.com/ruflin/Elastica/pull/1642)

### Improvements
* Added `native_function_invocation` CS rule [#1606](https://github.com/ruflin/Elastica/pull/1606)
Expand Down
63 changes: 63 additions & 0 deletions lib/Elastica/Aggregation/AdjacencyMatrix.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Elastica\Aggregation;

use Elastica\Exception\InvalidException;
use Elastica\Query\AbstractQuery;

class AdjacencyMatrix extends AbstractAggregation
{
/**
* Add a named filter.
*
* @param AbstractQuery $filter
* @param string $name
*
* @return $this
*/
public function addFilter(AbstractQuery $filter, $name): self
{
if (!\is_string($name)) {
throw new InvalidException('Name must be a string');
}

$filterArray = [];
$filterArray[$name] = $filter;

return $this->addParam('filters', $filterArray);
}

/**
* @param string $separator
*
* @return $this
*/
public function setSeparator(string $separator = '&'): self
{
return $this->setParam('separator', $separator);
}

/**
* @return array
*/
public function toArray(): array
{
$array = [];
$filters = $this->getParam('filters');

foreach ($filters as $filter) {
$key = \key($filter);
$array['adjacency_matrix']['filters'][$key] = \current($filter)->toArray();
}

if ($this->hasParam('separator')) {
$array['adjacency_matrix']['separator'] = $this->getParam('separator');
}

if ($this->_aggs) {
$array['aggs'] = $this->_convertArrayable($this->_aggs);
}

return $array;
}
}
15 changes: 15 additions & 0 deletions lib/Elastica/QueryBuilder/DSL/Aggregation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Elastica\QueryBuilder\DSL;

use Elastica\Aggregation\AdjacencyMatrix;
use Elastica\Aggregation\Avg;
use Elastica\Aggregation\AvgBucket;
use Elastica\Aggregation\BucketScript;
Expand Down Expand Up @@ -538,4 +539,18 @@ public function serial_diff(string $name, string $bucketsPath = null): SerialDif
{
return new SerialDiff($name, $bucketsPath);
}

/**
* adjacency matrix aggregation.
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-adjacency-matrix-aggregation.html
*
* @param string $name
*
* @return AdjacencyMatrix
*/
public function adjacency_matrix(string $name): AdjacencyMatrix
{
return new AdjacencyMatrix($name);
}
}
154 changes: 154 additions & 0 deletions test/Elastica/Aggregation/AdjacencyMatrixTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php

namespace Elastica\Test\Aggregation;

use Elastica\Aggregation\AdjacencyMatrix;
use Elastica\Aggregation\Avg;
use Elastica\Document;
use Elastica\Exception\InvalidException;
use Elastica\Index;
use Elastica\Query;
use Elastica\Query\Term;
use Elastica\Query\Terms;

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

$index->getType('_doc')->addDocuments([
new Document(1, ['accounts' => ['hillary', 'sidney']]),
new Document(2, ['accounts' => ['hillary', 'donald']]),
new Document(3, ['accounts' => ['vladimir', 'donald']]),
]);

$index->refresh();

return $index;
}

/**
* @group unit
*/
public function testToArray()
{
$expected = [
'adjacency_matrix' => [
'filters' => [
'' => [
'term' => ['color' => ''],
],
'0' => [
'term' => ['color' => '0'],
],
'blue' => [
'term' => ['color' => 'blue'],
],
'red' => [
'term' => ['color' => 'red'],
],
],
],
'aggs' => [
'avg_price' => ['avg' => ['field' => 'price']],
],
];

$agg = new AdjacencyMatrix('by_color');

$agg->addFilter(new Term(['color' => '']), '');
$agg->addFilter(new Term(['color' => '0']), '0');
$agg->addFilter(new Term(['color' => 'blue']), 'blue');
$agg->addFilter(new Term(['color' => 'red']), 'red');

$avg = new Avg('avg_price');
$avg->setField('price');
$agg->addAggregation($avg);

$this->assertEquals($expected, $agg->toArray());
}

/**
* @group unit
*/
public function testNonStringNamedFilters()
{
$this->expectException(InvalidException::class);
$this->expectExceptionMessage('Name must be a string');

$agg = new AdjacencyMatrix('by_color');

$agg->addFilter(new Term(['color' => '0']), 5);
$agg->addFilter(new Term(['color' => '1']), 'A');
$agg->addFilter(new Term(['color' => '2']), true);
}

/**
* @group unit
*/
public function testToArraySeparator()
{
$expected = [
'adjacency_matrix' => [
'filters' => [
'blue' => [
'term' => ['color' => 'blue'],
],
],
'separator' => '|',
],
];

$agg = new AdjacencyMatrix('by_color');

$agg->addFilter(new Term(['color' => 'blue']), 'blue');

$agg->setSeparator('|');

$this->assertEquals($expected, $agg->toArray());
}

/**
* @group functional
*/
public function testAdjacencyMatrixAggregation()
{
$agg = new AdjacencyMatrix('interactions');
$agg->addFilter(new Terms('accounts', ['hillary', 'sidney']), 'grpA');
$agg->addFilter(new Terms('accounts', ['donald', 'mitt']), 'grpB');
$agg->addFilter(new Terms('accounts', ['vladimir', 'nigel']), 'grpC');

$agg->setSeparator('|');

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

$results = $this->_getIndexForTest()->search($query)->getAggregation('interactions');

$expected = [
[
'key' => 'grpA',
'doc_count' => 2,
],
[
'key' => 'grpA|grpB',
'doc_count' => 1,
],
[
'key' => 'grpB',
'doc_count' => 2,
],
[
'key' => 'grpB|grpC',
'doc_count' => 1,
],
[
'key' => 'grpC',
'doc_count' => 1,
],
];

$this->assertEquals($expected, $results['buckets']);
}
}
1 change: 1 addition & 0 deletions test/Elastica/QueryBuilder/DSL/AggregationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function testInterface()
$this->_assertImplemented($aggregationDSL, 'serial_diff', Aggregation\SerialDiff::class, ['name']);
$this->_assertImplemented($aggregationDSL, 'avg_bucket', Aggregation\AvgBucket::class, ['name']);
$this->_assertImplemented($aggregationDSL, 'sum_bucket', Aggregation\SumBucket::class, ['name']);
$this->_assertImplemented($aggregationDSL, 'adjacency_matrix', Aggregation\AdjacencyMatrix::class, ['name']);

$this->_assertNotImplemented($aggregationDSL, 'children', ['name']);
$this->_assertNotImplemented($aggregationDSL, 'geo_bounds', ['name']);
Expand Down