1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Elastica\Test\Suggest; |
4
|
|
|
|
5
|
|
|
use Elastica\Document; |
6
|
|
|
use Elastica\Index; |
7
|
|
|
use Elastica\Suggest; |
8
|
|
|
use Elastica\Suggest\CandidateGenerator\DirectGenerator; |
9
|
|
|
use Elastica\Suggest\Phrase; |
10
|
|
|
use Elastica\Test\Base as BaseTest; |
11
|
|
|
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @internal |
15
|
|
|
*/ |
16
|
|
|
class PhraseTest extends BaseTest |
17
|
|
|
{ |
18
|
|
|
use ExpectDeprecationTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @group unit |
22
|
|
|
*/ |
23
|
|
|
public function testToArray(): void |
24
|
|
|
{ |
25
|
|
|
$suggest = new Suggest(); |
26
|
|
|
$phraseSuggest = new Phrase('suggest1', 'text'); |
27
|
|
|
$phraseSuggest->setText('elasticsearch is bansai coor'); |
28
|
|
|
$phraseSuggest->setAnalyzer('simple'); |
29
|
|
|
$suggest->addSuggestion($phraseSuggest); |
30
|
|
|
$suggest->setGlobalText('global!'); |
31
|
|
|
|
32
|
|
|
$expected = [ |
33
|
|
|
'suggest' => [ |
34
|
|
|
'text' => 'global!', |
35
|
|
|
'suggest1' => [ |
36
|
|
|
'text' => 'elasticsearch is bansai coor', |
37
|
|
|
'phrase' => [ |
38
|
|
|
'field' => 'text', |
39
|
|
|
'analyzer' => 'simple', |
40
|
|
|
], |
41
|
|
|
], |
42
|
|
|
], |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
$this->assertEquals($expected, $suggest->toArray()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @group functional |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
public function testPhraseSuggest(): void |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$suggest = new Suggest(); |
54
|
|
|
$phraseSuggest = new Phrase('suggest1', 'text'); |
55
|
|
|
$phraseSuggest->setText('elasticsearch is bansai coor'); |
56
|
|
|
$phraseSuggest->setAnalyzer('simple')->setHighlight('<suggest>', '</suggest>')->setStupidBackoffSmoothing(0.4); |
57
|
|
|
$phraseSuggest->addDirectGenerator(new DirectGenerator('text')); |
58
|
|
|
$suggest->addSuggestion($phraseSuggest); |
59
|
|
|
|
60
|
|
|
$index = $this->_getIndexForTest(); |
61
|
|
|
$result = $index->search($suggest); |
|
|
|
|
62
|
|
|
$suggests = $result->getSuggests(); |
63
|
|
|
|
64
|
|
|
// 3 suggestions should be returned: One in which both misspellings are corrected, and two in which only one misspelling is corrected. |
65
|
|
|
$this->assertCount(3, $suggests['suggest1'][0]['options']); |
66
|
|
|
|
67
|
|
|
$this->assertEquals('elasticsearch is <suggest>bonsai cool</suggest>', $suggests['suggest1'][0]['options'][0]['highlighted']); |
68
|
|
|
$this->assertEquals('elasticsearch is bonsai cool', $suggests['suggest1'][0]['options'][0]['text']); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @group functional |
73
|
|
|
* |
74
|
|
|
* @group legacy |
75
|
|
|
*/ |
76
|
|
View Code Duplication |
public function testPhraseSuggestWithBackwardsCompatibility(): void |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$suggest = new Suggest(); |
79
|
|
|
$phraseSuggest = new Phrase('suggest1', 'text'); |
80
|
|
|
$phraseSuggest->setText('elasticsearch is bansai coor'); |
81
|
|
|
$phraseSuggest->setAnalyzer('simple')->setHighlight('<suggest>', '</suggest>')->setStupidBackoffSmoothing(0.4); |
82
|
|
|
|
83
|
|
|
$this->expectDeprecation('Since ruflin/elastica 7.2.0: The "Elastica\Suggest\Phrase::addCandidateGenerator()" method is deprecated, use the "addDirectGenerator()" method instead. It will be removed in 8.0.'); |
84
|
|
|
$phraseSuggest->addCandidateGenerator(new DirectGenerator('text')); |
|
|
|
|
85
|
|
|
$suggest->addSuggestion($phraseSuggest); |
86
|
|
|
|
87
|
|
|
$index = $this->_getIndexForTest(); |
88
|
|
|
$result = $index->search($suggest); |
|
|
|
|
89
|
|
|
$suggests = $result->getSuggests(); |
90
|
|
|
|
91
|
|
|
// 3 suggestions should be returned: One in which both misspellings are corrected, and two in which only one misspelling is corrected. |
92
|
|
|
$this->assertCount(3, $suggests['suggest1'][0]['options']); |
93
|
|
|
|
94
|
|
|
$this->assertEquals('elasticsearch is <suggest>bonsai cool</suggest>', $suggests['suggest1'][0]['options'][0]['highlighted']); |
95
|
|
|
$this->assertEquals('elasticsearch is bonsai cool', $suggests['suggest1'][0]['options'][0]['text']); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
protected function _getIndexForTest(): Index |
99
|
|
|
{ |
100
|
|
|
$index = $this->_createIndex(); |
101
|
|
|
$index->addDocuments([ |
102
|
|
|
new Document(1, ['text' => 'Github is pretty cool']), |
103
|
|
|
new Document(2, ['text' => 'Elasticsearch is bonsai cool']), |
104
|
|
|
new Document(3, ['text' => 'This is a test phrase']), |
105
|
|
|
new Document(4, ['text' => 'Another sentence for testing']), |
106
|
|
|
new Document(5, ['text' => 'Some more words here']), |
107
|
|
|
]); |
108
|
|
|
$index->refresh(); |
109
|
|
|
|
110
|
|
|
return $index; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.