|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Elastica\Test; |
|
4
|
|
|
|
|
5
|
|
|
use Elastica\Document; |
|
6
|
|
|
use Elastica\Exception\InvalidException; |
|
7
|
|
|
use Elastica\Exception\ResponseException; |
|
8
|
|
|
use Elastica\Index; |
|
9
|
|
|
use Elastica\Mapping; |
|
10
|
|
|
use Elastica\Query\QueryString; |
|
11
|
|
|
use Elastica\Query\SimpleQueryString; |
|
12
|
|
|
use Elastica\Query\Term; |
|
13
|
|
|
use Elastica\Request; |
|
14
|
|
|
use Elastica\Script\Script; |
|
15
|
|
|
use Elastica\Status; |
|
16
|
|
|
use Elastica\Test\Base as BaseTest; |
|
17
|
|
|
use Elasticsearch\Endpoints\Indices\Analyze; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @group functional |
|
21
|
|
|
* |
|
22
|
|
|
* @internal |
|
23
|
|
|
*/ |
|
24
|
|
|
class IndexTest extends BaseTest |
|
25
|
|
|
{ |
|
26
|
|
|
public function testMapping(): void |
|
27
|
|
|
{ |
|
28
|
|
|
$index = $this->_createIndex(); |
|
29
|
|
|
|
|
30
|
|
|
$mappings = new Mapping([ |
|
31
|
|
|
'id' => ['type' => 'integer', 'store' => true], |
|
32
|
|
|
'email' => ['type' => 'text'], |
|
33
|
|
|
'username' => ['type' => 'text'], |
|
34
|
|
|
'test' => ['type' => 'integer'], |
|
35
|
|
|
]); |
|
36
|
|
|
$index->setMapping($mappings); |
|
37
|
|
|
$index->addDocument( |
|
38
|
|
|
new Document(1, ['id' => 1, 'email' => '[email protected]', 'username' => 'hanswurst', 'test' => ['2', '3', '5']]) |
|
39
|
|
|
); |
|
40
|
|
|
$index->forcemerge(); |
|
41
|
|
|
|
|
42
|
|
|
$storedMapping = $index->getMapping(); |
|
43
|
|
|
|
|
44
|
|
|
$this->assertEquals($storedMapping['properties']['id']['type'], 'integer'); |
|
45
|
|
|
$this->assertEquals($storedMapping['properties']['id']['store'], true); |
|
46
|
|
|
$this->assertEquals($storedMapping['properties']['email']['type'], 'text'); |
|
47
|
|
|
$this->assertEquals($storedMapping['properties']['username']['type'], 'text'); |
|
48
|
|
|
$this->assertEquals($storedMapping['properties']['test']['type'], 'integer'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function testGetMappingAlias(): void |
|
52
|
|
|
{ |
|
53
|
|
|
$index = $this->_createIndex(); |
|
54
|
|
|
$indexName = $index->getName(); |
|
55
|
|
|
|
|
56
|
|
|
$aliasName = 'test-mapping-alias'; |
|
57
|
|
|
$index->addAlias($aliasName); |
|
58
|
|
|
|
|
59
|
|
|
$mapping = new Mapping(['id' => ['type' => 'integer', 'store' => 'true']]); |
|
60
|
|
|
$index->setMapping($mapping); |
|
61
|
|
|
|
|
62
|
|
|
$client = $index->getClient(); |
|
63
|
|
|
|
|
64
|
|
|
// Index mapping |
|
65
|
|
|
$mapping1 = $client->getIndex($indexName)->getMapping(); |
|
66
|
|
|
|
|
67
|
|
|
// Alias mapping |
|
68
|
|
|
$mapping2 = $client->getIndex($aliasName)->getMapping(); |
|
69
|
|
|
|
|
70
|
|
|
// Make sure, a mapping is set |
|
71
|
|
|
$this->assertNotEmpty($mapping1); |
|
72
|
|
|
|
|
73
|
|
|
// Alias and index mapping should be identical |
|
74
|
|
|
$this->assertEquals($mapping1, $mapping2); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function testAddRemoveAlias(): void |
|
78
|
|
|
{ |
|
79
|
|
|
$this->expectException(ResponseException::class); |
|
80
|
|
|
|
|
81
|
|
|
$client = $this->_getClient(); |
|
82
|
|
|
|
|
83
|
|
|
$indexName1 = 'test1'; |
|
84
|
|
|
$aliasName = 'test-alias'; |
|
85
|
|
|
$typeName = 'test'; |
|
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
$index = $client->getIndex($indexName1); |
|
88
|
|
|
$index->create(['settings' => ['index' => ['number_of_shards' => 1, 'number_of_replicas' => 0]]], true); |
|
89
|
|
|
$index->addDocument(new Document(1, ['id' => 1, 'email' => '[email protected]', 'username' => 'ruflin'])); |
|
90
|
|
|
$index->refresh(); |
|
91
|
|
|
|
|
92
|
|
|
$resultSet = $index->search('ruflin'); |
|
93
|
|
|
$this->assertEquals(1, $resultSet->count()); |
|
94
|
|
|
|
|
95
|
|
|
$data = $index->addAlias($aliasName, true)->getData(); |
|
96
|
|
|
$this->assertTrue($data['acknowledged']); |
|
97
|
|
|
|
|
98
|
|
|
$response = $index->removeAlias($aliasName)->getData(); |
|
99
|
|
|
$this->assertTrue($response['acknowledged']); |
|
100
|
|
|
|
|
101
|
|
|
$client->getIndex($aliasName)->search('ruflin'); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
View Code Duplication |
public function testCount(): void |
|
|
|
|
|
|
105
|
|
|
{ |
|
106
|
|
|
$index = $this->_createIndex(); |
|
107
|
|
|
|
|
108
|
|
|
// Add document to normal index |
|
109
|
|
|
$doc1 = new Document(null, ['name' => 'ruflin']); |
|
110
|
|
|
$doc2 = new Document(null, ['name' => 'nicolas']); |
|
111
|
|
|
|
|
112
|
|
|
$index->addDocument($doc1); |
|
113
|
|
|
$index->addDocument($doc2); |
|
114
|
|
|
|
|
115
|
|
|
$index->refresh(); |
|
116
|
|
|
|
|
117
|
|
|
$this->assertEquals(2, $index->count()); |
|
118
|
|
|
|
|
119
|
|
|
$query = new Term(); |
|
120
|
|
|
$key = 'name'; |
|
121
|
|
|
$value = 'nicolas'; |
|
122
|
|
|
$query->setTerm($key, $value); |
|
123
|
|
|
|
|
124
|
|
|
$this->assertEquals(1, $index->count($query)); |
|
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
View Code Duplication |
public function testCountGet(): void |
|
|
|
|
|
|
128
|
|
|
{ |
|
129
|
|
|
$index = $this->_createIndex(); |
|
130
|
|
|
|
|
131
|
|
|
// Add document to normal index |
|
132
|
|
|
$doc1 = new Document(null, ['name' => 'ruflin']); |
|
133
|
|
|
$doc2 = new Document(null, ['name' => 'nicolas']); |
|
134
|
|
|
|
|
135
|
|
|
$index->addDocument($doc1); |
|
136
|
|
|
$index->addDocument($doc2); |
|
137
|
|
|
|
|
138
|
|
|
$index->refresh(); |
|
139
|
|
|
|
|
140
|
|
|
$this->assertEquals(2, $index->count('', Request::GET)); |
|
141
|
|
|
|
|
142
|
|
|
$query = new Term(); |
|
143
|
|
|
$key = 'name'; |
|
144
|
|
|
$value = 'nicolas'; |
|
145
|
|
|
$query->setTerm($key, $value); |
|
146
|
|
|
|
|
147
|
|
|
$this->assertEquals(1, $index->count($query, Request::GET)); |
|
|
|
|
|
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
View Code Duplication |
public function testDeleteByQueryWithQueryString(): void |
|
|
|
|
|
|
151
|
|
|
{ |
|
152
|
|
|
$index = $this->_createIndex(); |
|
153
|
|
|
$index->addDocuments([ |
|
154
|
|
|
new Document(1, ['name' => 'ruflin nicolas']), |
|
155
|
|
|
new Document(2, ['name' => 'ruflin']), |
|
156
|
|
|
]); |
|
157
|
|
|
$index->refresh(); |
|
158
|
|
|
|
|
159
|
|
|
$response = $index->search('ruflin*'); |
|
160
|
|
|
$this->assertEquals(2, $response->count()); |
|
161
|
|
|
|
|
162
|
|
|
$response = $index->search('nicolas'); |
|
163
|
|
|
$this->assertEquals(1, $response->count()); |
|
164
|
|
|
|
|
165
|
|
|
// Delete first document |
|
166
|
|
|
$response = $index->deleteByQuery('nicolas'); |
|
167
|
|
|
$this->assertTrue($response->isOk()); |
|
168
|
|
|
|
|
169
|
|
|
$index->refresh(); |
|
170
|
|
|
|
|
171
|
|
|
// Makes sure, document is deleted |
|
172
|
|
|
$response = $index->search('ruflin*'); |
|
173
|
|
|
$this->assertEquals(1, $response->count()); |
|
174
|
|
|
|
|
175
|
|
|
$response = $index->search('nicolas'); |
|
176
|
|
|
$this->assertEquals(0, $response->count()); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
View Code Duplication |
public function testDeleteByQueryWithQuery(): void |
|
|
|
|
|
|
180
|
|
|
{ |
|
181
|
|
|
$index = $this->_createIndex(); |
|
182
|
|
|
$index->addDocuments([ |
|
183
|
|
|
new Document(1, ['name' => 'ruflin nicolas']), |
|
184
|
|
|
new Document(2, ['name' => 'ruflin']), |
|
185
|
|
|
]); |
|
186
|
|
|
$index->refresh(); |
|
187
|
|
|
|
|
188
|
|
|
$response = $index->search('ruflin*'); |
|
189
|
|
|
$this->assertEquals(2, $response->count()); |
|
190
|
|
|
|
|
191
|
|
|
$response = $index->search('nicolas'); |
|
192
|
|
|
$this->assertEquals(1, $response->count()); |
|
193
|
|
|
|
|
194
|
|
|
// Delete first document |
|
195
|
|
|
$response = $index->deleteByQuery(new SimpleQueryString('nicolas')); |
|
196
|
|
|
$this->assertTrue($response->isOk()); |
|
197
|
|
|
|
|
198
|
|
|
$index->refresh(); |
|
199
|
|
|
|
|
200
|
|
|
// Makes sure, document is deleted |
|
201
|
|
|
$response = $index->search('ruflin*'); |
|
202
|
|
|
$this->assertEquals(1, $response->count()); |
|
203
|
|
|
|
|
204
|
|
|
$response = $index->search('nicolas'); |
|
205
|
|
|
$this->assertEquals(0, $response->count()); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
public function testDeleteByQueryWithArrayQuery(): void |
|
209
|
|
|
{ |
|
210
|
|
|
$index = $this->_createIndex(); |
|
211
|
|
|
$index->addDocuments([ |
|
212
|
|
|
new Document(1, ['name' => 'ruflin nicolas']), |
|
213
|
|
|
new Document(2, ['name' => 'ruflin']), |
|
214
|
|
|
]); |
|
215
|
|
|
$index->refresh(); |
|
216
|
|
|
|
|
217
|
|
|
$response = $index->search('ruflin*'); |
|
218
|
|
|
$this->assertEquals(2, $response->count()); |
|
219
|
|
|
|
|
220
|
|
|
$response = $index->search('nicolas'); |
|
221
|
|
|
$this->assertEquals(1, $response->count()); |
|
222
|
|
|
|
|
223
|
|
|
// Delete first document |
|
224
|
|
|
$response = $index->deleteByQuery(['query' => ['query_string' => ['query' => 'nicolas']]]); |
|
225
|
|
|
$this->assertTrue($response->isOk()); |
|
226
|
|
|
|
|
227
|
|
|
$index->refresh(); |
|
228
|
|
|
|
|
229
|
|
|
// Makes sure, document is deleted |
|
230
|
|
|
$response = $index->search('ruflin*'); |
|
231
|
|
|
$this->assertEquals(1, $response->count()); |
|
232
|
|
|
|
|
233
|
|
|
$response = $index->search('nicolas'); |
|
234
|
|
|
$this->assertEquals(0, $response->count()); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
public function testDeleteByQueryWithQueryAndOptions(): void |
|
238
|
|
|
{ |
|
239
|
|
|
$index = $this->_createIndex(null, true, 2); |
|
240
|
|
|
|
|
241
|
|
|
$routing1 = 'first_routing'; |
|
242
|
|
|
$routing2 = 'second_routing'; |
|
243
|
|
|
|
|
244
|
|
|
$doc = new Document(1, ['name' => 'ruflin nicolas']); |
|
245
|
|
|
$doc->setRouting($routing1); |
|
246
|
|
|
$index->addDocument($doc); |
|
247
|
|
|
|
|
248
|
|
|
$doc = new Document(2, ['name' => 'ruflin']); |
|
249
|
|
|
$doc->setRouting($routing1); |
|
250
|
|
|
$index->addDocument($doc); |
|
251
|
|
|
|
|
252
|
|
|
$doc = new Document(2, ['name' => 'ruflin']); |
|
253
|
|
|
$doc->setRouting($routing1); |
|
254
|
|
|
$index->addDocument($doc); |
|
255
|
|
|
|
|
256
|
|
|
$index->refresh(); |
|
257
|
|
|
|
|
258
|
|
|
$response = $index->search('ruflin*'); |
|
259
|
|
|
$this->assertEquals(2, $response->count()); |
|
260
|
|
|
|
|
261
|
|
|
$response = $index->search('ruflin*', ['routing' => $routing2]); |
|
262
|
|
|
$this->assertEquals(0, $response->count()); |
|
263
|
|
|
|
|
264
|
|
|
$response = $index->search('nicolas'); |
|
265
|
|
|
$this->assertEquals(1, $response->count()); |
|
266
|
|
|
|
|
267
|
|
|
// Route to the wrong document id; should not delete |
|
268
|
|
|
$response = $index->deleteByQuery(new SimpleQueryString('nicolas'), ['routing' => $routing2]); |
|
269
|
|
|
$this->assertTrue($response->isOk()); |
|
270
|
|
|
|
|
271
|
|
|
$index->refresh(); |
|
272
|
|
|
|
|
273
|
|
|
$response = $index->search('ruflin*'); |
|
274
|
|
|
$this->assertEquals(2, $response->count()); |
|
275
|
|
|
|
|
276
|
|
|
$response = $index->search('nicolas'); |
|
277
|
|
|
$this->assertEquals(1, $response->count()); |
|
278
|
|
|
|
|
279
|
|
|
// Delete first document |
|
280
|
|
|
$response = $index->deleteByQuery(new SimpleQueryString('nicolas'), ['routing' => $routing1]); |
|
281
|
|
|
$this->assertTrue($response->isOk()); |
|
282
|
|
|
|
|
283
|
|
|
$index->refresh(); |
|
284
|
|
|
|
|
285
|
|
|
// Makes sure, document is deleted |
|
286
|
|
|
$response = $index->search('ruflin*'); |
|
287
|
|
|
$this->assertEquals(1, $response->count()); |
|
288
|
|
|
|
|
289
|
|
|
$response = $index->search('nicolas'); |
|
290
|
|
|
$this->assertEquals(0, $response->count()); |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
View Code Duplication |
public function testUpdateByQueryWithQueryString(): void |
|
|
|
|
|
|
294
|
|
|
{ |
|
295
|
|
|
$index = $this->_createIndex(); |
|
296
|
|
|
$index->addDocuments([ |
|
297
|
|
|
new Document(1, ['name' => 'ruflin nicolas']), |
|
298
|
|
|
new Document(2, ['name' => 'ruflin']), |
|
299
|
|
|
]); |
|
300
|
|
|
$index->refresh(); |
|
301
|
|
|
|
|
302
|
|
|
$response = $index->search('ruflin*'); |
|
303
|
|
|
$this->assertEquals(2, $response->count()); |
|
304
|
|
|
|
|
305
|
|
|
$response = $index->search('nicolas'); |
|
306
|
|
|
$this->assertEquals(1, $response->count()); |
|
307
|
|
|
|
|
308
|
|
|
// Update the element, searched by specific word. Should match first one |
|
309
|
|
|
$response = $index->updateByQuery('nicolas', new Script('ctx._source.name = "marc"')); |
|
310
|
|
|
$this->assertTrue($response->isOk()); |
|
311
|
|
|
|
|
312
|
|
|
$index->refresh(); |
|
313
|
|
|
|
|
314
|
|
|
// Makes sure first element is updated and renamed to marc. Should match only second |
|
315
|
|
|
$response = $index->search('ruflin*'); |
|
316
|
|
|
$this->assertEquals(1, $response->count()); |
|
317
|
|
|
|
|
318
|
|
|
$response = $index->search('marc*'); |
|
319
|
|
|
$this->assertEquals(1, $response->count()); |
|
320
|
|
|
|
|
321
|
|
|
$response = $index->search('nicolas'); |
|
322
|
|
|
$this->assertEquals(0, $response->count()); |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
View Code Duplication |
public function testUpdateByQueryAll(): void |
|
|
|
|
|
|
326
|
|
|
{ |
|
327
|
|
|
$index = $this->_createIndex(); |
|
328
|
|
|
$index->addDocuments([ |
|
329
|
|
|
new Document(1, ['name' => 'ruflin nicolas']), |
|
330
|
|
|
new Document(2, ['name' => 'ruflin']), |
|
331
|
|
|
]); |
|
332
|
|
|
$index->refresh(); |
|
333
|
|
|
|
|
334
|
|
|
$response = $index->search('ruflin*'); |
|
335
|
|
|
$this->assertEquals(2, $response->count()); |
|
336
|
|
|
|
|
337
|
|
|
$response = $index->search('nicolas'); |
|
338
|
|
|
$this->assertEquals(1, $response->count()); |
|
339
|
|
|
|
|
340
|
|
|
// Update all elements to name "marc" |
|
341
|
|
|
$response = $index->updateByQuery('*', new Script('ctx._source.name = "marc"')); |
|
342
|
|
|
$this->assertTrue($response->isOk()); |
|
343
|
|
|
|
|
344
|
|
|
$index->refresh(); |
|
345
|
|
|
|
|
346
|
|
|
// Because all documents have changed to marc, searching by "ruflin*" should match 0 |
|
347
|
|
|
$response = $index->search('ruflin*'); |
|
348
|
|
|
$this->assertEquals(0, $response->count()); |
|
349
|
|
|
|
|
350
|
|
|
$response = $index->search('marc'); |
|
351
|
|
|
$this->assertEquals(2, $response->count()); |
|
352
|
|
|
|
|
353
|
|
|
$response = $index->search('nicolas'); |
|
354
|
|
|
$this->assertEquals(0, $response->count()); |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
public function testDeleteIndexDeleteAlias(): void |
|
358
|
|
|
{ |
|
359
|
|
|
$indexName = 'test'; |
|
360
|
|
|
$aliasName = 'test-aliase'; |
|
361
|
|
|
|
|
362
|
|
|
$client = $this->_getClient(); |
|
363
|
|
|
$index = $client->getIndex($indexName); |
|
364
|
|
|
|
|
365
|
|
|
$index->create([], true); |
|
366
|
|
|
$index->addAlias($aliasName); |
|
367
|
|
|
|
|
368
|
|
|
$status = new Status($client); |
|
369
|
|
|
$this->assertTrue($status->indexExists($indexName)); |
|
370
|
|
|
$this->assertTrue($status->aliasExists($aliasName)); |
|
371
|
|
|
|
|
372
|
|
|
// Deleting index should also remove alias |
|
373
|
|
|
$index->delete(); |
|
374
|
|
|
|
|
375
|
|
|
$status->refresh(); |
|
376
|
|
|
$this->assertFalse($status->indexExists($indexName)); |
|
377
|
|
|
$this->assertFalse($status->aliasExists($aliasName)); |
|
378
|
|
|
} |
|
379
|
|
|
|
|
380
|
|
View Code Duplication |
public function testAddAliasTwoIndices(): void |
|
|
|
|
|
|
381
|
|
|
{ |
|
382
|
|
|
$indexName1 = 'test1'; |
|
383
|
|
|
$aliasName = 'test-alias'; |
|
384
|
|
|
|
|
385
|
|
|
$client = $this->_getClient(); |
|
386
|
|
|
$index1 = $client->getIndex($indexName1); |
|
387
|
|
|
|
|
388
|
|
|
$index1->create([], true); |
|
389
|
|
|
$this->_waitForAllocation($index1); |
|
390
|
|
|
$index1->addAlias($aliasName); |
|
391
|
|
|
|
|
392
|
|
|
$index1->refresh(); |
|
393
|
|
|
$index1->forcemerge(); |
|
394
|
|
|
|
|
395
|
|
|
$status = new Status($client); |
|
396
|
|
|
|
|
397
|
|
|
$this->assertTrue($status->indexExists($indexName1)); |
|
398
|
|
|
|
|
399
|
|
|
$this->assertTrue($status->aliasExists($aliasName)); |
|
400
|
|
|
$this->assertTrue($index1->hasAlias($aliasName)); |
|
401
|
|
|
} |
|
402
|
|
|
|
|
403
|
|
View Code Duplication |
public function testReplaceAlias(): void |
|
|
|
|
|
|
404
|
|
|
{ |
|
405
|
|
|
$indexName1 = 'test1'; |
|
406
|
|
|
$aliasName = 'test-alias'; |
|
407
|
|
|
|
|
408
|
|
|
$client = $this->_getClient(); |
|
409
|
|
|
$index1 = $client->getIndex($indexName1); |
|
410
|
|
|
|
|
411
|
|
|
$index1->create([], true); |
|
412
|
|
|
$index1->addAlias($aliasName); |
|
413
|
|
|
|
|
414
|
|
|
$index1->refresh(); |
|
415
|
|
|
|
|
416
|
|
|
$status = new Status($client); |
|
417
|
|
|
|
|
418
|
|
|
$this->assertTrue($status->indexExists($indexName1)); |
|
419
|
|
|
$this->assertTrue($status->aliasExists($aliasName)); |
|
420
|
|
|
$this->assertTrue($index1->hasAlias($aliasName)); |
|
421
|
|
|
} |
|
422
|
|
|
|
|
423
|
|
|
public function testAddDocumentVersion(): void |
|
424
|
|
|
{ |
|
425
|
|
|
$client = $this->_getClient(); |
|
426
|
|
|
$index = $client->getIndex('test'); |
|
427
|
|
|
$index->create([], true); |
|
428
|
|
|
|
|
429
|
|
|
$doc1 = new Document(1); |
|
430
|
|
|
$doc1->set('title', 'Hello world'); |
|
431
|
|
|
|
|
432
|
|
|
$return = $index->addDocument($doc1); |
|
433
|
|
|
$data = $return->getData(); |
|
434
|
|
|
$this->assertEquals(1, $data['_version']); |
|
435
|
|
|
|
|
436
|
|
|
$return = $index->addDocument($doc1); |
|
437
|
|
|
$data = $return->getData(); |
|
438
|
|
|
$this->assertEquals(2, $data['_version']); |
|
439
|
|
|
} |
|
440
|
|
|
|
|
441
|
|
|
public function testClearCache(): void |
|
442
|
|
|
{ |
|
443
|
|
|
$index = $this->_createIndex(); |
|
444
|
|
|
$response = $index->clearCache(); |
|
445
|
|
|
$this->assertFalse($response->hasError()); |
|
446
|
|
|
} |
|
447
|
|
|
|
|
448
|
|
|
public function testFlush(): void |
|
449
|
|
|
{ |
|
450
|
|
|
$index = $this->_createIndex(); |
|
451
|
|
|
$response = $index->flush(); |
|
452
|
|
|
$this->assertFalse($response->hasError()); |
|
453
|
|
|
} |
|
454
|
|
|
|
|
455
|
|
|
public function testExists(): void |
|
456
|
|
|
{ |
|
457
|
|
|
$index = $this->_createIndex(); |
|
458
|
|
|
|
|
459
|
|
|
$this->assertTrue($index->exists()); |
|
460
|
|
|
|
|
461
|
|
|
$index->delete(); |
|
462
|
|
|
|
|
463
|
|
|
$this->assertFalse($index->exists()); |
|
464
|
|
|
} |
|
465
|
|
|
|
|
466
|
|
|
/** |
|
467
|
|
|
* Test $index->delete() return value for unknown index. |
|
468
|
|
|
* |
|
469
|
|
|
* Tests if deleting an index that does not exist in Elasticsearch, |
|
470
|
|
|
* correctly returns a boolean true from the hasError() method of |
|
471
|
|
|
* the \Elastica\Response object |
|
472
|
|
|
*/ |
|
473
|
|
View Code Duplication |
public function testDeleteMissingIndexHasError(): void |
|
|
|
|
|
|
474
|
|
|
{ |
|
475
|
|
|
$client = $this->_getClient(); |
|
476
|
|
|
$index = $client->getIndex('index_does_not_exist'); |
|
477
|
|
|
|
|
478
|
|
|
try { |
|
479
|
|
|
$index->delete(); |
|
480
|
|
|
$this->fail('This should never be reached. Deleting an unknown index will throw an exception'); |
|
481
|
|
|
} catch (ResponseException $error) { |
|
482
|
|
|
$response = $error->getResponse(); |
|
483
|
|
|
$this->assertTrue($response->hasError()); |
|
484
|
|
|
$request = $error->getRequest(); |
|
485
|
|
|
$this->assertInstanceOf(Request::class, $request); |
|
486
|
|
|
} |
|
487
|
|
|
} |
|
488
|
|
|
|
|
489
|
|
|
/** |
|
490
|
|
|
* Tests to see if the test type mapping exists when calling $index->getMapping(). |
|
491
|
|
|
*/ |
|
492
|
|
|
public function testIndexGetMapping(): void |
|
493
|
|
|
{ |
|
494
|
|
|
$index = $this->_createIndex(); |
|
495
|
|
|
$mappings = new Mapping([ |
|
496
|
|
|
'id' => ['type' => 'integer', 'store' => true], |
|
497
|
|
|
'email' => ['type' => 'text'], |
|
498
|
|
|
'username' => ['type' => 'text'], |
|
499
|
|
|
'test' => ['type' => 'integer'], |
|
500
|
|
|
]); |
|
501
|
|
|
|
|
502
|
|
|
$index->setMapping($mappings); |
|
503
|
|
|
$index->refresh(); |
|
504
|
|
|
$indexMappings = $index->getMapping(); |
|
505
|
|
|
|
|
506
|
|
|
$this->assertEquals($indexMappings['properties']['id']['type'], 'integer'); |
|
507
|
|
|
$this->assertEquals($indexMappings['properties']['id']['store'], true); |
|
508
|
|
|
$this->assertEquals($indexMappings['properties']['email']['type'], 'text'); |
|
509
|
|
|
$this->assertEquals($indexMappings['properties']['username']['type'], 'text'); |
|
510
|
|
|
$this->assertEquals($indexMappings['properties']['test']['type'], 'integer'); |
|
511
|
|
|
} |
|
512
|
|
|
|
|
513
|
|
|
public function testEmptyIndexGetMapping(): void |
|
514
|
|
|
{ |
|
515
|
|
|
$indexMappings = $this->_createIndex()->getMapping(); |
|
516
|
|
|
|
|
517
|
|
|
$this->assertEmpty($indexMappings); |
|
518
|
|
|
} |
|
519
|
|
|
|
|
520
|
|
|
/** |
|
521
|
|
|
* Test to see if search Default Limit works. |
|
522
|
|
|
*/ |
|
523
|
|
|
public function testLimitDefaultIndex(): void |
|
524
|
|
|
{ |
|
525
|
|
|
$client = $this->_getClient(); |
|
526
|
|
|
$index = $client->getIndex('zero'); |
|
527
|
|
|
$index->create(['settings' => ['index' => ['number_of_shards' => 1, 'number_of_replicas' => 0]]]); |
|
528
|
|
|
|
|
529
|
|
|
$docs = [ |
|
530
|
|
|
new Document(1, ['id' => 1, 'email' => '[email protected]', 'username' => 'farrelley']), |
|
531
|
|
|
new Document(2, ['id' => 1, 'email' => '[email protected]', 'username' => 'farrelley']), |
|
532
|
|
|
new Document(3, ['id' => 1, 'email' => '[email protected]', 'username' => 'farrelley']), |
|
533
|
|
|
new Document(4, ['id' => 1, 'email' => '[email protected]', 'username' => 'farrelley']), |
|
534
|
|
|
new Document(5, ['id' => 1, 'email' => '[email protected]', 'username' => 'farrelley']), |
|
535
|
|
|
new Document(6, ['id' => 1, 'email' => '[email protected]', 'username' => 'farrelley']), |
|
536
|
|
|
new Document(7, ['id' => 1, 'email' => '[email protected]', 'username' => 'farrelley']), |
|
537
|
|
|
new Document(8, ['id' => 1, 'email' => '[email protected]', 'username' => 'farrelley']), |
|
538
|
|
|
new Document(9, ['id' => 1, 'email' => '[email protected]', 'username' => 'farrelley']), |
|
539
|
|
|
new Document(10, ['id' => 1, 'email' => '[email protected]', 'username' => 'farrelley']), |
|
540
|
|
|
new Document(11, ['id' => 1, 'email' => '[email protected]', 'username' => 'farrelley']), |
|
541
|
|
|
]; |
|
542
|
|
|
$index->addDocuments($docs); |
|
543
|
|
|
$index->refresh(); |
|
544
|
|
|
|
|
545
|
|
|
// default limit results (default limit is 10) |
|
546
|
|
|
$resultSet = $index->search('farrelley'); |
|
547
|
|
|
$this->assertEquals(10, $resultSet->count()); |
|
548
|
|
|
|
|
549
|
|
|
// limit = 1 |
|
550
|
|
|
$resultSet = $index->search('farrelley', 1); |
|
551
|
|
|
$this->assertEquals(1, $resultSet->count()); |
|
552
|
|
|
} |
|
553
|
|
|
|
|
554
|
|
|
public function testCreate(): void |
|
555
|
|
|
{ |
|
556
|
|
|
$client = $this->_getClient(); |
|
557
|
|
|
$indexName = 'test'; |
|
558
|
|
|
|
|
559
|
|
|
//Testing recreate (backward compatibility) |
|
560
|
|
|
$index = $client->getIndex($indexName); |
|
561
|
|
|
$index->create([], true); |
|
562
|
|
|
$this->_waitForAllocation($index); |
|
563
|
|
|
$status = new Status($client); |
|
564
|
|
|
$this->assertTrue($status->indexExists($indexName)); |
|
565
|
|
|
|
|
566
|
|
|
//Testing create index with array options |
|
567
|
|
|
$opts = ['recreate' => true]; |
|
568
|
|
|
$index->create([], $opts); |
|
569
|
|
|
$this->_waitForAllocation($index); |
|
570
|
|
|
$status = new Status($client); |
|
571
|
|
|
$this->assertTrue($status->indexExists($indexName)); |
|
572
|
|
|
} |
|
573
|
|
|
|
|
574
|
|
|
/** |
|
575
|
|
|
* @group unit |
|
576
|
|
|
*/ |
|
577
|
|
|
public function testCreateWithInvalidOption(): void |
|
578
|
|
|
{ |
|
579
|
|
|
$this->expectException(InvalidException::class); |
|
580
|
|
|
|
|
581
|
|
|
$client = $this->_getClient(); |
|
582
|
|
|
$indexName = 'test'; |
|
583
|
|
|
$index = $client->getIndex($indexName); |
|
584
|
|
|
|
|
585
|
|
|
$opts = ['testing_invalid_option' => true]; |
|
586
|
|
|
$index->create([], $opts); |
|
587
|
|
|
} |
|
588
|
|
|
|
|
589
|
|
|
public function testCreateSearch(): void |
|
590
|
|
|
{ |
|
591
|
|
|
$client = $this->_getClient(); |
|
592
|
|
|
$index = new Index($client, 'test'); |
|
593
|
|
|
|
|
594
|
|
|
$query = new QueryString('test'); |
|
595
|
|
|
$options = 5; |
|
596
|
|
|
|
|
597
|
|
|
$search = $index->createSearch($query, $options); |
|
|
|
|
|
|
598
|
|
|
|
|
599
|
|
|
$expected = [ |
|
600
|
|
|
'query' => [ |
|
601
|
|
|
'query_string' => [ |
|
602
|
|
|
'query' => 'test', |
|
603
|
|
|
], |
|
604
|
|
|
], |
|
605
|
|
|
'size' => 5, |
|
606
|
|
|
]; |
|
607
|
|
|
$this->assertEquals($expected, $search->getQuery()->toArray()); |
|
608
|
|
|
$this->assertEquals(['test'], $search->getIndices()); |
|
609
|
|
|
$this->assertTrue($search->hasIndices()); |
|
610
|
|
|
$this->assertTrue($search->hasIndex('test')); |
|
611
|
|
|
$this->assertTrue($search->hasIndex($index)); |
|
612
|
|
|
} |
|
613
|
|
|
|
|
614
|
|
|
public function testSearch(): void |
|
615
|
|
|
{ |
|
616
|
|
|
$index = $this->_createIndex(); |
|
617
|
|
|
|
|
618
|
|
|
$docs = []; |
|
619
|
|
|
$docs[] = new Document(1, ['username' => 'hans', 'test' => ['2', '3', '5']]); |
|
620
|
|
|
$docs[] = new Document(2, ['username' => 'john', 'test' => ['1', '3', '6']]); |
|
621
|
|
|
$docs[] = new Document(3, ['username' => 'rolf', 'test' => ['2', '3', '7']]); |
|
622
|
|
|
$index->addDocuments($docs); |
|
623
|
|
|
$index->refresh(); |
|
624
|
|
|
|
|
625
|
|
|
$resultSet = $index->search('rolf'); |
|
626
|
|
|
$this->assertEquals(1, $resultSet->count()); |
|
627
|
|
|
|
|
628
|
|
|
$count = $index->count('rolf'); |
|
629
|
|
|
$this->assertEquals(1, $count); |
|
630
|
|
|
|
|
631
|
|
|
// Test if source is returned |
|
632
|
|
|
$result = $resultSet->current(); |
|
633
|
|
|
$this->assertEquals(3, $result->getId()); |
|
634
|
|
|
$data = $result->getData(); |
|
635
|
|
|
$this->assertEquals('rolf', $data['username']); |
|
636
|
|
|
|
|
637
|
|
|
$count = $index->count(); |
|
638
|
|
|
$this->assertEquals(3, $count); |
|
639
|
|
|
} |
|
640
|
|
|
|
|
641
|
|
|
public function testSearchGet(): void |
|
642
|
|
|
{ |
|
643
|
|
|
$index = $this->_createIndex(); |
|
644
|
|
|
$docs = []; |
|
645
|
|
|
$docs[] = new Document(1, ['username' => 'hans']); |
|
646
|
|
|
$index->addDocuments($docs); |
|
647
|
|
|
$index->refresh(); |
|
648
|
|
|
|
|
649
|
|
|
$resultSet = $index->search('hans', null, Request::GET); |
|
650
|
|
|
$this->assertEquals(1, $resultSet->count()); |
|
651
|
|
|
|
|
652
|
|
|
$count = $index->count('hans', Request::GET); |
|
653
|
|
|
$this->assertEquals(1, $count); |
|
654
|
|
|
} |
|
655
|
|
|
|
|
656
|
|
|
public function testForcemerge(): void |
|
657
|
|
|
{ |
|
658
|
|
|
$index = $this->_createIndex('testforcemerge_indextest', false, 3); |
|
659
|
|
|
|
|
660
|
|
|
$docs = []; |
|
661
|
|
|
$docs[] = new Document(1, ['foo' => 'bar']); |
|
662
|
|
|
$docs[] = new Document(2, ['foo' => 'bar']); |
|
663
|
|
|
$index->addDocuments($docs); |
|
664
|
|
|
$index->refresh(); |
|
665
|
|
|
|
|
666
|
|
|
$stats = $index->getStats()->getData(); |
|
667
|
|
|
$this->assertEquals(2, $stats['_all']['primaries']['docs']['count']); |
|
668
|
|
|
$this->assertEquals(0, $stats['_all']['primaries']['docs']['deleted']); |
|
669
|
|
|
|
|
670
|
|
|
$index->deleteById('1'); |
|
671
|
|
|
$index->refresh(); |
|
672
|
|
|
|
|
673
|
|
|
$stats = $index->getStats()->getData(); |
|
674
|
|
|
$this->assertEquals(1, $stats['_all']['primaries']['docs']['count']); |
|
675
|
|
|
|
|
676
|
|
|
$index->forcemerge(['max_num_segments' => 1]); |
|
677
|
|
|
|
|
678
|
|
|
$stats = $index->getStats()->getData(); |
|
679
|
|
|
$this->assertEquals(1, $stats['_all']['primaries']['docs']['count']); |
|
680
|
|
|
$this->assertEquals(0, $stats['_all']['primaries']['docs']['deleted']); |
|
681
|
|
|
} |
|
682
|
|
|
|
|
683
|
|
|
public function testAnalyze(): void |
|
684
|
|
|
{ |
|
685
|
|
|
$index = $this->_createIndex(); |
|
686
|
|
|
$index->refresh(); |
|
687
|
|
|
$returnedTokens = $index->analyze(['text' => 'foo']); |
|
688
|
|
|
|
|
689
|
|
|
$tokens = [ |
|
690
|
|
|
[ |
|
691
|
|
|
'token' => 'foo', |
|
692
|
|
|
'start_offset' => 0, |
|
693
|
|
|
'end_offset' => 3, |
|
694
|
|
|
'type' => '<ALPHANUM>', |
|
695
|
|
|
'position' => 0, |
|
696
|
|
|
], |
|
697
|
|
|
]; |
|
698
|
|
|
|
|
699
|
|
|
$this->assertEquals($tokens, $returnedTokens); |
|
700
|
|
|
} |
|
701
|
|
|
|
|
702
|
|
|
public function testRequestEndpoint(): void |
|
703
|
|
|
{ |
|
704
|
|
|
$index = $this->_createIndex(); |
|
705
|
|
|
$index->refresh(); |
|
706
|
|
|
$endpoint = new Analyze(); |
|
707
|
|
|
$endpoint->setIndex('fooIndex'); |
|
708
|
|
|
$endpoint->setBody(['text' => 'foo']); |
|
709
|
|
|
$returnedTokens = $index->requestEndpoint($endpoint)->getData()['tokens']; |
|
710
|
|
|
|
|
711
|
|
|
$tokens = [ |
|
712
|
|
|
[ |
|
713
|
|
|
'token' => 'foo', |
|
714
|
|
|
'start_offset' => 0, |
|
715
|
|
|
'end_offset' => 3, |
|
716
|
|
|
'type' => '<ALPHANUM>', |
|
717
|
|
|
'position' => 0, |
|
718
|
|
|
], |
|
719
|
|
|
]; |
|
720
|
|
|
|
|
721
|
|
|
$this->assertEquals($tokens, $returnedTokens); |
|
722
|
|
|
} |
|
723
|
|
|
|
|
724
|
|
|
public function testAnalyzeExplain(): void |
|
725
|
|
|
{ |
|
726
|
|
|
$index = $this->_createIndex(); |
|
727
|
|
|
$index->refresh(); |
|
728
|
|
|
$data = $index->analyze(['text' => 'foo', 'explain' => true], []); |
|
729
|
|
|
|
|
730
|
|
|
$this->assertArrayHasKey('custom_analyzer', $data); |
|
731
|
|
|
} |
|
732
|
|
|
|
|
733
|
|
|
public function testGetEmptyAliases(): void |
|
734
|
|
|
{ |
|
735
|
|
|
$indexName = 'test-getaliases'; |
|
736
|
|
|
|
|
737
|
|
|
$client = $this->_getClient(); |
|
738
|
|
|
$index = $client->getIndex($indexName); |
|
739
|
|
|
|
|
740
|
|
|
$index->create([], true); |
|
741
|
|
|
$this->_waitForAllocation($index); |
|
742
|
|
|
$index->refresh(); |
|
743
|
|
|
$index->forcemerge(); |
|
744
|
|
|
|
|
745
|
|
|
$this->assertEquals([], $index->getAliases()); |
|
746
|
|
|
} |
|
747
|
|
|
} |
|
748
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.