diff options
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 | 270 |
1 files changed, 192 insertions, 78 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 index 47d9dd2c..25600034 100644 --- a/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/FunctionScoreTest.php +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/FunctionScoreTest.php @@ -1,70 +1,56 @@ <?php -/** - * User: Joe Linn - * Date: 9/16/13 - * Time: 5:05 PM - */ - 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 { - /** - * @var \Elastica\Index - */ - protected $index; - - /** - * @var \Elastica\Type - */ - protected $type; - - protected $locationOrigin = "32.804654, -117.242594"; + protected $locationOrigin = '32.804654, -117.242594'; - protected function setUp() + protected function _getIndexForTest() { - parent::setUp(); - $this->index = $this->_createIndex('test_functionscore'); - $this->type = $this->index->getType('test'); - $this->type->setMapping(array( + $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') + 'price' => array('type' => 'float'), )); - $this->type->addDocument(new Document(1, array( - 'name' => "Mr. Frostie's", - 'location' => array('lat' => 32.799605, 'lon' => -117.243027), - 'price' => 4.5 - ))); - $this->type->addDocument(new Document(2, array( - 'name' => "Miller's Field", - 'location' => array('lat' => 32.795964, 'lon' => -117.255028), - 'price' => 9.5 - ))); - - $this->index->refresh(); - } + $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, + )), + )); - protected function tearDown() - { - $this->index->delete(); - parent::tearDown(); + $index->refresh(); + + return $index; } + /** + * @group unit + */ public function testToArray() { $priceOrigin = 0; $locationScale = '2mi'; $priceScale = 9.25; $query = new FunctionScore(); - $childQuery = new \Elastica\Query\MatchAll(); + $childQuery = new MatchAll(); $query->setQuery($childQuery); $query->addDecayFunction(FunctionScore::DECAY_GAUSS, 'location', $this->locationOrigin, $locationScale); $query->addDecayFunction(FunctionScore::DECAY_GAUSS, 'price', $priceOrigin, $priceScale); @@ -76,30 +62,82 @@ class FunctionScoreTest extends BaseTest 'gauss' => array( 'location' => array( 'origin' => $this->locationOrigin, - 'scale' => $locationScale - ) - ) + 'scale' => $locationScale, + ), + ), ), array( 'gauss' => array( 'price' => array( 'origin' => $priceOrigin, - 'scale' => $priceScale - ) - ) - ) - ) - ) + '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, 'location', $this->locationOrigin, '4mi'); $query->addDecayFunction(FunctionScore::DECAY_GAUSS, 'price', 0, 10); - $response = $this->type->search($query); + $response = $this->_getIndexForTest()->search($query); $results = $response->getResults(); // the document with the closest location and lowest price should be scored highest @@ -107,7 +145,10 @@ class FunctionScoreTest extends BaseTest $this->assertEquals("Mr. Frostie's", $result0['name']); } - public function testBoostFactor() + /** + * @group functional + */ + public function testWeight() { $filter = new Term(array('price' => 4.5)); $query = new FunctionScore(); @@ -116,20 +157,20 @@ class FunctionScoreTest extends BaseTest 'function_score' => array( 'functions' => array( array( - 'boost_factor' => 5.0, + 'weight' => 5.0, 'filter' => array( 'term' => array( - 'price' => 4.5 - ) - ) - ) - ) - ) + 'price' => 4.5, + ), + ), + ), + ), + ), ); $this->assertEquals($expected, $query->toArray()); - $response = $this->type->search($query); + $response = $this->_getIndexForTest()->search($query); $results = $response->getResults(); // the document with price = 4.5 should be scored highest @@ -137,6 +178,9 @@ class FunctionScoreTest extends BaseTest $this->assertEquals("Mr. Frostie's", $result0['name']); } + /** + * @group functional + */ public function testRandomScore() { $filter = new Term(array('price' => 4.5)); @@ -147,39 +191,74 @@ class FunctionScoreTest extends BaseTest 'functions' => array( array( 'random_score' => array( - 'seed' => 2 + 'seed' => 2, ), 'filter' => array( 'term' => array( - 'price' => 4.5 - ) - ) - ) - ) - ) + 'price' => 4.5, + ), + ), + ), + ), + ), ); $this->assertEquals($expected, $query->toArray()); - $response = $this->type->search($query); + $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[1]->getData(); - + $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->type->search($query); + $response = $this->_getIndexForTest()->search($query); $this->assertEquals(2, $response->count()); } + /** + * @group functional + */ public function testScriptScore() { $scriptString = "_score * doc['price'].value"; @@ -191,20 +270,55 @@ class FunctionScoreTest extends BaseTest 'functions' => array( array( 'script_score' => array( - 'script' => $scriptString - ) - ) - ) - ) + 'script' => $scriptString, + ), + ), + ), + ), ); $this->assertEquals($expected, $query->toArray()); - $response = $this->type->search($query); + $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()); + } } |