Skip to content

Commit e3479df

Browse files
authored
Merge pull request #8 from webgriffe/index-create-or-update-fix
Add separate methods for updating aliases, mappings and settings on indices
2 parents 5fa0ed7 + 223012c commit e3479df

File tree

2 files changed

+112
-38
lines changed

2 files changed

+112
-38
lines changed

src/Client.php

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,13 @@ public function __construct(string $baseUri)
2828
$this->baseUri = rtrim($baseUri, '/');
2929
}
3030

31-
public function createOrUpdateIndex(string $index, array $body = null): Promise
31+
public function createIndex(string $index, array $body = null): Promise
3232
{
3333
$method = 'PUT';
3434
$uri = implode('/', [$this->baseUri, urlencode($index)]);
3535
return $this->doJsonRequest($method, $uri, $body);
3636
}
3737

38-
/**
39-
* @deprecated Use createOrUpdateIndex instead
40-
*/
41-
public function createIndex(string $index): Promise
42-
{
43-
return $this->createOrUpdateIndex($index);
44-
}
45-
4638
public function existsIndex(string $index): Promise
4739
{
4840
$method = 'HEAD';
@@ -217,6 +209,30 @@ public function bulk(array $body, string $index = null, array $options = []): Pr
217209
);
218210
}
219211

212+
public function createOrUpdateAlias(string $target, string $alias, ?array $body = null): Promise
213+
{
214+
$method = 'PUT';
215+
$uri = implode('/', [$this->baseUri, urlencode($target), '_aliases', urlencode($alias)]);
216+
217+
return $this->doJsonRequest($method, $uri, $body);
218+
}
219+
220+
public function updateIndexSettings(string $target, array $body = null): Promise
221+
{
222+
$method = 'PUT';
223+
$uri = implode('/', [$this->baseUri, urlencode($target), '_settings']);
224+
225+
return $this->doJsonRequest($method, $uri, $body);
226+
}
227+
228+
public function updateMappings(string $target, array $body = null): Promise
229+
{
230+
$method = 'PUT';
231+
$uri = implode('/', [$this->baseUri, urlencode($target), '_mappings']);
232+
233+
return $this->doJsonRequest($method, $uri, $body);
234+
}
235+
220236
private function createJsonRequest(string $method, string $uri, string $body = null): Request
221237
{
222238
$request = new Request($uri, $method);
@@ -270,7 +286,7 @@ private function doJsonRequest(string $method, string $uri, array $body = null):
270286
{
271287
$jsonBody = null;
272288
if ($body !== null) {
273-
$jsonBody = json_encode($body, JSON_THROW_ON_ERROR);
289+
$jsonBody = json_encode($body, JSON_THROW_ON_ERROR | JSON_FORCE_OBJECT);
274290
}
275291
return $this->doRequest($this->createJsonRequest($method, $uri, $jsonBody));
276292
}

tests/Integration/ClientTest.php

Lines changed: 86 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,40 +31,32 @@ protected function setUp(): void
3131

3232
public function testCreateIndex(): void
3333
{
34-
$response = Promise\wait($this->client->createOrUpdateIndex(self::TEST_INDEX));
34+
$response = Promise\wait($this->client->createIndex(self::TEST_INDEX));
3535
$this->assertIsArray($response);
3636
$this->assertTrue($response['acknowledged']);
3737
$this->assertEquals(self::TEST_INDEX, $response['index']);
3838
}
3939

40-
public function testCreateIndexWithExplicitMapping(): void
40+
public function testCreateIndexWithExplicitMappingSettingsAndAliases(): void
4141
{
4242
$response = Promise\wait(
43-
$this->client->createOrUpdateIndex(
43+
$this->client->createIndex(
4444
self::TEST_INDEX,
45-
['mappings' => ['properties' => ['testField' => ['type' => 'text']]]]
45+
[
46+
'mappings' => ['properties' => ['testField' => ['type' => 'text']]],
47+
'settings' => ['index' => ['mapping' => ['total_fields' => ['limit' => 2000]]]],
48+
'aliases' => ['alias1' => [], 'alias2' => ['filter' => ['term' => ['user' => 'kimchy']]]]
49+
]
4650
)
4751
);
4852
$this->assertIsArray($response);
4953
$this->assertTrue($response['acknowledged']);
5054
$this->assertEquals(self::TEST_INDEX, $response['index']);
5155
$response = Promise\wait($this->client->getIndex(self::TEST_INDEX));
5256
$this->assertEquals('text', $response[self::TEST_INDEX]['mappings']['properties']['testField']['type']);
53-
}
54-
55-
public function testCreateIndexWithExplicitSettings(): void
56-
{
57-
$response = Promise\wait(
58-
$this->client->createOrUpdateIndex(
59-
self::TEST_INDEX,
60-
['settings' => ['index' => ['mapping' => ['total_fields' => ['limit' => 2000]]]]]
61-
)
62-
);
63-
$this->assertIsArray($response);
64-
$this->assertTrue($response['acknowledged']);
65-
$this->assertEquals(self::TEST_INDEX, $response['index']);
66-
$response = Promise\wait($this->client->getIndex(self::TEST_INDEX));
6757
$this->assertEquals(2000, $response[self::TEST_INDEX]['settings']['index']['mapping']['total_fields']['limit']);
58+
$this->assertCount(2, $response[self::TEST_INDEX]['aliases']);
59+
$this->assertEquals('kimchy', $response[self::TEST_INDEX]['aliases']['alias2']['filter']['term']['user']);
6860
}
6961

7062
public function testIndicesExistsShouldThrow404ErrorIfIndexDoesNotExists(): void
@@ -76,7 +68,7 @@ public function testIndicesExistsShouldThrow404ErrorIfIndexDoesNotExists(): void
7668

7769
public function testIndicesExistsShouldNotThrowAnErrorIfIndexExists(): void
7870
{
79-
Promise\wait($this->client->createOrUpdateIndex(self::TEST_INDEX));
71+
Promise\wait($this->client->createIndex(self::TEST_INDEX));
8072
$response = Promise\wait($this->client->existsIndex(self::TEST_INDEX));
8173
$this->assertNull($response);
8274
}
@@ -98,7 +90,7 @@ public function testDocumentsIndexWithAutomaticIdCreation(): void
9890

9991
public function testDocumentsExistsShouldThrowA404ErrorIfDocumentDoesNotExists(): void
10092
{
101-
Promise\wait($this->client->createOrUpdateIndex(self::TEST_INDEX));
93+
Promise\wait($this->client->createIndex(self::TEST_INDEX));
10294
$this->expectException(Error::class);
10395
$this->expectExceptionCode(404);
10496
Promise\wait($this->client->existsDocument(self::TEST_INDEX, 'not-existent-doc'));
@@ -233,29 +225,29 @@ public function testCatHealth(): void
233225

234226
public function testRefreshOneIndex(): void
235227
{
236-
Promise\wait($this->client->createOrUpdateIndex(self::TEST_INDEX));
228+
Promise\wait($this->client->createIndex(self::TEST_INDEX));
237229
$response = Promise\wait($this->client->refresh(self::TEST_INDEX));
238230
$this->assertCount(1, $response);
239231
}
240232

241233
public function testRefreshManyIndices(): void
242234
{
243-
Promise\wait($this->client->createOrUpdateIndex('an_index'));
244-
Promise\wait($this->client->createOrUpdateIndex('another_index'));
235+
Promise\wait($this->client->createIndex('an_index'));
236+
Promise\wait($this->client->createIndex('another_index'));
245237
$response = Promise\wait($this->client->refresh('an_index,another_index'));
246238
$this->assertCount(1, $response);
247239
}
248240

249241
public function testRefreshAllIndices(): void
250242
{
251-
Promise\wait($this->client->createOrUpdateIndex(self::TEST_INDEX));
243+
Promise\wait($this->client->createIndex(self::TEST_INDEX));
252244
$response = Promise\wait($this->client->refresh());
253245
$this->assertCount(1, $response);
254246
}
255247

256248
public function testSearch(): void
257249
{
258-
Promise\wait($this->client->createOrUpdateIndex(self::TEST_INDEX));
250+
Promise\wait($this->client->createIndex(self::TEST_INDEX));
259251
Promise\wait(
260252
$this->client->indexDocument(self::TEST_INDEX, 'document-id', ['uuid' => 'this-is-a-uuid', 'payload' => []], ['refresh' => 'true'])
261253
);
@@ -273,7 +265,7 @@ public function testSearch(): void
273265

274266
public function testCount(): void
275267
{
276-
Promise\wait($this->client->createOrUpdateIndex(self::TEST_INDEX));
268+
Promise\wait($this->client->createIndex(self::TEST_INDEX));
277269
Promise\wait(
278270
$this->client->indexDocument(self::TEST_INDEX, '', ['payload' => []], ['refresh' => 'true'])
279271
);
@@ -289,7 +281,7 @@ public function testCount(): void
289281

290282
public function testCountWithQuery(): void
291283
{
292-
Promise\wait($this->client->createOrUpdateIndex(self::TEST_INDEX));
284+
Promise\wait($this->client->createIndex(self::TEST_INDEX));
293285
Promise\wait(
294286
$this->client->indexDocument(self::TEST_INDEX, '', ['user' => 'kimchy'], ['refresh' => 'true'])
295287
);
@@ -305,7 +297,7 @@ public function testCountWithQuery(): void
305297

306298
public function testBulkIndex(): void
307299
{
308-
Promise\wait($this->client->createOrUpdateIndex(self::TEST_INDEX));
300+
Promise\wait($this->client->createIndex(self::TEST_INDEX));
309301
$body = [];
310302
$responses = [];
311303
for ($i = 1; $i <= 1234; $i++) {
@@ -326,4 +318,70 @@ public function testBulkIndex(): void
326318
$this->assertIsArray($responses);
327319
$this->assertCount(34, $responses['items']);
328320
}
321+
322+
public function testCreateAlias(): void
323+
{
324+
Promise\wait($this->client->createIndex(self::TEST_INDEX));
325+
326+
$response = Promise\wait(
327+
$this->client->createOrUpdateAlias(
328+
self::TEST_INDEX,
329+
'alias',
330+
['filter' => ['term' => ['user' => 'kimchy']]]
331+
)
332+
);
333+
$this->assertIsArray($response);
334+
$this->assertTrue($response['acknowledged']);
335+
$response = Promise\wait($this->client->getIndex(self::TEST_INDEX));
336+
$this->assertEquals('kimchy', $response[self::TEST_INDEX]['aliases']['alias']['filter']['term']['user']);
337+
}
338+
339+
public function testUpdateAlias(): void
340+
{
341+
Promise\wait($this->client->createIndex(self::TEST_INDEX));
342+
$this->client->createOrUpdateAlias(self::TEST_INDEX, 'alias');
343+
344+
$response = Promise\wait(
345+
$this->client->createOrUpdateAlias(
346+
self::TEST_INDEX,
347+
'alias',
348+
['filter' => ['term' => ['user' => 'kimchy']]]
349+
)
350+
);
351+
$this->assertIsArray($response);
352+
$this->assertTrue($response['acknowledged']);
353+
$response = Promise\wait($this->client->getIndex(self::TEST_INDEX));
354+
$this->assertEquals('kimchy', $response[self::TEST_INDEX]['aliases']['alias']['filter']['term']['user']);
355+
}
356+
357+
public function testUpdateIndexSettings(): void
358+
{
359+
Promise\wait($this->client->createIndex(self::TEST_INDEX));
360+
361+
$response = Promise\wait(
362+
$this->client->updateIndexSettings(
363+
self::TEST_INDEX,
364+
['index' => ['mapping' => ['total_fields' => ['limit' => 2000]]]]
365+
)
366+
);
367+
368+
$this->assertIsArray($response);
369+
$this->assertTrue($response['acknowledged']);
370+
$response = Promise\wait($this->client->getIndex(self::TEST_INDEX));
371+
$this->assertEquals(2000, $response[self::TEST_INDEX]['settings']['index']['mapping']['total_fields']['limit']);
372+
}
373+
374+
public function testUpdateMappings(): void
375+
{
376+
Promise\wait($this->client->createIndex(self::TEST_INDEX));
377+
378+
$response = Promise\wait(
379+
$this->client->updateMappings(self::TEST_INDEX, ['properties' => ['testField' => ['type' => 'text']]])
380+
);
381+
382+
$this->assertIsArray($response);
383+
$this->assertTrue($response['acknowledged']);
384+
$response = Promise\wait($this->client->getIndex(self::TEST_INDEX));
385+
$this->assertEquals('text', $response[self::TEST_INDEX]['mappings']['properties']['testField']['type']);
386+
}
329387
}

0 commit comments

Comments
 (0)