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

Added ability get defaults index settings #2115

Merged
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 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Use `ramsey/composer-install` to simplify CI jobs and test with the lowest set of dependencies [#2113](https://github.com/ruflin/Elastica/pull/2113)
* Bumped `elasticsearch/elasticsearch` to `7.10` to be able to use `OpenPointInTime` class [#2113](https://github.com/ruflin/Elastica/pull/2113)
* Updated `php-cs-fixer` to `3.9.5` [#2110](https://github.com/ruflin/Elastica/pull/2110)
* Changed `Elastica\Index\Settings::get` adding ability to get default settings by @krasilnikovm [#2115](https://github.com/ruflin/Elastica/pull/2115)
### Deprecated
### Removed
* Removed `CallbackStrategyTestHelper` and `ErrorsCollector` from `tests` [#2111](https://github.com/ruflin/Elastica/pull/2111)
Expand Down
16 changes: 12 additions & 4 deletions src/Index/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,24 @@ public function __construct(BaseIndex $index)
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-update-settings.html
*/
public function get(string $setting = '')
public function get(string $setting = '', bool $includeDefaults = false)
{
$requestData = $this->request()->getData();
$queryParameters = [
'include_defaults' => $includeDefaults,
];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surprised by this codecov comment as I think this part is covered in the tests. Maybe codecov is confused by the ] 🤷


$requestData = $this->request([], Request::GET, $queryParameters)->getData();
$data = \reset($requestData);

if (empty($data['settings']) || empty($data['settings']['index'])) {
// should not append, the request should throw a ResponseException
throw new NotFoundException('Index '.$this->getIndex()->getName().' not found');
}

$settings = $data['settings']['index'];
$defaults = $data['defaults']['index'] ?? [];

$settings = \array_merge($defaults, $settings);

if (!$setting) {
// return all array
Expand Down Expand Up @@ -329,14 +337,14 @@ public function getIndex(): BaseIndex
*
* @return Response Response object
*/
public function request(array $data = [], string $method = Request::GET): Response
public function request(array $data = [], string $method = Request::GET, array $queryParameters = []): Response
{
$path = '_settings';

if ($data) {
$data = ['index' => $data];
}

return $this->getIndex()->request($path, $method, $data);
return $this->getIndex()->request($path, $method, $data, $queryParameters);
}
}
50 changes: 50 additions & 0 deletions tests/Index/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,61 @@ public function testGet(): void
$this->assertIsArray($settings->get());
$this->assertNotNull($settings->get('number_of_replicas'));
$this->assertNotNull($settings->get('number_of_shards'));
$this->assertNull($settings->get('max_result_window'));
$this->assertNull($settings->get('kjqwerjlqwer'));

$index->delete();
}

/**
* @group functional
*/
public function testGetWithDefaultValueIncluded(): void
{
$indexName = 'elasticatest';

$client = $this->_getClient();
$index = $client->getIndex($indexName);
$index->create([], [
'recreate' => true,
]);
$index->refresh();
$settings = $index->getSettings();

$this->assertIsArray($settings->get());
$this->assertEquals(10000, $settings->get('max_result_window', true));
$this->assertNull($settings->get('kjqwerjlqwer', true));

$index->delete();
}

/**
* @group functional
*/
public function testGetWithDefaultValueOverride(): void
{
$indexName = 'elasticatest';

$client = $this->_getClient();

$index = $client->getIndex($indexName);
$index->create([
'settings' => [
'max_result_window' => 100,
],
], [
'recreate' => true,
]);

$index->refresh();
$settings = $index->getSettings();
$this->assertIsArray($settings->get());
$this->assertEquals(100, $settings->get('max_result_window', true));
$this->assertNull($settings->get('kjqwerjlqwer', true));

$index->delete();
}

/**
* @group functional
*/
Expand Down