|
45 | 45 | import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest;
|
46 | 46 | import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeResponse;
|
47 | 47 | import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
|
| 48 | +import org.elasticsearch.action.admin.indices.get.GetIndexResponse; |
48 | 49 | import org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsRequest;
|
49 | 50 | import org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse;
|
50 | 51 | import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest;
|
|
99 | 100 | import java.util.Map;
|
100 | 101 |
|
101 | 102 | import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_REPLICAS;
|
| 103 | +import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_SHARDS; |
102 | 104 | import static org.elasticsearch.common.xcontent.support.XContentMapValues.extractRawValues;
|
103 | 105 | import static org.elasticsearch.common.xcontent.support.XContentMapValues.extractValue;
|
104 | 106 | import static org.hamcrest.CoreMatchers.hasItem;
|
|
112 | 114 | import static org.hamcrest.Matchers.notNullValue;
|
113 | 115 | import static org.hamcrest.Matchers.nullValue;
|
114 | 116 | import static org.hamcrest.Matchers.startsWith;
|
| 117 | +import static org.hamcrest.core.IsInstanceOf.instanceOf; |
115 | 118 |
|
116 | 119 | public class IndicesClientIT extends ESRestHighLevelClientTestCase {
|
117 | 120 |
|
@@ -326,6 +329,75 @@ public void testGetSettingsWithDefaultsFiltered() throws IOException {
|
326 | 329 | assertEquals(1, getSettingsResponse.getIndexToDefaultSettings().get("get_settings_index").size());
|
327 | 330 | }
|
328 | 331 |
|
| 332 | + @SuppressWarnings("unchecked") |
| 333 | + public void testGetIndex() throws IOException { |
| 334 | + String indexName = "get_index_test"; |
| 335 | + Settings basicSettings = Settings.builder() |
| 336 | + .put(SETTING_NUMBER_OF_SHARDS, 1) |
| 337 | + .put(SETTING_NUMBER_OF_REPLICAS, 0) |
| 338 | + .build(); |
| 339 | + String mappings = "\"type-1\":{\"properties\":{\"field-1\":{\"type\":\"integer\"}}}"; |
| 340 | + createIndex(indexName, basicSettings, mappings); |
| 341 | + |
| 342 | + GetIndexRequest getIndexRequest = new GetIndexRequest() |
| 343 | + .indices(indexName).includeDefaults(false); |
| 344 | + GetIndexResponse getIndexResponse = |
| 345 | + execute(getIndexRequest, highLevelClient().indices()::get, highLevelClient().indices()::getAsync); |
| 346 | + |
| 347 | + // default settings should be null |
| 348 | + assertNull(getIndexResponse.getSetting(indexName, "index.refresh_interval")); |
| 349 | + assertEquals("1", getIndexResponse.getSetting(indexName, SETTING_NUMBER_OF_SHARDS)); |
| 350 | + assertEquals("0", getIndexResponse.getSetting(indexName, SETTING_NUMBER_OF_REPLICAS)); |
| 351 | + assertNotNull(getIndexResponse.getMappings().get(indexName)); |
| 352 | + assertNotNull(getIndexResponse.getMappings().get(indexName).get("type-1")); |
| 353 | + Object o = getIndexResponse.getMappings().get(indexName).get("type-1").getSourceAsMap().get("properties"); |
| 354 | + assertThat(o, instanceOf(Map.class)); |
| 355 | + //noinspection unchecked |
| 356 | + assertThat(((Map<String, Object>) o).get("field-1"), instanceOf(Map.class)); |
| 357 | + //noinspection unchecked |
| 358 | + Map<String, Object> fieldMapping = (Map<String, Object>) ((Map<String, Object>) o).get("field-1"); |
| 359 | + assertEquals("integer", fieldMapping.get("type")); |
| 360 | + } |
| 361 | + |
| 362 | + @SuppressWarnings("unchecked") |
| 363 | + public void testGetIndexWithDefaults() throws IOException { |
| 364 | + String indexName = "get_index_test"; |
| 365 | + Settings basicSettings = Settings.builder() |
| 366 | + .put(SETTING_NUMBER_OF_SHARDS, 1) |
| 367 | + .put(SETTING_NUMBER_OF_REPLICAS, 0) |
| 368 | + .build(); |
| 369 | + String mappings = "\"type-1\":{\"properties\":{\"field-1\":{\"type\":\"integer\"}}}"; |
| 370 | + createIndex(indexName, basicSettings, mappings); |
| 371 | + |
| 372 | + GetIndexRequest getIndexRequest = new GetIndexRequest() |
| 373 | + .indices(indexName).includeDefaults(true); |
| 374 | + GetIndexResponse getIndexResponse = |
| 375 | + execute(getIndexRequest, highLevelClient().indices()::get, highLevelClient().indices()::getAsync); |
| 376 | + |
| 377 | + assertNotNull(getIndexResponse.getSetting(indexName, "index.refresh_interval")); |
| 378 | + assertEquals(IndexSettings.DEFAULT_REFRESH_INTERVAL, |
| 379 | + getIndexResponse.defaultSettings().get(indexName).getAsTime("index.refresh_interval", null)); |
| 380 | + assertEquals("1", getIndexResponse.getSetting(indexName, SETTING_NUMBER_OF_SHARDS)); |
| 381 | + assertEquals("0", getIndexResponse.getSetting(indexName, SETTING_NUMBER_OF_REPLICAS)); |
| 382 | + assertNotNull(getIndexResponse.getMappings().get(indexName)); |
| 383 | + assertNotNull(getIndexResponse.getMappings().get(indexName).get("type-1")); |
| 384 | + Object o = getIndexResponse.getMappings().get(indexName).get("type-1").getSourceAsMap().get("properties"); |
| 385 | + assertThat(o, instanceOf(Map.class)); |
| 386 | + assertThat(((Map<String, Object>) o).get("field-1"), instanceOf(Map.class)); |
| 387 | + Map<String, Object> fieldMapping = (Map<String, Object>) ((Map<String, Object>) o).get("field-1"); |
| 388 | + assertEquals("integer", fieldMapping.get("type")); |
| 389 | + } |
| 390 | + |
| 391 | + public void testGetIndexNonExistentIndex() throws IOException { |
| 392 | + String nonExistentIndex = "index_that_doesnt_exist"; |
| 393 | + assertFalse(indexExists(nonExistentIndex)); |
| 394 | + |
| 395 | + GetIndexRequest getIndexRequest = new GetIndexRequest().indices(nonExistentIndex); |
| 396 | + ElasticsearchException exception = expectThrows(ElasticsearchException.class, |
| 397 | + () -> execute(getIndexRequest, highLevelClient().indices()::get, highLevelClient().indices()::getAsync)); |
| 398 | + assertEquals(RestStatus.NOT_FOUND, exception.status()); |
| 399 | + } |
| 400 | + |
329 | 401 | public void testPutMapping() throws IOException {
|
330 | 402 | // Add mappings to index
|
331 | 403 | String indexName = "mapping_index";
|
|
0 commit comments