-
Notifications
You must be signed in to change notification settings - Fork 729
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
12f7fdd
commit fa22d84
Showing
5 changed files
with
162 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Elastica\Suggest; | ||
|
||
/** | ||
* Comletion suggester | ||
* | ||
* @author Igor Denisenko <[email protected]> | ||
* @link http://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-completion.html | ||
*/ | ||
class Completion extends AbstractSuggest | ||
{ | ||
/** | ||
* Set fuzzy parameter | ||
* | ||
* @link http://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-completion.html#fuzzy | ||
* | ||
* @param array $fuzzy | ||
* @return $this | ||
*/ | ||
public function setFuzzy(array $fuzzy) | ||
{ | ||
return $this->setParam('fuzzy', $fuzzy); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?php | ||
|
||
namespace Elastica\Test\Suggest; | ||
|
||
use Elastica\Document; | ||
use Elastica\Index; | ||
use Elastica\Query; | ||
use Elastica\Suggest\Completion; | ||
use Elastica\Test\Base as BaseTest; | ||
|
||
class CompletionTest extends BaseTest | ||
{ | ||
/** | ||
* @var Index | ||
*/ | ||
protected $_index; | ||
|
||
protected function setUp() | ||
{ | ||
$this->_index = $this->_createIndex(); | ||
$type = $this->_index->getType('song'); | ||
|
||
$type->setMapping(array( | ||
'fieldName' => array( | ||
'type' => 'completion', | ||
'payloads' => true, | ||
), | ||
)); | ||
|
||
$type->addDocuments(array( | ||
new Document(1, array( | ||
'fieldName' => array( | ||
'input' => array('Nevermind', 'Nirvana'), | ||
'output' => 'Nevermind - Nirvana', | ||
'payload' => array( | ||
'year' => 1991, | ||
), | ||
), | ||
)), | ||
new Document(2, array( | ||
'fieldName' => array( | ||
'input' => array('Bleach', 'Nirvana'), | ||
'output' => 'Bleach - Nirvana', | ||
'payload' => array( | ||
'year' => 1989, | ||
), | ||
), | ||
)), | ||
new Document(3, array( | ||
'fieldName' => array( | ||
'input' => array('Incesticide', 'Nirvana'), | ||
'output' => 'Incesticide - Nirvana', | ||
'payload' => array( | ||
'year' => 1992, | ||
), | ||
), | ||
)), | ||
)); | ||
|
||
$this->_index->refresh(); | ||
} | ||
|
||
public function testToArray() | ||
{ | ||
$suggest = new Completion('suggestName', 'fieldName'); | ||
$suggest->setText('foo'); | ||
$suggest->setSize(10); | ||
$expected = array( | ||
'text' => 'foo', | ||
'completion' => array( | ||
'size' => 10, | ||
'field' => 'fieldName', | ||
), | ||
); | ||
$this->assertEquals($expected, $suggest->toArray()); | ||
} | ||
|
||
public function testSuggestWorks() | ||
{ | ||
$suggest = new Completion('suggestName', 'fieldName'); | ||
$suggest->setText('Never'); | ||
|
||
$resultSet = $this->_index->search(Query::create($suggest)); | ||
|
||
$this->assertTrue($resultSet->hasSuggests()); | ||
|
||
$suggests = $resultSet->getSuggests(); | ||
$options = $suggests['suggestName'][0]['options']; | ||
|
||
$this->assertCount(1, $options); | ||
$this->assertEquals('Nevermind - Nirvana', $options[0]['text']); | ||
$this->assertEquals(1991, $options[0]['payload']['year']); | ||
} | ||
|
||
public function testFuzzySuggestWorks() | ||
{ | ||
$suggest = new Completion('suggestName', 'fieldName'); | ||
$suggest->setFuzzy(array('fuzziness' => 2)); | ||
$suggest->setText('Neavermint'); | ||
|
||
$resultSet = $this->_index->search(Query::create($suggest)); | ||
|
||
$this->assertTrue($resultSet->hasSuggests()); | ||
|
||
$suggests = $resultSet->getSuggests(); | ||
$options = $suggests['suggestName'][0]['options']; | ||
|
||
$this->assertCount(1, $options); | ||
$this->assertEquals('Nevermind - Nirvana', $options[0]['text']); | ||
} | ||
|
||
public function testSetFuzzy() | ||
{ | ||
$suggest = new Completion('suggestName', 'fieldName'); | ||
|
||
$fuzzy = array( | ||
'unicode_aware' => true, | ||
'fuzziness' => 3, | ||
); | ||
|
||
$suggest->setFuzzy($fuzzy); | ||
|
||
$this->assertEquals($fuzzy, $suggest->getParam('fuzzy')); | ||
|
||
$this->assertInstanceOf('Elastica\\Suggest\\Completion', $suggest->setFuzzy($fuzzy)); | ||
} | ||
} |