Skip to content

Commit

Permalink
Allow to pass body to index create or update
Browse files Browse the repository at this point in the history
  • Loading branch information
mmenozzi committed Oct 28, 2024
1 parent 7e2b8d3 commit 5021e68
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 15 deletions.
22 changes: 18 additions & 4 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
}
}
52 changes: 41 additions & 11 deletions tests/Integration/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Expand All @@ -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'));
Expand Down Expand Up @@ -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'])
);
Expand All @@ -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'])
);
Expand All @@ -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'])
);
Expand All @@ -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++) {
Expand Down

0 comments on commit 5021e68

Please sign in to comment.