Skip to content

Commit

Permalink
Add index aliases support
Browse files Browse the repository at this point in the history
  • Loading branch information
babenkoivan committed Apr 6, 2021
1 parent 68e006c commit 505049d
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,36 @@ Close an index:
$indexManager->close('my_index');
```

### Put Alias

Create an alias:

```php
$alias = new \ElasticAdapter\Indices\Alias('my_alias', [
'term' => [
'user_id' => 12,
],
]);

$indexManager->putAlias('my_index', $alias);
```

### Get Aliases

Get index aliases:

```php
$indexManager->getAliases('my_index');
```

### Delete Alias

Delete an alias:

```php
$indexManager->deleteAlias('my_index', 'my_alias');
```

## Document Management

Similarly to `IndexManager`, the `DocumentManager` class also depends on Elasticsearch client:
Expand Down
30 changes: 30 additions & 0 deletions src/Indices/IndexManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,34 @@ public function getAliases(string $indexName): array
);
}, $aliases, array_keys($aliases));
}

public function putAlias(string $indexName, Alias $alias): self
{
$params = [
'index' => $indexName,
'name' => $alias->getName(),
];

if ($alias->getRouting()) {
$params['body']['routing'] = $alias->getRouting();
}

if ($alias->getFilter()) {
$params['body']['filter'] = $alias->getFilter();
}

$this->indices->putAlias($params);

return $this;
}

public function deleteAlias(string $indexName, string $aliasName): self
{
$this->indices->deleteAlias([
'index' => $indexName,
'name' => $aliasName,
]);

return $this;
}
}
40 changes: 40 additions & 0 deletions tests/Unit/Indices/IndexManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,44 @@ public function test_aliases_can_be_retrieved(): void

$this->assertEquals([new Alias($aliasName)], $this->indexManager->getAliases($indexName));
}

public function test_alias_can_be_created(): void
{
$indexName = 'foo';
$alias = (new Alias('bar', ['term' => ['user_id' => 12]], '12'));

$this->indices
->expects($this->once())
->method('putAlias')
->with([
'index' => $indexName,
'name' => $alias->getName(),
'body' => [
'routing' => '12',
'filter' => [
'term' => [
'user_id' => 12,
],
],
],
]);

$this->assertSame($this->indexManager, $this->indexManager->putAlias($indexName, $alias));
}

public function test_alias_can_be_deleted(): void
{
$indexName = 'foo';
$aliasName = 'bar';

$this->indices
->expects($this->once())
->method('deleteAlias')
->with([
'index' => $indexName,
'name' => $aliasName,
]);

$this->assertSame($this->indexManager, $this->indexManager->deleteAlias($indexName, $aliasName));
}
}

0 comments on commit 505049d

Please sign in to comment.