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/Query/FunctionScoreTest.php | |
parent | 9e06a62f265e3a2aaabecc598d4bc617e06fa32d (diff) |
Update to MediaWiki 1.26.0
Diffstat (limited to 'vendor/ruflin/elastica/test/lib/Elastica/Test/Query/FunctionScoreTest.php')
-rw-r--r-- | vendor/ruflin/elastica/test/lib/Elastica/Test/Query/FunctionScoreTest.php | 324 |
1 files changed, 324 insertions, 0 deletions
diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/FunctionScoreTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/FunctionScoreTest.php new file mode 100644 index 00000000..25600034 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/FunctionScoreTest.php @@ -0,0 +1,324 @@ +<?php +namespace Elastica\Test\Query; + +use Elastica\Document; +use Elastica\Filter\Term; +use Elastica\Query\FunctionScore; +use Elastica\Query\MatchAll; +use Elastica\Script; +use Elastica\Test\Base as BaseTest; + +class FunctionScoreTest extends BaseTest +{ + protected $locationOrigin = '32.804654, -117.242594'; + + protected function _getIndexForTest() + { + $index = $this->_createIndex(); + $type = $index->getType('test'); + + $type->setMapping(array( + 'name' => array('type' => 'string', 'index' => 'not_analyzed'), + 'location' => array('type' => 'geo_point'), + 'price' => array('type' => 'float'), + )); + + $type->addDocuments(array( + new Document(1, array( + 'name' => "Mr. Frostie's", + 'location' => array('lat' => 32.799605, 'lon' => -117.243027), + 'price' => 4.5, + )), + new Document(2, array( + 'name' => "Miller's Field", + 'location' => array('lat' => 32.795964, 'lon' => -117.255028), + 'price' => 9.5, + )), + )); + + $index->refresh(); + + return $index; + } + + /** + * @group unit + */ + public function testToArray() + { + $priceOrigin = 0; + $locationScale = '2mi'; + $priceScale = 9.25; + $query = new FunctionScore(); + $childQuery = new MatchAll(); + $query->setQuery($childQuery); + $query->addDecayFunction(FunctionScore::DECAY_GAUSS, 'location', $this->locationOrigin, $locationScale); + $query->addDecayFunction(FunctionScore::DECAY_GAUSS, 'price', $priceOrigin, $priceScale); + $expected = array( + 'function_score' => array( + 'query' => $childQuery->toArray(), + 'functions' => array( + array( + 'gauss' => array( + 'location' => array( + 'origin' => $this->locationOrigin, + 'scale' => $locationScale, + ), + ), + ), + array( + 'gauss' => array( + 'price' => array( + 'origin' => $priceOrigin, + 'scale' => $priceScale, + ), + ), + ), + ), + ), + ); + $this->assertEquals($expected, $query->toArray()); + } + + /** + * @group unit + */ + public function testDecayWeight() + { + $priceOrigin = 0; + $locationScale = '2mi'; + $priceScale = 9.25; + $query = new FunctionScore(); + $childQuery = new MatchAll(); + $query->setQuery($childQuery); + $query->addDecayFunction( + FunctionScore::DECAY_GAUSS, + 'location', + $this->locationOrigin, + $locationScale, + null, + null, + .5 + ); + $query->addDecayFunction(FunctionScore::DECAY_GAUSS, 'price', $priceOrigin, $priceScale, null, null, 2); + $expected = array( + 'function_score' => array( + 'query' => $childQuery->toArray(), + 'functions' => array( + array( + 'gauss' => array( + 'location' => array( + 'origin' => $this->locationOrigin, + 'scale' => $locationScale, + ), + ), + 'weight' => .5, + ), + array( + 'gauss' => array( + 'price' => array( + 'origin' => $priceOrigin, + 'scale' => $priceScale, + ), + ), + 'weight' => 2, + ), + ), + ), + ); + $this->assertEquals($expected, $query->toArray()); + } + + /** + * @group functional + */ + public function testGauss() + { + $query = new FunctionScore(); + $query->addDecayFunction(FunctionScore::DECAY_GAUSS, 'location', $this->locationOrigin, '4mi'); + $query->addDecayFunction(FunctionScore::DECAY_GAUSS, 'price', 0, 10); + $response = $this->_getIndexForTest()->search($query); + $results = $response->getResults(); + + // the document with the closest location and lowest price should be scored highest + $result0 = $results[0]->getData(); + $this->assertEquals("Mr. Frostie's", $result0['name']); + } + + /** + * @group functional + */ + public function testWeight() + { + $filter = new Term(array('price' => 4.5)); + $query = new FunctionScore(); + $query->addBoostFactorFunction(5.0, $filter); + $expected = array( + 'function_score' => array( + 'functions' => array( + array( + 'weight' => 5.0, + 'filter' => array( + 'term' => array( + 'price' => 4.5, + ), + ), + ), + ), + ), + ); + + $this->assertEquals($expected, $query->toArray()); + + $response = $this->_getIndexForTest()->search($query); + $results = $response->getResults(); + + // the document with price = 4.5 should be scored highest + $result0 = $results[0]->getData(); + $this->assertEquals("Mr. Frostie's", $result0['name']); + } + + /** + * @group functional + */ + public function testRandomScore() + { + $filter = new Term(array('price' => 4.5)); + $query = new FunctionScore(); + $query->addRandomScoreFunction(2, $filter); + $expected = array( + 'function_score' => array( + 'functions' => array( + array( + 'random_score' => array( + 'seed' => 2, + ), + 'filter' => array( + 'term' => array( + 'price' => 4.5, + ), + ), + ), + ), + ), + ); + + $this->assertEquals($expected, $query->toArray()); + + $response = $this->_getIndexForTest()->search($query); + $results = $response->getResults(); + + // the document with the random score should have a score > 1, means it is the first result + $result0 = $results[0]->getData(); + + $this->assertEquals("Miller's Field", $result0['name']); + } + + /** + * @group unit + */ + public function testRandomScoreWeight() + { + $filter = new Term(array('price' => 4.5)); + $query = new FunctionScore(); + $query->addRandomScoreFunction(2, $filter, 2); + $expected = array( + 'function_score' => array( + 'functions' => array( + array( + 'random_score' => array( + 'seed' => 2, + ), + 'filter' => array( + 'term' => array( + 'price' => 4.5, + ), + ), + 'weight' => 2, + ), + ), + ), + ); + + $this->assertEquals($expected, $query->toArray()); + } + + /** + * @group functional + */ + public function testRandomScoreWithoutSeed() + { + $query = new FunctionScore(); + $query->setRandomScore(); + + $response = $this->_getIndexForTest()->search($query); + + $this->assertEquals(2, $response->count()); + } + + /** + * @group functional + */ + public function testScriptScore() + { + $scriptString = "_score * doc['price'].value"; + $script = new Script($scriptString); + $query = new FunctionScore(); + $query->addScriptScoreFunction($script); + $expected = array( + 'function_score' => array( + 'functions' => array( + array( + 'script_score' => array( + 'script' => $scriptString, + ), + ), + ), + ), + ); + + $this->assertEquals($expected, $query->toArray()); + + $response = $this->_getIndexForTest()->search($query); + $results = $response->getResults(); + + // the document the highest price should be scored highest + $result0 = $results[0]->getData(); + $this->assertEquals("Miller's Field", $result0['name']); + } + + /** + * @group functional + */ + public function testSetMinScore() + { + $expected = array( + 'function_score' => array( + 'min_score' => 0.8, + 'functions' => array( + array( + 'gauss' => array( + 'price' => array( + 'origin' => 0, + 'scale' => 10, + ), + ), + ), + ), + ), + ); + + $query = new FunctionScore(); + $query->addDecayFunction(FunctionScore::DECAY_GAUSS, 'price', 0, 10); + $returnedValue = $query->setMinScore(0.8); + + $this->assertEquals($expected, $query->toArray()); + $this->assertInstanceOf('Elastica\Query\FunctionScore', $returnedValue); + + $response = $this->_getIndexForTest()->search($query); + $results = $response->getResults(); + + $this->assertCount(1, $results); + $this->assertEquals(1, $results[0]->getId()); + } +} |