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