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
|
1 |
|
public function setUp() { |
10
|
1 |
|
parent::setUp(); |
11
|
1 |
|
} |
12
|
|
|
|
13
|
1 |
|
public function tearDown() { |
14
|
1 |
|
parent::tearDown(); |
15
|
1 |
|
} |
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
|
1 |
|
public function testSimilarGood() { |
122
|
1 |
|
$fp = $this->objFromFixture('FlickrPhotoTO', 'photo0076'); |
123
|
1 |
|
$es = new ElasticSearcher(); |
124
|
1 |
|
$es->setClasses('FlickrPhotoTO'); |
125
|
1 |
|
$fields = array('Title.standard' => 1, 'Description.standard' => 1); |
126
|
1 |
|
$paginated = $es->moreLikeThis($fp, $fields, true); |
|
|
|
|
127
|
|
|
|
128
|
1 |
|
$results = $paginated->getList()->toArray(); |
129
|
|
|
|
130
|
|
|
// FIXME - this test appears fragile due to sharding issues with more like this |
131
|
1 |
|
$ctr = 0; |
132
|
1 |
|
if ($ctr < 9) { |
133
|
1 |
|
$this->assertStringStartsWith( |
|
|
|
|
134
|
1 |
|
'[Texas and New Orleans, Southern Pacific', |
135
|
1 |
|
$results[$ctr]->Title |
136
|
1 |
|
); |
137
|
1 |
|
$ctr++; |
138
|
1 |
|
} |
139
|
1 |
|
} |
140
|
|
|
|
141
|
|
|
|
142
|
|
|
// if this is not set to unbounded, zero, a conditional is triggered to add max doc freq to the request |
143
|
|
|
public function testSimilarChangeMaxDocFreq() { |
144
|
|
|
$fp = $this->objFromFixture('FlickrPhotoTO', 'photo0076'); |
145
|
|
|
$es = new ElasticSearcher(); |
146
|
|
|
$es->setMaxDocFreq(4); |
147
|
|
|
$es->setClasses('FlickrPhotoTO'); |
148
|
|
|
$fields = array('Title.standard' => 1, 'Description.standard' => 1); |
149
|
|
|
$paginated = $es->moreLikeThis($fp, $fields, true); |
|
|
|
|
150
|
|
|
|
151
|
|
|
$this->assertEquals(14, $paginated->getTotalItems()); |
152
|
|
|
$results = $paginated->getList()->toArray(); |
|
|
|
|
153
|
|
|
$this->makeCode($paginated); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
|
157
|
|
|
public function testSimilarNullFields() { |
158
|
|
|
$fp = $this->objFromFixture('FlickrPhotoTO', 'photo0076'); |
159
|
|
|
$es = new ElasticSearcher(); |
160
|
|
|
$es->setClasses('FlickrPhotoTO'); |
161
|
|
|
try { |
162
|
|
|
$paginated = $es->moreLikeThis($fp, null, true); |
|
|
|
|
163
|
|
|
} catch (InvalidArgumentException $e) { |
164
|
|
|
$this->assertEquals('Fields cannot be null', $e->getMessage()); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
|
169
|
|
|
public function testSimilarNullItem() { |
170
|
|
|
$es = new ElasticSearcher(); |
171
|
|
|
$es->setClasses('FlickrPhotoTO'); |
172
|
|
|
$fields = array('Title.standard' => 1, 'Description.standard' => 1); |
173
|
|
|
|
174
|
|
|
try { |
175
|
|
|
$paginated = $es->moreLikeThis(null, $fields, true); |
|
|
|
|
176
|
|
|
} catch (InvalidArgumentException $e) { |
177
|
|
|
$this->assertEquals('A searchable item cannot be null', $e->getMessage()); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
|
182
|
|
|
|
183
|
|
|
public function testHighlightsAsIfCMSEdited() { |
184
|
|
|
$es = new ElasticSearcher(); |
185
|
|
|
$locale = \i18n::default_locale(); |
186
|
|
|
$es->setLocale($locale); |
187
|
|
|
$es->setClasses('FlickrPhotoTO'); |
188
|
|
|
|
189
|
|
|
$filter = array('ClazzName' => 'FlickrPhotoTO', 'Name' => 'Title'); |
190
|
|
|
$titleField = SearchableField::get()->filter($filter)->first(); |
191
|
|
|
$titleField->ShowHighlights = true; |
192
|
|
|
$titleField->write(); |
193
|
|
|
|
194
|
|
|
$filter = array('ClazzName' => 'FlickrPhotoTO', 'Name' => 'Description'); |
195
|
|
|
$nameField = SearchableField::get()->filter($filter)->first(); |
196
|
|
|
$nameField->ShowHighlights = true; |
197
|
|
|
$nameField->write(); |
198
|
|
|
|
199
|
|
|
$fields = array('Title' => 1, 'Description' => 1); |
200
|
|
|
$query = 'New Zealand'; |
201
|
|
|
$paginated = $es->search($query, $fields); |
202
|
|
|
$ctr = 0; |
203
|
|
|
|
204
|
|
|
foreach ($paginated->getList()->toArray() as $result) { |
205
|
|
|
$ctr++; |
206
|
|
|
foreach ($result->SearchHighlightsByField->Description_standard->getIterator() as $highlight) { |
207
|
|
|
$snippet = $highlight->Snippet; |
208
|
|
|
$snippet = strtolower($snippet); |
209
|
|
|
$wordFound = false; |
210
|
|
|
$lcquery = explode(' ', strtolower($query)); |
211
|
|
|
foreach ($lcquery as $part) { |
212
|
|
|
$bracketed = '<strong class="hl">' . $part . '</strong>'; |
213
|
|
|
if (strpos($snippet, $bracketed) > 0) { |
214
|
|
|
$wordFound = true; |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
$this->assertTrue($wordFound, 'Highlight should have been found'); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
|
223
|
|
|
public function testHighlightPassingFields() { |
224
|
|
|
$es = new ElasticSearcher(); |
225
|
|
|
$es->setClasses('FlickrPhotoTO'); |
226
|
|
|
$es->setHighlightedFields(array('Title', 'Title.standard', 'Description')); |
227
|
|
|
|
228
|
|
|
$fields = array('Title' => 1, 'Description' => 1); |
229
|
|
|
$query = 'New Zealand'; |
230
|
|
|
$paginated = $es->search($query, $fields); |
231
|
|
|
$ctr = 0; |
232
|
|
|
|
233
|
|
|
foreach ($paginated->getList()->toArray() as $result) { |
234
|
|
|
$ctr++; |
235
|
|
|
|
236
|
|
|
foreach ($result->SearchHighlightsByField->Description->getIterator() as $highlight) { |
237
|
|
|
$snippet = $highlight->Snippet; |
238
|
|
|
$snippet = strtolower($snippet); |
239
|
|
|
$wordFound = false; |
240
|
|
|
$lcquery = explode(' ', strtolower($query)); |
241
|
|
|
foreach ($lcquery as $part) { |
242
|
|
|
$bracketed = '<strong class="hl">' . $part . '</strong>'; |
243
|
|
|
if (strpos($snippet, $bracketed) > 0) { |
244
|
|
|
$wordFound = true; |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
$this->assertTrue($wordFound, 'Highlight should have been found'); |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
|
253
|
|
|
public function testAutoCompleteGood() { |
254
|
|
|
$es = new ElasticSearcher(); |
255
|
|
|
$es->setClasses('FlickrPhotoTO'); |
256
|
|
|
$fields = array('Title' => 1, 'Description' => 1); |
|
|
|
|
257
|
|
|
$query = 'Lond'; |
258
|
|
|
$results = $es->autocomplete_search($query, 'Title'); |
259
|
|
|
$this->assertEquals(7, $results->getTotalItems()); |
260
|
|
|
foreach ($results->toArray() as $result) { |
261
|
|
|
$this->assertTrue(strpos($result->Title, $query) > 0); |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
|
266
|
|
View Code Duplication |
private function makeCode($paginated) { |
|
|
|
|
267
|
|
|
$results = $paginated->getList()->toArray(); |
268
|
|
|
$ctr = 0; |
269
|
|
|
echo '$result = $paginated->getList()->toArray();' . "\n"; |
270
|
|
|
foreach ($results as $result) { |
271
|
|
|
echo '$this->assertEquals("' . $result->Title . '", $results[' . $ctr . ']->Title);' . "\n"; |
272
|
|
|
$ctr++; |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
} |
277
|
|
|
|
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: