Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add aliases support to different Adapters (#381) #383

Draft
wants to merge 2 commits into
base: 0.6
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Schranz\Search\SEAL\Adapter\Elasticsearch;

use Elastic\Elasticsearch\Client;
use Elastic\Elasticsearch\Exception\ClientResponseException;
use Elastic\Elasticsearch\Response\Elasticsearch;
use Schranz\Search\SEAL\Adapter\SchemaManagerInterface;
use Schranz\Search\SEAL\Schema\Field;
Expand All @@ -40,8 +41,20 @@ public function existIndex(Index $index): bool

public function dropIndex(Index $index, array $options = []): TaskInterface|null
{
/** @var Elasticsearch $response */
$response = $this->client->indices()->getAlias([
'name' => $index->name,
]);

$targetIndexName = \array_key_first($response->asArray());

$this->client->indices()->deleteAlias([
'name' => $index->name,
'index' => $targetIndexName,
]);

$this->client->indices()->delete([
'index' => $index->name,
'index' => $targetIndexName,
]);

if (!($options['return_slow_promise_result'] ?? false)) {
Expand All @@ -55,8 +68,20 @@ public function createIndex(Index $index, array $options = []): TaskInterface|nu
{
$properties = $this->createPropertiesMapping($index->fields);

$targetIndexName = $index->name . '_' . \date('YmdHis');
try {
/** @var Elasticsearch $response */
$response = $this->client->indices()->getAlias([
'name' => $index->name,
]);

$targetIndexName = \array_key_first($response->asArray()) ?? $targetIndexName;
} catch (ClientResponseException) {
// @ignoreException
}

$this->client->indices()->create([
'index' => $index->name,
'index' => $targetIndexName,
'body' => [
'mappings' => [
'dynamic' => 'strict',
Expand All @@ -65,6 +90,11 @@ public function createIndex(Index $index, array $options = []): TaskInterface|nu
],
]);

$this->client->indices()->putAlias([
'name' => $index->name,
'index' => $targetIndexName,
]);

if (!($options['return_slow_promise_result'] ?? false)) {
return null;
}
Expand Down
19 changes: 16 additions & 3 deletions packages/seal-elasticsearch-adapter/src/ElasticsearchSearcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,26 @@ public function search(Search $search): Result
private function hitsToDocuments(array $indexes, array $hits): \Generator
{
$indexesByInternalName = [];
foreach ($indexes as $index) {
$indexesByInternalName[$index->name] = $index;
}

/** @var array{_index: string, _source: array<string, mixed>} $hit */
foreach ($hits as $hit) {
$index = $indexesByInternalName[$hit['_index']] ?? null;
if (!$index instanceof Index) {
$lastMaxLength = 0;

// find the correct index alias as we use %indexName%_%datetime% as index name
foreach ($indexes as $otherIndex) {
if (
\str_starts_with($hit['_index'], $otherIndex->name . '_')
&& $lastMaxLength < \strlen($otherIndex->name)
) {
$index = $otherIndex;
$lastMaxLength = \strlen($index->name);
}
}
$indexesByInternalName[$hit['_index']] = $index;
}

if (!$index instanceof Index) {
throw new \RuntimeException('SchemaMetadata for Index "' . $hit['_index'] . '" not found.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ public function testSimpleElasticsearchMapping(): void
$task = static::$schemaManager->dropIndex($index, ['return_slow_promise_result' => true]);
$task->wait();

$this->assertTrue(isset($mapping[$index->name]['mappings']['properties']));
$targetIndexName = \array_key_first($mapping);

$this->assertTrue(isset($mapping[$targetIndexName]['mappings']['properties']));

$this->assertSame([
'id' => [
Expand All @@ -57,7 +59,7 @@ public function testSimpleElasticsearchMapping(): void
'title' => [
'type' => 'text',
],
], $mapping[$index->name]['mappings']['properties']);
], $mapping[$targetIndexName]['mappings']['properties']);
}

public function testComplexElasticsearchMapping(): void
Expand All @@ -76,7 +78,9 @@ public function testComplexElasticsearchMapping(): void
$task = static::$schemaManager->dropIndex($index, ['return_slow_promise_result' => true]);
$task->wait();

$this->assertTrue(isset($mapping[$index->name]['mappings']['properties']));
$targetIndexName = \array_key_first($mapping);

$this->assertTrue(isset($mapping[$targetIndexName]['mappings']['properties']));

$this->assertSame([
'article' => [
Expand Down Expand Up @@ -194,6 +198,6 @@ public function testComplexElasticsearchMapping(): void
'type' => 'keyword',
'index' => false,
],
], $mapping[$index->name]['mappings']['properties']);
], $mapping[$targetIndexName]['mappings']['properties']);
}
}
33 changes: 31 additions & 2 deletions packages/seal-opensearch-adapter/src/OpensearchSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Schranz\Search\SEAL\Adapter\Opensearch;

use OpenSearch\Client;
use OpenSearch\Common\Exceptions\Missing404Exception;
use Schranz\Search\SEAL\Adapter\SchemaManagerInterface;
use Schranz\Search\SEAL\Schema\Field;
use Schranz\Search\SEAL\Schema\Index;
Expand All @@ -36,8 +37,19 @@ public function existIndex(Index $index): bool

public function dropIndex(Index $index, array $options = []): TaskInterface|null
{
$responseData = $this->client->indices()->getAlias([
'name' => $index->name,
]);

$targetIndexName = \array_key_first($responseData);

$this->client->indices()->deleteAlias([
'name' => $index->name,
'index' => $targetIndexName,
]);

$this->client->indices()->delete([
'index' => $index->name,
'index' => $targetIndexName,
]);

if (!($options['return_slow_promise_result'] ?? false)) {
Expand All @@ -51,8 +63,20 @@ public function createIndex(Index $index, array $options = []): TaskInterface|nu
{
$properties = $this->createPropertiesMapping($index->fields);

$targetIndexName = $index->name . '_' . \date('YmdHis');

try {
$responseData = $this->client->indices()->getAlias([
'name' => $index->name,
]);

$targetIndexName = \array_key_first($responseData) ?? $targetIndexName;
} catch (Missing404Exception) {
// @ignoreException
}

$this->client->indices()->create([
'index' => $index->name,
'index' => $targetIndexName,
'body' => [
'mappings' => [
'dynamic' => 'strict',
Expand All @@ -61,6 +85,11 @@ public function createIndex(Index $index, array $options = []): TaskInterface|nu
],
]);

$this->client->indices()->putAlias([
'name' => $index->name,
'index' => $targetIndexName,
]);

if (!($options['return_slow_promise_result'] ?? false)) {
return null;
}
Expand Down
19 changes: 16 additions & 3 deletions packages/seal-opensearch-adapter/src/OpensearchSearcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,26 @@ public function search(Search $search): Result
private function hitsToDocuments(array $indexes, array $hits): \Generator
{
$indexesByInternalName = [];
foreach ($indexes as $index) {
$indexesByInternalName[$index->name] = $index;
}

/** @var array{_index: string, _source: array<string, mixed>} $hit */
foreach ($hits as $hit) {
$index = $indexesByInternalName[$hit['_index']] ?? null;
if (!$index instanceof Index) {
$lastMaxLength = 0;

// find the correct index alias as we use %indexName%_%datetime% as index name
foreach ($indexes as $otherIndex) {
if (
\str_starts_with($hit['_index'], $otherIndex->name . '_')
&& $lastMaxLength < \strlen($otherIndex->name)
) {
$index = $otherIndex;
$lastMaxLength = \strlen($index->name);
}
}
$indexesByInternalName[$hit['_index']] = $index;
}

if (!$index instanceof Index) {
throw new \RuntimeException('SchemaMetadata for Index "' . $hit['_index'] . '" not found.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ public function testSimpleOpensearchMapping(): void
$task = static::$schemaManager->dropIndex($index, ['return_slow_promise_result' => true]);
$task->wait();

$this->assertTrue(isset($mapping[$index->name]['mappings']['properties']));
$targetIndexName = \array_key_first($mapping);

$this->assertTrue(isset($mapping[$targetIndexName]['mappings']['properties']));

$this->assertSame([
'id' => [
Expand All @@ -52,7 +54,7 @@ public function testSimpleOpensearchMapping(): void
'title' => [
'type' => 'text',
],
], $mapping[$index->name]['mappings']['properties']);
], $mapping[$targetIndexName]['mappings']['properties']);
}

public function testComplexOpensearchMapping(): void
Expand All @@ -68,7 +70,9 @@ public function testComplexOpensearchMapping(): void
$task = static::$schemaManager->dropIndex($index, ['return_slow_promise_result' => true]);
$task->wait();

$this->assertTrue(isset($mapping[$index->name]['mappings']['properties']));
$targetIndexName = \array_key_first($mapping);

$this->assertTrue(isset($mapping[$targetIndexName]['mappings']['properties']));

$this->assertSame([
'article' => [
Expand Down Expand Up @@ -180,6 +184,6 @@ public function testComplexOpensearchMapping(): void
'uuid' => [
'type' => 'keyword',
],
], $mapping[$index->name]['mappings']['properties']);
], $mapping[$targetIndexName]['mappings']['properties']);
}
}
Loading