forked from webgriffe/amp-elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientTest.php
299 lines (260 loc) · 11.2 KB
/
ClientTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
declare(strict_types=1);
namespace Webgriffe\AmpElasticsearch\Tests\Integration;
use Webgriffe\AmpElasticsearch\Client;
use Webgriffe\AmpElasticsearch\Error;
use Amp\Promise;
use PHPUnit\Framework\TestCase;
class ClientTest extends TestCase
{
const TEST_INDEX = 'test_index';
const DEFAULT_ES_URL = 'http://127.0.0.1:9200';
/**
* @var Client
*/
private $client;
protected function setUp(): void
{
$esUrl = getenv('ES_URL') ?: self::DEFAULT_ES_URL;
$this->client = new Client($esUrl);
$indices = Promise\wait($this->client->catIndices());
foreach ($indices as $index) {
Promise\wait($this->client->deleteIndex($index['index']));
}
}
public function testCreateIndex(): void
{
$response = Promise\wait($this->client->createIndex(self::TEST_INDEX));
$this->assertIsArray($response);
$this->assertTrue($response['acknowledged']);
$this->assertEquals(self::TEST_INDEX, $response['index']);
}
public function testIndicesExistsShouldThrow404ErrorIfIndexDoesNotExists(): void
{
$this->expectException(Error::class);
$this->expectExceptionCode(404);
Promise\wait($this->client->existsIndex(self::TEST_INDEX));
}
public function testIndicesExistsShouldNotThrowAnErrorIfIndexExists(): void
{
Promise\wait($this->client->createIndex(self::TEST_INDEX));
$response = Promise\wait($this->client->existsIndex(self::TEST_INDEX));
$this->assertNull($response);
}
public function testDocumentsIndex(): void
{
$response = Promise\wait($this->client->indexDocument(self::TEST_INDEX, 'my_id', ['testField' => 'abc']));
$this->assertIsArray($response);
$this->assertEquals(self::TEST_INDEX, $response['_index']);
}
public function testDocumentsIndexWithAutomaticIdCreation(): void
{
$response = Promise\wait($this->client->indexDocument(self::TEST_INDEX, '', ['testField' => 'abc']));
$this->assertIsArray($response);
$this->assertEquals(self::TEST_INDEX, $response['_index']);
$this->assertEquals('created', $response['result']);
}
public function testDocumentsExistsShouldThrowA404ErrorIfDocumentDoesNotExists(): void
{
Promise\wait($this->client->createIndex(self::TEST_INDEX));
$this->expectException(Error::class);
$this->expectExceptionCode(404);
Promise\wait($this->client->existsDocument(self::TEST_INDEX, 'not-existent-doc'));
}
public function testDocumentsExistsShouldNotThrowAnErrorIfDocumentExists(): void
{
Promise\wait($this->client->indexDocument(self::TEST_INDEX, 'my_id', ['testField' => 'abc']));
$response = Promise\wait($this->client->existsDocument(self::TEST_INDEX, 'my_id'));
$this->assertNull($response);
}
public function testDocumentsGet(): void
{
Promise\wait($this->client->indexDocument(self::TEST_INDEX, 'my_id', ['testField' => 'abc']));
$response = Promise\wait($this->client->getDocument(self::TEST_INDEX, 'my_id'));
$this->assertIsArray($response);
$this->assertTrue($response['found']);
$this->assertEquals('my_id', $response['_id']);
$this->assertEquals('abc', $response['_source']['testField']);
}
public function testDocumentsGetWithOptions(): void
{
Promise\wait($this->client->indexDocument(self::TEST_INDEX, 'my_id', ['testField' => 'abc']));
$response = Promise\wait($this->client->getDocument(self::TEST_INDEX, 'my_id', ['_source' => 'false']));
$this->assertIsArray($response);
$this->assertTrue($response['found']);
$this->assertArrayNotHasKey('_source', $response);
}
public function testDocumentsGetWithOnlySource(): void
{
Promise\wait($this->client->indexDocument(self::TEST_INDEX, 'my_id', ['testField' => 'abc']));
$response = Promise\wait($this->client->getDocument(self::TEST_INDEX, 'my_id', [], '_source'));
$this->assertIsArray($response);
$this->assertEquals('abc', $response['testField']);
}
public function testDocumentsDelete(): void
{
Promise\wait($this->client->indexDocument(self::TEST_INDEX, 'my_id', ['testField' => 'abc']));
$response = Promise\wait($this->client->deleteDocument(self::TEST_INDEX, 'my_id'));
$this->assertIsArray($response);
$this->assertEquals('deleted', $response['result']);
}
public function testUriSearchOneIndex(): void
{
Promise\wait(
$this->client->indexDocument(self::TEST_INDEX, 'my_id', ['testField' => 'abc'], ['refresh' => 'true'])
);
$response = Promise\wait($this->client->uriSearchOneIndex(self::TEST_INDEX, 'testField:abc'));
$this->assertIsArray($response);
$this->assertCount(1, $response['hits']['hits']);
}
public function testUriSearchAllIndices(): void
{
Promise\wait(
$this->client->indexDocument(self::TEST_INDEX, 'my_id', ['testField' => 'abc'], ['refresh' => 'true'])
);
$response = Promise\wait($this->client->uriSearchAllIndices('testField:abc'));
$this->assertIsArray($response);
$this->assertCount(1, $response['hits']['hits']);
}
public function testUriSearchManyIndices(): void
{
Promise\wait(
$this->client->indexDocument(self::TEST_INDEX, 'my_id', ['testField' => 'abc'], ['refresh' => 'true'])
);
$response = Promise\wait($this->client->uriSearchManyIndices([self::TEST_INDEX], 'testField:abc'));
$this->assertIsArray($response);
$this->assertCount(1, $response['hits']['hits']);
}
public function testStatsIndexWithAllMetric(): void
{
Promise\wait(
$this->client->indexDocument(self::TEST_INDEX, 'my_id', ['testField' => 'abc'], ['refresh' => 'true'])
);
$response = Promise\wait($this->client->statsIndex(self::TEST_INDEX));
$this->assertEquals(1, $response['indices'][self::TEST_INDEX]['total']['indexing']['index_total']);
}
public function testStatsIndexWithDocsMetric(): void
{
Promise\wait(
$this->client->indexDocument(self::TEST_INDEX, 'my_id', ['testField' => 'abc'], ['refresh' => 'true'])
);
$response = Promise\wait($this->client->statsIndex(self::TEST_INDEX, 'docs'));
$this->assertArrayNotHasKey('indexing', $response['indices'][self::TEST_INDEX]['total']);
$this->assertEquals(1, $response['indices'][self::TEST_INDEX]['total']['docs']['count']);
}
public function testCatIndices(): void
{
Promise\wait(
$this->client->indexDocument(self::TEST_INDEX, 'my_id', ['testField' => 'abc'], ['refresh' => 'true'])
);
$response = Promise\wait($this->client->catIndices());
$this->assertCount(1, $response);
$this->assertEquals(self::TEST_INDEX, $response[0]['index']);
}
public function testCatIndicesWithoutIndices(): void
{
$response = Promise\wait($this->client->catIndices());
$this->assertCount(0, $response);
}
public function testCatIndicesWithSpecificIndex(): void
{
Promise\wait(
$this->client->indexDocument(self::TEST_INDEX, 'my_id', ['testField' => 'abc'], ['refresh' => 'true'])
);
Promise\wait(
$this->client->indexDocument('another_index', 'my_id', ['testField' => 'abc'], ['refresh' => 'true'])
);
$response = Promise\wait($this->client->catIndices(self::TEST_INDEX));
$this->assertCount(1, $response);
$this->assertEquals(self::TEST_INDEX, $response[0]['index']);
}
public function testCatHealth(): void
{
$response = Promise\wait($this->client->catHealth());
$this->assertCount(1, $response);
$this->assertArrayHasKey('status', $response[0]);
}
public function testRefreshOneIndex(): void
{
Promise\wait($this->client->createIndex(self::TEST_INDEX));
$response = Promise\wait($this->client->refresh(self::TEST_INDEX));
$this->assertCount(1, $response);
}
public function testRefreshManyIndices(): void
{
Promise\wait($this->client->createIndex('an_index'));
Promise\wait($this->client->createIndex('another_index'));
$response = Promise\wait($this->client->refresh('an_index,another_index'));
$this->assertCount(1, $response);
}
public function testRefreshAllIndices(): void
{
Promise\wait($this->client->createIndex(self::TEST_INDEX));
$response = Promise\wait($this->client->refresh());
$this->assertCount(1, $response);
}
public function testSearch(): void
{
Promise\wait($this->client->createIndex(self::TEST_INDEX));
Promise\wait(
$this->client->indexDocument(self::TEST_INDEX, 'document-id', ['uuid' => 'this-is-a-uuid', 'payload' => []], ['refresh' => 'true'])
);
$query = [
'term' => [
'uuid.keyword' => [
'value' => 'this-is-a-uuid'
]
]
];
$response = Promise\wait($this->client->search($query));
$this->assertIsArray($response);
$this->assertCount(1, $response['hits']['hits']);
}
public function testCount(): void
{
Promise\wait($this->client->createIndex(self::TEST_INDEX));
Promise\wait(
$this->client->indexDocument(self::TEST_INDEX, '', ['payload' => []], ['refresh' => 'true'])
);
Promise\wait(
$this->client->indexDocument(self::TEST_INDEX, '', ['payload' => []], ['refresh' => 'true'])
);
$response = Promise\wait($this->client->count(self::TEST_INDEX));
$this->assertIsArray($response);
$this->assertEquals(2, $response['count']);
}
public function testCountWithQuery(): void
{
Promise\wait($this->client->createIndex(self::TEST_INDEX));
Promise\wait(
$this->client->indexDocument(self::TEST_INDEX, '', ['user' => 'kimchy'], ['refresh' => 'true'])
);
Promise\wait(
$this->client->indexDocument(self::TEST_INDEX, '', ['user' => 'foo'], ['refresh' => 'true'])
);
$response = Promise\wait($this->client->count(self::TEST_INDEX, [], ['term' => ['user' => 'kimchy']]));
$this->assertIsArray($response);
$this->assertEquals(1, $response['count']);
}
public function testBulkIndex(): void
{
Promise\wait($this->client->createIndex(self::TEST_INDEX));
$body = [];
$responses = [];
for ($i = 1; $i <= 1234; $i++) {
$body[] = ['index' => ['_id' => '']];
$body[] = ['test' => 'bulk', 'my_field' => 'my_value_' . $i];
// Every 100 documents stop and send the bulk request
if ($i % 100 === 0) {
$responses = Promise\wait($this->client->bulk($body, self::TEST_INDEX));
$body = [];
unset($responses);
}
}
if (!empty($body)) {
$responses = Promise\wait($this->client->bulk($body, self::TEST_INDEX));
}
$this->assertIsArray($responses);
$this->assertCount(34, $responses['items']);
}
}