Skip to content

Commit

Permalink
Merge pull request #8 from webgriffe/index-create-or-update-fix
Browse files Browse the repository at this point in the history
Add separate methods for updating aliases, mappings and settings on indices
  • Loading branch information
mmenozzi authored Nov 7, 2024
2 parents 5fa0ed7 + 223012c commit e3479df
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 38 deletions.
36 changes: 26 additions & 10 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,13 @@ public function __construct(string $baseUri)
$this->baseUri = rtrim($baseUri, '/');
}

public function createOrUpdateIndex(string $index, array $body = null): Promise
public function createIndex(string $index, array $body = null): Promise

Check failure on line 31 in src/Client.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

Parameter body was added to Method createIndex() of class Webgriffe\AmpElasticsearch\Client

Check failure on line 31 in src/Client.php

View workflow job for this annotation

GitHub Actions / PHP 8.3

Parameter body was added to Method createIndex() of class Webgriffe\AmpElasticsearch\Client
{
$method = 'PUT';
$uri = implode('/', [$this->baseUri, urlencode($index)]);
return $this->doJsonRequest($method, $uri, $body);
}

/**
* @deprecated Use createOrUpdateIndex instead
*/
public function createIndex(string $index): Promise
{
return $this->createOrUpdateIndex($index);
}

public function existsIndex(string $index): Promise
{
$method = 'HEAD';
Expand Down Expand Up @@ -217,6 +209,30 @@ public function bulk(array $body, string $index = null, array $options = []): Pr
);
}

public function createOrUpdateAlias(string $target, string $alias, ?array $body = null): Promise
{
$method = 'PUT';
$uri = implode('/', [$this->baseUri, urlencode($target), '_aliases', urlencode($alias)]);

return $this->doJsonRequest($method, $uri, $body);
}

public function updateIndexSettings(string $target, array $body = null): Promise
{
$method = 'PUT';
$uri = implode('/', [$this->baseUri, urlencode($target), '_settings']);

return $this->doJsonRequest($method, $uri, $body);
}

public function updateMappings(string $target, array $body = null): Promise
{
$method = 'PUT';
$uri = implode('/', [$this->baseUri, urlencode($target), '_mappings']);

return $this->doJsonRequest($method, $uri, $body);
}

private function createJsonRequest(string $method, string $uri, string $body = null): Request
{
$request = new Request($uri, $method);
Expand Down Expand Up @@ -270,7 +286,7 @@ private function doJsonRequest(string $method, string $uri, array $body = null):
{
$jsonBody = null;
if ($body !== null) {
$jsonBody = json_encode($body, JSON_THROW_ON_ERROR);
$jsonBody = json_encode($body, JSON_THROW_ON_ERROR | JSON_FORCE_OBJECT);
}
return $this->doRequest($this->createJsonRequest($method, $uri, $jsonBody));
}
Expand Down
114 changes: 86 additions & 28 deletions tests/Integration/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,40 +31,32 @@ protected function setUp(): void

public function testCreateIndex(): void
{
$response = Promise\wait($this->client->createOrUpdateIndex(self::TEST_INDEX));
$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 testCreateIndexWithExplicitMapping(): void
public function testCreateIndexWithExplicitMappingSettingsAndAliases(): void
{
$response = Promise\wait(
$this->client->createOrUpdateIndex(
$this->client->createIndex(
self::TEST_INDEX,
['mappings' => ['properties' => ['testField' => ['type' => 'text']]]]
[
'mappings' => ['properties' => ['testField' => ['type' => 'text']]],
'settings' => ['index' => ['mapping' => ['total_fields' => ['limit' => 2000]]]],
'aliases' => ['alias1' => [], 'alias2' => ['filter' => ['term' => ['user' => 'kimchy']]]]
]
)
);
$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']);
$this->assertCount(2, $response[self::TEST_INDEX]['aliases']);
$this->assertEquals('kimchy', $response[self::TEST_INDEX]['aliases']['alias2']['filter']['term']['user']);
}

public function testIndicesExistsShouldThrow404ErrorIfIndexDoesNotExists(): void
Expand All @@ -76,7 +68,7 @@ public function testIndicesExistsShouldThrow404ErrorIfIndexDoesNotExists(): void

public function testIndicesExistsShouldNotThrowAnErrorIfIndexExists(): void
{
Promise\wait($this->client->createOrUpdateIndex(self::TEST_INDEX));
Promise\wait($this->client->createIndex(self::TEST_INDEX));
$response = Promise\wait($this->client->existsIndex(self::TEST_INDEX));
$this->assertNull($response);
}
Expand All @@ -98,7 +90,7 @@ public function testDocumentsIndexWithAutomaticIdCreation(): void

public function testDocumentsExistsShouldThrowA404ErrorIfDocumentDoesNotExists(): void
{
Promise\wait($this->client->createOrUpdateIndex(self::TEST_INDEX));
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'));
Expand Down Expand Up @@ -233,29 +225,29 @@ public function testCatHealth(): void

public function testRefreshOneIndex(): void
{
Promise\wait($this->client->createOrUpdateIndex(self::TEST_INDEX));
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->createOrUpdateIndex('an_index'));
Promise\wait($this->client->createOrUpdateIndex('another_index'));
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->createOrUpdateIndex(self::TEST_INDEX));
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->createOrUpdateIndex(self::TEST_INDEX));
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'])
);
Expand All @@ -273,7 +265,7 @@ public function testSearch(): void

public function testCount(): void
{
Promise\wait($this->client->createOrUpdateIndex(self::TEST_INDEX));
Promise\wait($this->client->createIndex(self::TEST_INDEX));
Promise\wait(
$this->client->indexDocument(self::TEST_INDEX, '', ['payload' => []], ['refresh' => 'true'])
);
Expand All @@ -289,7 +281,7 @@ public function testCount(): void

public function testCountWithQuery(): void
{
Promise\wait($this->client->createOrUpdateIndex(self::TEST_INDEX));
Promise\wait($this->client->createIndex(self::TEST_INDEX));
Promise\wait(
$this->client->indexDocument(self::TEST_INDEX, '', ['user' => 'kimchy'], ['refresh' => 'true'])
);
Expand All @@ -305,7 +297,7 @@ public function testCountWithQuery(): void

public function testBulkIndex(): void
{
Promise\wait($this->client->createOrUpdateIndex(self::TEST_INDEX));
Promise\wait($this->client->createIndex(self::TEST_INDEX));
$body = [];
$responses = [];
for ($i = 1; $i <= 1234; $i++) {
Expand All @@ -326,4 +318,70 @@ public function testBulkIndex(): void
$this->assertIsArray($responses);
$this->assertCount(34, $responses['items']);
}

public function testCreateAlias(): void
{
Promise\wait($this->client->createIndex(self::TEST_INDEX));

$response = Promise\wait(
$this->client->createOrUpdateAlias(
self::TEST_INDEX,
'alias',
['filter' => ['term' => ['user' => 'kimchy']]]
)
);
$this->assertIsArray($response);
$this->assertTrue($response['acknowledged']);
$response = Promise\wait($this->client->getIndex(self::TEST_INDEX));
$this->assertEquals('kimchy', $response[self::TEST_INDEX]['aliases']['alias']['filter']['term']['user']);
}

public function testUpdateAlias(): void
{
Promise\wait($this->client->createIndex(self::TEST_INDEX));
$this->client->createOrUpdateAlias(self::TEST_INDEX, 'alias');

$response = Promise\wait(
$this->client->createOrUpdateAlias(
self::TEST_INDEX,
'alias',
['filter' => ['term' => ['user' => 'kimchy']]]
)
);
$this->assertIsArray($response);
$this->assertTrue($response['acknowledged']);
$response = Promise\wait($this->client->getIndex(self::TEST_INDEX));
$this->assertEquals('kimchy', $response[self::TEST_INDEX]['aliases']['alias']['filter']['term']['user']);
}

public function testUpdateIndexSettings(): void
{
Promise\wait($this->client->createIndex(self::TEST_INDEX));

$response = Promise\wait(
$this->client->updateIndexSettings(
self::TEST_INDEX,
['index' => ['mapping' => ['total_fields' => ['limit' => 2000]]]]
)
);

$this->assertIsArray($response);
$this->assertTrue($response['acknowledged']);
$response = Promise\wait($this->client->getIndex(self::TEST_INDEX));
$this->assertEquals(2000, $response[self::TEST_INDEX]['settings']['index']['mapping']['total_fields']['limit']);
}

public function testUpdateMappings(): void
{
Promise\wait($this->client->createIndex(self::TEST_INDEX));

$response = Promise\wait(
$this->client->updateMappings(self::TEST_INDEX, ['properties' => ['testField' => ['type' => 'text']]])
);

$this->assertIsArray($response);
$this->assertTrue($response['acknowledged']);
$response = Promise\wait($this->client->getIndex(self::TEST_INDEX));
$this->assertEquals('text', $response[self::TEST_INDEX]['mappings']['properties']['testField']['type']);
}
}

0 comments on commit e3479df

Please sign in to comment.