diff --git a/src/Client.php b/src/Client.php index 54cdd9b..8f6fd52 100644 --- a/src/Client.php +++ b/src/Client.php @@ -28,11 +28,19 @@ public function __construct(string $baseUri) $this->baseUri = rtrim($baseUri, '/'); } - public function createIndex(string $index): Promise + public function createOrUpdateIndex(string $index, array $body = null): Promise { $method = 'PUT'; $uri = implode('/', [$this->baseUri, urlencode($index)]); - return $this->doJsonRequest($method, $uri); + return $this->doJsonRequest($method, $uri, $body); + } + + /** + * @deprecated Use createOrUpdateIndex instead + */ + public function createIndex(string $index, array $body = null): Promise + { + return $this->createOrUpdateIndex($index, $body); } public function existsIndex(string $index): Promise @@ -255,9 +263,15 @@ private function uriSearch(string $indexOrIndicesOrAll, string $query, array $op * @param string $method * @param string $uri * @return Promise + * + * @throws \JsonException */ - private function doJsonRequest(string $method, string $uri): Promise + private function doJsonRequest(string $method, string $uri, array $body = null): Promise { - return $this->doRequest($this->createJsonRequest($method, $uri)); + $jsonBody = null; + if ($body !== null) { + $jsonBody = json_encode($body, JSON_THROW_ON_ERROR); + } + return $this->doRequest($this->createJsonRequest($method, $uri, $jsonBody)); } } diff --git a/tests/Integration/ClientTest.php b/tests/Integration/ClientTest.php index 34825e9..6c590e0 100644 --- a/tests/Integration/ClientTest.php +++ b/tests/Integration/ClientTest.php @@ -31,12 +31,42 @@ protected function setUp(): void public function testCreateIndex(): void { - $response = Promise\wait($this->client->createIndex(self::TEST_INDEX)); + $response = Promise\wait($this->client->createOrUpdateIndex(self::TEST_INDEX)); $this->assertIsArray($response); $this->assertTrue($response['acknowledged']); $this->assertEquals(self::TEST_INDEX, $response['index']); } + public function testCreateIndexWithExplicitMapping(): void + { + $response = Promise\wait( + $this->client->createOrUpdateIndex( + self::TEST_INDEX, + ['mappings' => ['properties' => ['testField' => ['type' => 'text']]]] + ) + ); + $this->assertIsArray($response); + $this->assertTrue($response['acknowledged']); + $this->assertEquals(self::TEST_INDEX, $response['index']); + $response = Promise\wait($this->client->getIndex(self::TEST_INDEX)); + $this->assertEquals('text', $response[self::TEST_INDEX]['mappings']['properties']['testField']['type']); + } + + public function testCreateIndexWithExplicitSettings(): void + { + $response = Promise\wait( + $this->client->createOrUpdateIndex( + self::TEST_INDEX, + ['settings' => ['index' => ['mapping' => ['total_fields' => ['limit' => 2000]]]]] + ) + ); + $this->assertIsArray($response); + $this->assertTrue($response['acknowledged']); + $this->assertEquals(self::TEST_INDEX, $response['index']); + $response = Promise\wait($this->client->getIndex(self::TEST_INDEX)); + $this->assertEquals(2000, $response[self::TEST_INDEX]['settings']['index']['mapping']['total_fields']['limit']); + } + public function testIndicesExistsShouldThrow404ErrorIfIndexDoesNotExists(): void { $this->expectException(Error::class); @@ -46,7 +76,7 @@ public function testIndicesExistsShouldThrow404ErrorIfIndexDoesNotExists(): void public function testIndicesExistsShouldNotThrowAnErrorIfIndexExists(): void { - Promise\wait($this->client->createIndex(self::TEST_INDEX)); + Promise\wait($this->client->createOrUpdateIndex(self::TEST_INDEX)); $response = Promise\wait($this->client->existsIndex(self::TEST_INDEX)); $this->assertNull($response); } @@ -68,7 +98,7 @@ public function testDocumentsIndexWithAutomaticIdCreation(): void public function testDocumentsExistsShouldThrowA404ErrorIfDocumentDoesNotExists(): void { - Promise\wait($this->client->createIndex(self::TEST_INDEX)); + Promise\wait($this->client->createOrUpdateIndex(self::TEST_INDEX)); $this->expectException(Error::class); $this->expectExceptionCode(404); Promise\wait($this->client->existsDocument(self::TEST_INDEX, 'not-existent-doc')); @@ -203,29 +233,29 @@ public function testCatHealth(): void public function testRefreshOneIndex(): void { - Promise\wait($this->client->createIndex(self::TEST_INDEX)); + Promise\wait($this->client->createOrUpdateIndex(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')); + Promise\wait($this->client->createOrUpdateIndex('an_index')); + Promise\wait($this->client->createOrUpdateIndex('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)); + Promise\wait($this->client->createOrUpdateIndex(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->createOrUpdateIndex(self::TEST_INDEX)); Promise\wait( $this->client->indexDocument(self::TEST_INDEX, 'document-id', ['uuid' => 'this-is-a-uuid', 'payload' => []], ['refresh' => 'true']) ); @@ -243,7 +273,7 @@ public function testSearch(): void public function testCount(): void { - Promise\wait($this->client->createIndex(self::TEST_INDEX)); + Promise\wait($this->client->createOrUpdateIndex(self::TEST_INDEX)); Promise\wait( $this->client->indexDocument(self::TEST_INDEX, '', ['payload' => []], ['refresh' => 'true']) ); @@ -259,7 +289,7 @@ public function testCount(): void public function testCountWithQuery(): void { - Promise\wait($this->client->createIndex(self::TEST_INDEX)); + Promise\wait($this->client->createOrUpdateIndex(self::TEST_INDEX)); Promise\wait( $this->client->indexDocument(self::TEST_INDEX, '', ['user' => 'kimchy'], ['refresh' => 'true']) ); @@ -275,7 +305,7 @@ public function testCountWithQuery(): void public function testBulkIndex(): void { - Promise\wait($this->client->createIndex(self::TEST_INDEX)); + Promise\wait($this->client->createOrUpdateIndex(self::TEST_INDEX)); $body = []; $responses = []; for ($i = 1; $i <= 1234; $i++) {