Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add completion suggester #808

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changes.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
CHANGES

2015-03-29
- Added Elastica\Suggest\Completion

2015-03-12
- Fixed short match construction in query DSL #796

Expand Down
8 changes: 6 additions & 2 deletions lib/Elastica/QueryBuilder/DSL/Suggest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Elastica\Exception\NotImplementedException;
use Elastica\QueryBuilder\DSL;
use Elastica\Suggest\Completion;
use Elastica\Suggest\Phrase;
use Elastica\Suggest\Term;

Expand Down Expand Up @@ -56,10 +57,13 @@ public function phrase($name, $field)
* completion suggester
*
* @link http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-suggesters-completion.html
* @param string $name
* @param string $field
* @return Completion
*/
public function completion()
public function completion($name, $field)
{
throw new NotImplementedException();
return new Completion($name, $field);
}

/**
Expand Down
25 changes: 25 additions & 0 deletions lib/Elastica/Suggest/Completion.php
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);
}
}
2 changes: 1 addition & 1 deletion test/lib/Elastica/Test/QueryBuilder/DSL/SuggestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class SuggestTest extends \PHPUnit_Framework_TestCase
private $suggesters = array(
'term' => array('name', 'field'),
'phrase' => array('name', 'field'),
'completion' => array(),
'completion' => array('name', 'field'),
'context' => array(),
);

Expand Down
127 changes: 127 additions & 0 deletions test/lib/Elastica/Test/Suggest/CompletionTest.php
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));
}
}