Skip to content

Commit 7acab37

Browse files
cjost1988ruflin
authored andcommitted
Add support for multiple bucket sort orders (#1480)
1 parent 9e840a1 commit 7acab37

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file based on the
1111
### Added
1212

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

1516
### Improvements
1617

lib/Elastica/Aggregation/Terms.php

+12
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,16 @@ public function setOrder($order, $direction)
2020
{
2121
return $this->setParam('order', [$order => $direction]);
2222
}
23+
24+
/**
25+
* Sets a list of bucket sort orders.
26+
*
27+
* @param array $orders A list of [<aggregationField>|"_count"|"_term" => <direction>] definitions.
28+
*
29+
* @return $this
30+
*/
31+
public function setOrders(array $orders)
32+
{
33+
return $this->setParam('order', $orders);
34+
}
2335
}

lib/Elastica/Document.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ public function isAutoPopulate()
276276
}
277277

278278
/**
279-
* Sets pipeline
279+
* Sets pipeline.
280280
*
281281
* @param string $pipeline
282282
*

test/Elastica/Aggregation/TermsTest.php

+21
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,25 @@ public function testTermsSetOrder()
6060

6161
$this->assertEquals('blue', $results['buckets'][2]['key']);
6262
}
63+
64+
/**
65+
* @group functional
66+
*/
67+
public function testTermsSetOrders()
68+
{
69+
$agg = new Terms('terms');
70+
$agg->setField('color');
71+
$agg->setOrders([
72+
['_count' => 'asc'], // 1. red, 2. green, 3. blue
73+
['_key' => 'asc'], // 1. green, 2. red, 3. blue
74+
]);
75+
76+
$query = new Query();
77+
$query->addAggregation($agg);
78+
$results = $this->_getIndexForTest()->search($query)->getAggregation('terms');
79+
80+
$this->assertSame('green', $results['buckets'][0]['key']);
81+
$this->assertSame('red', $results['buckets'][1]['key']);
82+
$this->assertSame('blue', $results['buckets'][2]['key']);
83+
}
6384
}

0 commit comments

Comments
 (0)