Skip to content

Commit 5fa0ed7

Browse files
authored
Merge pull request #7 from webgriffe/create-or-update-index-with-body
Allow to pass body to index create or update
2 parents ef73c49 + b50d4f6 commit 5fa0ed7

File tree

2 files changed

+59
-15
lines changed

2 files changed

+59
-15
lines changed

src/Client.php

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

31-
public function createIndex(string $index): Promise
31+
public function createOrUpdateIndex(string $index, array $body = null): Promise
3232
{
3333
$method = 'PUT';
3434
$uri = implode('/', [$this->baseUri, urlencode($index)]);
35-
return $this->doJsonRequest($method, $uri);
35+
return $this->doJsonRequest($method, $uri, $body);
36+
}
37+
38+
/**
39+
* @deprecated Use createOrUpdateIndex instead
40+
*/
41+
public function createIndex(string $index): Promise
42+
{
43+
return $this->createOrUpdateIndex($index);
3644
}
3745

3846
public function existsIndex(string $index): Promise
@@ -255,9 +263,15 @@ private function uriSearch(string $indexOrIndicesOrAll, string $query, array $op
255263
* @param string $method
256264
* @param string $uri
257265
* @return Promise
266+
*
267+
* @throws \JsonException
258268
*/
259-
private function doJsonRequest(string $method, string $uri): Promise
269+
private function doJsonRequest(string $method, string $uri, array $body = null): Promise
260270
{
261-
return $this->doRequest($this->createJsonRequest($method, $uri));
271+
$jsonBody = null;
272+
if ($body !== null) {
273+
$jsonBody = json_encode($body, JSON_THROW_ON_ERROR);
274+
}
275+
return $this->doRequest($this->createJsonRequest($method, $uri, $jsonBody));
262276
}
263277
}

tests/Integration/ClientTest.php

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,42 @@ protected function setUp(): void
3131

3232
public function testCreateIndex(): void
3333
{
34-
$response = Promise\wait($this->client->createIndex(self::TEST_INDEX));
34+
$response = Promise\wait($this->client->createOrUpdateIndex(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
41+
{
42+
$response = Promise\wait(
43+
$this->client->createOrUpdateIndex(
44+
self::TEST_INDEX,
45+
['mappings' => ['properties' => ['testField' => ['type' => 'text']]]]
46+
)
47+
);
48+
$this->assertIsArray($response);
49+
$this->assertTrue($response['acknowledged']);
50+
$this->assertEquals(self::TEST_INDEX, $response['index']);
51+
$response = Promise\wait($this->client->getIndex(self::TEST_INDEX));
52+
$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));
67+
$this->assertEquals(2000, $response[self::TEST_INDEX]['settings']['index']['mapping']['total_fields']['limit']);
68+
}
69+
4070
public function testIndicesExistsShouldThrow404ErrorIfIndexDoesNotExists(): void
4171
{
4272
$this->expectException(Error::class);
@@ -46,7 +76,7 @@ public function testIndicesExistsShouldThrow404ErrorIfIndexDoesNotExists(): void
4676

4777
public function testIndicesExistsShouldNotThrowAnErrorIfIndexExists(): void
4878
{
49-
Promise\wait($this->client->createIndex(self::TEST_INDEX));
79+
Promise\wait($this->client->createOrUpdateIndex(self::TEST_INDEX));
5080
$response = Promise\wait($this->client->existsIndex(self::TEST_INDEX));
5181
$this->assertNull($response);
5282
}
@@ -68,7 +98,7 @@ public function testDocumentsIndexWithAutomaticIdCreation(): void
6898

6999
public function testDocumentsExistsShouldThrowA404ErrorIfDocumentDoesNotExists(): void
70100
{
71-
Promise\wait($this->client->createIndex(self::TEST_INDEX));
101+
Promise\wait($this->client->createOrUpdateIndex(self::TEST_INDEX));
72102
$this->expectException(Error::class);
73103
$this->expectExceptionCode(404);
74104
Promise\wait($this->client->existsDocument(self::TEST_INDEX, 'not-existent-doc'));
@@ -203,29 +233,29 @@ public function testCatHealth(): void
203233

204234
public function testRefreshOneIndex(): void
205235
{
206-
Promise\wait($this->client->createIndex(self::TEST_INDEX));
236+
Promise\wait($this->client->createOrUpdateIndex(self::TEST_INDEX));
207237
$response = Promise\wait($this->client->refresh(self::TEST_INDEX));
208238
$this->assertCount(1, $response);
209239
}
210240

211241
public function testRefreshManyIndices(): void
212242
{
213-
Promise\wait($this->client->createIndex('an_index'));
214-
Promise\wait($this->client->createIndex('another_index'));
243+
Promise\wait($this->client->createOrUpdateIndex('an_index'));
244+
Promise\wait($this->client->createOrUpdateIndex('another_index'));
215245
$response = Promise\wait($this->client->refresh('an_index,another_index'));
216246
$this->assertCount(1, $response);
217247
}
218248

219249
public function testRefreshAllIndices(): void
220250
{
221-
Promise\wait($this->client->createIndex(self::TEST_INDEX));
251+
Promise\wait($this->client->createOrUpdateIndex(self::TEST_INDEX));
222252
$response = Promise\wait($this->client->refresh());
223253
$this->assertCount(1, $response);
224254
}
225255

226256
public function testSearch(): void
227257
{
228-
Promise\wait($this->client->createIndex(self::TEST_INDEX));
258+
Promise\wait($this->client->createOrUpdateIndex(self::TEST_INDEX));
229259
Promise\wait(
230260
$this->client->indexDocument(self::TEST_INDEX, 'document-id', ['uuid' => 'this-is-a-uuid', 'payload' => []], ['refresh' => 'true'])
231261
);
@@ -243,7 +273,7 @@ public function testSearch(): void
243273

244274
public function testCount(): void
245275
{
246-
Promise\wait($this->client->createIndex(self::TEST_INDEX));
276+
Promise\wait($this->client->createOrUpdateIndex(self::TEST_INDEX));
247277
Promise\wait(
248278
$this->client->indexDocument(self::TEST_INDEX, '', ['payload' => []], ['refresh' => 'true'])
249279
);
@@ -259,7 +289,7 @@ public function testCount(): void
259289

260290
public function testCountWithQuery(): void
261291
{
262-
Promise\wait($this->client->createIndex(self::TEST_INDEX));
292+
Promise\wait($this->client->createOrUpdateIndex(self::TEST_INDEX));
263293
Promise\wait(
264294
$this->client->indexDocument(self::TEST_INDEX, '', ['user' => 'kimchy'], ['refresh' => 'true'])
265295
);
@@ -275,7 +305,7 @@ public function testCountWithQuery(): void
275305

276306
public function testBulkIndex(): void
277307
{
278-
Promise\wait($this->client->createIndex(self::TEST_INDEX));
308+
Promise\wait($this->client->createOrUpdateIndex(self::TEST_INDEX));
279309
$body = [];
280310
$responses = [];
281311
for ($i = 1; $i <= 1234; $i++) {

0 commit comments

Comments
 (0)