|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Elastica\Test\Query; |
|
4
|
|
|
|
|
5
|
|
|
use Elastica\Document; |
|
6
|
|
|
use Elastica\Index; |
|
7
|
|
|
use Elastica\Mapping; |
|
8
|
|
|
use Elastica\Query; |
|
9
|
|
|
use Elastica\Query\BoolQuery; |
|
10
|
|
|
use Elastica\Query\MoreLikeThis; |
|
11
|
|
|
use Elastica\Query\Term; |
|
12
|
|
|
use Elastica\Test\Base as BaseTest; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @internal |
|
16
|
|
|
*/ |
|
17
|
|
|
class MoreLikeThisTest extends BaseTest |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @group functional |
|
21
|
|
|
*/ |
|
22
|
|
|
public function testSearch(): void |
|
23
|
|
|
{ |
|
24
|
|
|
$client = $this->_getClient(); |
|
25
|
|
|
$index = new Index($client, 'test'); |
|
26
|
|
|
$index->create([], [ |
|
27
|
|
|
'recreate' => true, |
|
28
|
|
|
]); |
|
29
|
|
|
$index->getSettings()->setNumberOfReplicas(0); |
|
30
|
|
|
|
|
31
|
|
|
$mapping = new Mapping([ |
|
32
|
|
|
'email' => ['store' => true, 'type' => 'text', 'index' => true], |
|
33
|
|
|
'content' => ['store' => true, 'type' => 'text', 'index' => true], |
|
34
|
|
|
]); |
|
35
|
|
|
|
|
36
|
|
|
$mapping->setSource(['enabled' => false]); |
|
37
|
|
|
$index->setMapping($mapping); |
|
38
|
|
|
|
|
39
|
|
|
$doc = new Document(1000, ['email' => '[email protected]', 'content' => 'This is a sample post. Hello World Fuzzy Like This!']); |
|
40
|
|
|
$index->addDocument($doc); |
|
41
|
|
|
|
|
42
|
|
|
$doc = new Document(1001, ['email' => '[email protected]', 'content' => 'This is a fake nospam email address for gmail']); |
|
43
|
|
|
$index->addDocument($doc); |
|
44
|
|
|
|
|
45
|
|
|
// Refresh index |
|
46
|
|
|
$index->refresh(); |
|
47
|
|
|
|
|
48
|
|
|
$mltQuery = new MoreLikeThis(); |
|
49
|
|
|
$mltQuery->setLike('fake gmail sample'); |
|
50
|
|
|
$mltQuery->setFields(['email', 'content']); |
|
51
|
|
|
$mltQuery->setMaxQueryTerms(3); |
|
52
|
|
|
$mltQuery->setMinDocFrequency(1); |
|
53
|
|
|
$mltQuery->setMinTermFrequency(1); |
|
54
|
|
|
|
|
55
|
|
|
$query = new Query(); |
|
56
|
|
|
$query->setQuery($mltQuery); |
|
57
|
|
|
|
|
58
|
|
|
$resultSet = $index->search($query); |
|
59
|
|
|
$resultSet->getResponse()->getData(); |
|
60
|
|
|
$this->assertEquals(2, $resultSet->count()); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @group functional |
|
65
|
|
|
*/ |
|
66
|
|
|
public function testSearchByDocument(): void |
|
67
|
|
|
{ |
|
68
|
|
|
$client = $this->_getClient(['persistent' => false]); |
|
69
|
|
|
$index = $client->getIndex('elastica_test'); |
|
70
|
|
|
$index->create( |
|
71
|
|
|
[ |
|
72
|
|
|
'settings' => [ |
|
73
|
|
|
'index' => [ |
|
74
|
|
|
'number_of_shards' => 1, |
|
75
|
|
|
'number_of_replicas' => 0, |
|
76
|
|
|
], |
|
77
|
|
|
], |
|
78
|
|
|
], |
|
79
|
|
|
[ |
|
80
|
|
|
'recreate' => true, |
|
81
|
|
|
] |
|
82
|
|
|
); |
|
83
|
|
|
|
|
84
|
|
|
$index->addDocuments([ |
|
85
|
|
|
new Document(1, ['visible' => true, 'name' => 'bruce wayne batman']), |
|
86
|
|
|
new Document(2, ['visible' => true, 'name' => 'bruce wayne']), |
|
87
|
|
|
new Document(3, ['visible' => false, 'name' => 'bruce wayne']), |
|
88
|
|
|
new Document(4, ['visible' => true, 'name' => 'batman']), |
|
89
|
|
|
new Document(5, ['visible' => false, 'name' => 'batman']), |
|
90
|
|
|
new Document(6, ['visible' => true, 'name' => 'superman']), |
|
91
|
|
|
new Document(7, ['visible' => true, 'name' => 'spiderman']), |
|
92
|
|
|
]); |
|
93
|
|
|
|
|
94
|
|
|
$index->refresh(); |
|
95
|
|
|
|
|
96
|
|
|
$doc = $index->getDocument(1); |
|
97
|
|
|
|
|
98
|
|
|
// Return all similar from id |
|
99
|
|
|
$mltQuery = new MoreLikeThis(); |
|
100
|
|
|
$mltQuery->setMinTermFrequency(1); |
|
101
|
|
|
$mltQuery->setMinDocFrequency(1); |
|
102
|
|
|
$mltQuery->setLike($doc); |
|
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
$this->assertEquals(4, $index->count($mltQuery)); |
|
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
$mltQuery = new MoreLikeThis(); |
|
107
|
|
|
$mltQuery->setMinTermFrequency(1); |
|
108
|
|
|
$mltQuery->setMinDocFrequency(1); |
|
109
|
|
|
$mltQuery->setLike($doc); |
|
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
$bool = new BoolQuery(); |
|
112
|
|
|
$bool->addMust($mltQuery); |
|
113
|
|
|
// Return just the visible similar from id |
|
114
|
|
|
$filterTerm = new Term(); |
|
115
|
|
|
$filterTerm->setTerm('visible', true); |
|
|
|
|
|
|
116
|
|
|
$bool->addFilter($filterTerm); |
|
117
|
|
|
|
|
118
|
|
|
$this->assertEquals(2, $index->count($bool)); |
|
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
// Return all similar from source |
|
121
|
|
|
$mltQuery = new MoreLikeThis(); |
|
122
|
|
|
$mltQuery->setMinTermFrequency(1); |
|
123
|
|
|
$mltQuery->setMinDocFrequency(1); |
|
124
|
|
|
$mltQuery->setMinimumShouldMatch('100%'); |
|
125
|
|
|
$mltQuery->setLike( |
|
126
|
|
|
$index->getDocument(1)->setId('') |
|
|
|
|
|
|
127
|
|
|
); |
|
128
|
|
|
|
|
129
|
|
|
$this->assertEquals(1, $index->count($mltQuery)); |
|
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @group unit |
|
134
|
|
|
*/ |
|
135
|
|
|
public function testSetFields(): void |
|
136
|
|
|
{ |
|
137
|
|
|
$query = new MoreLikeThis(); |
|
138
|
|
|
|
|
139
|
|
|
$fields = ['firstname', 'lastname']; |
|
140
|
|
|
$query->setFields($fields); |
|
141
|
|
|
|
|
142
|
|
|
$data = $query->toArray(); |
|
143
|
|
|
$this->assertEquals($fields, $data['more_like_this']['fields']); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @group unit |
|
148
|
|
|
*/ |
|
149
|
|
|
public function testSetLike(): void |
|
150
|
|
|
{ |
|
151
|
|
|
$query = new MoreLikeThis(); |
|
152
|
|
|
$query->setLike(' hello world'); |
|
153
|
|
|
|
|
154
|
|
|
$data = $query->toArray(); |
|
155
|
|
|
$this->assertEquals(' hello world', $data['more_like_this']['like']); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @group unit |
|
160
|
|
|
*/ |
|
161
|
|
View Code Duplication |
public function testSetBoost(): void |
|
|
|
|
|
|
162
|
|
|
{ |
|
163
|
|
|
$query = new MoreLikeThis(); |
|
164
|
|
|
|
|
165
|
|
|
$boost = 1.3; |
|
166
|
|
|
$query->setBoost($boost); |
|
167
|
|
|
|
|
168
|
|
|
$this->assertEquals($boost, $query->getParam('boost')); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @group unit |
|
173
|
|
|
*/ |
|
174
|
|
View Code Duplication |
public function testSetMaxQueryTerms(): void |
|
|
|
|
|
|
175
|
|
|
{ |
|
176
|
|
|
$query = new MoreLikeThis(); |
|
177
|
|
|
|
|
178
|
|
|
$max = 3; |
|
179
|
|
|
$query->setMaxQueryTerms($max); |
|
180
|
|
|
|
|
181
|
|
|
$this->assertEquals($max, $query->getParam('max_query_terms')); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @group unit |
|
186
|
|
|
*/ |
|
187
|
|
View Code Duplication |
public function testSetMinimumShouldMatch(): void |
|
|
|
|
|
|
188
|
|
|
{ |
|
189
|
|
|
$query = new MoreLikeThis(); |
|
190
|
|
|
|
|
191
|
|
|
$match = '80%'; |
|
192
|
|
|
$query->setMinimumShouldMatch($match); |
|
193
|
|
|
|
|
194
|
|
|
$this->assertEquals($match, $query->getParam('minimum_should_match')); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* @group unit |
|
199
|
|
|
*/ |
|
200
|
|
View Code Duplication |
public function testSetMinDocFrequency(): void |
|
|
|
|
|
|
201
|
|
|
{ |
|
202
|
|
|
$query = new MoreLikeThis(); |
|
203
|
|
|
|
|
204
|
|
|
$freq = 2; |
|
205
|
|
|
$query->setMinDocFrequency($freq); |
|
206
|
|
|
|
|
207
|
|
|
$this->assertEquals($freq, $query->getParam('min_doc_freq')); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @group unit |
|
212
|
|
|
*/ |
|
213
|
|
View Code Duplication |
public function testSetMaxDocFrequency(): void |
|
|
|
|
|
|
214
|
|
|
{ |
|
215
|
|
|
$query = new MoreLikeThis(); |
|
216
|
|
|
|
|
217
|
|
|
$freq = 2; |
|
218
|
|
|
$query->setMaxDocFrequency($freq); |
|
219
|
|
|
|
|
220
|
|
|
$this->assertEquals($freq, $query->getParam('max_doc_freq')); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @group unit |
|
225
|
|
|
*/ |
|
226
|
|
View Code Duplication |
public function testSetMinWordLength(): void |
|
|
|
|
|
|
227
|
|
|
{ |
|
228
|
|
|
$query = new MoreLikeThis(); |
|
229
|
|
|
|
|
230
|
|
|
$length = 4; |
|
231
|
|
|
$query->setMinWordLength($length); |
|
232
|
|
|
|
|
233
|
|
|
$this->assertEquals($length, $query->getParam('min_word_length')); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* @group unit |
|
238
|
|
|
*/ |
|
239
|
|
View Code Duplication |
public function testSetMaxWordLength(): void |
|
|
|
|
|
|
240
|
|
|
{ |
|
241
|
|
|
$query = new MoreLikeThis(); |
|
242
|
|
|
|
|
243
|
|
|
$length = 5; |
|
244
|
|
|
$query->setMaxWordLength($length); |
|
245
|
|
|
|
|
246
|
|
|
$this->assertEquals($length, $query->getParam('max_word_length')); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* @group unit |
|
251
|
|
|
*/ |
|
252
|
|
View Code Duplication |
public function testSetBoostTerms(): void |
|
|
|
|
|
|
253
|
|
|
{ |
|
254
|
|
|
$query = new MoreLikeThis(); |
|
255
|
|
|
|
|
256
|
|
|
$boost = false; |
|
257
|
|
|
$query->setBoostTerms($boost); |
|
258
|
|
|
|
|
259
|
|
|
$this->assertEquals($boost, $query->getParam('boost_terms')); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* @group unit |
|
264
|
|
|
*/ |
|
265
|
|
View Code Duplication |
public function testSetAnalyzer(): void |
|
|
|
|
|
|
266
|
|
|
{ |
|
267
|
|
|
$query = new MoreLikeThis(); |
|
268
|
|
|
|
|
269
|
|
|
$analyzer = 'UpperCase'; |
|
270
|
|
|
$query->setAnalyzer($analyzer); |
|
271
|
|
|
|
|
272
|
|
|
$this->assertEquals($analyzer, $query->getParam('analyzer')); |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* @group unit |
|
277
|
|
|
*/ |
|
278
|
|
|
public function testSetStopWords(): void |
|
279
|
|
|
{ |
|
280
|
|
|
$query = new MoreLikeThis(); |
|
281
|
|
|
|
|
282
|
|
|
$stopWords = ['no', 'yes', 'test']; |
|
283
|
|
|
$query->setStopWords($stopWords); |
|
284
|
|
|
|
|
285
|
|
|
$this->assertEquals($stopWords, $query->getParam('stop_words')); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* @group unit |
|
290
|
|
|
*/ |
|
291
|
|
|
public function testToArrayForId(): void |
|
292
|
|
|
{ |
|
293
|
|
|
$query = new MoreLikeThis(); |
|
294
|
|
|
$query->setLike(new Document('1', [], 'index')); |
|
|
|
|
|
|
295
|
|
|
|
|
296
|
|
|
$data = $query->toArray(); |
|
297
|
|
|
|
|
298
|
|
|
$this->assertEquals( |
|
299
|
|
|
['more_like_this' => [ |
|
300
|
|
|
'like' => [ |
|
301
|
|
|
'_id' => '1', |
|
302
|
|
|
'_index' => 'index', |
|
303
|
|
|
], |
|
304
|
|
|
], |
|
305
|
|
|
], |
|
306
|
|
|
$data |
|
307
|
|
|
); |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* @group unit |
|
312
|
|
|
*/ |
|
313
|
|
View Code Duplication |
public function testToArrayForSource(): void |
|
|
|
|
|
|
314
|
|
|
{ |
|
315
|
|
|
$query = new MoreLikeThis(); |
|
316
|
|
|
$query->setLike(new Document('', ['Foo' => 'Bar'], 'index')); |
|
|
|
|
|
|
317
|
|
|
|
|
318
|
|
|
$data = $query->toArray(); |
|
319
|
|
|
|
|
320
|
|
|
$this->assertEquals( |
|
321
|
|
|
['more_like_this' => [ |
|
322
|
|
|
'like' => [ |
|
323
|
|
|
'_index' => 'index', |
|
324
|
|
|
'doc' => [ |
|
325
|
|
|
'Foo' => 'Bar', |
|
326
|
|
|
], |
|
327
|
|
|
], |
|
328
|
|
|
], |
|
329
|
|
|
], |
|
330
|
|
|
$data |
|
331
|
|
|
); |
|
332
|
|
|
} |
|
333
|
|
|
} |
|
334
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: