Skip to content

Commit

Permalink
Add raw versions of create, putMapping and putSettings methods
Browse files Browse the repository at this point in the history
  • Loading branch information
babenkoivan committed Sep 24, 2021
1 parent 660e368 commit 09f4a6b
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 2 deletions.
46 changes: 44 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,24 @@ $index = new \ElasticAdapter\Indices\IndexBlueprint('my_index', $mapping, $setti
$indexManager->create($index);
```

Alternatively, you can create an index using raw input:

```php
$mapping = [
'properties' => [
'title' => [
'type' => 'text'
]
]
];

$settings = [
'number_of_replicas' => 2
];

$indexManager->createRaw('my_index', $mapping, $settings);
```

### Drop

Delete an index:
Expand All @@ -107,7 +125,7 @@ $indexManager->drop('my_index');

### Put Mapping

Update an index mapping:
Update an index mapping using builder:

```php
$mapping = (new \ElasticAdapter\Indices\Mapping())
Expand All @@ -122,9 +140,23 @@ $mapping = (new \ElasticAdapter\Indices\Mapping())
$indexManager->putMapping('my_index', $mapping);
```

or using raw input:

```php
$mapping = [
'properties' => [
'title' => [
'type' => 'text'
]
]
];

$indexManager->putMappingRaw('my_index', $mapping);
```

### Put Settings

Update an index settings:
Update an index settings using builder:

```php
$settings = (new \ElasticAdapter\Indices\Settings())
Expand All @@ -140,6 +172,16 @@ $settings = (new \ElasticAdapter\Indices\Settings())
$indexManager->putSettings('my_index', $settings);
```

or using raw input:

```php
$settings = [
'number_of_replicas' => 2
];

$indexManager->putSettingsRaw('my_index', $settings);
```

### Exists

Check if an index exists:
Expand Down
41 changes: 41 additions & 0 deletions src/Indices/IndexManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,25 @@ public function create(IndexBlueprint $index): self
return $this;
}

public function createRaw(string $indexName, ?array $mapping = null, ?array $settings = null): self
{
$params = [
'index' => $indexName,
];

if (isset($mapping)) {
$params['body']['mappings'] = $mapping;
}

if (isset($settings)) {
$params['body']['settings'] = $settings;
}

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

return $this;
}

public function putMapping(string $indexName, Mapping $mapping): self
{
$this->indices->putMapping([
Expand All @@ -75,6 +94,16 @@ public function putMapping(string $indexName, Mapping $mapping): self
return $this;
}

public function putMappingRaw(string $indexName, array $mapping): self
{
$this->indices->putMapping([
'index' => $indexName,
'body' => $mapping,
]);

return $this;
}

public function putSettings(string $indexName, Settings $settings): self
{
$this->indices->putSettings([
Expand All @@ -87,6 +116,18 @@ public function putSettings(string $indexName, Settings $settings): self
return $this;
}

public function putSettingsRaw(string $indexName, array $settings): self
{
$this->indices->putSettings([
'index' => $indexName,
'body' => [
'settings' => $settings,
],
]);

return $this;
}

public function drop(string $indexName): self
{
$this->indices->delete([
Expand Down
54 changes: 54 additions & 0 deletions tests/Unit/Indices/IndexManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,26 @@ public function test_index_can_be_created_with_empty_settings_and_mapping(): voi
$this->assertSame($this->indexManager, $this->indexManager->create($index));
}

public function test_index_can_be_created_with_raw_mapping_and_settings(): void
{
$indexName = 'foo';
$mapping = ['properties' => ['bar' => ['type' => 'text']]];
$settings = ['index' => ['number_of_replicas' => 2]];

$this->indices
->expects($this->once())
->method('create')
->with([
'index' => $indexName,
'body' => [
'mappings' => $mapping,
'settings' => $settings,
],
]);

$this->assertSame($this->indexManager, $this->indexManager->createRaw($indexName, $mapping, $settings));
}

public function test_mapping_can_be_updated(): void
{
$indexName = 'foo';
Expand All @@ -185,6 +205,22 @@ public function test_mapping_can_be_updated(): void
$this->assertSame($this->indexManager, $this->indexManager->putMapping($indexName, $mapping));
}

public function test_mapping_can_be_updated_with_raw_data(): void
{
$indexName = 'foo';
$mapping = ['properties' => ['bar' => ['type' => 'text']]];

$this->indices
->expects($this->once())
->method('putMapping')
->with([
'index' => $indexName,
'body' => $mapping,
]);

$this->assertSame($this->indexManager, $this->indexManager->putMappingRaw($indexName, $mapping));
}

public function test_settings_can_be_updated(): void
{
$indexName = 'foo';
Expand All @@ -207,6 +243,24 @@ public function test_settings_can_be_updated(): void
$this->assertSame($this->indexManager, $this->indexManager->putSettings($indexName, $settings));
}

public function test_settings_can_be_updated_with_raw_data(): void
{
$indexName = 'foo';
$settings = ['index' => ['number_of_replicas' => 2]];

$this->indices
->expects($this->once())
->method('putSettings')
->with([
'index' => $indexName,
'body' => [
'settings' => $settings,
],
]);

$this->assertSame($this->indexManager, $this->indexManager->putSettingsRaw($indexName, $settings));
}

public function test_index_can_be_dropped(): void
{
$indexName = 'foo';
Expand Down

0 comments on commit 09f4a6b

Please sign in to comment.