1
|
|
|
<?php |
2
|
|
|
use SilverStripe\Elastica\ElasticSearcher; |
3
|
|
|
use Elastica\Type; |
4
|
|
|
class ElasticSearcherUnitTest extends ElasticsearchBaseTest { |
5
|
|
|
public static $fixture_file = 'elastica/tests/lotsOfPhotos.yml'; |
6
|
|
|
|
7
|
|
|
public static $ignoreFixtureFileFor = array('testResultsForEmptySearch'); |
8
|
|
|
|
9
|
|
|
public function setUp() { |
10
|
|
|
parent::setUp(); |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
public function tearDown() { |
14
|
|
|
parent::tearDown(); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
public function testSuggested() { |
19
|
|
|
$es = new ElasticSearcher(); |
20
|
|
|
$locale = \i18n::default_locale(); |
21
|
|
|
$es->setLocale($locale); |
22
|
|
|
$es->setClasses('FlickrPhotoTO'); |
23
|
|
|
$fields = array('Description' => 1,'Title' => 1); |
|
|
|
|
24
|
|
|
$this->assertEquals('New Zealand', $es->getSuggestedQuery()); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
public function testResultsForEmptySearch() { |
29
|
|
|
$es = new ElasticSearcher(); |
30
|
|
|
|
31
|
|
|
$es->hideResultsForEmptySearch(); |
32
|
|
|
$this->assertFalse($es->getShowResultsForEmptySearch()); |
33
|
|
|
|
34
|
|
|
$es->showResultsForEmptySearch(); |
35
|
|
|
$this->assertTrue($es->getShowResultsForEmptySearch()); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
public function testMoreLikeThisSinglePhoto() { |
40
|
|
|
$fp = $this->objFromFixture('FlickrPhotoTO', 'photo0076'); |
41
|
|
|
|
42
|
|
|
echo "FP: Original title: {$fp->Title}\n"; |
43
|
|
|
$es = new ElasticSearcher(); |
44
|
|
|
$locale = \i18n::default_locale(); |
45
|
|
|
$es->setLocale($locale); |
46
|
|
|
$es->setClasses('FlickrPhotoTO'); |
47
|
|
|
|
48
|
|
|
$fields = array('Description.standard' => 1,'Title.standard' => 1); |
49
|
|
|
$results = $es->moreLikeThis($fp, $fields, true); |
|
|
|
|
50
|
|
|
|
51
|
|
|
echo "RESULTS:\n"; |
52
|
|
|
foreach ($results as $result) { |
53
|
|
|
echo "-\t{$result->Title}\n"; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$terms = $results->getList()->MoreLikeThisTerms; |
|
|
|
|
57
|
|
|
|
58
|
|
|
$fieldNamesReturned = array_keys($terms); |
59
|
|
|
$fieldNames = array_keys($fields); |
60
|
|
|
sort($fieldNames); |
61
|
|
|
sort($fieldNamesReturned); |
62
|
|
|
|
63
|
|
|
$this->assertEquals($fieldNames, $fieldNamesReturned); |
64
|
|
|
|
65
|
|
|
//FIXME - this seems anomolyous, check in more detail |
66
|
|
|
$expected = array('texas'); |
67
|
|
|
$this->assertEquals($expected, $terms['Title.standard']); |
68
|
|
|
|
69
|
|
|
$expected = array('collection', 'company', 'degolyer', 'everett', 'file', 'high', |
70
|
|
|
'information', 'new', 'orleans', 'pacific', 'photographs', 'railroad', 'resolution', |
71
|
|
|
'see', 'southern', 'texas', 'view'); |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
|
75
|
|
|
$actual = $terms['Description.standard']; |
76
|
|
|
sort($expected); |
77
|
|
|
sort($actual); |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
$this->assertEquals($expected, $actual); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
|
85
|
|
View Code Duplication |
public function testSimilarNoWeighting() { |
|
|
|
|
86
|
|
|
$fp = $this->objFromFixture('FlickrPhotoTO', 'photo0076'); |
87
|
|
|
$es = new ElasticSearcher(); |
88
|
|
|
$es->setClasses('FlickrPhotoTO'); |
89
|
|
|
$fields = array('Title.standard', 'Description.standard'); |
90
|
|
|
try { |
91
|
|
|
$paginated = $es->moreLikeThis($fp, $fields, true); |
|
|
|
|
92
|
|
|
$this->fail('Query has no weight and thus should have failed'); |
|
|
|
|
93
|
|
|
} catch (InvalidArgumentException $e) { |
94
|
|
|
$this->assertEquals('Fields must be of the form fieldname => weight', $e->getMessage()); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
|
99
|
|
View Code Duplication |
public function testSimilarWeightingNotNumeric() { |
|
|
|
|
100
|
|
|
$fp = $this->objFromFixture('FlickrPhotoTO', 'photo0076'); |
101
|
|
|
$es = new ElasticSearcher(); |
102
|
|
|
$es->setClasses('FlickrPhotoTO'); |
103
|
|
|
$fields = array('Title.standard' => 4, 'Description.standard' => 'not numeric'); |
104
|
|
|
try { |
105
|
|
|
$paginated = $es->moreLikeThis($fp, $fields, true); |
|
|
|
|
106
|
|
|
$this->fail('Query has non numeric weight and thus should have failed'); |
|
|
|
|
107
|
|
|
} catch (InvalidArgumentException $e) { |
108
|
|
|
$this->assertEquals('Fields must be of the form fieldname => weight', $e->getMessage()); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
View Code Duplication |
public function testSimilarToNonSearchable() { |
|
|
|
|
114
|
|
|
$m = Member::get()->first(); // this is not by default Searchable |
|
|
|
|
115
|
|
|
$es = new ElasticSearcher(); |
116
|
|
|
$es->setClasses('FlickrPhotoTO'); |
117
|
|
|
$fields = array('Title.standard' => 4, 'Description.standard' => 2); |
118
|
|
|
try { |
119
|
|
|
$paginated = $es->moreLikeThis($m, $fields, true); |
|
|
|
|
120
|
|
|
$this->fail('Querying for a non searchable object, thus should have failed'); |
|
|
|
|
121
|
|
|
} catch (InvalidArgumentException $e) { |
122
|
|
|
$this->assertEquals('Objects of class Member are not searchable', $e->getMessage()); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
View Code Duplication |
public function testSimilarGood() { |
|
|
|
|
127
|
|
|
$fp = $this->objFromFixture('FlickrPhotoTO', 'photo0076'); |
128
|
|
|
$es = new ElasticSearcher(); |
129
|
|
|
$es->setClasses('FlickrPhotoTO'); |
130
|
|
|
$fields = array('Title.standard' => 1, 'Description.standard' => 1); |
131
|
|
|
$paginated = $es->moreLikeThis($fp, $fields, true); |
|
|
|
|
132
|
|
|
foreach ($paginated->getList() as $result) { |
133
|
|
|
echo $result->ID. ' : '.$result->Title."\n"; |
134
|
|
|
} |
135
|
|
|
$this->assertEquals(32, $paginated->getTotalItems()); |
136
|
|
|
$results = $paginated->getList()->toArray(); |
137
|
|
|
$this->assertEquals("[Texas and New Orleans, Southern Pacific Railroad Station, Stockdale, Texas]", $results[0]->Title); |
138
|
|
|
$this->assertEquals("[Texas and New Orleans, Southern Pacific Railroad Station, Taft, Texas]", $results[1]->Title); |
139
|
|
|
$this->assertEquals("[Texas and New Orleans, Southern Pacific Railroad Station, Sierra Blanca, Texas]", $results[2]->Title); |
140
|
|
|
$this->assertEquals("[Texas and New Orleans, Southern Pacific Freight Station, Waxahachie, Texas]", $results[3]->Title); |
141
|
|
|
$this->assertEquals("[Texas and New Orleans, Southern Pacific Passenger Station, Waxahachie, Texas]", $results[4]->Title); |
142
|
|
|
$this->assertEquals("[Texas and New Orleans, Southern Pacific, Tower No. 63, Mexia, Texas]", $results[5]->Title); |
143
|
|
|
$this->assertEquals("[Texas and New Orleans, Southern Pacific, Eakin Street Yard Office, Dallas, Texas]", $results[6]->Title); |
144
|
|
|
$this->assertEquals("[Texas and New Orleans, Southern Pacific Locomotive Scrap Line, Englewood Yards, Houston, Texas]", $results[7]->Title); |
145
|
|
|
$this->assertEquals("[Texas and New Orleans, Southern Pacific, Switchman's Tower, San Antonio, Texas]", $results[8]->Title); |
146
|
|
|
$this->assertEquals("Flash Light view in new Subterranean", $results[9]->Title); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
// if this is not set to unbounded, zero, a conditional is triggered to add max doc freq to the request |
151
|
|
View Code Duplication |
public function testSimilarChangeMaxDocFreq() { |
|
|
|
|
152
|
|
|
$fp = $this->objFromFixture('FlickrPhotoTO', 'photo0076'); |
153
|
|
|
$es = new ElasticSearcher(); |
154
|
|
|
$es->setMaxDocFreq(4); |
155
|
|
|
$es->setClasses('FlickrPhotoTO'); |
156
|
|
|
$fields = array('Title.standard' => 1, 'Description.standard' => 1); |
157
|
|
|
$paginated = $es->moreLikeThis($fp, $fields, true); |
|
|
|
|
158
|
|
|
foreach ($paginated->getList() as $result) { |
159
|
|
|
echo $result->ID. ' : '.$result->Title."\n"; |
160
|
|
|
} |
161
|
|
|
$this->assertEquals(14, $paginated->getTotalItems()); |
162
|
|
|
$results = $paginated->getList()->toArray(); |
|
|
|
|
163
|
|
|
$this->makeCode($paginated); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
|
167
|
|
View Code Duplication |
public function testSimilarNullFields() { |
|
|
|
|
168
|
|
|
$fp = $this->objFromFixture('FlickrPhotoTO', 'photo0076'); |
169
|
|
|
$es = new ElasticSearcher(); |
170
|
|
|
$es->setClasses('FlickrPhotoTO'); |
171
|
|
|
try { |
172
|
|
|
$paginated = $es->moreLikeThis($fp, null, true); |
|
|
|
|
173
|
|
|
} catch (InvalidArgumentException $e) { |
174
|
|
|
$this->assertEquals('Fields cannot be null', $e->getMessage()); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
|
179
|
|
View Code Duplication |
public function testSimilarNullItem() { |
|
|
|
|
180
|
|
|
$es = new ElasticSearcher(); |
181
|
|
|
$es->setClasses('FlickrPhotoTO'); |
182
|
|
|
$fields = array('Title.standard' => 1, 'Description.standard' => 1); |
183
|
|
|
|
184
|
|
|
try { |
185
|
|
|
$paginated = $es->moreLikeThis(null, $fields, true); |
|
|
|
|
186
|
|
|
} catch (InvalidArgumentException $e) { |
187
|
|
|
$this->assertEquals('A searchable item cannot be null', $e->getMessage()); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
|
192
|
|
|
|
193
|
|
|
public function testHighlightsAsIfCMSEdited() { |
194
|
|
|
$es = new ElasticSearcher(); |
195
|
|
|
$locale = \i18n::default_locale(); |
196
|
|
|
$es->setLocale($locale); |
197
|
|
|
$es->setClasses('FlickrPhotoTO'); |
198
|
|
|
|
199
|
|
|
$filter = array('ClazzName' => 'FlickrPhotoTO', 'Name' => 'Title'); |
200
|
|
|
$titleField = SearchableField::get()->filter($filter)->first(); |
201
|
|
|
$titleField->ShowHighlights = true; |
202
|
|
|
$titleField->write(); |
203
|
|
|
|
204
|
|
|
$filter = array('ClazzName' => 'FlickrPhotoTO', 'Name' => 'Description'); |
205
|
|
|
$nameField = SearchableField::get()->filter($filter)->first(); |
206
|
|
|
$nameField->ShowHighlights = true; |
207
|
|
|
$nameField->write(); |
208
|
|
|
|
209
|
|
|
$fields = array('Title' => 1, 'Description' => 1); |
210
|
|
|
$query = 'New Zealand'; |
211
|
|
|
$paginated = $es->search($query, $fields); |
212
|
|
|
$ctr = 0; |
213
|
|
|
|
214
|
|
|
foreach ($paginated->getList()->toArray() as $result) { |
215
|
|
|
$ctr++; |
216
|
|
|
foreach ($result->SearchHighlightsByField->Description_standard->getIterator() as $highlight) { |
217
|
|
|
$snippet = $highlight->Snippet; |
218
|
|
|
$snippet = strtolower($snippet); |
219
|
|
|
$wordFound = false; |
220
|
|
|
$lcquery = explode(' ', strtolower($query)); |
221
|
|
|
foreach ($lcquery as $part) { |
222
|
|
|
$bracketed = '<strong class="hl">'.$part.'</strong>'; |
223
|
|
|
if (strpos($snippet, $bracketed) > 0) { |
224
|
|
|
$wordFound = true; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
$this->assertTrue($wordFound,'Highlight should have been found'); |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
|
233
|
|
View Code Duplication |
public function testHighlightPassingFields() { |
|
|
|
|
234
|
|
|
$es = new ElasticSearcher(); |
235
|
|
|
$es->setClasses('FlickrPhotoTO'); |
236
|
|
|
$es->setHighlightedFields(array('Title', 'Title.standard', 'Description')); |
237
|
|
|
|
238
|
|
|
$fields = array('Title' => 1, 'Description' => 1); |
239
|
|
|
$query = 'New Zealand'; |
240
|
|
|
$paginated = $es->search($query, $fields); |
241
|
|
|
$ctr = 0; |
242
|
|
|
|
243
|
|
|
foreach ($paginated->getList()->toArray() as $result) { |
244
|
|
|
$ctr++; |
245
|
|
|
|
246
|
|
|
foreach ($result->SearchHighlightsByField->Description->getIterator() as $highlight) { |
247
|
|
|
$snippet = $highlight->Snippet; |
248
|
|
|
$snippet = strtolower($snippet); |
249
|
|
|
$wordFound = false; |
250
|
|
|
$lcquery = explode(' ', strtolower($query)); |
251
|
|
|
foreach ($lcquery as $part) { |
252
|
|
|
$bracketed = '<strong class="hl">'.$part.'</strong>'; |
253
|
|
|
if (strpos($snippet, $bracketed) > 0) { |
254
|
|
|
$wordFound = true; |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
$this->assertTrue($wordFound,'Highlight should have been found'); |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
|
263
|
|
View Code Duplication |
public function testAutoCompleteGood() { |
|
|
|
|
264
|
|
|
$es = new ElasticSearcher(); |
265
|
|
|
$es->setClasses('FlickrPhotoTO'); |
266
|
|
|
$fields = array('Title' => 1, 'Description' => 1); |
|
|
|
|
267
|
|
|
$query = 'Lond'; |
268
|
|
|
$results = $es->autocomplete_search($query, 'Title'); |
269
|
|
|
$this->assertEquals(7, $results->getTotalItems()); |
270
|
|
|
foreach ($results->toArray() as $result) { |
271
|
|
|
$this->assertTrue(strpos($result->Title, $query) > 0); |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
|
276
|
|
View Code Duplication |
private function makeCode($paginated) { |
|
|
|
|
277
|
|
|
$results = $paginated->getList()->toArray(); |
278
|
|
|
$ctr = 0; |
279
|
|
|
echo '$result = $paginated->getList()->toArray();'."\n"; |
280
|
|
|
foreach ($results as $result) { |
281
|
|
|
echo '$this->assertEquals("'.$result->Title.'", $results['.$ctr.']->Title);'."\n"; |
282
|
|
|
$ctr++; |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
} |
287
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.