|
60 | 60 | import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
|
61 | 61 | import org.elasticsearch.action.admin.indices.shrink.ResizeResponse;
|
62 | 62 | import org.elasticsearch.action.admin.indices.shrink.ResizeType;
|
| 63 | +import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesRequest; |
| 64 | +import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse; |
63 | 65 | import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest;
|
64 | 66 | import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateResponse;
|
65 | 67 | import org.elasticsearch.action.index.IndexRequest;
|
66 | 68 | import org.elasticsearch.action.support.IndicesOptions;
|
67 | 69 | import org.elasticsearch.action.support.WriteRequest;
|
68 | 70 | import org.elasticsearch.action.support.broadcast.BroadcastResponse;
|
69 | 71 | import org.elasticsearch.cluster.metadata.IndexMetaData;
|
| 72 | +import org.elasticsearch.cluster.metadata.IndexTemplateMetaData; |
70 | 73 | import org.elasticsearch.common.ValidationException;
|
71 | 74 | import org.elasticsearch.common.settings.Setting;
|
72 | 75 | import org.elasticsearch.common.settings.Settings;
|
|
89 | 92 | import static org.elasticsearch.common.xcontent.support.XContentMapValues.extractRawValues;
|
90 | 93 | import static org.elasticsearch.common.xcontent.support.XContentMapValues.extractValue;
|
91 | 94 | import static org.hamcrest.CoreMatchers.hasItem;
|
| 95 | +import static org.hamcrest.Matchers.arrayContainingInAnyOrder; |
92 | 96 | import static org.hamcrest.Matchers.contains;
|
93 | 97 | import static org.hamcrest.Matchers.containsString;
|
94 | 98 | import static org.hamcrest.Matchers.equalTo;
|
@@ -999,4 +1003,51 @@ public void testPutTemplateBadRequests() throws Exception {
|
999 | 1003 | () -> execute(unknownSettingTemplate, client.indices()::putTemplate, client.indices()::putTemplateAsync));
|
1000 | 1004 | assertThat(unknownSettingError.getDetailedMessage(), containsString("unknown setting [index.this-setting-does-not-exist]"));
|
1001 | 1005 | }
|
| 1006 | + |
| 1007 | + public void testGetIndexTemplate() throws Exception { |
| 1008 | + RestHighLevelClient client = highLevelClient(); |
| 1009 | + |
| 1010 | + PutIndexTemplateRequest putTemplate1 = new PutIndexTemplateRequest().name("template-1") |
| 1011 | + .patterns(Arrays.asList("pattern-1", "name-1")).alias(new Alias("alias-1")); |
| 1012 | + assertThat(execute(putTemplate1, client.indices()::putTemplate, client.indices()::putTemplateAsync).isAcknowledged(), |
| 1013 | + equalTo(true)); |
| 1014 | + PutIndexTemplateRequest putTemplate2 = new PutIndexTemplateRequest().name("template-2") |
| 1015 | + .patterns(Arrays.asList("pattern-2", "name-2")) |
| 1016 | + .settings(Settings.builder().put("number_of_shards", "2").put("number_of_replicas", "0")); |
| 1017 | + assertThat(execute(putTemplate2, client.indices()::putTemplate, client.indices()::putTemplateAsync).isAcknowledged(), |
| 1018 | + equalTo(true)); |
| 1019 | + |
| 1020 | + GetIndexTemplatesResponse getTemplate1 = execute(new GetIndexTemplatesRequest().names("template-1"), |
| 1021 | + client.indices()::getTemplate, client.indices()::getTemplateAsync); |
| 1022 | + assertThat(getTemplate1.getIndexTemplates(), hasSize(1)); |
| 1023 | + IndexTemplateMetaData template1 = getTemplate1.getIndexTemplates().get(0); |
| 1024 | + assertThat(template1.name(), equalTo("template-1")); |
| 1025 | + assertThat(template1.patterns(), contains("pattern-1", "name-1")); |
| 1026 | + assertTrue(template1.aliases().containsKey("alias-1")); |
| 1027 | + |
| 1028 | + GetIndexTemplatesResponse getTemplate2 = execute(new GetIndexTemplatesRequest().names("template-2"), |
| 1029 | + client.indices()::getTemplate, client.indices()::getTemplateAsync); |
| 1030 | + assertThat(getTemplate2.getIndexTemplates(), hasSize(1)); |
| 1031 | + IndexTemplateMetaData template2 = getTemplate2.getIndexTemplates().get(0); |
| 1032 | + assertThat(template2.name(), equalTo("template-2")); |
| 1033 | + assertThat(template2.patterns(), contains("pattern-2", "name-2")); |
| 1034 | + assertTrue(template2.aliases().isEmpty()); |
| 1035 | + assertThat(template2.settings().get("index.number_of_shards"), equalTo("2")); |
| 1036 | + assertThat(template2.settings().get("index.number_of_replicas"), equalTo("0")); |
| 1037 | + |
| 1038 | + GetIndexTemplatesRequest getBothRequest = new GetIndexTemplatesRequest(); |
| 1039 | + if (randomBoolean()) { |
| 1040 | + getBothRequest.names("*-1", "template-2"); |
| 1041 | + } else { |
| 1042 | + getBothRequest.names("template-*"); |
| 1043 | + } |
| 1044 | + GetIndexTemplatesResponse getBoth = execute(getBothRequest, client.indices()::getTemplate, client.indices()::getTemplateAsync); |
| 1045 | + assertThat(getBoth.getIndexTemplates(), hasSize(2)); |
| 1046 | + assertThat(getBoth.getIndexTemplates().stream().map(IndexTemplateMetaData::getName).toArray(), |
| 1047 | + arrayContainingInAnyOrder("template-1", "template-2")); |
| 1048 | + |
| 1049 | + ElasticsearchException notFound = expectThrows(ElasticsearchException.class, () -> execute( |
| 1050 | + new GetIndexTemplatesRequest().names("the-template-*"), client.indices()::getTemplate, client.indices()::getTemplateAsync)); |
| 1051 | + assertThat(notFound.status(), equalTo(RestStatus.NOT_FOUND)); |
| 1052 | + } |
1002 | 1053 | }
|
0 commit comments