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

Expose getNumberOfReplicas() from index settings #1324

Merged
merged 2 commits into from
Jun 13, 2017
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file based on the
### Bugfixes

### Added
- Added getNumberOfReplicas() for index settings [PR#1324](https://github.com/ruflin/Elastica/pull/1324)

### Improvements

Expand Down
20 changes: 20 additions & 0 deletions lib/Elastica/Index/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class Settings
{
const DEFAULT_REFRESH_INTERVAL = '1s';

const DEFAULT_NUMBER_OF_REPLICAS = 1;

/**
* Response.
*
Expand Down Expand Up @@ -133,6 +135,24 @@ public function setNumberOfReplicas($replicas)
return $this->set(['number_of_replicas' => (int) $replicas]);
}

/**
* Returns the number of replicas.
*
* If no number of replicas is set, the default number is returned
*
* @return int The number of replicas
*/
public function getNumberOfReplicas()
{
$replicas = $this->get('number_of_replicas');

if (null === $replicas) {
$replicas = self::DEFAULT_NUMBER_OF_REPLICAS;
}

return $replicas;
}

/**
* Sets the index to read only.
*
Expand Down
30 changes: 27 additions & 3 deletions test/Elastica/Index/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function testGetWithAlias()
/**
* @group functional
*/
public function testSetNumberOfReplicas()
public function testSetGetNumberOfReplicas()
{
$indexName = 'test';

Expand All @@ -68,13 +68,37 @@ public function testSetNumberOfReplicas()
$index->create([], true);
$settings = $index->getSettings();

$settings->setNumberOfReplicas(2);
// Check for zero replicas
$settings->setNumberOfReplicas(0);
$index->refresh();
$this->assertEquals(2, $settings->get('number_of_replicas'));
$this->assertEquals(0, $settings->get('number_of_replicas'));
$this->assertEquals(0, $settings->getNumberOfReplicas());

// Check with 3 replicas
$settings->setNumberOfReplicas(3);
$index->refresh();
$this->assertEquals(3, $settings->get('number_of_replicas'));
$this->assertEquals(3, $settings->getNumberOfReplicas());

$index->delete();
}

/**
* @group functional
*/
public function testGetNumberOfReplicas()
{
$indexName = 'test';

$client = $this->_getClient();
$index = $client->getIndex($indexName);
$index->create([], true);

$settings = $index->getSettings();

// Test with default number of replicas
$this->assertEquals(IndexSettings::DEFAULT_NUMBER_OF_REPLICAS, $settings->get('number_of_replicas'));
$this->assertEquals(IndexSettings::DEFAULT_NUMBER_OF_REPLICAS, $settings->getNumberOfReplicas());

$index->delete();
}
Expand Down