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

Add support for multiple bucket sort orders #1480

Merged
merged 1 commit into from Apr 6, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file based on the
### Added

* Added support for pipeline when indexing document. [#1455](https://github.com/ruflin/Elastica/pull/1455)
* Added support for multiple bucket sort orders for aggregations.

### Improvements

Expand Down
12 changes: 12 additions & 0 deletions lib/Elastica/Aggregation/Terms.php
Expand Up @@ -20,4 +20,16 @@ public function setOrder($order, $direction)
{
return $this->setParam('order', [$order => $direction]);
}

/**
* Sets a list of bucket sort orders.
*
* @param array $orders A list of [<aggregationField>|"_count"|"_term" => <direction>] definitions.
*
* @return $this
*/
public function setOrders(array $orders)
{
return $this->setParam('order', $orders);
}
}
2 changes: 1 addition & 1 deletion lib/Elastica/Document.php
Expand Up @@ -276,7 +276,7 @@ public function isAutoPopulate()
}

/**
* Sets pipeline
* Sets pipeline.
*
* @param string $pipeline
*
Expand Down
21 changes: 21 additions & 0 deletions test/Elastica/Aggregation/TermsTest.php
Expand Up @@ -60,4 +60,25 @@ public function testTermsSetOrder()

$this->assertEquals('blue', $results['buckets'][2]['key']);
}

/**
* @group functional
*/
public function testTermsSetOrders()
{
$agg = new Terms('terms');
$agg->setField('color');
$agg->setOrders([
['_count' => 'asc'], // 1. red, 2. green, 3. blue
['_key' => 'asc'], // 1. green, 2. red, 3. blue
]);

$query = new Query();
$query->addAggregation($agg);
$results = $this->_getIndexForTest()->search($query)->getAggregation('terms');

$this->assertSame('green', $results['buckets'][0]['key']);
$this->assertSame('red', $results['buckets'][1]['key']);
$this->assertSame('blue', $results['buckets'][2]['key']);
}
}