diff --git a/src/ElasticSearch/FilteredAlias.php b/src/ElasticSearch/FilteredAlias.php new file mode 100644 index 00000000..4924adcf --- /dev/null +++ b/src/ElasticSearch/FilteredAlias.php @@ -0,0 +1,44 @@ +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(), + ], + ], + ], + ], + ], + ]); + } +} diff --git a/src/Jobs/Stages/CreateWriteIndex.php b/src/Jobs/Stages/CreateWriteIndex.php index a19dd442..98612f15 100644 --- a/src/Jobs/Stages/CreateWriteIndex.php +++ b/src/Jobs/Stages/CreateWriteIndex.php @@ -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; @@ -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(), diff --git a/tests/Integration/Jobs/Stages/CreateWriteIndexTest.php b/tests/Integration/Jobs/Stages/CreateWriteIndexTest.php index 50b42001..95769acc 100644 --- a/tests/Integration/Jobs/Stages/CreateWriteIndexTest.php +++ b/tests/Integration/Jobs/Stages/CreateWriteIndexTest.php @@ -6,6 +6,8 @@ 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; @@ -13,6 +15,10 @@ final class CreateWriteIndexTest extends IntegrationTestCase { + /** + * @throws ClientResponseException + * @throws ServerResponseException + */ public function test_create_write_index(): void { /** @var Client $elasticsearch */ @@ -20,14 +26,30 @@ public function test_create_write_index(): void $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; } }