|
49 | 49 | import org.elasticsearch.action.admin.indices.refresh.RefreshResponse;
|
50 | 50 | import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
|
51 | 51 | import org.elasticsearch.action.admin.indices.rollover.RolloverResponse;
|
| 52 | +import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest; |
| 53 | +import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsResponse; |
52 | 54 | import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
|
53 | 55 | import org.elasticsearch.action.admin.indices.shrink.ResizeResponse;
|
54 | 56 | import org.elasticsearch.action.admin.indices.shrink.ResizeType;
|
55 | 57 | import org.elasticsearch.action.index.IndexRequest;
|
56 | 58 | import org.elasticsearch.action.support.IndicesOptions;
|
57 | 59 | import org.elasticsearch.action.support.WriteRequest;
|
58 | 60 | import org.elasticsearch.action.support.broadcast.BroadcastResponse;
|
| 61 | +import org.elasticsearch.cluster.metadata.IndexMetaData; |
| 62 | +import org.elasticsearch.common.settings.Setting; |
59 | 63 | import org.elasticsearch.common.settings.Settings;
|
60 | 64 | import org.elasticsearch.common.unit.ByteSizeUnit;
|
61 | 65 | import org.elasticsearch.common.unit.ByteSizeValue;
|
62 | 66 | import org.elasticsearch.common.unit.TimeValue;
|
63 | 67 | import org.elasticsearch.common.xcontent.XContentBuilder;
|
64 | 68 | import org.elasticsearch.common.xcontent.json.JsonXContent;
|
65 | 69 | import org.elasticsearch.common.xcontent.support.XContentMapValues;
|
| 70 | +import org.elasticsearch.index.IndexSettings; |
66 | 71 | import org.elasticsearch.rest.RestStatus;
|
67 | 72 |
|
68 | 73 | import java.io.IOException;
|
|
72 | 77 | import static org.hamcrest.CoreMatchers.hasItem;
|
73 | 78 | import static org.hamcrest.Matchers.equalTo;
|
74 | 79 | import static org.hamcrest.Matchers.not;
|
| 80 | +import static org.hamcrest.Matchers.startsWith; |
75 | 81 |
|
76 | 82 | public class IndicesClientIT extends ESRestHighLevelClientTestCase {
|
77 | 83 |
|
@@ -609,4 +615,97 @@ public void testRollover() throws IOException {
|
609 | 615 | assertEquals("test_new", rolloverResponse.getNewIndex());
|
610 | 616 | }
|
611 | 617 | }
|
| 618 | + |
| 619 | + public void testIndexPutSettings() throws IOException { |
| 620 | + |
| 621 | + final Setting<Integer> dynamicSetting = IndexMetaData.INDEX_NUMBER_OF_REPLICAS_SETTING; |
| 622 | + final String dynamicSettingKey = IndexMetaData.SETTING_NUMBER_OF_REPLICAS; |
| 623 | + final int dynamicSettingValue = 0; |
| 624 | + |
| 625 | + final Setting<String> staticSetting = IndexSettings.INDEX_CHECK_ON_STARTUP; |
| 626 | + final String staticSettingKey = IndexSettings.INDEX_CHECK_ON_STARTUP.getKey(); |
| 627 | + final String staticSettingValue = "true"; |
| 628 | + |
| 629 | + final Setting<Integer> unmodifiableSetting = IndexMetaData.INDEX_NUMBER_OF_SHARDS_SETTING; |
| 630 | + final String unmodifiableSettingKey = IndexMetaData.SETTING_NUMBER_OF_SHARDS; |
| 631 | + final int unmodifiableSettingValue = 3; |
| 632 | + |
| 633 | + String index = "index"; |
| 634 | + createIndex(index, Settings.EMPTY); |
| 635 | + |
| 636 | + assertThat(dynamicSetting.getDefault(Settings.EMPTY), not(dynamicSettingValue)); |
| 637 | + UpdateSettingsRequest dynamicSettingRequest = new UpdateSettingsRequest(); |
| 638 | + dynamicSettingRequest.settings(Settings.builder().put(dynamicSettingKey, dynamicSettingValue).build()); |
| 639 | + UpdateSettingsResponse response = execute(dynamicSettingRequest, highLevelClient().indices()::putSettings, |
| 640 | + highLevelClient().indices()::putSettingsAsync); |
| 641 | + |
| 642 | + assertTrue(response.isAcknowledged()); |
| 643 | + Map<String, Object> indexSettingsAsMap = getIndexSettingsAsMap(index); |
| 644 | + assertThat(indexSettingsAsMap.get(dynamicSettingKey), equalTo(String.valueOf(dynamicSettingValue))); |
| 645 | + |
| 646 | + assertThat(staticSetting.getDefault(Settings.EMPTY), not(staticSettingValue)); |
| 647 | + UpdateSettingsRequest staticSettingRequest = new UpdateSettingsRequest(); |
| 648 | + staticSettingRequest.settings(Settings.builder().put(staticSettingKey, staticSettingValue).build()); |
| 649 | + ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> execute(staticSettingRequest, |
| 650 | + highLevelClient().indices()::putSettings, highLevelClient().indices()::putSettingsAsync)); |
| 651 | + assertThat(exception.getMessage(), |
| 652 | + startsWith("Elasticsearch exception [type=illegal_argument_exception, " |
| 653 | + + "reason=Can't update non dynamic settings [[index.shard.check_on_startup]] for open indices [[index/")); |
| 654 | + |
| 655 | + indexSettingsAsMap = getIndexSettingsAsMap(index); |
| 656 | + assertNull(indexSettingsAsMap.get(staticSettingKey)); |
| 657 | + |
| 658 | + closeIndex(index); |
| 659 | + response = execute(staticSettingRequest, highLevelClient().indices()::putSettings, |
| 660 | + highLevelClient().indices()::putSettingsAsync); |
| 661 | + assertTrue(response.isAcknowledged()); |
| 662 | + openIndex(index); |
| 663 | + indexSettingsAsMap = getIndexSettingsAsMap(index); |
| 664 | + assertThat(indexSettingsAsMap.get(staticSettingKey), equalTo(staticSettingValue)); |
| 665 | + |
| 666 | + assertThat(unmodifiableSetting.getDefault(Settings.EMPTY), not(unmodifiableSettingValue)); |
| 667 | + UpdateSettingsRequest unmodifiableSettingRequest = new UpdateSettingsRequest(); |
| 668 | + unmodifiableSettingRequest.settings(Settings.builder().put(unmodifiableSettingKey, unmodifiableSettingValue).build()); |
| 669 | + exception = expectThrows(ElasticsearchException.class, () -> execute(unmodifiableSettingRequest, |
| 670 | + highLevelClient().indices()::putSettings, highLevelClient().indices()::putSettingsAsync)); |
| 671 | + assertThat(exception.getMessage(), startsWith( |
| 672 | + "Elasticsearch exception [type=illegal_argument_exception, " |
| 673 | + + "reason=Can't update non dynamic settings [[index.number_of_shards]] for open indices [[index/")); |
| 674 | + closeIndex(index); |
| 675 | + exception = expectThrows(ElasticsearchException.class, () -> execute(unmodifiableSettingRequest, |
| 676 | + highLevelClient().indices()::putSettings, highLevelClient().indices()::putSettingsAsync)); |
| 677 | + assertThat(exception.getMessage(), startsWith( |
| 678 | + "Elasticsearch exception [type=illegal_argument_exception, " |
| 679 | + + "reason=final index setting [index.number_of_shards], not updateable")); |
| 680 | + } |
| 681 | + |
| 682 | + @SuppressWarnings("unchecked") |
| 683 | + private Map<String, Object> getIndexSettingsAsMap(String index) throws IOException { |
| 684 | + Map<String, Object> indexSettings = getIndexSettings(index); |
| 685 | + return (Map<String, Object>)((Map<String, Object>) indexSettings.get(index)).get("settings"); |
| 686 | + } |
| 687 | + |
| 688 | + public void testIndexPutSettingNonExistent() throws IOException { |
| 689 | + |
| 690 | + String index = "index"; |
| 691 | + UpdateSettingsRequest indexUpdateSettingsRequest = new UpdateSettingsRequest(index); |
| 692 | + String setting = "no_idea_what_you_are_talking_about"; |
| 693 | + int value = 10; |
| 694 | + indexUpdateSettingsRequest.settings(Settings.builder().put(setting, value).build()); |
| 695 | + |
| 696 | + ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> execute(indexUpdateSettingsRequest, |
| 697 | + highLevelClient().indices()::putSettings, highLevelClient().indices()::putSettingsAsync)); |
| 698 | + assertEquals(RestStatus.NOT_FOUND, exception.status()); |
| 699 | + assertThat(exception.getMessage(), equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index]")); |
| 700 | + |
| 701 | + createIndex(index, Settings.EMPTY); |
| 702 | + exception = expectThrows(ElasticsearchException.class, () -> execute(indexUpdateSettingsRequest, |
| 703 | + highLevelClient().indices()::putSettings, highLevelClient().indices()::putSettingsAsync)); |
| 704 | + assertThat(exception.status(), equalTo(RestStatus.BAD_REQUEST)); |
| 705 | + assertThat(exception.getMessage(), equalTo( |
| 706 | + "Elasticsearch exception [type=illegal_argument_exception, " |
| 707 | + + "reason=unknown setting [index.no_idea_what_you_are_talking_about] please check that any required plugins are installed, " |
| 708 | + + "or check the breaking changes documentation for removed settings]")); |
| 709 | + } |
| 710 | + |
612 | 711 | }
|
0 commit comments