Skip to content

Commit

Permalink
Expose the number of shards from the Index settings (#1331)
Browse files Browse the repository at this point in the history
  • Loading branch information
thePanz authored and ruflin committed Jul 10, 2017
1 parent 993700f commit 1b7d4ce
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file based on the

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

### 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 @@ -24,6 +24,8 @@ class Settings

const DEFAULT_NUMBER_OF_REPLICAS = 1;

const DEFAULT_NUMBER_OF_SHARDS = 5;

/**
* Response.
*
Expand Down Expand Up @@ -153,6 +155,24 @@ public function getNumberOfReplicas()
return $replicas;
}

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

if (null === $shards) {
$shards = self::DEFAULT_NUMBER_OF_SHARDS;
}

return $shards;
}

/**
* Sets the index to read only.
*
Expand Down
40 changes: 40 additions & 0 deletions test/Elastica/Index/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,46 @@ public function testGetNumberOfReplicas()
$index->delete();
}

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

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

$settings = $index->getSettings();

// Test with default number of replicas
$this->assertEquals(1, $settings->get('number_of_shards'));
$this->assertEquals(1, $settings->getNumberOfShards());

$index->delete();
}

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

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

$settings = $index->getSettings();

// Test with default number of shards
$this->assertEquals(IndexSettings::DEFAULT_NUMBER_OF_SHARDS, $settings->get('number_of_shards'));
$this->assertEquals(IndexSettings::DEFAULT_NUMBER_OF_SHARDS, $settings->getNumberOfShards());

$index->delete();
}

/**
* @group functional
*/
Expand Down

0 comments on commit 1b7d4ce

Please sign in to comment.