|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Elastica\Test\Query; |
|
4
|
|
|
|
|
5
|
|
|
use Elastica\Document; |
|
6
|
|
|
use Elastica\Mapping; |
|
7
|
|
|
use Elastica\Query; |
|
8
|
|
|
use Elastica\Query\MultiMatch; |
|
9
|
|
|
use Elastica\Test\Base as BaseTest; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @internal |
|
13
|
|
|
*/ |
|
14
|
|
|
class MultiMatchTest extends BaseTest |
|
15
|
|
|
{ |
|
16
|
|
|
private static $data = [ |
|
|
|
|
|
|
17
|
|
|
['id' => 1, 'name' => 'Rodolfo', 'last_name' => 'Moraes', 'full_name' => 'Rodolfo Moraes'], |
|
18
|
|
|
['id' => 2, 'name' => 'Tristan', 'last_name' => 'Maindron', 'full_name' => 'Tristan Maindron'], |
|
19
|
|
|
['id' => 3, 'name' => 'Monique', 'last_name' => 'Maindron', 'full_name' => 'Monique Maindron'], |
|
20
|
|
|
['id' => 4, 'name' => 'John', 'last_name' => 'not Doe', 'full_name' => 'John not Doe'], |
|
21
|
|
|
]; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @group functional |
|
25
|
|
|
*/ |
|
26
|
|
|
public function testMinimumShouldMatch(): void |
|
27
|
|
|
{ |
|
28
|
|
|
$multiMatch = new MultiMatch(); |
|
29
|
|
|
$multiMatch->setQuery('Tristan Maindron'); |
|
30
|
|
|
$multiMatch->setFields(['full_name', 'name']); |
|
31
|
|
|
$multiMatch->setMinimumShouldMatch('2<100%'); |
|
32
|
|
|
$resultSet = $this->_getResults($multiMatch); |
|
33
|
|
|
|
|
34
|
|
|
$this->assertEquals(1, $resultSet->count()); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @group functional |
|
39
|
|
|
*/ |
|
40
|
|
|
public function testAndOperator(): void |
|
41
|
|
|
{ |
|
42
|
|
|
$multiMatch = new MultiMatch(); |
|
43
|
|
|
$multiMatch->setQuery('Monique Maindron'); |
|
44
|
|
|
$multiMatch->setFields(['full_name', 'name']); |
|
45
|
|
|
$multiMatch->setOperator(MultiMatch::OPERATOR_AND); |
|
46
|
|
|
$resultSet = $this->_getResults($multiMatch); |
|
47
|
|
|
|
|
48
|
|
|
$this->assertEquals(1, $resultSet->count()); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @group functional |
|
53
|
|
|
*/ |
|
54
|
|
|
public function testType(): void |
|
55
|
|
|
{ |
|
56
|
|
|
$multiMatch = new MultiMatch(); |
|
57
|
|
|
$multiMatch->setQuery('Trist'); |
|
58
|
|
|
$multiMatch->setFields(['full_name', 'name']); |
|
59
|
|
|
$multiMatch->setType(MultiMatch::TYPE_PHRASE_PREFIX); |
|
60
|
|
|
$resultSet = $this->_getResults($multiMatch); |
|
61
|
|
|
|
|
62
|
|
|
$this->assertEquals(1, $resultSet->count()); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @group functional |
|
67
|
|
|
*/ |
|
68
|
|
|
public function testFuzzy(): void |
|
69
|
|
|
{ |
|
70
|
|
|
$multiMatch = new MultiMatch(); |
|
71
|
|
|
$multiMatch->setQuery('Tritsan'); // Misspell on purpose |
|
72
|
|
|
$multiMatch->setFields(['full_name', 'name']); |
|
73
|
|
|
$multiMatch->setFuzziness(2); |
|
74
|
|
|
$resultSet = $this->_getResults($multiMatch); |
|
75
|
|
|
|
|
76
|
|
|
$this->assertEquals(1, $resultSet->count()); |
|
77
|
|
|
|
|
78
|
|
|
$multiMatch = new MultiMatch(); |
|
79
|
|
|
$multiMatch->setQuery('Tritsan'); // Misspell on purpose |
|
80
|
|
|
$multiMatch->setFields(['full_name', 'name']); |
|
81
|
|
|
$multiMatch->setFuzziness(0); |
|
82
|
|
|
$resultSet = $this->_getResults($multiMatch); |
|
83
|
|
|
|
|
84
|
|
|
$this->assertEquals(0, $resultSet->count()); |
|
85
|
|
|
|
|
86
|
|
|
$multiMatch = new MultiMatch(); |
|
87
|
|
|
$multiMatch->setQuery('Tritsan'); // Misspell on purpose |
|
88
|
|
|
$multiMatch->setFields(['full_name', 'name']); |
|
89
|
|
|
$multiMatch->setFuzziness(MultiMatch::FUZZINESS_AUTO); |
|
90
|
|
|
$resultSet = $this->_getResults($multiMatch); |
|
91
|
|
|
|
|
92
|
|
|
$this->assertEquals(1, $resultSet->count()); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @group functional |
|
97
|
|
|
*/ |
|
98
|
|
View Code Duplication |
public function testFuzzyWithOptions1(): void |
|
|
|
|
|
|
99
|
|
|
{ |
|
100
|
|
|
// Here Elasticsearch will not accept mispells |
|
101
|
|
|
// on the first 6 letters. |
|
102
|
|
|
$multiMatch = new MultiMatch(); |
|
103
|
|
|
$multiMatch->setQuery('Tritsan'); // Misspell on purpose |
|
104
|
|
|
$multiMatch->setFields(['full_name', 'name']); |
|
105
|
|
|
$multiMatch->setFuzziness(2); |
|
106
|
|
|
$multiMatch->setPrefixLength(6); |
|
107
|
|
|
$resultSet = $this->_getResults($multiMatch); |
|
108
|
|
|
|
|
109
|
|
|
$this->assertEquals(0, $resultSet->count()); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @group functional |
|
114
|
|
|
*/ |
|
115
|
|
View Code Duplication |
public function testFuzzyWithOptions2(): void |
|
|
|
|
|
|
116
|
|
|
{ |
|
117
|
|
|
// Here with a 'M' search we should hit 'Moraes' first |
|
118
|
|
|
// and then stop because MaxExpansion = 1. |
|
119
|
|
|
// If MaxExpansion was set to 2, we could hit "Maindron" too. |
|
120
|
|
|
$multiMatch = new MultiMatch(); |
|
121
|
|
|
$multiMatch->setQuery('M'); |
|
122
|
|
|
$multiMatch->setFields(['name']); |
|
123
|
|
|
$multiMatch->setType(MultiMatch::TYPE_PHRASE_PREFIX); |
|
124
|
|
|
$multiMatch->setPrefixLength(0); |
|
125
|
|
|
$multiMatch->setMaxExpansions(1); |
|
126
|
|
|
$resultSet = $this->_getResults($multiMatch); |
|
127
|
|
|
|
|
128
|
|
|
$this->assertEquals(1, $resultSet->count()); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @group functional |
|
133
|
|
|
*/ |
|
134
|
|
|
public function testZeroTerm(): void |
|
135
|
|
|
{ |
|
136
|
|
|
$multiMatch = new MultiMatch(); |
|
137
|
|
|
$multiMatch->setQuery('not'); // This is a stopword. |
|
138
|
|
|
$multiMatch->setFields(['full_name', 'last_name']); |
|
139
|
|
|
$multiMatch->setZeroTermsQuery(MultiMatch::ZERO_TERM_NONE); |
|
140
|
|
|
$multiMatch->setAnalyzer('stops'); |
|
141
|
|
|
$resultSet = $this->_getResults($multiMatch); |
|
142
|
|
|
|
|
143
|
|
|
$this->assertEquals(0, $resultSet->count()); |
|
144
|
|
|
|
|
145
|
|
|
$multiMatch->setZeroTermsQuery(MultiMatch::ZERO_TERM_ALL); |
|
146
|
|
|
$resultSet = $this->_getResults($multiMatch); |
|
147
|
|
|
|
|
148
|
|
|
$this->assertEquals(4, $resultSet->count()); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @group functional |
|
153
|
|
|
*/ |
|
154
|
|
|
public function testBaseMultiMatch(): void |
|
155
|
|
|
{ |
|
156
|
|
|
$multiMatch = new MultiMatch(); |
|
157
|
|
|
$multiMatch->setQuery('Rodolfo'); |
|
158
|
|
|
$multiMatch->setFields(['name', 'last_name']); |
|
159
|
|
|
$resultSet = $this->_getResults($multiMatch); |
|
160
|
|
|
|
|
161
|
|
|
$this->assertEquals(1, $resultSet->count()); |
|
162
|
|
|
|
|
163
|
|
|
$multiMatch = new MultiMatch(); |
|
164
|
|
|
$multiMatch->setQuery('Moraes'); |
|
165
|
|
|
$multiMatch->setFields(['name', 'last_name']); |
|
166
|
|
|
$resultSet = $this->_getResults($multiMatch); |
|
167
|
|
|
|
|
168
|
|
|
$this->assertEquals(1, $resultSet->count()); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Executes the query with the current multimatch. |
|
173
|
|
|
*/ |
|
174
|
|
|
private function _getResults(MultiMatch $multiMatch) |
|
175
|
|
|
{ |
|
176
|
|
|
return $this->_generateIndex()->search(new Query($multiMatch)); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Builds an index for testing. |
|
181
|
|
|
*/ |
|
182
|
|
|
private function _generateIndex() |
|
183
|
|
|
{ |
|
184
|
|
|
$client = $this->_getClient(); |
|
185
|
|
|
$index = $client->getIndex('test'); |
|
186
|
|
|
|
|
187
|
|
|
$index->create( |
|
188
|
|
|
[ |
|
189
|
|
|
'settings' => [ |
|
190
|
|
|
'analysis' => [ |
|
191
|
|
|
'analyzer' => [ |
|
192
|
|
|
'noStops' => [ |
|
193
|
|
|
'type' => 'standard', |
|
194
|
|
|
'stopwords' => '_none_', |
|
195
|
|
|
], |
|
196
|
|
|
'stops' => [ |
|
197
|
|
|
'type' => 'standard', |
|
198
|
|
|
'stopwords' => ['not'], |
|
199
|
|
|
], |
|
200
|
|
|
], |
|
201
|
|
|
], |
|
202
|
|
|
], |
|
203
|
|
|
], |
|
204
|
|
|
[ |
|
205
|
|
|
'recreate' => true, |
|
206
|
|
|
] |
|
207
|
|
|
); |
|
208
|
|
|
|
|
209
|
|
|
$mapping = new Mapping([ |
|
210
|
|
|
'name' => ['type' => 'text', 'analyzer' => 'noStops'], |
|
211
|
|
|
'last_name' => ['type' => 'text', 'analyzer' => 'noStops'], |
|
212
|
|
|
'full_name' => ['type' => 'text', 'analyzer' => 'noStops'], |
|
213
|
|
|
]); |
|
214
|
|
|
|
|
215
|
|
|
$index->setMapping($mapping); |
|
216
|
|
|
|
|
217
|
|
|
foreach (self::$data as $key => $docData) { |
|
218
|
|
|
$index->addDocument(new Document($key, $docData)); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
// Refresh index |
|
222
|
|
|
$index->refresh(); |
|
223
|
|
|
|
|
224
|
|
|
return $index; |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
|