Skip to content

Commit 0a93956

Browse files
authored
Add Get Settings API support to java high-level rest client (#29229)
This PR adds support for the Get Settings API to the java high-level rest client. Furthermore, logic related to the retrieval of default settings has been moved from the rest layer into the transport layer and now default settings may be retrieved consistency via both the rest API and the transport API.
1 parent bf51a21 commit 0a93956

File tree

16 files changed

+1046
-42
lines changed

16 files changed

+1046
-42
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
import org.elasticsearch.action.admin.indices.open.OpenIndexResponse;
4444
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
4545
import org.elasticsearch.action.admin.indices.refresh.RefreshResponse;
46+
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest;
47+
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;
4648
import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
4749
import org.elasticsearch.action.admin.indices.rollover.RolloverResponse;
4850
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
@@ -265,6 +267,28 @@ public void flushAsync(FlushRequest flushRequest, ActionListener<FlushResponse>
265267
listener, emptySet(), headers);
266268
}
267269

270+
/**
271+
* Retrieve the settings of one or more indices
272+
* <p>
273+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-settings.html">
274+
* Indices Get Settings API on elastic.co</a>
275+
*/
276+
public GetSettingsResponse getSettings(GetSettingsRequest getSettingsRequest, Header... headers) throws IOException {
277+
return restHighLevelClient.performRequestAndParseEntity(getSettingsRequest, RequestConverters::getSettings,
278+
GetSettingsResponse::fromXContent, emptySet(), headers);
279+
}
280+
281+
/**
282+
* Asynchronously retrieve the settings of one or more indices
283+
* <p>
284+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-settings.html">
285+
* Indices Get Settings API on elastic.co</a>
286+
*/
287+
public void getSettingsAsync(GetSettingsRequest getSettingsRequest, ActionListener<GetSettingsResponse> listener, Header... headers) {
288+
restHighLevelClient.performRequestAsyncAndParseEntity(getSettingsRequest, RequestConverters::getSettings,
289+
GetSettingsResponse::fromXContent, listener, emptySet(), headers);
290+
}
291+
268292
/**
269293
* Force merge one or more indices using the Force Merge API
270294
* <p>

client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
4545
import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
4646
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
47+
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest;
4748
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
4849
import org.elasticsearch.action.admin.indices.shrink.ResizeType;
4950
import org.elasticsearch.action.bulk.BulkRequest;
@@ -600,6 +601,22 @@ static Request rollover(RolloverRequest rolloverRequest) throws IOException {
600601
return request;
601602
}
602603

604+
static Request getSettings(GetSettingsRequest getSettingsRequest) throws IOException {
605+
String[] indices = getSettingsRequest.indices() == null ? Strings.EMPTY_ARRAY : getSettingsRequest.indices();
606+
String[] names = getSettingsRequest.names() == null ? Strings.EMPTY_ARRAY : getSettingsRequest.names();
607+
608+
String endpoint = endpoint(indices, "_settings", names);
609+
Request request = new Request(HttpGet.METHOD_NAME, endpoint);
610+
611+
Params params = new Params(request);
612+
params.withIndicesOptions(getSettingsRequest.indicesOptions());
613+
params.withLocal(getSettingsRequest.local());
614+
params.withIncludeDefaults(getSettingsRequest.includeDefaults());
615+
params.withMasterTimeout(getSettingsRequest.masterNodeTimeout());
616+
617+
return request;
618+
}
619+
603620
static Request indicesExist(GetIndexRequest getIndexRequest) {
604621
// this can be called with no indices as argument by transport client, not via REST though
605622
if (getIndexRequest.indices() == null || getIndexRequest.indices().length == 0) {

client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
import org.elasticsearch.action.admin.indices.rollover.RolloverResponse;
5252
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
5353
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsResponse;
54+
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest;
55+
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;
5456
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
5557
import org.elasticsearch.action.admin.indices.shrink.ResizeResponse;
5658
import org.elasticsearch.action.admin.indices.shrink.ResizeType;
@@ -189,6 +191,108 @@ public void testCreateIndex() throws IOException {
189191
}
190192
}
191193

194+
public void testGetSettings() throws IOException {
195+
String indexName = "get_settings_index";
196+
Settings basicSettings = Settings.builder()
197+
.put("number_of_shards", 1)
198+
.put("number_of_replicas", 0)
199+
.build();
200+
createIndex(indexName, basicSettings);
201+
202+
GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices(indexName);
203+
GetSettingsResponse getSettingsResponse = execute(getSettingsRequest, highLevelClient().indices()::getSettings,
204+
highLevelClient().indices()::getSettingsAsync);
205+
206+
assertNull(getSettingsResponse.getSetting(indexName, "index.refresh_interval"));
207+
assertEquals("1", getSettingsResponse.getSetting(indexName, "index.number_of_shards"));
208+
209+
updateIndexSettings(indexName, Settings.builder().put("refresh_interval", "30s"));
210+
211+
GetSettingsResponse updatedResponse = execute(getSettingsRequest, highLevelClient().indices()::getSettings,
212+
highLevelClient().indices()::getSettingsAsync);
213+
assertEquals("30s", updatedResponse.getSetting(indexName, "index.refresh_interval"));
214+
}
215+
216+
public void testGetSettingsNonExistentIndex() throws IOException {
217+
String nonExistentIndex = "index_that_doesnt_exist";
218+
assertFalse(indexExists(nonExistentIndex));
219+
220+
GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices(nonExistentIndex);
221+
ElasticsearchException exception = expectThrows(ElasticsearchException.class,
222+
() -> execute(getSettingsRequest, highLevelClient().indices()::getSettings, highLevelClient().indices()::getSettingsAsync));
223+
assertEquals(RestStatus.NOT_FOUND, exception.status());
224+
}
225+
226+
public void testGetSettingsFromMultipleIndices() throws IOException {
227+
String indexName1 = "get_multiple_settings_one";
228+
createIndex(indexName1, Settings.builder().put("number_of_shards", 2).build());
229+
230+
String indexName2 = "get_multiple_settings_two";
231+
createIndex(indexName2, Settings.builder().put("number_of_shards", 3).build());
232+
233+
GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices("get_multiple_settings*");
234+
GetSettingsResponse getSettingsResponse = execute(getSettingsRequest, highLevelClient().indices()::getSettings,
235+
highLevelClient().indices()::getSettingsAsync);
236+
237+
assertEquals("2", getSettingsResponse.getSetting(indexName1, "index.number_of_shards"));
238+
assertEquals("3", getSettingsResponse.getSetting(indexName2, "index.number_of_shards"));
239+
}
240+
241+
public void testGetSettingsFiltered() throws IOException {
242+
String indexName = "get_settings_index";
243+
Settings basicSettings = Settings.builder()
244+
.put("number_of_shards", 1)
245+
.put("number_of_replicas", 0)
246+
.build();
247+
createIndex(indexName, basicSettings);
248+
249+
GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices(indexName).names("index.number_of_shards");
250+
GetSettingsResponse getSettingsResponse = execute(getSettingsRequest, highLevelClient().indices()::getSettings,
251+
highLevelClient().indices()::getSettingsAsync);
252+
253+
assertNull(getSettingsResponse.getSetting(indexName, "index.number_of_replicas"));
254+
assertEquals("1", getSettingsResponse.getSetting(indexName, "index.number_of_shards"));
255+
assertEquals(1, getSettingsResponse.getIndexToSettings().get("get_settings_index").size());
256+
}
257+
258+
public void testGetSettingsWithDefaults() throws IOException {
259+
String indexName = "get_settings_index";
260+
Settings basicSettings = Settings.builder()
261+
.put("number_of_shards", 1)
262+
.put("number_of_replicas", 0)
263+
.build();
264+
createIndex(indexName, basicSettings);
265+
266+
GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices(indexName).includeDefaults(true);
267+
GetSettingsResponse getSettingsResponse = execute(getSettingsRequest, highLevelClient().indices()::getSettings,
268+
highLevelClient().indices()::getSettingsAsync);
269+
270+
assertNotNull(getSettingsResponse.getSetting(indexName, "index.refresh_interval"));
271+
assertEquals(IndexSettings.DEFAULT_REFRESH_INTERVAL,
272+
getSettingsResponse.getIndexToDefaultSettings().get("get_settings_index").getAsTime("index.refresh_interval", null));
273+
assertEquals("1", getSettingsResponse.getSetting(indexName, "index.number_of_shards"));
274+
}
275+
276+
public void testGetSettingsWithDefaultsFiltered() throws IOException {
277+
String indexName = "get_settings_index";
278+
Settings basicSettings = Settings.builder()
279+
.put("number_of_shards", 1)
280+
.put("number_of_replicas", 0)
281+
.build();
282+
createIndex(indexName, basicSettings);
283+
284+
GetSettingsRequest getSettingsRequest = new GetSettingsRequest()
285+
.indices(indexName)
286+
.names("index.refresh_interval")
287+
.includeDefaults(true);
288+
GetSettingsResponse getSettingsResponse = execute(getSettingsRequest, highLevelClient().indices()::getSettings,
289+
highLevelClient().indices()::getSettingsAsync);
290+
291+
assertNull(getSettingsResponse.getSetting(indexName, "index.number_of_replicas"));
292+
assertNull(getSettingsResponse.getSetting(indexName, "index.number_of_shards"));
293+
assertEquals(0, getSettingsResponse.getIndexToSettings().get("get_settings_index").size());
294+
assertEquals(1, getSettingsResponse.getIndexToDefaultSettings().get("get_settings_index").size());
295+
}
192296
public void testPutMapping() throws IOException {
193297
{
194298
// Add mappings to index

client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
4848
import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
4949
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
50+
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest;
5051
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
5152
import org.elasticsearch.action.admin.indices.shrink.ResizeType;
5253
import org.elasticsearch.action.bulk.BulkRequest;
@@ -76,6 +77,7 @@
7677
import org.elasticsearch.common.bytes.BytesReference;
7778
import org.elasticsearch.common.io.Streams;
7879
import org.elasticsearch.common.lucene.uid.Versions;
80+
import org.elasticsearch.common.settings.IndexScopedSettings;
7981
import org.elasticsearch.common.unit.TimeValue;
8082
import org.elasticsearch.common.xcontent.ToXContent;
8183
import org.elasticsearch.common.xcontent.XContentBuilder;
@@ -405,6 +407,52 @@ public void testDeleteIndex() {
405407
assertNull(request.getEntity());
406408
}
407409

410+
public void testGetSettings() throws IOException {
411+
String[] indicesUnderTest = randomBoolean() ? null : randomIndicesNames(0, 5);
412+
413+
GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices(indicesUnderTest);
414+
415+
Map<String, String> expectedParams = new HashMap<>();
416+
setRandomMasterTimeout(getSettingsRequest, expectedParams);
417+
setRandomIndicesOptions(getSettingsRequest::indicesOptions, getSettingsRequest::indicesOptions, expectedParams);
418+
419+
setRandomLocal(getSettingsRequest, expectedParams);
420+
421+
if (randomBoolean()) {
422+
//the request object will not have include_defaults present unless it is set to true
423+
getSettingsRequest.includeDefaults(randomBoolean());
424+
if (getSettingsRequest.includeDefaults()) {
425+
expectedParams.put("include_defaults", Boolean.toString(true));
426+
}
427+
}
428+
429+
StringJoiner endpoint = new StringJoiner("/", "/", "");
430+
if (indicesUnderTest != null && indicesUnderTest.length > 0) {
431+
endpoint.add(String.join(",", indicesUnderTest));
432+
}
433+
endpoint.add("_settings");
434+
435+
if (randomBoolean()) {
436+
String[] names = randomBoolean() ? null : new String[randomIntBetween(0, 3)];
437+
if (names != null) {
438+
for (int x = 0; x < names.length; x++) {
439+
names[x] = randomAlphaOfLengthBetween(3, 10);
440+
}
441+
}
442+
getSettingsRequest.names(names);
443+
if (names != null && names.length > 0) {
444+
endpoint.add(String.join(",", names));
445+
}
446+
}
447+
448+
Request request = RequestConverters.getSettings(getSettingsRequest);
449+
450+
assertThat(endpoint.toString(), equalTo(request.getEndpoint()));
451+
assertThat(request.getParameters(), equalTo(expectedParams));
452+
assertThat(request.getMethod(), equalTo(HttpGet.METHOD_NAME));
453+
assertThat(request.getEntity(), nullValue());
454+
}
455+
408456
public void testDeleteIndexEmptyIndices() {
409457
String[] indices = randomBoolean() ? null : Strings.EMPTY_ARRAY;
410458
ActionRequestValidationException validationException = new DeleteIndexRequest(indices).validate();

client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IndicesClientDocumentationIT.java

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
import org.elasticsearch.action.admin.indices.rollover.RolloverResponse;
5151
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
5252
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsResponse;
53+
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest;
54+
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;
5355
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
5456
import org.elasticsearch.action.admin.indices.shrink.ResizeResponse;
5557
import org.elasticsearch.action.admin.indices.shrink.ResizeType;
@@ -775,6 +777,119 @@ public void onFailure(Exception e) {
775777
}
776778
}
777779

780+
public void testGetSettings() throws Exception {
781+
RestHighLevelClient client = highLevelClient();
782+
783+
{
784+
Settings settings = Settings.builder().put("number_of_shards", 3).build();
785+
CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("index", settings));
786+
assertTrue(createIndexResponse.isAcknowledged());
787+
}
788+
789+
// tag::get-settings-request
790+
GetSettingsRequest request = new GetSettingsRequest().indices("index");
791+
// end::get-settings-request
792+
793+
// tag::get-settings-request-names
794+
request.names("index.number_of_shards"); // <1>
795+
// end::get-settings-request-names
796+
797+
// tag::get-settings-request-indicesOptions
798+
request.indicesOptions(IndicesOptions.lenientExpandOpen()); // <1>
799+
// end::get-settings-request-indicesOptions
800+
801+
// tag::get-settings-execute
802+
GetSettingsResponse getSettingsResponse = client.indices().getSettings(request);
803+
// end::get-settings-execute
804+
805+
// tag::get-settings-response
806+
String numberOfShardsString = getSettingsResponse.getSetting("index", "index.number_of_shards"); // <1>
807+
Settings indexSettings = getSettingsResponse.getIndexToSettings().get("index"); // <2>
808+
Integer numberOfShards = indexSettings.getAsInt("index.number_of_shards", null); // <3>
809+
// end::get-settings-response
810+
811+
assertEquals("3", numberOfShardsString);
812+
assertEquals(Integer.valueOf(3), numberOfShards);
813+
814+
assertNull("refresh_interval returned but was never set!",
815+
getSettingsResponse.getSetting("index", "index.refresh_interval"));
816+
817+
// tag::get-settings-execute-listener
818+
ActionListener<GetSettingsResponse> listener =
819+
new ActionListener<GetSettingsResponse>() {
820+
@Override
821+
public void onResponse(GetSettingsResponse GetSettingsResponse) {
822+
// <1>
823+
}
824+
825+
@Override
826+
public void onFailure(Exception e) {
827+
// <2>
828+
}
829+
};
830+
// end::get-settings-execute-listener
831+
832+
// Replace the empty listener by a blocking listener in test
833+
final CountDownLatch latch = new CountDownLatch(1);
834+
listener = new LatchedActionListener<>(listener, latch);
835+
836+
// tag::get-settings-execute-async
837+
client.indices().getSettingsAsync(request, listener); // <1>
838+
// end::get-settings-execute-async
839+
840+
assertTrue(latch.await(30L, TimeUnit.SECONDS));
841+
}
842+
843+
public void testGetSettingsWithDefaults() throws Exception {
844+
RestHighLevelClient client = highLevelClient();
845+
846+
{
847+
Settings settings = Settings.builder().put("number_of_shards", 3).build();
848+
CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("index", settings));
849+
assertTrue(createIndexResponse.isAcknowledged());
850+
}
851+
852+
GetSettingsRequest request = new GetSettingsRequest().indices("index");
853+
request.indicesOptions(IndicesOptions.lenientExpandOpen());
854+
855+
// tag::get-settings-request-include-defaults
856+
request.includeDefaults(true); // <1>
857+
// end::get-settings-request-include-defaults
858+
859+
GetSettingsResponse getSettingsResponse = client.indices().getSettings(request);
860+
String numberOfShardsString = getSettingsResponse.getSetting("index", "index.number_of_shards");
861+
Settings indexSettings = getSettingsResponse.getIndexToSettings().get("index");
862+
Integer numberOfShards = indexSettings.getAsInt("index.number_of_shards", null);
863+
864+
// tag::get-settings-defaults-response
865+
String refreshInterval = getSettingsResponse.getSetting("index", "index.refresh_interval"); // <1>
866+
Settings indexDefaultSettings = getSettingsResponse.getIndexToDefaultSettings().get("index"); // <2>
867+
// end::get-settings-defaults-response
868+
869+
assertEquals("3", numberOfShardsString);
870+
assertEquals(Integer.valueOf(3), numberOfShards);
871+
assertNotNull("with defaults enabled we should get a value for refresh_interval!", refreshInterval);
872+
873+
assertEquals(refreshInterval, indexDefaultSettings.get("index.refresh_interval"));
874+
ActionListener<GetSettingsResponse> listener =
875+
new ActionListener<GetSettingsResponse>() {
876+
@Override
877+
public void onResponse(GetSettingsResponse GetSettingsResponse) {
878+
}
879+
880+
@Override
881+
public void onFailure(Exception e) {
882+
}
883+
};
884+
885+
// Replace the empty listener by a blocking listener in test
886+
final CountDownLatch latch = new CountDownLatch(1);
887+
listener = new LatchedActionListener<>(listener, latch);
888+
889+
client.indices().getSettingsAsync(request, listener);
890+
assertTrue(latch.await(30L, TimeUnit.SECONDS));
891+
}
892+
778893
public void testForceMergeIndex() throws Exception {
779894
RestHighLevelClient client = highLevelClient();
780895

0 commit comments

Comments
 (0)