Skip to content

Commit

Permalink
Expose getNumberOfReplicas() from index settings (#1324)
Browse files Browse the repository at this point in the history
The index settings exposes most of the available settings, while for the numberOfReplicas the "get" method is missing
  • Loading branch information
thePanz authored and ruflin committed Jun 13, 2017
1 parent e40ca53 commit 464899a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file based on the
- Send the `scroll_id` inside a json body instead of plain text [#1325](https://github.com/ruflin/Elastica/pull/1325)

### 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

0 comments on commit 464899a

Please sign in to comment.