|
| 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