diff options
author | Pierre Schmitz <pierre@archlinux.de> | 2015-12-17 09:15:42 +0100 |
---|---|---|
committer | Pierre Schmitz <pierre@archlinux.de> | 2015-12-17 09:44:51 +0100 |
commit | a1789ddde42033f1b05cc4929491214ee6e79383 (patch) | |
tree | 63615735c4ddffaaabf2428946bb26f90899f7bf /vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation | |
parent | 9e06a62f265e3a2aaabecc598d4bc617e06fa32d (diff) |
Update to MediaWiki 1.26.0
Diffstat (limited to 'vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation')
28 files changed, 2152 insertions, 0 deletions
diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/AvgTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/AvgTest.php new file mode 100644 index 00000000..650a4655 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/AvgTest.php @@ -0,0 +1,39 @@ +<?php +namespace Elastica\Test\Aggregation; + +use Elastica\Aggregation\Avg; +use Elastica\Document; +use Elastica\Query; + +class AvgTest extends BaseAggregationTest +{ + protected function _getIndexForTest() + { + $index = $this->_createIndex(); + + $index->getType('test')->addDocuments(array( + new Document(1, array('price' => 5)), + new Document(2, array('price' => 8)), + new Document(3, array('price' => 1)), + new Document(4, array('price' => 3)), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testAvgAggregation() + { + $agg = new Avg('avg'); + $agg->setField('price'); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregations(); + $this->assertEquals((5 + 8 + 1 + 3) / 4.0, $results['avg']['value']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/BaseAggregationTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/BaseAggregationTest.php new file mode 100644 index 00000000..48003d7e --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/BaseAggregationTest.php @@ -0,0 +1,8 @@ +<?php +namespace Elastica\Test\Aggregation; + +use Elastica\Test\Base; + +abstract class BaseAggregationTest extends Base +{ +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/CardinalityTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/CardinalityTest.php new file mode 100644 index 00000000..7bc383f0 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/CardinalityTest.php @@ -0,0 +1,132 @@ +<?php +namespace Elastica\Test\Aggregation; + +use Elastica\Aggregation\Cardinality; +use Elastica\Document; +use Elastica\Query; + +class CardinalityTest extends BaseAggregationTest +{ + protected function _getIndexForTest() + { + $index = $this->_createIndex(); + + $index->getType('test')->addDocuments(array( + new Document(1, array('color' => 'blue')), + new Document(2, array('color' => 'blue')), + new Document(3, array('color' => 'red')), + new Document(4, array('color' => 'green')), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testCardinalityAggregation() + { + $agg = new Cardinality('cardinality'); + $agg->setField('color'); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('cardinality'); + + $this->assertEquals(3, $results['value']); + } + + /** + * @dataProvider invalidPrecisionThresholdProvider + * @expectedException \InvalidArgumentException + * + * @param $threshold + */ + public function testInvalidPrecisionThreshold($threshold) + { + $agg = new Cardinality('threshold'); + $agg->setPrecisionThreshold($threshold); + } + + /** + * @dataProvider validPrecisionThresholdProvider + * + * @param $threshold + */ + public function testPrecisionThreshold($threshold) + { + $agg = new Cardinality('threshold'); + $agg->setPrecisionThreshold($threshold); + + $this->assertNotNull($agg->getParam('precision_threshold')); + $this->assertInternalType('int', $agg->getParam('precision_threshold')); + } + + public function invalidPrecisionThresholdProvider() + { + return array( + 'string' => array('100'), + 'float' => array(7.8), + 'boolean' => array(true), + 'array' => array(array()), + 'object' => array(new \StdClass()), + ); + } + + public function validPrecisionThresholdProvider() + { + return array( + 'negative-int' => array(-140), + 'zero' => array(0), + 'positive-int' => array(150), + 'more-than-max' => array(40001), + ); + } + + /** + * @dataProvider validRehashProvider + * + * @param bool $rehash + */ + public function testRehash($rehash) + { + $agg = new Cardinality('rehash'); + $agg->setRehash($rehash); + + $this->assertNotNull($agg->getParam('rehash')); + $this->assertInternalType('boolean', $agg->getParam('rehash')); + } + + /** + * @dataProvider invalidRehashProvider + * @expectedException \InvalidArgumentException + * + * @param mixed $rehash + */ + public function testInvalidRehash($rehash) + { + $agg = new Cardinality('rehash'); + $agg->setRehash($rehash); + } + + public function invalidRehashProvider() + { + return array( + 'string' => array('100'), + 'int' => array(100), + 'float' => array(7.8), + 'array' => array(array()), + 'object' => array(new \StdClass()), + ); + } + + public function validRehashProvider() + { + return array( + 'true' => array(true), + 'false' => array(false), + ); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/DateHistogramTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/DateHistogramTest.php new file mode 100644 index 00000000..ca115ccc --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/DateHistogramTest.php @@ -0,0 +1,103 @@ +<?php +namespace Elastica\Test\Aggregation; + +use Elastica\Aggregation\DateHistogram; +use Elastica\Document; +use Elastica\Query; +use Elastica\Type\Mapping; + +class DateHistogramTest extends BaseAggregationTest +{ + protected function _getIndexForTest() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + + $type->setMapping(new Mapping(null, array( + 'created' => array('type' => 'date'), + ))); + + $type->addDocuments(array( + new Document(1, array('created' => '2014-01-29T00:20:00')), + new Document(2, array('created' => '2014-01-29T02:20:00')), + new Document(3, array('created' => '2014-01-29T03:20:00')), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testDateHistogramAggregation() + { + $agg = new DateHistogram('hist', 'created', '1h'); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('hist'); + + $this->assertEquals(3, sizeof($results['buckets'])); + } + + /** + * @group unit + */ + public function testSetOffset() + { + $agg = new DateHistogram('hist', 'created', '1h'); + + $agg->setOffset('3m'); + + $expected = array( + 'date_histogram' => array( + 'field' => 'created', + 'interval' => '1h', + 'offset' => '3m', + ), + ); + + $this->assertEquals($expected, $agg->toArray()); + + $this->assertInstanceOf('Elastica\Aggregation\DateHistogram', $agg->setOffset('3m')); + } + + /** + * @group functional + */ + public function testSetOffsetWorks() + { + $agg = new DateHistogram('hist', 'created', '1m'); + $agg->setOffset('+40s'); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('hist'); + + $this->assertEquals('2014-01-29T00:19:40.000Z', $results['buckets'][0]['key_as_string']); + } + + /** + * @group unit + */ + public function testSetTimezone() + { + $agg = new DateHistogram('hist', 'created', '1h'); + + $agg->setTimezone('-02:30'); + + $expected = array( + 'date_histogram' => array( + 'field' => 'created', + 'interval' => '1h', + 'time_zone' => '-02:30', + ), + ); + + $this->assertEquals($expected, $agg->toArray()); + + $this->assertInstanceOf('Elastica\Aggregation\DateHistogram', $agg->setTimezone('-02:30')); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/DateRangeTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/DateRangeTest.php new file mode 100644 index 00000000..b8078a4c --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/DateRangeTest.php @@ -0,0 +1,52 @@ +<?php +namespace Elastica\Test\Aggregation; + +use Elastica\Aggregation\DateRange; +use Elastica\Document; +use Elastica\Query; +use Elastica\Type\Mapping; + +class DateRangeTest extends BaseAggregationTest +{ + protected function _getIndexForTest() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + + $type->setMapping(new Mapping(null, array( + 'created' => array('type' => 'date'), + ))); + + $type->addDocuments(array( + new Document(1, array('created' => 1390962135000)), + new Document(2, array('created' => 1390965735000)), + new Document(3, array('created' => 1390954935000)), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testDateRangeAggregation() + { + $agg = new DateRange('date'); + $agg->setField('created'); + $agg->addRange(1390958535000)->addRange(null, 1390958535000); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('date'); + + foreach ($results['buckets'] as $bucket) { + if (array_key_exists('to', $bucket)) { + $this->assertEquals(1, $bucket['doc_count']); + } elseif (array_key_exists('from', $bucket)) { + $this->assertEquals(2, $bucket['doc_count']); + } + } + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/ExtendedStatsTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/ExtendedStatsTest.php new file mode 100644 index 00000000..8c336245 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/ExtendedStatsTest.php @@ -0,0 +1,45 @@ +<?php +namespace Elastica\Test\Aggregation; + +use Elastica\Aggregation\ExtendedStats; +use Elastica\Document; +use Elastica\Query; + +class ExtendedStatsTest extends BaseAggregationTest +{ + protected function _getIndexForTest() + { + $index = $this->_createIndex(); + + $index->getType('test')->addDocuments(array( + new Document(1, array('price' => 5)), + new Document(2, array('price' => 8)), + new Document(3, array('price' => 1)), + new Document(4, array('price' => 3)), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testExtendedStatsAggregation() + { + $agg = new ExtendedStats('stats'); + $agg->setField('price'); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('stats'); + + $this->assertEquals(4, $results['count']); + $this->assertEquals(1, $results['min']); + $this->assertEquals(8, $results['max']); + $this->assertEquals((5 + 8 + 1 + 3) / 4.0, $results['avg']); + $this->assertEquals((5 + 8 + 1 + 3), $results['sum']); + $this->assertTrue(array_key_exists('sum_of_squares', $results)); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/FilterTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/FilterTest.php new file mode 100644 index 00000000..9198bb95 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/FilterTest.php @@ -0,0 +1,113 @@ +<?php +namespace Elastica\Test\Aggregation; + +use Elastica\Aggregation\Avg; +use Elastica\Aggregation\Filter; +use Elastica\Document; +use Elastica\Filter\Range; +use Elastica\Filter\Term; +use Elastica\Query; + +class FilterTest extends BaseAggregationTest +{ + protected function _getIndexForTest() + { + $index = $this->_createIndex(); + + $index->getType('test')->addDocuments(array( + new Document(1, array('price' => 5, 'color' => 'blue')), + new Document(2, array('price' => 8, 'color' => 'blue')), + new Document(3, array('price' => 1, 'color' => 'red')), + new Document(4, array('price' => 3, 'color' => 'green')), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group unit + */ + public function testToArray() + { + $expected = array( + 'filter' => array('range' => array('stock' => array('gt' => 0))), + 'aggs' => array( + 'avg_price' => array('avg' => array('field' => 'price')), + ), + ); + + $agg = new Filter('in_stock_products'); + $agg->setFilter(new Range('stock', array('gt' => 0))); + $avg = new Avg('avg_price'); + $avg->setField('price'); + $agg->addAggregation($avg); + + $this->assertEquals($expected, $agg->toArray()); + } + + /** + * @group functional + */ + public function testFilterAggregation() + { + $agg = new Filter('filter'); + $agg->setFilter(new Term(array('color' => 'blue'))); + $avg = new Avg('price'); + $avg->setField('price'); + $agg->addAggregation($avg); + + $query = new Query(); + $query->addAggregation($agg); + + $results = $this->_getIndexForTest()->search($query)->getAggregation('filter'); + $results = $results['price']['value']; + + $this->assertEquals((5 + 8) / 2.0, $results); + } + + /** + * @group functional + */ + public function testFilterNoSubAggregation() + { + $agg = new Avg('price'); + $agg->setField('price'); + + $query = new Query(); + $query->addAggregation($agg); + + $results = $this->_getIndexForTest()->search($query)->getAggregation('price'); + $results = $results['value']; + + $this->assertEquals((5 + 8 + 1 + 3) / 4.0, $results); + } + + /** + * @group unit + */ + public function testConstruct() + { + $agg = new Filter('foo', new Term(array('color' => 'blue'))); + + $expected = array( + 'filter' => array( + 'term' => array( + 'color' => 'blue', + ), + ), + ); + + $this->assertEquals($expected, $agg->toArray()); + } + + /** + * @group unit + */ + public function testConstructWithoutFilter() + { + $agg = new Filter('foo'); + $this->assertEquals('foo', $agg->getName()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/FiltersTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/FiltersTest.php new file mode 100644 index 00000000..36ebcd45 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/FiltersTest.php @@ -0,0 +1,120 @@ +<?php +namespace Elastica\Test\Aggregation; + +use Elastica\Aggregation\Avg; +use Elastica\Aggregation\Filter; +use Elastica\Aggregation\Filters; +use Elastica\Document; +use Elastica\Filter\Term; +use Elastica\Query; + +class FiltersTest extends BaseAggregationTest +{ + protected function _getIndexForTest() + { + $index = $this->_createIndex('filter'); + + $index->getType('test')->addDocuments(array( + new Document(1, array('price' => 5, 'color' => 'blue')), + new Document(2, array('price' => 8, 'color' => 'blue')), + new Document(3, array('price' => 1, 'color' => 'red')), + new Document(4, array('price' => 3, 'color' => 'green')), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group unit + */ + public function testToArrayUsingNamedFilters() + { + $expected = array( + 'filters' => array( + 'filters' => array( + 'blue' => array( + 'term' => array('color' => 'blue'), + ), + 'red' => array( + 'term' => array('color' => 'red'), + ), + ), + ), + 'aggs' => array( + 'avg_price' => array('avg' => array('field' => 'price')), + ), + ); + + $agg = new Filters('by_color'); + $agg->addFilter(new Term(array('color' => 'blue')), 'blue'); + $agg->addFilter(new Term(array('color' => 'red')), 'red'); + + $avg = new Avg('avg_price'); + $avg->setField('price'); + $agg->addAggregation($avg); + + $this->assertEquals($expected, $agg->toArray()); + } + + /** + * @group unit + */ + public function testToArrayUsingAnonymousFilters() + { + $expected = array( + 'filters' => array( + 'filters' => array( + array( + 'term' => array('color' => 'blue'), + ), + array( + 'term' => array('color' => 'red'), + ), + ), + ), + 'aggs' => array( + 'avg_price' => array('avg' => array('field' => 'price')), + ), + ); + + $agg = new Filters('by_color'); + $agg->addFilter(new Term(array('color' => 'blue'))); + $agg->addFilter(new Term(array('color' => 'red'))); + + $avg = new Avg('avg_price'); + $avg->setField('price'); + $agg->addAggregation($avg); + + $this->assertEquals($expected, $agg->toArray()); + } + + /** + * @group functional + */ + public function testFilterAggregation() + { + $agg = new Filters('by_color'); + $agg->addFilter(new Term(array('color' => 'blue')), 'blue'); + $agg->addFilter(new Term(array('color' => 'red')), 'red'); + + $avg = new Avg('avg_price'); + $avg->setField('price'); + $agg->addAggregation($avg); + + $query = new Query(); + $query->addAggregation($agg); + + $results = $this->_getIndexForTest()->search($query)->getAggregation('by_color'); + + $resultsForBlue = $results['buckets']['blue']; + $resultsForRed = $results['buckets']['red']; + + $this->assertEquals(2, $resultsForBlue['doc_count']); + $this->assertEquals(1, $resultsForRed['doc_count']); + + $this->assertEquals((5 + 8) / 2, $resultsForBlue['avg_price']['value']); + $this->assertEquals(1, $resultsForRed['avg_price']['value']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/GeoDistanceTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/GeoDistanceTest.php new file mode 100644 index 00000000..f8a02d1d --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/GeoDistanceTest.php @@ -0,0 +1,46 @@ +<?php +namespace Elastica\Test\Aggregation; + +use Elastica\Aggregation\GeoDistance; +use Elastica\Document; +use Elastica\Query; +use Elastica\Type\Mapping; + +class GeoDistanceTest extends BaseAggregationTest +{ + protected function _getIndexForTest() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + + $type->setMapping(new Mapping(null, array( + 'location' => array('type' => 'geo_point'), + ))); + + $type->addDocuments(array( + new Document(1, array('location' => array('lat' => 32.849437, 'lon' => -117.271732))), + new Document(2, array('location' => array('lat' => 32.798320, 'lon' => -117.246648))), + new Document(3, array('location' => array('lat' => 37.782439, 'lon' => -122.392560))), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testGeoDistanceAggregation() + { + $agg = new GeoDistance('geo', 'location', array('lat' => 32.804654, 'lon' => -117.242594)); + $agg->addRange(null, 100); + $agg->setUnit('mi'); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('geo'); + + $this->assertEquals(2, $results['buckets'][0]['doc_count']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/GeohashGridTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/GeohashGridTest.php new file mode 100644 index 00000000..6e0d43fd --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/GeohashGridTest.php @@ -0,0 +1,46 @@ +<?php +namespace Elastica\Test\Aggregation; + +use Elastica\Aggregation\GeohashGrid; +use Elastica\Document; +use Elastica\Query; +use Elastica\Type\Mapping; + +class GeohashGridTest extends BaseAggregationTest +{ + protected function _getIndexForTest() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + + $type->setMapping(new Mapping(null, array( + 'location' => array('type' => 'geo_point'), + ))); + + $type->addDocuments(array( + new Document(1, array('location' => array('lat' => 32.849437, 'lon' => -117.271732))), + new Document(2, array('location' => array('lat' => 32.798320, 'lon' => -117.246648))), + new Document(3, array('location' => array('lat' => 37.782439, 'lon' => -122.392560))), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testGeohashGridAggregation() + { + $agg = new GeohashGrid('hash', 'location'); + $agg->setPrecision(3); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('hash'); + + $this->assertEquals(2, $results['buckets'][0]['doc_count']); + $this->assertEquals(1, $results['buckets'][1]['doc_count']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/GlobalAggregationTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/GlobalAggregationTest.php new file mode 100644 index 00000000..6ab086d0 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/GlobalAggregationTest.php @@ -0,0 +1,27 @@ +<?php +namespace Elastica\Test\Aggregation; + +use Elastica\Aggregation\Avg; +use Elastica\Aggregation\GlobalAggregation; + +class GlobalAggregationTest extends BaseAggregationTest +{ + /** + * @group unit + */ + public function testToArray() + { + $expected = array( + 'global' => new \stdClass(), + 'aggs' => array( + 'avg_price' => array('avg' => array('field' => 'price')), + ), + ); + + $agg = new GlobalAggregation('all_products'); + $avg = new Avg('avg_price'); + $avg->setField('price'); + $agg->addAggregation($avg); + $this->assertEquals($expected, $agg->toArray()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/HistogramTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/HistogramTest.php new file mode 100644 index 00000000..ffdf73a4 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/HistogramTest.php @@ -0,0 +1,47 @@ +<?php +namespace Elastica\Test\Aggregation; + +use Elastica\Aggregation\Histogram; +use Elastica\Document; +use Elastica\Query; + +class HistogramTest extends BaseAggregationTest +{ + protected function _getIndexForTest() + { + $index = $this->_createIndex(); + + $index->getType('test')->addDocuments(array( + new Document(1, array('price' => 5, 'color' => 'blue')), + new Document(2, array('price' => 8, 'color' => 'blue')), + new Document(3, array('price' => 1, 'color' => 'red')), + new Document(4, array('price' => 30, 'color' => 'green')), + new Document(5, array('price' => 40, 'color' => 'red')), + new Document(6, array('price' => 35, 'color' => 'green')), + new Document(7, array('price' => 42, 'color' => 'red')), + new Document(8, array('price' => 41, 'color' => 'blue')), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testHistogramAggregation() + { + $agg = new Histogram('hist', 'price', 10); + $agg->setMinimumDocumentCount(0); // should return empty buckets + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('hist'); + + $buckets = $results['buckets']; + $this->assertEquals(5, sizeof($buckets)); + $this->assertEquals(30, $buckets[3]['key']); + $this->assertEquals(2, $buckets[3]['doc_count']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/IpRangeTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/IpRangeTest.php new file mode 100644 index 00000000..2f3099f6 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/IpRangeTest.php @@ -0,0 +1,57 @@ +<?php +namespace Elastica\Test\Aggregation; + +use Elastica\Aggregation\IpRange; +use Elastica\Document; +use Elastica\Query; +use Elastica\Type\Mapping; + +class IpRangeTest extends BaseAggregationTest +{ + protected function _getIndexForTest() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + + $type->setMapping(new Mapping(null, array( + 'address' => array('type' => 'ip'), + ))); + + $type->addDocuments(array( + new Document(1, array('address' => '192.168.1.100')), + new Document(2, array('address' => '192.168.1.150')), + new Document(3, array('address' => '192.168.1.200')), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testIpRangeAggregation() + { + $agg = new IpRange('ip', 'address'); + $agg->addRange('192.168.1.101'); + $agg->addRange(null, '192.168.1.200'); + + $cidrRange = '192.168.1.0/24'; + $agg->addMaskRange($cidrRange); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('ip'); + + foreach ($results['buckets'] as $bucket) { + if (array_key_exists('key', $bucket) && $bucket['key'] == $cidrRange) { + // the CIDR mask + $this->assertEquals(3, $bucket['doc_count']); + } else { + // the normal ip ranges + $this->assertEquals(2, $bucket['doc_count']); + } + } + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/MaxTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/MaxTest.php new file mode 100644 index 00000000..f057b81d --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/MaxTest.php @@ -0,0 +1,79 @@ +<?php +namespace Elastica\Test\Aggregation; + +use Elastica\Aggregation\Max; +use Elastica\Document; +use Elastica\Query; +use Elastica\Script; + +class MaxTest extends BaseAggregationTest +{ + protected function _getIndexForTest() + { + $index = $this->_createIndex(); + + $index->getType('test')->addDocuments(array( + new Document(1, array('price' => 5)), + new Document(2, array('price' => 8)), + new Document(3, array('price' => 1)), + new Document(4, array('price' => 3)), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group unit + */ + public function testToArray() + { + $expected = array( + 'max' => array( + 'field' => 'price', + 'script' => '_value * conversion_rate', + 'params' => array( + 'conversion_rate' => 1.2, + ), + ), + 'aggs' => array( + 'subagg' => array('max' => array('field' => 'foo')), + ), + ); + + $agg = new Max('min_price_in_euros'); + $agg->setField('price'); + $agg->setScript(new Script('_value * conversion_rate', array('conversion_rate' => 1.2))); + $max = new Max('subagg'); + $max->setField('foo'); + $agg->addAggregation($max); + + $this->assertEquals($expected, $agg->toArray()); + } + + /** + * @group functional + */ + public function testMaxAggregation() + { + $index = $this->_getIndexForTest(); + + $agg = new Max('min_price'); + $agg->setField('price'); + + $query = new Query(); + $query->addAggregation($agg); + $results = $index->search($query)->getAggregation('min_price'); + + $this->assertEquals(8, $results['value']); + + // test using a script + $agg->setScript(new Script('_value * conversion_rate', array('conversion_rate' => 1.2))); + $query = new Query(); + $query->addAggregation($agg); + $results = $index->search($query)->getAggregation('min_price'); + + $this->assertEquals(8 * 1.2, $results['value']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/MinTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/MinTest.php new file mode 100644 index 00000000..ce0ad5e7 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/MinTest.php @@ -0,0 +1,40 @@ +<?php +namespace Elastica\Test\Aggregation; + +use Elastica\Aggregation\Min; +use Elastica\Document; +use Elastica\Query; + +class MinTest extends BaseAggregationTest +{ + protected function _getIndexForTest() + { + $index = $this->_createIndex(); + + $index->getType('test')->addDocuments(array( + new Document(1, array('price' => 5)), + new Document(2, array('price' => 8)), + new Document(3, array('price' => 1)), + new Document(4, array('price' => 3)), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testMinAggregation() + { + $agg = new Min('min_price'); + $agg->setField('price'); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('min_price'); + + $this->assertEquals(1, $results['value']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/MissingTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/MissingTest.php new file mode 100644 index 00000000..85461879 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/MissingTest.php @@ -0,0 +1,39 @@ +<?php +namespace Elastica\Test\Aggregation; + +use Elastica\Aggregation\Missing; +use Elastica\Document; +use Elastica\Query; + +class MissingTest extends BaseAggregationTest +{ + protected function _getIndexForTest() + { + $index = $this->_createIndex(); + + $index->getType('test')->addDocuments(array( + new Document(1, array('price' => 5, 'color' => 'blue')), + new Document(2, array('price' => 8, 'color' => 'blue')), + new Document(3, array('price' => 1)), + new Document(4, array('price' => 3, 'color' => 'green')), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testMissingAggregation() + { + $agg = new Missing('missing', 'color'); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('missing'); + + $this->assertEquals(1, $results['doc_count']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/NestedTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/NestedTest.php new file mode 100644 index 00000000..58c5d13a --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/NestedTest.php @@ -0,0 +1,63 @@ +<?php +namespace Elastica\Test\Aggregation; + +use Elastica\Aggregation\Min; +use Elastica\Aggregation\Nested; +use Elastica\Document; +use Elastica\Query; +use Elastica\Type\Mapping; + +class NestedTest extends BaseAggregationTest +{ + protected function _getIndexForTest() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + + $type->setMapping(new Mapping(null, array( + 'resellers' => array( + 'type' => 'nested', + 'properties' => array( + 'name' => array('type' => 'string'), + 'price' => array('type' => 'double'), + ), + ), + ))); + + $type->addDocuments(array( + new Document(1, array( + 'resellers' => array( + 'name' => 'spacely sprockets', + 'price' => 5.55, + ), + )), + new Document(2, array( + 'resellers' => array( + 'name' => 'cogswell cogs', + 'price' => 4.98, + ), + )), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testNestedAggregation() + { + $agg = new Nested('resellers', 'resellers'); + $min = new Min('min_price'); + $min->setField('price'); + $agg->addAggregation($min); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('resellers'); + + $this->assertEquals(4.98, $results['min_price']['value']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/PercentilesTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/PercentilesTest.php new file mode 100644 index 00000000..ee4dd2c5 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/PercentilesTest.php @@ -0,0 +1,125 @@ +<?php +namespace Elastica\Test\Aggregation; + +use Elastica\Aggregation\Percentiles; +use Elastica\Document; +use Elastica\Query; + +class PercentilesTest extends BaseAggregationTest +{ + /** + * @group functional + */ + public function testConstruct() + { + $aggr = new Percentiles('price_percentile'); + $this->assertEquals('price_percentile', $aggr->getName()); + + $aggr = new Percentiles('price_percentile', 'price'); + $this->assertEquals('price', $aggr->getParam('field')); + } + + /** + * @group functional + */ + public function testSetField() + { + $aggr = new Percentiles('price_percentile'); + $aggr->setField('price'); + + $this->assertEquals('price', $aggr->getParam('field')); + $this->assertInstanceOf('Elastica\Aggregation\Percentiles', $aggr->setField('price')); + } + + /** + * @group functional + */ + public function testSetCompression() + { + $aggr = new Percentiles('price_percentile'); + $aggr->setCompression(200); + $this->assertEquals(200, $aggr->getParam('compression')); + $this->assertInstanceOf('Elastica\Aggregation\Percentiles', $aggr->setCompression(200)); + } + + /** + * @group functional + */ + public function testSetPercents() + { + $percents = array(1, 2, 3); + $aggr = new Percentiles('price_percentile'); + $aggr->setPercents($percents); + $this->assertEquals($percents, $aggr->getParam('percents')); + $this->assertInstanceOf('Elastica\Aggregation\Percentiles', $aggr->setPercents($percents)); + } + + /** + * @group functional + */ + public function testAddPercent() + { + $percents = array(1, 2, 3); + $aggr = new Percentiles('price_percentile'); + $aggr->setPercents($percents); + $this->assertEquals($percents, $aggr->getParam('percents')); + $aggr->addPercent(4); + $percents[] = 4; + $this->assertEquals($percents, $aggr->getParam('percents')); + $this->assertInstanceOf('Elastica\Aggregation\Percentiles', $aggr->addPercent(4)); + } + + /** + * @group functional + */ + public function testSetScript() + { + $script = 'doc["load_time"].value / 20'; + $aggr = new Percentiles('price_percentile'); + $aggr->setScript($script); + $this->assertEquals($script, $aggr->getParam('script')); + $this->assertInstanceOf('Elastica\Aggregation\Percentiles', $aggr->setScript($script)); + } + + /** + * @group functional + */ + public function testActualWork() + { + // prepare + $index = $this->_createIndex(); + $type = $index->getType('offer'); + $type->addDocuments(array( + new Document(1, array('price' => 100)), + new Document(2, array('price' => 200)), + new Document(3, array('price' => 300)), + new Document(4, array('price' => 400)), + new Document(5, array('price' => 500)), + new Document(6, array('price' => 600)), + new Document(7, array('price' => 700)), + new Document(8, array('price' => 800)), + new Document(9, array('price' => 900)), + new Document(10, array('price' => 1000)), + )); + $index->refresh(); + + // execute + $aggr = new Percentiles('price_percentile'); + $aggr->setField('price'); + + $query = new Query(); + $query->addAggregation($aggr); + + $resultSet = $type->search($query); + $aggrResult = $resultSet->getAggregation('price_percentile'); + + // hope it's ok to hardcode results... + $this->assertEquals(109.0, $aggrResult['values']['1.0']); + $this->assertEquals(145.0, $aggrResult['values']['5.0']); + $this->assertEquals(325.0, $aggrResult['values']['25.0']); + $this->assertEquals(550.0, $aggrResult['values']['50.0']); + $this->assertEquals(775.0, $aggrResult['values']['75.0']); + $this->assertEquals(955.0, $aggrResult['values']['95.0']); + $this->assertEquals(991.0, $aggrResult['values']['99.0']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/RangeTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/RangeTest.php new file mode 100644 index 00000000..f96e4096 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/RangeTest.php @@ -0,0 +1,78 @@ +<?php +namespace Elastica\Test\Aggregation; + +use Elastica\Aggregation\Range; +use Elastica\Document; +use Elastica\Query; + +class RangeTest extends BaseAggregationTest +{ + protected function _getIndexForTest() + { + $index = $this->_createIndex(); + + $index->getType('test')->addDocuments(array( + new Document(1, array('price' => 5)), + new Document(2, array('price' => 8)), + new Document(3, array('price' => 1)), + new Document(4, array('price' => 3)), + new Document(5, array('price' => 1.5)), + new Document(6, array('price' => 2)), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testRangeAggregation() + { + $agg = new Range('range'); + $agg->setField('price'); + $agg->addRange(1.5, 5); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('range'); + + $this->assertEquals(2, $results['buckets'][0]['doc_count']); + } + + /** + * @group unit + */ + public function testRangeAggregationWithKey() + { + $agg = new Range('range'); + $agg->setField('price'); + $agg->addRange(null, 50, 'cheap'); + $agg->addRange(50, 100, 'average'); + $agg->addRange(100, null, 'expensive'); + + $expected = array( + 'range' => array( + 'field' => 'price', + 'ranges' => array( + array( + 'to' => 50, + 'key' => 'cheap', + ), + array( + 'from' => 50, + 'to' => 100, + 'key' => 'average', + ), + array( + 'from' => 100, + 'key' => 'expensive', + ), + ), + ), + ); + + $this->assertEquals($expected, $agg->toArray()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/ReverseNestedTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/ReverseNestedTest.php new file mode 100644 index 00000000..0e2ed2e6 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/ReverseNestedTest.php @@ -0,0 +1,134 @@ +<?php +namespace Elastica\Test\Aggregation; + +use Elastica\Aggregation\Nested; +use Elastica\Aggregation\ReverseNested; +use Elastica\Aggregation\Terms; +use Elastica\Document; +use Elastica\Query; +use Elastica\Type\Mapping; + +class ReverseNestedTest extends BaseAggregationTest +{ + protected function _getIndexForTest() + { + $index = $this->_createIndex(); + $mapping = new Mapping(); + $mapping->setProperties(array( + 'comments' => array( + 'type' => 'nested', + 'properties' => array( + 'name' => array('type' => 'string'), + 'body' => array('type' => 'string'), + ), + ), + )); + $type = $index->getType('test'); + $type->setMapping($mapping); + + $type->addDocuments(array( + new Document(1, array( + 'comments' => array( + array( + 'name' => 'bob', + 'body' => 'this is bobs comment', + ), + array( + 'name' => 'john', + 'body' => 'this is johns comment', + ), + ), + 'tags' => array('foo', 'bar'), + )), + new Document(2, array( + 'comments' => array( + array( + 'name' => 'bob', + 'body' => 'this is another comment from bob', + ), + array( + 'name' => 'susan', + 'body' => 'this is susans comment', + ), + ), + 'tags' => array('foo', 'baz'), + )), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group unit + */ + public function testPathNotSetIfNull() + { + $agg = new ReverseNested('nested'); + $this->assertFalse($agg->hasParam('path')); + } + + /** + * @group unit + */ + public function testPathSetIfNotNull() + { + $agg = new ReverseNested('nested', 'some_field'); + $this->assertEquals('some_field', $agg->getParam('path')); + } + + /** + * @group functional + */ + public function testReverseNestedAggregation() + { + $agg = new Nested('comments', 'comments'); + $names = new Terms('name'); + $names->setField('comments.name'); + + $tags = new Terms('tags'); + $tags->setField('tags'); + + $reverseNested = new ReverseNested('main'); + $reverseNested->addAggregation($tags); + + $names->addAggregation($reverseNested); + + $agg->addAggregation($names); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('comments'); + + $this->assertArrayHasKey('name', $results); + $nameResults = $results['name']; + + $this->assertCount(3, $nameResults['buckets']); + + // bob + $this->assertEquals('bob', $nameResults['buckets'][0]['key']); + $tags = array( + array('key' => 'foo', 'doc_count' => 2), + array('key' => 'bar', 'doc_count' => 1), + array('key' => 'baz', 'doc_count' => 1), + ); + $this->assertEquals($tags, $nameResults['buckets'][0]['main']['tags']['buckets']); + + // john + $this->assertEquals('john', $nameResults['buckets'][1]['key']); + $tags = array( + array('key' => 'bar', 'doc_count' => 1), + array('key' => 'foo', 'doc_count' => 1), + ); + $this->assertEquals($tags, $nameResults['buckets'][1]['main']['tags']['buckets']); + + // susan + $this->assertEquals('susan', $nameResults['buckets'][2]['key']); + $tags = array( + array('key' => 'baz', 'doc_count' => 1), + array('key' => 'foo', 'doc_count' => 1), + ); + $this->assertEquals($tags, $nameResults['buckets'][2]['main']['tags']['buckets']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/ScriptTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/ScriptTest.php new file mode 100644 index 00000000..bf32b251 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/ScriptTest.php @@ -0,0 +1,87 @@ +<?php +namespace Elastica\Test\Aggregation; + +use Elastica\Aggregation\Sum; +use Elastica\Document; +use Elastica\Query; +use Elastica\Script; + +class ScriptTest extends BaseAggregationTest +{ + protected function _getIndexForTest() + { + $index = $this->_createIndex(); + + $index->getType('test')->addDocuments(array( + new Document('1', array('price' => 5)), + new Document('2', array('price' => 8)), + new Document('3', array('price' => 1)), + new Document('4', array('price' => 3)), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testAggregationScript() + { + $agg = new Sum('sum'); + // x = (0..1) is groovy-specific syntax, to see if lang is recognized + $script = new Script("x = (0..1); return doc['price'].value", null, 'groovy'); + $agg->setScript($script); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('sum'); + + $this->assertEquals(5 + 8 + 1 + 3, $results['value']); + } + + /** + * @group functional + */ + public function testAggregationScriptAsString() + { + $agg = new Sum('sum'); + $agg->setScript("doc['price'].value"); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('sum'); + + $this->assertEquals(5 + 8 + 1 + 3, $results['value']); + } + + /** + * @group unit + */ + public function testSetScript() + { + $aggregation = 'sum'; + $string = "doc['price'].value"; + $params = array( + 'param1' => 'one', + 'param2' => 1, + ); + $lang = 'groovy'; + + $agg = new Sum($aggregation); + $script = new Script($string, $params, $lang); + $agg->setScript($script); + + $array = $agg->toArray(); + + $expected = array( + $aggregation => array( + 'script' => $string, + 'params' => $params, + 'lang' => $lang, + ), + ); + $this->assertEquals($expected, $array); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/ScriptedMetricTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/ScriptedMetricTest.php new file mode 100644 index 00000000..31f5798b --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/ScriptedMetricTest.php @@ -0,0 +1,50 @@ +<?php +namespace Elastica\Test\Aggregation; + +use Elastica\Aggregation\ScriptedMetric; +use Elastica\Document; +use Elastica\Query; +use Elastica\Type\Mapping; + +class ScriptedMetricTest extends BaseAggregationTest +{ + protected function _getIndexForTest() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + + $type->setMapping(new Mapping(null, array( + 'start' => array('type' => 'long'), + 'end' => array('type' => 'long'), + ))); + + $type->addDocuments(array( + new Document(1, array('start' => 100, 'end' => 200)), + new Document(2, array('start' => 200, 'end' => 250)), + new Document(3, array('start' => 300, 'end' => 450)), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testScriptedMetricAggregation() + { + $agg = new ScriptedMetric( + 'scripted', + "_agg['durations'] = [:]", + "key = doc['start'].value+ \":\"+ doc['end'].value; _agg.durations[key] = doc['end'].value - doc['start'].value;", + 'values = []; for (item in _agg.durations) { values.add(item.value) }; return values' + ); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('scripted'); + + $this->assertEquals(array(100, 50, 150), $results['value'][0]); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/SignificantTermsTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/SignificantTermsTest.php new file mode 100644 index 00000000..8960768b --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/SignificantTermsTest.php @@ -0,0 +1,72 @@ +<?php +namespace Elastica\Test\Aggregation; + +use Elastica\Aggregation\SignificantTerms; +use Elastica\Document; +use Elastica\Filter\Terms as TermsFilter; +use Elastica\Query; +use Elastica\Query\Terms; + +class SignificantTermsTest extends BaseAggregationTest +{ + protected function _getIndexForTest() + { + $index = $this->_createIndex(); + $colors = array('blue', 'blue', 'red', 'red', 'green', 'yellow', 'white', 'cyan', 'magenta'); + $temperatures = array(1500, 1500, 1500, 1500, 2500, 3500, 4500, 5500, 6500, 7500, 7500, 8500, 9500); + $docs = array(); + for ($i = 0;$i < 250;$i++) { + $docs[] = new Document($i, array('color' => $colors[$i % count($colors)], 'temperature' => $temperatures[$i % count($temperatures)])); + } + $index->getType('test')->addDocuments($docs); + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testSignificantTermsAggregation() + { + $agg = new SignificantTerms('significantTerms'); + $agg->setField('temperature'); + $agg->setSize(1); + + $termsQuery = new Terms(); + $termsQuery->setTerms('color', array('blue', 'red', 'green', 'yellow', 'white')); + + $query = new Query($termsQuery); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('significantTerms'); + + $this->assertEquals(1, count($results['buckets'])); + $this->assertEquals(63, $results['buckets'][0]['doc_count']); + $this->assertEquals(79, $results['buckets'][0]['bg_count']); + $this->assertEquals('1500', $results['buckets'][0]['key_as_string']); + } + + /** + * @group functional + */ + public function testSignificantTermsAggregationWithBackgroundFilter() + { + $agg = new SignificantTerms('significantTerms'); + $agg->setField('temperature'); + $agg->setSize(1); + $termsFilter = new TermsFilter(); + $termsFilter->setTerms('color', array('blue', 'red', 'green', 'yellow')); + $agg->setBackgroundFilter($termsFilter); + + $termsQuery = new Terms(); + $termsQuery->setTerms('color', array('blue', 'red', 'green', 'yellow', 'white')); + + $query = new Query($termsQuery); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('significantTerms'); + + $this->assertEquals(15, $results['buckets'][0]['doc_count']); + $this->assertEquals(12, $results['buckets'][0]['bg_count']); + $this->assertEquals('4500', $results['buckets'][0]['key_as_string']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/StatsTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/StatsTest.php new file mode 100644 index 00000000..45c9d08c --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/StatsTest.php @@ -0,0 +1,44 @@ +<?php +namespace Elastica\Test\Aggregation; + +use Elastica\Aggregation\Stats; +use Elastica\Document; +use Elastica\Query; + +class StatsTest extends BaseAggregationTest +{ + protected function _getIndexForTest() + { + $index = $this->_createIndex(); + + $index->getType('test')->addDocuments(array( + new Document(1, array('price' => 5)), + new Document(2, array('price' => 8)), + new Document(3, array('price' => 1)), + new Document(4, array('price' => 3)), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testStatsAggregation() + { + $agg = new Stats('stats'); + $agg->setField('price'); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('stats'); + + $this->assertEquals(4, $results['count']); + $this->assertEquals(1, $results['min']); + $this->assertEquals(8, $results['max']); + $this->assertEquals((5 + 8 + 1 + 3) / 4.0, $results['avg']); + $this->assertEquals((5 + 8 + 1 + 3), $results['sum']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/SumTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/SumTest.php new file mode 100644 index 00000000..b60e6e14 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/SumTest.php @@ -0,0 +1,40 @@ +<?php +namespace Elastica\Test\Aggregation; + +use Elastica\Aggregation\Sum; +use Elastica\Document; +use Elastica\Query; + +class SumTest extends BaseAggregationTest +{ + protected function _getIndexForTest() + { + $index = $this->_createIndex(); + + $index->getType('test')->addDocuments(array( + new Document(1, array('price' => 5)), + new Document(2, array('price' => 8)), + new Document(3, array('price' => 1)), + new Document(4, array('price' => 3)), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testSumAggregation() + { + $agg = new Sum('sum'); + $agg->setField('price'); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('sum'); + + $this->assertEquals(5 + 8 + 1 + 3, $results['value']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/TermsTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/TermsTest.php new file mode 100644 index 00000000..58eb02c2 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/TermsTest.php @@ -0,0 +1,41 @@ +<?php +namespace Elastica\Test\Aggregation; + +use Elastica\Aggregation\Terms; +use Elastica\Document; +use Elastica\Query; + +class TermsTest extends BaseAggregationTest +{ + protected function _getIndexForTest() + { + $index = $this->_createIndex(); + + $index->getType('test')->addDocuments(array( + new Document(1, array('color' => 'blue')), + new Document(2, array('color' => 'blue')), + new Document(3, array('color' => 'red')), + new Document(4, array('color' => 'green')), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testTermsAggregation() + { + $agg = new Terms('terms'); + $agg->setField('color'); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('terms'); + + $this->assertEquals(2, $results['buckets'][0]['doc_count']); + $this->assertEquals('blue', $results['buckets'][0]['key']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/TopHitsTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/TopHitsTest.php new file mode 100644 index 00000000..afe23e27 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/TopHitsTest.php @@ -0,0 +1,385 @@ +<?php +namespace Elastica\Test\Aggregation; + +use Elastica\Aggregation\Terms; +use Elastica\Aggregation\TopHits; +use Elastica\Document; +use Elastica\Query; +use Elastica\Query\MatchAll; +use Elastica\Query\SimpleQueryString; +use Elastica\Script; +use Elastica\ScriptFields; + +class TopHitsTest extends BaseAggregationTest +{ + protected function _getIndexForTest() + { + $index = $this->_createIndex(); + + $index->getType('questions')->addDocuments(array( + new Document(1, array( + 'tags' => array('linux'), + 'last_activity_date' => '2015-01-05', + 'title' => 'Question about linux #1', + )), + new Document(2, array( + 'tags' => array('linux'), + 'last_activity_date' => '2014-12-23', + 'title' => 'Question about linux #2', + )), + new Document(3, array( + 'tags' => array('windows'), + 'last_activity_date' => '2015-01-05', + 'title' => 'Question about windows #1', + )), + new Document(4, array( + 'tags' => array('windows'), + 'last_activity_date' => '2014-12-23', + 'title' => 'Question about windows #2', + )), + new Document(5, array( + 'tags' => array('osx', 'apple'), + 'last_activity_date' => '2014-12-23', + 'title' => 'Question about osx', + )), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group unit + */ + public function testSetSize() + { + $agg = new TopHits('agg_name'); + $returnValue = $agg->setSize(12); + $this->assertEquals(12, $agg->getParam('size')); + $this->assertInstanceOf('Elastica\Aggregation\TopHits', $returnValue); + } + + /** + * @group unit + */ + public function testSetFrom() + { + $agg = new TopHits('agg_name'); + $returnValue = $agg->setFrom(12); + $this->assertEquals(12, $agg->getParam('from')); + $this->assertInstanceOf('Elastica\Aggregation\TopHits', $returnValue); + } + + /** + * @group unit + */ + public function testSetSort() + { + $sort = array('last_activity_date' => array('order' => 'desc')); + $agg = new TopHits('agg_name'); + $returnValue = $agg->setSort($sort); + $this->assertEquals($sort, $agg->getParam('sort')); + $this->assertInstanceOf('Elastica\Aggregation\TopHits', $returnValue); + } + + /** + * @group unit + */ + public function testSetSource() + { + $fields = array('title', 'tags'); + $agg = new TopHits('agg_name'); + $returnValue = $agg->setSource($fields); + $this->assertEquals($fields, $agg->getParam('_source')); + $this->assertInstanceOf('Elastica\Aggregation\TopHits', $returnValue); + } + + /** + * @group unit + */ + public function testSetVersion() + { + $agg = new TopHits('agg_name'); + $returnValue = $agg->setVersion(true); + $this->assertTrue($agg->getParam('version')); + $this->assertInstanceOf('Elastica\Aggregation\TopHits', $returnValue); + + $agg->setVersion(false); + $this->assertFalse($agg->getParam('version')); + } + + /** + * @group unit + */ + public function testSetExplain() + { + $agg = new TopHits('agg_name'); + $returnValue = $agg->setExplain(true); + $this->assertTrue($agg->getParam('explain')); + $this->assertInstanceOf('Elastica\Aggregation\TopHits', $returnValue); + + $agg->setExplain(false); + $this->assertFalse($agg->getParam('explain')); + } + + /** + * @group unit + */ + public function testSetHighlight() + { + $highlight = array( + 'fields' => array( + 'title', + ), + ); + $agg = new TopHits('agg_name'); + $returnValue = $agg->setHighlight($highlight); + $this->assertEquals($highlight, $agg->getParam('highlight')); + $this->assertInstanceOf('Elastica\Aggregation\TopHits', $returnValue); + } + + /** + * @group unit + */ + public function testSetFieldDataFields() + { + $fields = array('title', 'tags'); + $agg = new TopHits('agg_name'); + $returnValue = $agg->setFieldDataFields($fields); + $this->assertEquals($fields, $agg->getParam('fielddata_fields')); + $this->assertInstanceOf('Elastica\Aggregation\TopHits', $returnValue); + } + + /** + * @group unit + */ + public function testSetScriptFields() + { + $script = new Script('1 + 2'); + $scriptFields = new ScriptFields(array('three' => $script)); + + $agg = new TopHits('agg_name'); + $returnValue = $agg->setScriptFields($scriptFields); + $this->assertEquals($scriptFields->toArray(), $agg->getParam('script_fields')); + $this->assertInstanceOf('Elastica\Aggregation\TopHits', $returnValue); + } + + /** + * @group unit + */ + public function testAddScriptField() + { + $script = new Script('2+3'); + $agg = new TopHits('agg_name'); + $returnValue = $agg->addScriptField('five', $script); + $this->assertEquals(array('five' => $script->toArray()), $agg->getParam('script_fields')); + $this->assertInstanceOf('Elastica\Aggregation\TopHits', $returnValue); + } + + protected function getOuterAggregationResult($innerAggr) + { + $outerAggr = new Terms('top_tags'); + $outerAggr->setField('tags'); + $outerAggr->setMinimumDocumentCount(2); + $outerAggr->addAggregation($innerAggr); + + $query = new Query(new MatchAll()); + $query->addAggregation($outerAggr); + + return $this->_getIndexForTest()->search($query)->getAggregation('top_tags'); + } + + /** + * @group functional + */ + public function testAggregateUpdatedRecently() + { + $aggr = new TopHits('top_tag_hits'); + $aggr->setSize(1); + $aggr->setSort(array('last_activity_date' => array('order' => 'desc'))); + + $resultDocs = array(); + $outerAggrResult = $this->getOuterAggregationResult($aggr); + foreach ($outerAggrResult['buckets'] as $bucket) { + foreach ($bucket['top_tag_hits']['hits']['hits'] as $doc) { + $resultDocs[] = $doc['_id']; + } + } + $this->assertEquals(array(1, 3), $resultDocs); + } + + /** + * @group functional + */ + public function testAggregateUpdatedFarAgo() + { + $aggr = new TopHits('top_tag_hits'); + $aggr->setSize(1); + $aggr->setSort(array('last_activity_date' => array('order' => 'asc'))); + + $resultDocs = array(); + $outerAggrResult = $this->getOuterAggregationResult($aggr); + foreach ($outerAggrResult['buckets'] as $bucket) { + foreach ($bucket['top_tag_hits']['hits']['hits'] as $doc) { + $resultDocs[] = $doc['_id']; + } + } + $this->assertEquals(array(2, 4), $resultDocs); + } + + /** + * @group functional + */ + public function testAggregateTwoDocumentPerTag() + { + $aggr = new TopHits('top_tag_hits'); + $aggr->setSize(2); + + $resultDocs = array(); + $outerAggrResult = $this->getOuterAggregationResult($aggr); + foreach ($outerAggrResult['buckets'] as $bucket) { + foreach ($bucket['top_tag_hits']['hits']['hits'] as $doc) { + $resultDocs[] = $doc['_id']; + } + } + $this->assertEquals(array(1, 2, 3, 4), $resultDocs); + } + + /** + * @group functional + */ + public function testAggregateTwoDocumentPerTagWithOffset() + { + $aggr = new TopHits('top_tag_hits'); + $aggr->setSize(2); + $aggr->setFrom(1); + $aggr->setSort(array('last_activity_date' => array('order' => 'desc'))); + + $resultDocs = array(); + $outerAggrResult = $this->getOuterAggregationResult($aggr); + foreach ($outerAggrResult['buckets'] as $bucket) { + foreach ($bucket['top_tag_hits']['hits']['hits'] as $doc) { + $resultDocs[] = $doc['_id']; + } + } + $this->assertEquals(array(2, 4), $resultDocs); + } + + /** + * @group functional + */ + public function testAggregateWithLimitedSource() + { + $aggr = new TopHits('top_tag_hits'); + $aggr->setSource(array('title')); + + $resultDocs = array(); + $outerAggrResult = $this->getOuterAggregationResult($aggr); + foreach ($outerAggrResult['buckets'] as $bucket) { + foreach ($bucket['top_tag_hits']['hits']['hits'] as $doc) { + $this->assertArrayHasKey('title', $doc['_source']); + $this->assertArrayNotHasKey('tags', $doc['_source']); + $this->assertArrayNotHasKey('last_activity_date', $doc['_source']); + } + } + } + + /** + * @group functional + */ + public function testAggregateWithVersion() + { + $aggr = new TopHits('top_tag_hits'); + $aggr->setVersion(true); + + $resultDocs = array(); + $outerAggrResult = $this->getOuterAggregationResult($aggr); + foreach ($outerAggrResult['buckets'] as $bucket) { + foreach ($bucket['top_tag_hits']['hits']['hits'] as $doc) { + $this->assertArrayHasKey('_version', $doc); + } + } + } + + /** + * @group functional + */ + public function testAggregateWithExplain() + { + $aggr = new TopHits('top_tag_hits'); + $aggr->setExplain(true); + + $resultDocs = array(); + $outerAggrResult = $this->getOuterAggregationResult($aggr); + foreach ($outerAggrResult['buckets'] as $bucket) { + foreach ($bucket['top_tag_hits']['hits']['hits'] as $doc) { + $this->assertArrayHasKey('_explanation', $doc); + } + } + } + + /** + * @group functional + */ + public function testAggregateWithScriptFields() + { + $aggr = new TopHits('top_tag_hits'); + $aggr->setSize(1); + $aggr->setScriptFields(array('three' => new Script('1 + 2'))); + $aggr->addScriptField('five', new Script('3 + 2')); + + $resultDocs = array(); + $outerAggrResult = $this->getOuterAggregationResult($aggr); + foreach ($outerAggrResult['buckets'] as $bucket) { + foreach ($bucket['top_tag_hits']['hits']['hits'] as $doc) { + $this->assertEquals(3, $doc['fields']['three'][0]); + $this->assertEquals(5, $doc['fields']['five'][0]); + } + } + } + + /** + * @group functional + */ + public function testAggregateWithHighlight() + { + $queryString = new SimpleQueryString('linux', array('title')); + + $aggr = new TopHits('top_tag_hits'); + $aggr->setHighlight(array('fields' => array('title' => new \stdClass()))); + + $query = new Query($queryString); + $query->addAggregation($aggr); + + $resultSet = $this->_getIndexForTest()->search($query); + $aggrResult = $resultSet->getAggregation('top_tag_hits'); + + foreach ($aggrResult['hits']['hits'] as $doc) { + $this->assertArrayHasKey('highlight', $doc); + $this->assertRegExp('#<em>linux</em>#', $doc['highlight']['title'][0]); + } + } + + /** + * @group functional + */ + public function testAggregateWithFieldData() + { + $aggr = new TopHits('top_tag_hits'); + $aggr->setFieldDataFields(array('title')); + + $query = new Query(new MatchAll()); + $query->addAggregation($aggr); + + $resultSet = $this->_getIndexForTest()->search($query); + $aggrResult = $resultSet->getAggregation('top_tag_hits'); + + foreach ($aggrResult['hits']['hits'] as $doc) { + $this->assertArrayHasKey('fields', $doc); + $this->assertArrayHasKey('title', $doc['fields']); + $this->assertArrayNotHasKey('tags', $doc['fields']); + $this->assertArrayNotHasKey('last_activity_date', $doc['fields']); + } + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/ValueCountTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/ValueCountTest.php new file mode 100644 index 00000000..21b63cbe --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Aggregation/ValueCountTest.php @@ -0,0 +1,40 @@ +<?php +namespace Elastica\Test\Aggregation; + +use Elastica\Aggregation\ValueCount; +use Elastica\Document; +use Elastica\Query; + +class ValueCountTest extends BaseAggregationTest +{ + protected function _getIndexForTest() + { + $index = $this->_createIndex(); + + $index->getType('test')->addDocuments(array( + new Document(1, array('price' => 5)), + new Document(2, array('price' => 8)), + new Document(3, array('price' => 1)), + new Document(4, array('price' => 3)), + new Document(5, array('price' => 3)), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group functional + */ + public function testValueCountAggregation() + { + $agg = new ValueCount('count', 'price'); + + $query = new Query(); + $query->addAggregation($agg); + $results = $this->_getIndexForTest()->search($query)->getAggregation('count'); + + $this->assertEquals(5, $results['value']); + } +} |