Skip to content

Commit 95458ab

Browse files
sohaibiftikharnik9000
authored andcommitted
REST high-level client: add get index API (#31703)
Also added master_timeout parameter for the indices.get spec Relates to #27205
1 parent 4c24e41 commit 95458ab

File tree

8 files changed

+322
-0
lines changed

8 files changed

+322
-0
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest;
4141
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeResponse;
4242
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
43+
import org.elasticsearch.action.admin.indices.get.GetIndexResponse;
4344
import org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsRequest;
4445
import org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse;
4546
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest;
@@ -672,6 +673,34 @@ public void getSettingsAsync(GetSettingsRequest getSettingsRequest, RequestOptio
672673
GetSettingsResponse::fromXContent, listener, emptySet());
673674
}
674675

676+
/**
677+
* Retrieve information about one or more indexes
678+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-index.html">
679+
* Indices Get Index API on elastic.co</a>
680+
* @param getIndexRequest the request
681+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
682+
* @return the response
683+
* @throws IOException in case there is a problem sending the request or parsing back the response
684+
*/
685+
public GetIndexResponse get(GetIndexRequest getIndexRequest, RequestOptions options) throws IOException {
686+
return restHighLevelClient.performRequestAndParseEntity(getIndexRequest, RequestConverters::getIndex, options,
687+
GetIndexResponse::fromXContent, emptySet());
688+
}
689+
690+
/**
691+
* Retrieve information about one or more indexes
692+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-index.html">
693+
* Indices Get Index API on elastic.co</a>
694+
* @param getIndexRequest the request
695+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
696+
* @param listener the listener to be notified upon request completion
697+
*/
698+
public void getAsync(GetIndexRequest getIndexRequest, RequestOptions options,
699+
ActionListener<GetIndexResponse> listener) {
700+
restHighLevelClient.performRequestAsyncAndParseEntity(getIndexRequest, RequestConverters::getIndex, options,
701+
GetIndexResponse::fromXContent, listener, emptySet());
702+
}
703+
675704
/**
676705
* Force merge one or more indices using the Force Merge API.
677706
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-forcemerge.html">

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,22 @@ static Request getSettings(GetSettingsRequest getSettingsRequest) {
844844
return request;
845845
}
846846

847+
static Request getIndex(GetIndexRequest getIndexRequest) {
848+
String[] indices = getIndexRequest.indices() == null ? Strings.EMPTY_ARRAY : getIndexRequest.indices();
849+
850+
String endpoint = endpoint(indices);
851+
Request request = new Request(HttpGet.METHOD_NAME, endpoint);
852+
853+
Params params = new Params(request);
854+
params.withIndicesOptions(getIndexRequest.indicesOptions());
855+
params.withLocal(getIndexRequest.local());
856+
params.withIncludeDefaults(getIndexRequest.includeDefaults());
857+
params.withHuman(getIndexRequest.humanReadable());
858+
params.withMasterTimeout(getIndexRequest.masterNodeTimeout());
859+
860+
return request;
861+
}
862+
847863
static Request indicesExist(GetIndexRequest getIndexRequest) {
848864
// this can be called with no indices as argument by transport client, not via REST though
849865
if (getIndexRequest.indices() == null || getIndexRequest.indices().length == 0) {

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

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest;
4646
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeResponse;
4747
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
48+
import org.elasticsearch.action.admin.indices.get.GetIndexResponse;
4849
import org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsRequest;
4950
import org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse;
5051
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest;
@@ -100,6 +101,7 @@
100101
import java.util.Map;
101102

102103
import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_REPLICAS;
104+
import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_SHARDS;
103105
import static org.elasticsearch.common.xcontent.support.XContentMapValues.extractRawValues;
104106
import static org.elasticsearch.common.xcontent.support.XContentMapValues.extractValue;
105107
import static org.hamcrest.CoreMatchers.hasItem;
@@ -113,6 +115,7 @@
113115
import static org.hamcrest.Matchers.notNullValue;
114116
import static org.hamcrest.Matchers.nullValue;
115117
import static org.hamcrest.Matchers.startsWith;
118+
import static org.hamcrest.core.IsInstanceOf.instanceOf;
116119

117120
public class IndicesClientIT extends ESRestHighLevelClientTestCase {
118121

@@ -335,6 +338,75 @@ public void testGetSettingsWithDefaultsFiltered() throws IOException {
335338
assertEquals(1, getSettingsResponse.getIndexToDefaultSettings().get("get_settings_index").size());
336339
}
337340

341+
@SuppressWarnings("unchecked")
342+
public void testGetIndex() throws IOException {
343+
String indexName = "get_index_test";
344+
Settings basicSettings = Settings.builder()
345+
.put(SETTING_NUMBER_OF_SHARDS, 1)
346+
.put(SETTING_NUMBER_OF_REPLICAS, 0)
347+
.build();
348+
String mappings = "\"type-1\":{\"properties\":{\"field-1\":{\"type\":\"integer\"}}}";
349+
createIndex(indexName, basicSettings, mappings);
350+
351+
GetIndexRequest getIndexRequest = new GetIndexRequest()
352+
.indices(indexName).includeDefaults(false);
353+
GetIndexResponse getIndexResponse =
354+
execute(getIndexRequest, highLevelClient().indices()::get, highLevelClient().indices()::getAsync);
355+
356+
// default settings should be null
357+
assertNull(getIndexResponse.getSetting(indexName, "index.refresh_interval"));
358+
assertEquals("1", getIndexResponse.getSetting(indexName, SETTING_NUMBER_OF_SHARDS));
359+
assertEquals("0", getIndexResponse.getSetting(indexName, SETTING_NUMBER_OF_REPLICAS));
360+
assertNotNull(getIndexResponse.getMappings().get(indexName));
361+
assertNotNull(getIndexResponse.getMappings().get(indexName).get("type-1"));
362+
Object o = getIndexResponse.getMappings().get(indexName).get("type-1").getSourceAsMap().get("properties");
363+
assertThat(o, instanceOf(Map.class));
364+
//noinspection unchecked
365+
assertThat(((Map<String, Object>) o).get("field-1"), instanceOf(Map.class));
366+
//noinspection unchecked
367+
Map<String, Object> fieldMapping = (Map<String, Object>) ((Map<String, Object>) o).get("field-1");
368+
assertEquals("integer", fieldMapping.get("type"));
369+
}
370+
371+
@SuppressWarnings("unchecked")
372+
public void testGetIndexWithDefaults() throws IOException {
373+
String indexName = "get_index_test";
374+
Settings basicSettings = Settings.builder()
375+
.put(SETTING_NUMBER_OF_SHARDS, 1)
376+
.put(SETTING_NUMBER_OF_REPLICAS, 0)
377+
.build();
378+
String mappings = "\"type-1\":{\"properties\":{\"field-1\":{\"type\":\"integer\"}}}";
379+
createIndex(indexName, basicSettings, mappings);
380+
381+
GetIndexRequest getIndexRequest = new GetIndexRequest()
382+
.indices(indexName).includeDefaults(true);
383+
GetIndexResponse getIndexResponse =
384+
execute(getIndexRequest, highLevelClient().indices()::get, highLevelClient().indices()::getAsync);
385+
386+
assertNotNull(getIndexResponse.getSetting(indexName, "index.refresh_interval"));
387+
assertEquals(IndexSettings.DEFAULT_REFRESH_INTERVAL,
388+
getIndexResponse.defaultSettings().get(indexName).getAsTime("index.refresh_interval", null));
389+
assertEquals("1", getIndexResponse.getSetting(indexName, SETTING_NUMBER_OF_SHARDS));
390+
assertEquals("0", getIndexResponse.getSetting(indexName, SETTING_NUMBER_OF_REPLICAS));
391+
assertNotNull(getIndexResponse.getMappings().get(indexName));
392+
assertNotNull(getIndexResponse.getMappings().get(indexName).get("type-1"));
393+
Object o = getIndexResponse.getMappings().get(indexName).get("type-1").getSourceAsMap().get("properties");
394+
assertThat(o, instanceOf(Map.class));
395+
assertThat(((Map<String, Object>) o).get("field-1"), instanceOf(Map.class));
396+
Map<String, Object> fieldMapping = (Map<String, Object>) ((Map<String, Object>) o).get("field-1");
397+
assertEquals("integer", fieldMapping.get("type"));
398+
}
399+
400+
public void testGetIndexNonExistentIndex() throws IOException {
401+
String nonExistentIndex = "index_that_doesnt_exist";
402+
assertFalse(indexExists(nonExistentIndex));
403+
404+
GetIndexRequest getIndexRequest = new GetIndexRequest().indices(nonExistentIndex);
405+
ElasticsearchException exception = expectThrows(ElasticsearchException.class,
406+
() -> execute(getIndexRequest, highLevelClient().indices()::get, highLevelClient().indices()::getAsync));
407+
assertEquals(RestStatus.NOT_FOUND, exception.status());
408+
}
409+
338410
public void testPutMapping() throws IOException {
339411
// Add mappings to index
340412
String indexName = "mapping_index";

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,39 @@ public void testGetSettings() throws IOException {
592592
assertThat(request.getEntity(), nullValue());
593593
}
594594

595+
public void testGetIndex() throws IOException {
596+
String[] indicesUnderTest = randomBoolean() ? null : randomIndicesNames(0, 5);
597+
598+
GetIndexRequest getIndexRequest = new GetIndexRequest().indices(indicesUnderTest);
599+
600+
Map<String, String> expectedParams = new HashMap<>();
601+
setRandomMasterTimeout(getIndexRequest, expectedParams);
602+
setRandomIndicesOptions(getIndexRequest::indicesOptions, getIndexRequest::indicesOptions, expectedParams);
603+
setRandomLocal(getIndexRequest, expectedParams);
604+
setRandomHumanReadable(getIndexRequest, expectedParams);
605+
606+
if (randomBoolean()) {
607+
// the request object will not have include_defaults present unless it is set to
608+
// true
609+
getIndexRequest.includeDefaults(randomBoolean());
610+
if (getIndexRequest.includeDefaults()) {
611+
expectedParams.put("include_defaults", Boolean.toString(true));
612+
}
613+
}
614+
615+
StringJoiner endpoint = new StringJoiner("/", "/", "");
616+
if (indicesUnderTest != null && indicesUnderTest.length > 0) {
617+
endpoint.add(String.join(",", indicesUnderTest));
618+
}
619+
620+
Request request = RequestConverters.getIndex(getIndexRequest);
621+
622+
assertThat(endpoint.toString(), equalTo(request.getEndpoint()));
623+
assertThat(request.getParameters(), equalTo(expectedParams));
624+
assertThat(request.getMethod(), equalTo(HttpGet.METHOD_NAME));
625+
assertThat(request.getEntity(), nullValue());
626+
}
627+
595628
public void testDeleteIndexEmptyIndices() {
596629
String[] indices = randomBoolean() ? null : Strings.EMPTY_ARRAY;
597630
ActionRequestValidationException validationException = new DeleteIndexRequest(indices).validate();

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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest;
4545
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeResponse;
4646
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
47+
import org.elasticsearch.action.admin.indices.get.GetIndexResponse;
4748
import org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsRequest;
4849
import org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse;
4950
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest;
@@ -89,12 +90,14 @@
8990
import org.elasticsearch.common.xcontent.XContentBuilder;
9091
import org.elasticsearch.common.xcontent.XContentFactory;
9192
import org.elasticsearch.common.xcontent.XContentType;
93+
import org.elasticsearch.index.IndexSettings;
9294
import org.elasticsearch.index.query.QueryBuilder;
9395
import org.elasticsearch.index.query.QueryBuilders;
9496
import org.elasticsearch.rest.RestStatus;
9597

9698
import java.io.IOException;
9799
import java.util.Arrays;
100+
import java.util.Collections;
98101
import java.util.HashMap;
99102
import java.util.List;
100103
import java.util.Map;
@@ -1235,6 +1238,81 @@ public void onFailure(Exception e) {
12351238
assertTrue(latch.await(30L, TimeUnit.SECONDS));
12361239
}
12371240

1241+
public void testGetIndex() throws Exception {
1242+
RestHighLevelClient client = highLevelClient();
1243+
1244+
{
1245+
Settings settings = Settings.builder().put("number_of_shards", 3).build();
1246+
String mappings = "{\"properties\":{\"field-1\":{\"type\":\"integer\"}}}";
1247+
CreateIndexResponse createIndexResponse = client.indices().create(
1248+
new CreateIndexRequest("index", settings).mapping("doc", mappings, XContentType.JSON),
1249+
RequestOptions.DEFAULT);
1250+
assertTrue(createIndexResponse.isAcknowledged());
1251+
}
1252+
1253+
// tag::get-index-request
1254+
GetIndexRequest request = new GetIndexRequest().indices("index"); // <1>
1255+
// end::get-index-request
1256+
1257+
// tag::get-index-request-indicesOptions
1258+
request.indicesOptions(IndicesOptions.lenientExpandOpen()); // <1>
1259+
// end::get-index-request-indicesOptions
1260+
1261+
// tag::get-index-request-includeDefaults
1262+
request.includeDefaults(true); // <1>
1263+
// end::get-index-request-includeDefaults
1264+
1265+
// tag::get-index-execute
1266+
GetIndexResponse getIndexResponse = client.indices().get(request, RequestOptions.DEFAULT);
1267+
// end::get-index-execute
1268+
1269+
// tag::get-index-response
1270+
ImmutableOpenMap<String, MappingMetaData> indexMappings = getIndexResponse.getMappings().get("index"); // <1>
1271+
Map<String, Object> indexTypeMappings = indexMappings.get("doc").getSourceAsMap(); // <2>
1272+
List<AliasMetaData> indexAliases = getIndexResponse.getAliases().get("index"); // <3>
1273+
String numberOfShardsString = getIndexResponse.getSetting("index", "index.number_of_shards"); // <4>
1274+
Settings indexSettings = getIndexResponse.getSettings().get("index"); // <5>
1275+
Integer numberOfShards = indexSettings.getAsInt("index.number_of_shards", null); // <6>
1276+
TimeValue time = getIndexResponse.defaultSettings().get("index")
1277+
.getAsTime("index.refresh_interval", null); // <7>
1278+
// end::get-index-response
1279+
1280+
assertEquals(
1281+
Collections.singletonMap("properties",
1282+
Collections.singletonMap("field-1", Collections.singletonMap("type", "integer"))),
1283+
indexTypeMappings
1284+
);
1285+
assertTrue(indexAliases.isEmpty());
1286+
assertEquals(IndexSettings.DEFAULT_REFRESH_INTERVAL, time);
1287+
assertEquals("3", numberOfShardsString);
1288+
assertEquals(Integer.valueOf(3), numberOfShards);
1289+
1290+
// tag::get-index-execute-listener
1291+
ActionListener<GetIndexResponse> listener =
1292+
new ActionListener<GetIndexResponse>() {
1293+
@Override
1294+
public void onResponse(GetIndexResponse getIndexResponse) {
1295+
// <1>
1296+
}
1297+
1298+
@Override
1299+
public void onFailure(Exception e) {
1300+
// <2>
1301+
}
1302+
};
1303+
// end::get-index-execute-listener
1304+
1305+
// Replace the empty listener by a blocking listener in test
1306+
final CountDownLatch latch = new CountDownLatch(1);
1307+
listener = new LatchedActionListener<>(listener, latch);
1308+
1309+
// tag::get-index-execute-async
1310+
client.indices().getAsync(request, RequestOptions.DEFAULT, listener); // <1>
1311+
// end::get-index-execute-async
1312+
1313+
assertTrue(latch.await(30L, TimeUnit.SECONDS));
1314+
}
1315+
12381316
public void testForceMergeIndex() throws Exception {
12391317
RestHighLevelClient client = highLevelClient();
12401318

0 commit comments

Comments
 (0)