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 the number of shards from the Index settings #1331

Merged
merged 1 commit into from
Jul 10, 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 @@ -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