|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Elastica\Test\Query; |
| 4 | + |
| 5 | +use Elastica\Document; |
| 6 | +use Elastica\Query\Boosting; |
| 7 | +use Elastica\Test\Base as BaseTest; |
| 8 | + |
| 9 | +class BoostingTest extends BaseTest |
| 10 | +{ |
| 11 | + /** |
| 12 | + * @var \Elastica\Index |
| 13 | + */ |
| 14 | + protected $index; |
| 15 | + |
| 16 | + /** |
| 17 | + * @var \Elastica\Type |
| 18 | + */ |
| 19 | + protected $type; |
| 20 | + |
| 21 | + /* |
| 22 | + * @var array |
| 23 | + */ |
| 24 | + protected $sampleData; |
| 25 | + |
| 26 | + protected function setUp() |
| 27 | + { |
| 28 | + parent::setUp(); |
| 29 | + $this->index = $this->_createIndex('test_boostingquery'); |
| 30 | + $this->type = $this->index->getType('test'); |
| 31 | + $this->type->setMapping(array( |
| 32 | + 'name' => array('type' => 'string', 'index' => 'analyzed'), |
| 33 | + 'price' => array('type' => 'float') |
| 34 | + )); |
| 35 | + |
| 36 | + $this->sampleData = array( |
| 37 | + array("name" => "Vital Lama", "price" => 5.2), |
| 38 | + array("name" => "Vital Match", "price" => 2.1), |
| 39 | + array("name" => "Mercury Vital", "price" => 7.5), |
| 40 | + array("name" => "Fist Mercury", "price" => 3.8), |
| 41 | + array("name" => "Lama Vital 2nd", "price" => 3.2) |
| 42 | + ); |
| 43 | + |
| 44 | + foreach($this->sampleData as $key => $value) { |
| 45 | + $this->type->addDocument(new Document($key, $value)); |
| 46 | + } |
| 47 | + |
| 48 | + $this->index->refresh(); |
| 49 | + } |
| 50 | + |
| 51 | + protected function tearDown() |
| 52 | + { |
| 53 | + $this->index->delete(); |
| 54 | + parent::tearDown(); |
| 55 | + } |
| 56 | + |
| 57 | + public function testToArray() |
| 58 | + { |
| 59 | + $keyword = "vital"; |
| 60 | + $negativeKeyword = "Mercury"; |
| 61 | + |
| 62 | + $query = new Boosting(); |
| 63 | + $positiveQuery = new \Elastica\Query\Term(array('name' => $keyword)); |
| 64 | + $negativeQuery = new \Elastica\Query\Term(array('name' => $negativeKeyword)); |
| 65 | + $query->setPositiveQuery($positiveQuery); |
| 66 | + $query->setNegativeQuery($negativeQuery); |
| 67 | + $query->setNegativeBoost(0.3); |
| 68 | + |
| 69 | + $expected = array( |
| 70 | + 'boosting' => array( |
| 71 | + 'positive' => $positiveQuery->toArray(), |
| 72 | + 'negative' => $negativeQuery->toArray(), |
| 73 | + 'negative_boost' => 0.3 |
| 74 | + ) |
| 75 | + ); |
| 76 | + $this->assertEquals($expected, $query->toArray()); |
| 77 | + } |
| 78 | + |
| 79 | + public function testNegativeBoost() |
| 80 | + { |
| 81 | + $keyword = "vital"; |
| 82 | + $negativeKeyword = "mercury"; |
| 83 | + |
| 84 | + $query = new Boosting(); |
| 85 | + $positiveQuery = new \Elastica\Query\Term(array('name' => $keyword)); |
| 86 | + $negativeQuery = new \Elastica\Query\Term(array('name' => $negativeKeyword)); |
| 87 | + $query->setPositiveQuery($positiveQuery); |
| 88 | + $query->setNegativeQuery($negativeQuery); |
| 89 | + $query->setNegativeBoost(0.2); |
| 90 | + |
| 91 | + $response = $this->type->search($query); |
| 92 | + $results = $response->getResults(); |
| 93 | + |
| 94 | + $this->assertEquals($response->getTotalHits(), 4); |
| 95 | + |
| 96 | + $lastResult = $results[3]->getData(); |
| 97 | + $this->assertEquals($lastResult['name'], $this->sampleData[2]['name']); |
| 98 | + } |
| 99 | +} |
0 commit comments