Skip to content

Commit

Permalink
update Term Suggester, test multiple completion suggestions (#1543)
Browse files Browse the repository at this point in the history
  • Loading branch information
p365labs authored and ruflin committed Dec 3, 2018
1 parent fe1a1b6 commit 7232cd6
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ All notable changes to this project will be documented in this file based on the
* Added a transport class for mocking a HTTP 403 error codes, useful for testing response failures in inheriting clients
* [Field](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-random) param for `Elastica\Query\FunctionScore::addRandomScoreFunction`
* [Index Recovery](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-recovery.html) : the indices recovery API provides insight into on-going index shard recoveries. It was never been implemented into Elastica. [#1537](https://github.com/ruflin/Elastica/pull/1537)
* add parent_id (reference [#1518](https://github.com/ruflin/Elastica/issues/1518)) in QueryBuilder. [#1533]([#1518](https://github.com/ruflin/Elastica/issues/1533))
* add parent_id (reference [#1518](https://github.com/ruflin/Elastica/issues/1518)) in QueryBuilder. [#1533]([#1518](https://github.com/ruflin/Elastica/issues/1533))
* implemented ```string_distance``` option in Term Suggestion [#1543](https://github.com/ruflin/Elastica/pull/1543)

### Improvements

Expand Down
19 changes: 19 additions & 0 deletions lib/Elastica/Suggest/Term.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,23 @@ public function setMaxTermFrequency($max)
{
return $this->setParam('max_term_freq', $max);
}

/**
* Which string distance implementation to use for comparing how similar suggested terms are.
* Five possible values can be specified:
*
* - internal
* - damerau_levenshtein
* - levenshtein
* - jaro_winkler
* - ngram
*
* @param string $distanceAlgrorithm
*
* @return $this
*/
public function setStringDistanceAlgorithm($distanceAlgorithm)
{
return $this->setParam('string_distance', $distanceAlgorithm);
}
}
61 changes: 61 additions & 0 deletions test/Elastica/Suggest/CompletionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Elastica\Document;
use Elastica\Index;
use Elastica\Query;
use Elastica\Suggest;
use Elastica\Suggest\Completion;
use Elastica\Test\Base as BaseTest;

Expand All @@ -21,6 +22,9 @@ protected function _getIndexForTest()
'fieldName' => [
'type' => 'completion',
],
'fieldName2' => [
'type' => 'completion',
],
]);

$type->addDocuments([
Expand All @@ -42,6 +46,19 @@ protected function _getIndexForTest()
'weight' => 7,
],
]),
new Document(4, [
'fieldName2' => [
'input' => ['Bleach', 'Nirvana'],
'weight' => 3,
],
]),
new Document(5, [
'fieldName2' => [
'input' => ['Incesticide', 'Nirvana'],
'weight' => 3,
],
]),

]);

$index->refresh();
Expand Down Expand Up @@ -108,6 +125,50 @@ public function testFuzzySuggestWorks()
$this->assertEquals('Nevermind', $options[0]['text']);
}

/**
* @group functional
*/
public function testCompletion()
{
$suggest = new Completion('suggestName1', 'fieldName');
$suggest->setPrefix('Neavermint');

$suggest2 = new Completion('suggestName2', 'fieldName2');
$suggest2->setPrefix('Neverdint');


$sug = new Suggest();
$sug->addSuggestion($suggest);
$sug->addSuggestion($suggest2);
$index = $this->_getIndexForTest();
$query = Query::create($sug);

$expectedSuggestions = [
'suggestName1' => [
0 => [
'text' => 'Neavermint',
'offset' => 0,
'length' => 10,
'options' => []
]
],
'suggestName2' => [
0 => [
'text' => 'Neverdint',
'offset' => 0,
'length' => 9,
'options' => []
]
]
];


$resultSet = $index->search($query);

$this->assertTrue($resultSet->hasSuggests());
$this->assertEquals($expectedSuggestions, $resultSet->getSuggests());
}

/**
* @group unit
*/
Expand Down
42 changes: 42 additions & 0 deletions test/Elastica/Suggest/TermTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,47 @@ public function testToArray()
$this->assertEquals($expected, $suggest->toArray());
}

/**
* @group unit
*/
public function testDistanceAlgorithm()
{
$suggest = new Suggest();

$suggest1 = new Term('suggest1', '_all');
$suggest1->setSort(Term::SORT_FREQUENCY);

$suggest->addSuggestion($suggest1->setText('Foor'));

$suggest2 = new Term('suggest2', '_all');
$suggest2->setSuggestMode(Term::SUGGEST_MODE_POPULAR);
$suggest2->setStringDistanceAlgorithm('jaro_winkler');
$suggest->addSuggestion($suggest2->setText('Girhub'));

$expected = [
'suggest' => [
'suggest1' => [
'term' => [
'field' => '_all',
'sort' => 'frequency',
],
'text' => 'Foor',
],
'suggest2' => [
'term' => [
'field' => '_all',
'suggest_mode' => 'popular',
'string_distance' => 'jaro_winkler'
],
'text' => 'Girhub',
],
],
];

$this->assertEquals($expected, $suggest->toArray());

}

/**
* @group functional
*/
Expand All @@ -77,6 +118,7 @@ public function testSuggestResults()
$suggest->addSuggestion($suggest1->setText('Foor seach'));
$suggest2 = new Term('suggest2', 'text');
$suggest->addSuggestion($suggest2->setText('Girhub'));
$suggest2->setStringDistanceAlgorithm('jaro_winkler');

$index = $this->_getIndexForTest();
$result = $index->search($suggest);
Expand Down

0 comments on commit 7232cd6

Please sign in to comment.