|
51 | 51 | import org.elasticsearch.action.admin.indices.rollover.RolloverResponse;
|
52 | 52 | import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
|
53 | 53 | 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; |
54 | 56 | import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
|
55 | 57 | import org.elasticsearch.action.admin.indices.shrink.ResizeResponse;
|
56 | 58 | import org.elasticsearch.action.admin.indices.shrink.ResizeType;
|
| 59 | +import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest; |
| 60 | +import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateResponse; |
57 | 61 | import org.elasticsearch.action.index.IndexRequest;
|
58 | 62 | import org.elasticsearch.action.support.IndicesOptions;
|
59 | 63 | import org.elasticsearch.action.support.WriteRequest;
|
60 | 64 | import org.elasticsearch.action.support.broadcast.BroadcastResponse;
|
61 | 65 | import org.elasticsearch.cluster.metadata.IndexMetaData;
|
| 66 | +import org.elasticsearch.common.ValidationException; |
62 | 67 | import org.elasticsearch.common.settings.Setting;
|
63 | 68 | import org.elasticsearch.common.settings.Settings;
|
64 | 69 | import org.elasticsearch.common.unit.ByteSizeUnit;
|
|
71 | 76 | import org.elasticsearch.rest.RestStatus;
|
72 | 77 |
|
73 | 78 | import java.io.IOException;
|
| 79 | +import java.util.Arrays; |
| 80 | +import java.util.Collections; |
74 | 81 | import java.util.Map;
|
75 | 82 |
|
76 | 83 | import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_REPLICAS;
|
| 84 | +import static org.elasticsearch.common.xcontent.support.XContentMapValues.extractRawValues; |
| 85 | +import static org.elasticsearch.common.xcontent.support.XContentMapValues.extractValue; |
77 | 86 | import static org.hamcrest.CoreMatchers.hasItem;
|
| 87 | +import static org.hamcrest.Matchers.contains; |
| 88 | +import static org.hamcrest.Matchers.containsString; |
78 | 89 | import static org.hamcrest.Matchers.equalTo;
|
| 90 | +import static org.hamcrest.Matchers.hasEntry; |
| 91 | +import static org.hamcrest.Matchers.hasSize; |
79 | 92 | import static org.hamcrest.Matchers.not;
|
80 | 93 | import static org.hamcrest.Matchers.startsWith;
|
81 | 94 |
|
@@ -189,6 +202,108 @@ public void testCreateIndex() throws IOException {
|
189 | 202 | }
|
190 | 203 | }
|
191 | 204 |
|
| 205 | + public void testGetSettings() throws IOException { |
| 206 | + String indexName = "get_settings_index"; |
| 207 | + Settings basicSettings = Settings.builder() |
| 208 | + .put("number_of_shards", 1) |
| 209 | + .put("number_of_replicas", 0) |
| 210 | + .build(); |
| 211 | + createIndex(indexName, basicSettings); |
| 212 | + |
| 213 | + GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices(indexName); |
| 214 | + GetSettingsResponse getSettingsResponse = execute(getSettingsRequest, highLevelClient().indices()::getSettings, |
| 215 | + highLevelClient().indices()::getSettingsAsync); |
| 216 | + |
| 217 | + assertNull(getSettingsResponse.getSetting(indexName, "index.refresh_interval")); |
| 218 | + assertEquals("1", getSettingsResponse.getSetting(indexName, "index.number_of_shards")); |
| 219 | + |
| 220 | + updateIndexSettings(indexName, Settings.builder().put("refresh_interval", "30s")); |
| 221 | + |
| 222 | + GetSettingsResponse updatedResponse = execute(getSettingsRequest, highLevelClient().indices()::getSettings, |
| 223 | + highLevelClient().indices()::getSettingsAsync); |
| 224 | + assertEquals("30s", updatedResponse.getSetting(indexName, "index.refresh_interval")); |
| 225 | + } |
| 226 | + |
| 227 | + public void testGetSettingsNonExistentIndex() throws IOException { |
| 228 | + String nonExistentIndex = "index_that_doesnt_exist"; |
| 229 | + assertFalse(indexExists(nonExistentIndex)); |
| 230 | + |
| 231 | + GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices(nonExistentIndex); |
| 232 | + ElasticsearchException exception = expectThrows(ElasticsearchException.class, |
| 233 | + () -> execute(getSettingsRequest, highLevelClient().indices()::getSettings, highLevelClient().indices()::getSettingsAsync)); |
| 234 | + assertEquals(RestStatus.NOT_FOUND, exception.status()); |
| 235 | + } |
| 236 | + |
| 237 | + public void testGetSettingsFromMultipleIndices() throws IOException { |
| 238 | + String indexName1 = "get_multiple_settings_one"; |
| 239 | + createIndex(indexName1, Settings.builder().put("number_of_shards", 2).build()); |
| 240 | + |
| 241 | + String indexName2 = "get_multiple_settings_two"; |
| 242 | + createIndex(indexName2, Settings.builder().put("number_of_shards", 3).build()); |
| 243 | + |
| 244 | + GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices("get_multiple_settings*"); |
| 245 | + GetSettingsResponse getSettingsResponse = execute(getSettingsRequest, highLevelClient().indices()::getSettings, |
| 246 | + highLevelClient().indices()::getSettingsAsync); |
| 247 | + |
| 248 | + assertEquals("2", getSettingsResponse.getSetting(indexName1, "index.number_of_shards")); |
| 249 | + assertEquals("3", getSettingsResponse.getSetting(indexName2, "index.number_of_shards")); |
| 250 | + } |
| 251 | + |
| 252 | + public void testGetSettingsFiltered() throws IOException { |
| 253 | + String indexName = "get_settings_index"; |
| 254 | + Settings basicSettings = Settings.builder() |
| 255 | + .put("number_of_shards", 1) |
| 256 | + .put("number_of_replicas", 0) |
| 257 | + .build(); |
| 258 | + createIndex(indexName, basicSettings); |
| 259 | + |
| 260 | + GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices(indexName).names("index.number_of_shards"); |
| 261 | + GetSettingsResponse getSettingsResponse = execute(getSettingsRequest, highLevelClient().indices()::getSettings, |
| 262 | + highLevelClient().indices()::getSettingsAsync); |
| 263 | + |
| 264 | + assertNull(getSettingsResponse.getSetting(indexName, "index.number_of_replicas")); |
| 265 | + assertEquals("1", getSettingsResponse.getSetting(indexName, "index.number_of_shards")); |
| 266 | + assertEquals(1, getSettingsResponse.getIndexToSettings().get("get_settings_index").size()); |
| 267 | + } |
| 268 | + |
| 269 | + public void testGetSettingsWithDefaults() throws IOException { |
| 270 | + String indexName = "get_settings_index"; |
| 271 | + Settings basicSettings = Settings.builder() |
| 272 | + .put("number_of_shards", 1) |
| 273 | + .put("number_of_replicas", 0) |
| 274 | + .build(); |
| 275 | + createIndex(indexName, basicSettings); |
| 276 | + |
| 277 | + GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices(indexName).includeDefaults(true); |
| 278 | + GetSettingsResponse getSettingsResponse = execute(getSettingsRequest, highLevelClient().indices()::getSettings, |
| 279 | + highLevelClient().indices()::getSettingsAsync); |
| 280 | + |
| 281 | + assertNotNull(getSettingsResponse.getSetting(indexName, "index.refresh_interval")); |
| 282 | + assertEquals(IndexSettings.DEFAULT_REFRESH_INTERVAL, |
| 283 | + getSettingsResponse.getIndexToDefaultSettings().get("get_settings_index").getAsTime("index.refresh_interval", null)); |
| 284 | + assertEquals("1", getSettingsResponse.getSetting(indexName, "index.number_of_shards")); |
| 285 | + } |
| 286 | + |
| 287 | + public void testGetSettingsWithDefaultsFiltered() throws IOException { |
| 288 | + String indexName = "get_settings_index"; |
| 289 | + Settings basicSettings = Settings.builder() |
| 290 | + .put("number_of_shards", 1) |
| 291 | + .put("number_of_replicas", 0) |
| 292 | + .build(); |
| 293 | + createIndex(indexName, basicSettings); |
| 294 | + |
| 295 | + GetSettingsRequest getSettingsRequest = new GetSettingsRequest() |
| 296 | + .indices(indexName) |
| 297 | + .names("index.refresh_interval") |
| 298 | + .includeDefaults(true); |
| 299 | + GetSettingsResponse getSettingsResponse = execute(getSettingsRequest, highLevelClient().indices()::getSettings, |
| 300 | + highLevelClient().indices()::getSettingsAsync); |
| 301 | + |
| 302 | + assertNull(getSettingsResponse.getSetting(indexName, "index.number_of_replicas")); |
| 303 | + assertNull(getSettingsResponse.getSetting(indexName, "index.number_of_shards")); |
| 304 | + assertEquals(0, getSettingsResponse.getIndexToSettings().get("get_settings_index").size()); |
| 305 | + assertEquals(1, getSettingsResponse.getIndexToDefaultSettings().get("get_settings_index").size()); |
| 306 | + } |
192 | 307 | public void testPutMapping() throws IOException {
|
193 | 308 | {
|
194 | 309 | // Add mappings to index
|
@@ -708,4 +823,59 @@ public void testIndexPutSettingNonExistent() throws IOException {
|
708 | 823 | + "or check the breaking changes documentation for removed settings]"));
|
709 | 824 | }
|
710 | 825 |
|
| 826 | + @SuppressWarnings("unchecked") |
| 827 | + public void testPutTemplate() throws Exception { |
| 828 | + PutIndexTemplateRequest putTemplateRequest = new PutIndexTemplateRequest() |
| 829 | + .name("my-template") |
| 830 | + .patterns(Arrays.asList("pattern-1", "name-*")) |
| 831 | + .order(10) |
| 832 | + .create(randomBoolean()) |
| 833 | + .settings(Settings.builder().put("number_of_shards", "3").put("number_of_replicas", "0")) |
| 834 | + .mapping("doc", "host_name", "type=keyword", "description", "type=text") |
| 835 | + .alias(new Alias("alias-1").indexRouting("abc")).alias(new Alias("{index}-write").searchRouting("xyz")); |
| 836 | + |
| 837 | + PutIndexTemplateResponse putTemplateResponse = execute(putTemplateRequest, |
| 838 | + highLevelClient().indices()::putTemplate, highLevelClient().indices()::putTemplateAsync); |
| 839 | + assertThat(putTemplateResponse.isAcknowledged(), equalTo(true)); |
| 840 | + |
| 841 | + Map<String, Object> templates = getAsMap("/_template/my-template"); |
| 842 | + assertThat(templates.keySet(), hasSize(1)); |
| 843 | + assertThat(extractValue("my-template.order", templates), equalTo(10)); |
| 844 | + assertThat(extractRawValues("my-template.index_patterns", templates), contains("pattern-1", "name-*")); |
| 845 | + assertThat(extractValue("my-template.settings.index.number_of_shards", templates), equalTo("3")); |
| 846 | + assertThat(extractValue("my-template.settings.index.number_of_replicas", templates), equalTo("0")); |
| 847 | + assertThat(extractValue("my-template.mappings.doc.properties.host_name.type", templates), equalTo("keyword")); |
| 848 | + assertThat(extractValue("my-template.mappings.doc.properties.description.type", templates), equalTo("text")); |
| 849 | + assertThat((Map<String, String>) extractValue("my-template.aliases.alias-1", templates), hasEntry("index_routing", "abc")); |
| 850 | + assertThat((Map<String, String>) extractValue("my-template.aliases.{index}-write", templates), hasEntry("search_routing", "xyz")); |
| 851 | + } |
| 852 | + |
| 853 | + public void testPutTemplateBadRequests() throws Exception { |
| 854 | + RestHighLevelClient client = highLevelClient(); |
| 855 | + |
| 856 | + // Failed to validate because index patterns are missing |
| 857 | + PutIndexTemplateRequest withoutPattern = new PutIndexTemplateRequest("t1"); |
| 858 | + ValidationException withoutPatternError = expectThrows(ValidationException.class, |
| 859 | + () -> execute(withoutPattern, client.indices()::putTemplate, client.indices()::putTemplateAsync)); |
| 860 | + assertThat(withoutPatternError.validationErrors(), contains("index patterns are missing")); |
| 861 | + |
| 862 | + // Create-only specified but an template exists already |
| 863 | + PutIndexTemplateRequest goodTemplate = new PutIndexTemplateRequest("t2").patterns(Arrays.asList("qa-*", "prod-*")); |
| 864 | + assertTrue(execute(goodTemplate, client.indices()::putTemplate, client.indices()::putTemplateAsync).isAcknowledged()); |
| 865 | + goodTemplate.create(true); |
| 866 | + ElasticsearchException alreadyExistsError = expectThrows(ElasticsearchException.class, |
| 867 | + () -> execute(goodTemplate, client.indices()::putTemplate, client.indices()::putTemplateAsync)); |
| 868 | + assertThat(alreadyExistsError.getDetailedMessage(), |
| 869 | + containsString("[type=illegal_argument_exception, reason=index_template [t2] already exists]")); |
| 870 | + goodTemplate.create(false); |
| 871 | + assertTrue(execute(goodTemplate, client.indices()::putTemplate, client.indices()::putTemplateAsync).isAcknowledged()); |
| 872 | + |
| 873 | + // Rejected due to unknown settings |
| 874 | + PutIndexTemplateRequest unknownSettingTemplate = new PutIndexTemplateRequest("t3") |
| 875 | + .patterns(Collections.singletonList("any")) |
| 876 | + .settings(Settings.builder().put("this-setting-does-not-exist", 100)); |
| 877 | + ElasticsearchStatusException unknownSettingError = expectThrows(ElasticsearchStatusException.class, |
| 878 | + () -> execute(unknownSettingTemplate, client.indices()::putTemplate, client.indices()::putTemplateAsync)); |
| 879 | + assertThat(unknownSettingError.getDetailedMessage(), containsString("unknown setting [index.this-setting-does-not-exist]")); |
| 880 | + } |
711 | 881 | }
|
0 commit comments