Skip to content

Commit

Permalink
Added ability get defaults index settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihails Krasilnikovs committed Aug 17, 2022
1 parent 535fb09 commit 109a2b3
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/Index/Settings.php
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
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($settings->get('max_result_window', true), 10000);
$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($settings->get('max_result_window', true), 100);
$this->assertNull($settings->get('kjqwerjlqwer', true));

$index->delete();
}

/**
* @group functional
*/
Expand Down

0 comments on commit 109a2b3

Please sign in to comment.