Skip to content

Commit 7232cd6

Browse files
p365labsruflin
authored andcommitted
update Term Suggester, test multiple completion suggestions (#1543)
1 parent fe1a1b6 commit 7232cd6

File tree

4 files changed

+124
-1
lines changed

4 files changed

+124
-1
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ All notable changes to this project will be documented in this file based on the
3737
* Added a transport class for mocking a HTTP 403 error codes, useful for testing response failures in inheriting clients
3838
* [Field](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-random) param for `Elastica\Query\FunctionScore::addRandomScoreFunction`
3939
* [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)
40-
* add parent_id (reference [#1518](https://github.com/ruflin/Elastica/issues/1518)) in QueryBuilder. [#1533]([#1518](https://github.com/ruflin/Elastica/issues/1533))
40+
* add parent_id (reference [#1518](https://github.com/ruflin/Elastica/issues/1518)) in QueryBuilder. [#1533]([#1518](https://github.com/ruflin/Elastica/issues/1533))
41+
* implemented ```string_distance``` option in Term Suggestion [#1543](https://github.com/ruflin/Elastica/pull/1543)
4142

4243
### Improvements
4344

lib/Elastica/Suggest/Term.php

+19
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,23 @@ public function setMaxTermFrequency($max)
126126
{
127127
return $this->setParam('max_term_freq', $max);
128128
}
129+
130+
/**
131+
* Which string distance implementation to use for comparing how similar suggested terms are.
132+
* Five possible values can be specified:
133+
*
134+
* - internal
135+
* - damerau_levenshtein
136+
* - levenshtein
137+
* - jaro_winkler
138+
* - ngram
139+
*
140+
* @param string $distanceAlgrorithm
141+
*
142+
* @return $this
143+
*/
144+
public function setStringDistanceAlgorithm($distanceAlgorithm)
145+
{
146+
return $this->setParam('string_distance', $distanceAlgorithm);
147+
}
129148
}

test/Elastica/Suggest/CompletionTest.php

+61
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Elastica\Document;
55
use Elastica\Index;
66
use Elastica\Query;
7+
use Elastica\Suggest;
78
use Elastica\Suggest\Completion;
89
use Elastica\Test\Base as BaseTest;
910

@@ -21,6 +22,9 @@ protected function _getIndexForTest()
2122
'fieldName' => [
2223
'type' => 'completion',
2324
],
25+
'fieldName2' => [
26+
'type' => 'completion',
27+
],
2428
]);
2529

2630
$type->addDocuments([
@@ -42,6 +46,19 @@ protected function _getIndexForTest()
4246
'weight' => 7,
4347
],
4448
]),
49+
new Document(4, [
50+
'fieldName2' => [
51+
'input' => ['Bleach', 'Nirvana'],
52+
'weight' => 3,
53+
],
54+
]),
55+
new Document(5, [
56+
'fieldName2' => [
57+
'input' => ['Incesticide', 'Nirvana'],
58+
'weight' => 3,
59+
],
60+
]),
61+
4562
]);
4663

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

128+
/**
129+
* @group functional
130+
*/
131+
public function testCompletion()
132+
{
133+
$suggest = new Completion('suggestName1', 'fieldName');
134+
$suggest->setPrefix('Neavermint');
135+
136+
$suggest2 = new Completion('suggestName2', 'fieldName2');
137+
$suggest2->setPrefix('Neverdint');
138+
139+
140+
$sug = new Suggest();
141+
$sug->addSuggestion($suggest);
142+
$sug->addSuggestion($suggest2);
143+
$index = $this->_getIndexForTest();
144+
$query = Query::create($sug);
145+
146+
$expectedSuggestions = [
147+
'suggestName1' => [
148+
0 => [
149+
'text' => 'Neavermint',
150+
'offset' => 0,
151+
'length' => 10,
152+
'options' => []
153+
]
154+
],
155+
'suggestName2' => [
156+
0 => [
157+
'text' => 'Neverdint',
158+
'offset' => 0,
159+
'length' => 9,
160+
'options' => []
161+
]
162+
]
163+
];
164+
165+
166+
$resultSet = $index->search($query);
167+
168+
$this->assertTrue($resultSet->hasSuggests());
169+
$this->assertEquals($expectedSuggestions, $resultSet->getSuggests());
170+
}
171+
111172
/**
112173
* @group unit
113174
*/

test/Elastica/Suggest/TermTest.php

+42
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,47 @@ public function testToArray()
6767
$this->assertEquals($expected, $suggest->toArray());
6868
}
6969

70+
/**
71+
* @group unit
72+
*/
73+
public function testDistanceAlgorithm()
74+
{
75+
$suggest = new Suggest();
76+
77+
$suggest1 = new Term('suggest1', '_all');
78+
$suggest1->setSort(Term::SORT_FREQUENCY);
79+
80+
$suggest->addSuggestion($suggest1->setText('Foor'));
81+
82+
$suggest2 = new Term('suggest2', '_all');
83+
$suggest2->setSuggestMode(Term::SUGGEST_MODE_POPULAR);
84+
$suggest2->setStringDistanceAlgorithm('jaro_winkler');
85+
$suggest->addSuggestion($suggest2->setText('Girhub'));
86+
87+
$expected = [
88+
'suggest' => [
89+
'suggest1' => [
90+
'term' => [
91+
'field' => '_all',
92+
'sort' => 'frequency',
93+
],
94+
'text' => 'Foor',
95+
],
96+
'suggest2' => [
97+
'term' => [
98+
'field' => '_all',
99+
'suggest_mode' => 'popular',
100+
'string_distance' => 'jaro_winkler'
101+
],
102+
'text' => 'Girhub',
103+
],
104+
],
105+
];
106+
107+
$this->assertEquals($expected, $suggest->toArray());
108+
109+
}
110+
70111
/**
71112
* @group functional
72113
*/
@@ -77,6 +118,7 @@ public function testSuggestResults()
77118
$suggest->addSuggestion($suggest1->setText('Foor seach'));
78119
$suggest2 = new Term('suggest2', 'text');
79120
$suggest->addSuggestion($suggest2->setText('Girhub'));
121+
$suggest2->setStringDistanceAlgorithm('jaro_winkler');
80122

81123
$index = $this->_getIndexForTest();
82124
$result = $index->search($suggest);

0 commit comments

Comments
 (0)