Skip to content

Commit

Permalink
Added ability get defaults index settings (#2115)
Browse files Browse the repository at this point in the history
Co-authored-by: Mihails Krasilnikovs <m.krasilnikovs@dynatech.lv>
  • Loading branch information
krasilnikovm and Mihails Krasilnikovs authored Aug 18, 2022
1 parent 535fb09 commit a996872
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 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 @@ 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,
];

$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

0 comments on commit a996872

Please sign in to comment.