Skip to content

Commit 7f62b40

Browse files
committed
Merge pull request #786 from im-denisenko/feature/percentiles-aggregation
Add support for percentiles aggregation
2 parents bfa9363 + c69a1f2 commit 7f62b40

File tree

4 files changed

+170
-4
lines changed

4 files changed

+170
-4
lines changed

changes.txt

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
CHANGES
22

3+
2015-02-18
4+
- Added support for percentiles aggregation
5+
36
2015-02-17
47
- Release v1.4.3.0
58
- Added Elastica\Query\MatchPhrase #599
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
namespace Elastica\Aggregation;
3+
4+
/**
5+
* Class Percentiles
6+
* @package Elastica\Aggregation
7+
* @link http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-percentile-aggregation.html
8+
*/
9+
class Percentiles extends AbstractSimpleAggregation
10+
{
11+
/**
12+
* @param string $name the name of this aggregation
13+
* @param string $field the field on which to perform this aggregation
14+
*/
15+
public function __construct($name, $field = null)
16+
{
17+
parent::__construct($name);
18+
19+
if (!is_null($field)) {
20+
$this->setField($field);
21+
}
22+
}
23+
24+
/**
25+
* Set compression parameter
26+
*
27+
* @param float $value
28+
* @return $this
29+
*/
30+
public function setCompression($value)
31+
{
32+
return $this->setParam('compression', (float) $value);
33+
}
34+
35+
/**
36+
* Set which percents must be returned
37+
*
38+
* @param float[] $percents
39+
* @return $this
40+
*/
41+
public function setPercents(array $percents)
42+
{
43+
return $this->setParam('percents', $percents);
44+
}
45+
46+
/**
47+
* Add yet another percent to result
48+
*
49+
* @param float $percent
50+
* @return $this
51+
*/
52+
public function addPercent($percent)
53+
{
54+
return $this->addParam('percents', (float) $percent);
55+
}
56+
}

lib/Elastica/QueryBuilder/DSL/Aggregation.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Elastica\Aggregation\Min;
1919
use Elastica\Aggregation\Missing;
2020
use Elastica\Aggregation\Nested;
21+
use Elastica\Aggregation\Percentiles;
2122
use Elastica\Aggregation\Range;
2223
use Elastica\Aggregation\ReverseNested;
2324
use Elastica\Aggregation\ScriptedMetric;
@@ -138,11 +139,13 @@ public function value_count($name, $field)
138139
* percentiles aggregation
139140
*
140141
* @link http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-percentile-aggregation.html
141-
* @param string $name
142+
* @param string $name the name of this aggregation
143+
* @param string $field the field on which to perform this aggregation
144+
* @return Percentiles
142145
*/
143-
public function percentiles($name)
146+
public function percentiles($name, $field = null)
144147
{
145-
throw new NotImplementedException();
148+
return new Percentiles($name, $field);
146149
}
147150

148151
/**
@@ -241,7 +244,7 @@ public function filter($name, AbstractFilter $filter)
241244
* filters aggregation
242245
*
243246
* @link http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filters-aggregation.html
244-
* @param string $name
247+
* @param string $name
245248
* @return Filters
246249
*/
247250
public function filters($name)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
namespace Elastica\Test\Aggregation;
3+
4+
use Elastica\Aggregation\Percentiles;
5+
use Elastica\Document;
6+
use Elastica\Query;
7+
8+
class PercentilesTest extends BaseAggregationTest
9+
{
10+
public function testConstruct()
11+
{
12+
$aggr = new Percentiles('price_percentile');
13+
$this->assertEquals('price_percentile', $aggr->getName());
14+
15+
$aggr = new Percentiles('price_percentile', 'price');
16+
$this->assertEquals('price', $aggr->getParam('field'));
17+
}
18+
19+
public function testSetField()
20+
{
21+
$aggr = new Percentiles('price_percentile');
22+
$aggr->setField('price');
23+
24+
$this->assertEquals('price', $aggr->getParam('field'));
25+
$this->assertInstanceOf('Elastica\Aggregation\Percentiles', $aggr->setField('price'));
26+
}
27+
28+
public function testSetCompression()
29+
{
30+
$aggr = new Percentiles('price_percentile');
31+
$aggr->setCompression(200);
32+
$this->assertEquals(200, $aggr->getParam('compression'));
33+
$this->assertInstanceOf('Elastica\Aggregation\Percentiles', $aggr->setCompression(200));
34+
}
35+
36+
public function testSetPercents()
37+
{
38+
$percents = array(1, 2, 3);
39+
$aggr = new Percentiles('price_percentile');
40+
$aggr->setPercents($percents);
41+
$this->assertEquals($percents, $aggr->getParam('percents'));
42+
$this->assertInstanceOf('Elastica\Aggregation\Percentiles', $aggr->setPercents($percents));
43+
}
44+
45+
public function testAddPercent()
46+
{
47+
$percents = array(1, 2, 3);
48+
$aggr = new Percentiles('price_percentile');
49+
$aggr->setPercents($percents);
50+
$this->assertEquals($percents, $aggr->getParam('percents'));
51+
$aggr->addPercent(4);
52+
$percents[] = 4;
53+
$this->assertEquals($percents, $aggr->getParam('percents'));
54+
$this->assertInstanceOf('Elastica\Aggregation\Percentiles', $aggr->addPercent(4));
55+
}
56+
57+
public function testSetScript()
58+
{
59+
$script = 'doc["load_time"].value / 20';
60+
$aggr = new Percentiles('price_percentile');
61+
$aggr->setScript($script);
62+
$this->assertEquals($script, $aggr->getParam('script'));
63+
$this->assertInstanceOf('Elastica\Aggregation\Percentiles', $aggr->setScript($script));
64+
}
65+
66+
public function testActualWork()
67+
{
68+
// prepare
69+
$index = $this->_createIndex();
70+
$type = $index->getType('offer');
71+
$type->addDocuments(array(
72+
new Document(1, array('price' => 100)),
73+
new Document(2, array('price' => 200)),
74+
new Document(3, array('price' => 300)),
75+
new Document(4, array('price' => 400)),
76+
new Document(5, array('price' => 500)),
77+
new Document(6, array('price' => 600)),
78+
new Document(7, array('price' => 700)),
79+
new Document(8, array('price' => 800)),
80+
new Document(9, array('price' => 900)),
81+
new Document(10, array('price' => 1000)),
82+
));
83+
$index->refresh();
84+
85+
// execute
86+
$aggr = new Percentiles('price_percentile');
87+
$aggr->setField('price');
88+
89+
$query = new Query();
90+
$query->addAggregation($aggr);
91+
92+
$resultSet = $type->search($query);
93+
$aggrResult = $resultSet->getAggregation('price_percentile');
94+
95+
// hope it's ok to hardcode results...
96+
$this->assertEquals(109.0, $aggrResult['values']['1.0']);
97+
$this->assertEquals(145.0, $aggrResult['values']['5.0']);
98+
$this->assertEquals(325.0, $aggrResult['values']['25.0']);
99+
$this->assertEquals(550.0, $aggrResult['values']['50.0']);
100+
$this->assertEquals(775.0, $aggrResult['values']['75.0']);
101+
$this->assertEquals(955.0, $aggrResult['values']['95.0']);
102+
$this->assertEquals(991.0, $aggrResult['values']['99.0']);
103+
}
104+
}

0 commit comments

Comments
 (0)