Skip to content

Commit

Permalink
Merge pull request #231 from hkulekci/alias-with-filter-solution
Browse files Browse the repository at this point in the history
filtered alias introduced for the write index to prevent duplicated d…
  • Loading branch information
matchish authored Feb 1, 2023
2 parents e545cde + 78d9619 commit b83b237
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 5 deletions.
44 changes: 44 additions & 0 deletions src/ElasticSearch/FilteredAlias.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Matchish\ScoutElasticSearch\ElasticSearch;

/**
* @internal
*/
final class FilteredAlias implements Alias
{
/**
* @var Alias
*/
private Alias $origin;

private Index $index;

public function __construct(Alias $origin, Index $index)
{
$this->origin = $origin;
$this->index = $index;
}

public function name(): string
{
return $this->origin->name();
}

public function config(): array
{
return array_merge($this->origin->config(), [
'filter' => [
'bool' => [
'must_not' => [
[
'term' => [
'_index' => $this->index->name(),
],
],
],
],
],
]);
}
}
8 changes: 7 additions & 1 deletion src/Jobs/Stages/CreateWriteIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Elastic\Elasticsearch\Client;
use Matchish\ScoutElasticSearch\ElasticSearch\DefaultAlias;
use Matchish\ScoutElasticSearch\ElasticSearch\FilteredAlias;
use Matchish\ScoutElasticSearch\ElasticSearch\Index;
use Matchish\ScoutElasticSearch\ElasticSearch\Params\Indices\Create;
use Matchish\ScoutElasticSearch\ElasticSearch\WriteAlias;
Expand Down Expand Up @@ -36,7 +37,12 @@ public function __construct(ImportSource $source, Index $index)
public function handle(Client $elasticsearch): void
{
$source = $this->source;
$this->index->addAlias(new WriteAlias(new DefaultAlias($source->searchableAs())));
$this->index->addAlias(
new FilteredAlias(
new WriteAlias(new DefaultAlias($source->searchableAs())),
$this->index
)
);

$params = new Create(
$this->index->name(),
Expand Down
30 changes: 26 additions & 4 deletions tests/Integration/Jobs/Stages/CreateWriteIndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,50 @@

use App\Product;
use Elastic\Elasticsearch\Client;
use Elastic\Elasticsearch\Exception\ClientResponseException;
use Elastic\Elasticsearch\Exception\ServerResponseException;
use Matchish\ScoutElasticSearch\ElasticSearch\Index;
use Matchish\ScoutElasticSearch\Jobs\Stages\CreateWriteIndex;
use Matchish\ScoutElasticSearch\Searchable\DefaultImportSourceFactory;
use Tests\IntegrationTestCase;

final class CreateWriteIndexTest extends IntegrationTestCase
{
/**
* @throws ClientResponseException
* @throws ServerResponseException
*/
public function test_create_write_index(): void
{
/** @var Client $elasticsearch */
$elasticsearch = $this->app->make(Client::class);
$stage = new CreateWriteIndex(DefaultImportSourceFactory::from(Product::class), Index::fromSource(DefaultImportSourceFactory::from(Product::class)));
$stage->handle($elasticsearch);
$response = $elasticsearch->indices()->getAlias(['index' => '*', 'name' => 'products'])->asArray();
$this->assertTrue($this->containsWriteIndex($response, 'products'));
$this->assertTrue($this->containsWriteIndex($response));
}

private function containsWriteIndex($response, $requiredAlias)
private function containsWriteIndex($response): bool
{
foreach ($response as $index) {
foreach ($response as $indexName => $index) {
foreach ($index['aliases'] as $alias => $data) {
if ($alias == $requiredAlias && array_key_exists('is_write_index', $data) && $data['is_write_index']) {
if ($alias === 'products') {
$this->assertIsArray($data);
$this->assertArrayHasKey('is_write_index', $data);
$this->assertTrue($data['is_write_index']);
$this->assertArrayHasKey('filter', $data);
$this->assertEquals([
'bool' => [
'must_not' => [
[
'term' => [
'_index' => $indexName,
],
],
],
],
], $data['filter']);

return true;
}
}
Expand Down

0 comments on commit b83b237

Please sign in to comment.