use of org.opensearch.client.indices.GetIndexResponse in project OpenSearch by opensearch-project.
the class IndicesClientDocumentationIT method testGetIndex.
public void testGetIndex() throws Exception {
RestHighLevelClient client = highLevelClient();
{
Settings settings = Settings.builder().put("number_of_shards", 3).build();
String mappings = "{\"properties\":{\"field-1\":{\"type\":\"integer\"}}}";
CreateIndexRequest createIndexRequest = new CreateIndexRequest("index").settings(settings).mapping(mappings, XContentType.JSON);
CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
assertTrue(createIndexResponse.isAcknowledged());
}
// tag::get-index-request
// <1>
GetIndexRequest request = new GetIndexRequest("index");
// end::get-index-request
// tag::get-index-request-indicesOptions
// <1>
request.indicesOptions(IndicesOptions.lenientExpandOpen());
// end::get-index-request-indicesOptions
// tag::get-index-request-includeDefaults
// <1>
request.includeDefaults(true);
// end::get-index-request-includeDefaults
// tag::get-index-execute
GetIndexResponse getIndexResponse = client.indices().get(request, RequestOptions.DEFAULT);
// end::get-index-execute
// tag::get-index-response
// <1>
MappingMetadata indexMappings = getIndexResponse.getMappings().get("index");
// <2>
Map<String, Object> indexTypeMappings = indexMappings.getSourceAsMap();
// <3>
List<AliasMetadata> indexAliases = getIndexResponse.getAliases().get("index");
// <4>
String numberOfShardsString = getIndexResponse.getSetting("index", "index.number_of_shards");
// <5>
Settings indexSettings = getIndexResponse.getSettings().get("index");
// <6>
Integer numberOfShards = indexSettings.getAsInt("index.number_of_shards", null);
TimeValue time = getIndexResponse.getDefaultSettings().get("index").getAsTime("index.refresh_interval", // <7>
null);
// end::get-index-response
assertEquals(Collections.singletonMap("properties", Collections.singletonMap("field-1", Collections.singletonMap("type", "integer"))), indexTypeMappings);
assertTrue(indexAliases.isEmpty());
assertEquals(IndexSettings.DEFAULT_REFRESH_INTERVAL, time);
assertEquals("3", numberOfShardsString);
assertEquals(Integer.valueOf(3), numberOfShards);
// tag::get-index-execute-listener
ActionListener<GetIndexResponse> listener = new ActionListener<GetIndexResponse>() {
@Override
public void onResponse(GetIndexResponse getIndexResponse) {
// <1>
}
@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::get-index-execute-listener
// Replace the empty listener by a blocking listener in test
final CountDownLatch latch = new CountDownLatch(1);
listener = new LatchedActionListener<>(listener, latch);
// tag::get-index-execute-async
// <1>
client.indices().getAsync(request, RequestOptions.DEFAULT, listener);
// end::get-index-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
use of org.opensearch.client.indices.GetIndexResponse in project OpenSearch by opensearch-project.
the class IndicesClientIT method testGetIndex.
@SuppressWarnings("unchecked")
public void testGetIndex() throws IOException {
String indexName = "get_index_test";
Settings basicSettings = Settings.builder().put(SETTING_NUMBER_OF_SHARDS, 1).put(SETTING_NUMBER_OF_REPLICAS, 0).build();
String mappings = "\"properties\":{\"field-1\":{\"type\":\"integer\"}}";
createIndex(indexName, basicSettings, mappings);
GetIndexRequest getIndexRequest = new GetIndexRequest(indexName).includeDefaults(false);
GetIndexResponse getIndexResponse = execute(getIndexRequest, highLevelClient().indices()::get, highLevelClient().indices()::getAsync);
// default settings should be null
assertNull(getIndexResponse.getSetting(indexName, "index.refresh_interval"));
assertEquals("1", getIndexResponse.getSetting(indexName, SETTING_NUMBER_OF_SHARDS));
assertEquals("0", getIndexResponse.getSetting(indexName, SETTING_NUMBER_OF_REPLICAS));
assertNotNull(getIndexResponse.getMappings().get(indexName));
assertNotNull(getIndexResponse.getMappings().get(indexName));
MappingMetadata mappingMetadata = getIndexResponse.getMappings().get(indexName);
assertNotNull(mappingMetadata);
assertEquals("_doc", mappingMetadata.type());
assertEquals("{\"properties\":{\"field-1\":{\"type\":\"integer\"}}}", mappingMetadata.source().string());
Object o = mappingMetadata.getSourceAsMap().get("properties");
assertThat(o, instanceOf(Map.class));
// noinspection unchecked
assertThat(((Map<String, Object>) o).get("field-1"), instanceOf(Map.class));
// noinspection unchecked
Map<String, Object> fieldMapping = (Map<String, Object>) ((Map<String, Object>) o).get("field-1");
assertEquals("integer", fieldMapping.get("type"));
}
use of org.opensearch.client.indices.GetIndexResponse in project OpenSearch by opensearch-project.
the class IndicesClientIT method testGetIndexWithDefaults.
@SuppressWarnings("unchecked")
public void testGetIndexWithDefaults() throws IOException {
String indexName = "get_index_test";
Settings basicSettings = Settings.builder().put(SETTING_NUMBER_OF_SHARDS, 1).put(SETTING_NUMBER_OF_REPLICAS, 0).build();
String mappings = "\"properties\":{\"field-1\":{\"type\":\"integer\"}}";
createIndex(indexName, basicSettings, mappings);
GetIndexRequest getIndexRequest = new GetIndexRequest(indexName).includeDefaults(true);
GetIndexResponse getIndexResponse = execute(getIndexRequest, highLevelClient().indices()::get, highLevelClient().indices()::getAsync);
assertNotNull(getIndexResponse.getSetting(indexName, "index.refresh_interval"));
assertEquals(IndexSettings.DEFAULT_REFRESH_INTERVAL, getIndexResponse.getDefaultSettings().get(indexName).getAsTime("index.refresh_interval", null));
assertEquals("1", getIndexResponse.getSetting(indexName, SETTING_NUMBER_OF_SHARDS));
assertEquals("0", getIndexResponse.getSetting(indexName, SETTING_NUMBER_OF_REPLICAS));
assertNotNull(getIndexResponse.getMappings().get(indexName));
assertNotNull(getIndexResponse.getMappings().get(indexName));
Object o = getIndexResponse.getMappings().get(indexName).getSourceAsMap().get("properties");
assertThat(o, instanceOf(Map.class));
assertThat(((Map<String, Object>) o).get("field-1"), instanceOf(Map.class));
Map<String, Object> fieldMapping = (Map<String, Object>) ((Map<String, Object>) o).get("field-1");
assertEquals("integer", fieldMapping.get("type"));
}
Aggregations