Skip to content

Commit b889221

Browse files
authored
Types removal - deprecate include_type_name with index templates (elastic#37484)
Added deprecation warnings for use of include_type_name in put/get index templates. HLRC changes: GetIndexTemplateRequest has a new client-side class which is a copy of server's GetIndexTemplateResponse but modified to be typeless. PutIndexTemplateRequest has a new client-side counterpart which doesn't use types in the mappings Relates to elastic#35190
1 parent 1930176 commit b889221

File tree

17 files changed

+1623
-108
lines changed

17 files changed

+1623
-108
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesClient.java

Lines changed: 95 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@
4949
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
5050
import org.elasticsearch.action.admin.indices.shrink.ResizeResponse;
5151
import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest;
52-
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse;
53-
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest;
5452
import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest;
5553
import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryResponse;
5654
import org.elasticsearch.action.support.master.AcknowledgedResponse;
@@ -61,7 +59,9 @@
6159
import org.elasticsearch.client.indices.GetIndexTemplatesRequest;
6260
import org.elasticsearch.client.indices.GetMappingsRequest;
6361
import org.elasticsearch.client.indices.GetMappingsResponse;
62+
import org.elasticsearch.client.indices.GetIndexTemplatesResponse;
6463
import org.elasticsearch.client.indices.IndexTemplatesExistRequest;
64+
import org.elasticsearch.client.indices.PutIndexTemplateRequest;
6565
import org.elasticsearch.client.indices.PutMappingRequest;
6666
import org.elasticsearch.client.indices.UnfreezeIndexRequest;
6767
import org.elasticsearch.rest.RestStatus;
@@ -908,6 +908,7 @@ public void putSettingsAsync(UpdateSettingsRequest updateSettingsRequest, Reques
908908
AcknowledgedResponse::fromXContent, listener, emptySet());
909909
}
910910

911+
911912
/**
912913
* Puts an index template using the Index Templates API.
913914
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html"> Index Templates API
@@ -916,9 +917,13 @@ public void putSettingsAsync(UpdateSettingsRequest updateSettingsRequest, Reques
916917
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
917918
* @return the response
918919
* @throws IOException in case there is a problem sending the request or parsing back the response
920+
* @deprecated This old form of request allows types in mappings. Use {@link #putTemplate(PutIndexTemplateRequest, RequestOptions)}
921+
* instead which introduces a new request object without types.
919922
*/
920-
public AcknowledgedResponse putTemplate(PutIndexTemplateRequest putIndexTemplateRequest,
921-
RequestOptions options) throws IOException {
923+
@Deprecated
924+
public AcknowledgedResponse putTemplate(
925+
org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest putIndexTemplateRequest,
926+
RequestOptions options) throws IOException {
922927
return restHighLevelClient.performRequestAndParseEntity(putIndexTemplateRequest, IndicesRequestConverters::putTemplate, options,
923928
AcknowledgedResponse::fromXContent, emptySet());
924929
}
@@ -930,9 +935,44 @@ public AcknowledgedResponse putTemplate(PutIndexTemplateRequest putIndexTemplate
930935
* @param putIndexTemplateRequest the request
931936
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
932937
* @param listener the listener to be notified upon request completion
938+
* @deprecated This old form of request allows types in mappings.
939+
* Use {@link #putTemplateAsync(PutIndexTemplateRequest, RequestOptions, ActionListener)}
940+
* instead which introduces a new request object without types.
933941
*/
934-
public void putTemplateAsync(PutIndexTemplateRequest putIndexTemplateRequest, RequestOptions options,
935-
ActionListener<AcknowledgedResponse> listener) {
942+
@Deprecated
943+
public void putTemplateAsync(org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest putIndexTemplateRequest,
944+
RequestOptions options, ActionListener<AcknowledgedResponse> listener) {
945+
restHighLevelClient.performRequestAsyncAndParseEntity(putIndexTemplateRequest, IndicesRequestConverters::putTemplate, options,
946+
AcknowledgedResponse::fromXContent, listener, emptySet());
947+
}
948+
949+
950+
/**
951+
* Puts an index template using the Index Templates API.
952+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html"> Index Templates API
953+
* on elastic.co</a>
954+
* @param putIndexTemplateRequest the request
955+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
956+
* @return the response
957+
* @throws IOException in case there is a problem sending the request or parsing back the response
958+
*/
959+
public AcknowledgedResponse putTemplate(
960+
PutIndexTemplateRequest putIndexTemplateRequest,
961+
RequestOptions options) throws IOException {
962+
return restHighLevelClient.performRequestAndParseEntity(putIndexTemplateRequest, IndicesRequestConverters::putTemplate, options,
963+
AcknowledgedResponse::fromXContent, emptySet());
964+
}
965+
966+
/**
967+
* Asynchronously puts an index template using the Index Templates API.
968+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html"> Index Templates API
969+
* on elastic.co</a>
970+
* @param putIndexTemplateRequest the request
971+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
972+
* @param listener the listener to be notified upon request completion
973+
*/
974+
public void putTemplateAsync(PutIndexTemplateRequest putIndexTemplateRequest,
975+
RequestOptions options, ActionListener<AcknowledgedResponse> listener) {
936976
restHighLevelClient.performRequestAsyncAndParseEntity(putIndexTemplateRequest, IndicesRequestConverters::putTemplate, options,
937977
AcknowledgedResponse::fromXContent, listener, emptySet());
938978
}
@@ -968,33 +1008,74 @@ public void validateQueryAsync(ValidateQueryRequest validateQueryRequest, Reques
9681008
}
9691009

9701010
/**
971-
* Gets index templates using the Index Templates API
1011+
* Gets index templates using the Index Templates API. The mappings will be returned in a legacy deprecated format, where the
1012+
* mapping definition is nested under the type name.
9721013
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html"> Index Templates API
9731014
* on elastic.co</a>
9741015
* @param getIndexTemplatesRequest the request
9751016
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
9761017
* @return the response
9771018
* @throws IOException in case there is a problem sending the request or parsing back the response
1019+
* @deprecated This method uses an old response object which still refers to types, a deprecated feature. Use
1020+
* {@link #getIndexTemplate(GetIndexTemplatesRequest, RequestOptions)} instead which returns a new response object
9781021
*/
979-
public GetIndexTemplatesResponse getTemplate(GetIndexTemplatesRequest getIndexTemplatesRequest,
980-
RequestOptions options) throws IOException {
981-
return restHighLevelClient.performRequestAndParseEntity(getIndexTemplatesRequest, IndicesRequestConverters::getTemplates,
982-
options, GetIndexTemplatesResponse::fromXContent, emptySet());
1022+
@Deprecated
1023+
public org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse getTemplate(
1024+
GetIndexTemplatesRequest getIndexTemplatesRequest, RequestOptions options) throws IOException {
1025+
return restHighLevelClient.performRequestAndParseEntity(getIndexTemplatesRequest,
1026+
IndicesRequestConverters::getTemplatesWithDocumentTypes,
1027+
options, org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse::fromXContent, emptySet());
9831028
}
1029+
1030+
/**
1031+
* Gets index templates using the Index Templates API
1032+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html"> Index Templates API
1033+
* on elastic.co</a>
1034+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
1035+
* @param getIndexTemplatesRequest the request
1036+
* @return the response
1037+
* @throws IOException in case there is a problem sending the request or parsing back the response
1038+
*/
1039+
public GetIndexTemplatesResponse getIndexTemplate(GetIndexTemplatesRequest getIndexTemplatesRequest, RequestOptions options)
1040+
throws IOException {
1041+
return restHighLevelClient.performRequestAndParseEntity(getIndexTemplatesRequest,
1042+
IndicesRequestConverters::getTemplates,
1043+
options, GetIndexTemplatesResponse::fromXContent, emptySet());
1044+
}
9841045

9851046
/**
986-
* Asynchronously gets index templates using the Index Templates API
1047+
* Asynchronously gets index templates using the Index Templates API. The mappings will be returned in a legacy deprecated format,
1048+
* where the mapping definition is nested under the type name.
9871049
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html"> Index Templates API
9881050
* on elastic.co</a>
9891051
* @param getIndexTemplatesRequest the request
9901052
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
9911053
* @param listener the listener to be notified upon request completion
1054+
* @deprecated This method uses an old response object which still refers to types, a deprecated feature. Use
1055+
* {@link #getIndexTemplateAsync(GetIndexTemplatesRequest, RequestOptions, ActionListener)} instead which returns a new response object
9921056
*/
1057+
@Deprecated
9931058
public void getTemplateAsync(GetIndexTemplatesRequest getIndexTemplatesRequest, RequestOptions options,
1059+
ActionListener<org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse> listener) {
1060+
restHighLevelClient.performRequestAsyncAndParseEntity(getIndexTemplatesRequest,
1061+
IndicesRequestConverters::getTemplatesWithDocumentTypes,
1062+
options, org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse::fromXContent, listener, emptySet());
1063+
}
1064+
1065+
/**
1066+
* Asynchronously gets index templates using the Index Templates API
1067+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html"> Index Templates API
1068+
* on elastic.co</a>
1069+
* @param getIndexTemplatesRequest the request
1070+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
1071+
* @param listener the listener to be notified upon request completion
1072+
*/
1073+
public void getIndexTemplateAsync(GetIndexTemplatesRequest getIndexTemplatesRequest, RequestOptions options,
9941074
ActionListener<GetIndexTemplatesResponse> listener) {
995-
restHighLevelClient.performRequestAsyncAndParseEntity(getIndexTemplatesRequest, IndicesRequestConverters::getTemplates,
1075+
restHighLevelClient.performRequestAsyncAndParseEntity(getIndexTemplatesRequest,
1076+
IndicesRequestConverters::getTemplates,
9961077
options, GetIndexTemplatesResponse::fromXContent, listener, emptySet());
997-
}
1078+
}
9981079

9991080
/**
10001081
* Uses the Index Templates API to determine if index templates exist

client/rest-high-level/src/main/java/org/elasticsearch/client/IndicesRequestConverters.java

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@
4343
import org.elasticsearch.action.admin.indices.shrink.ResizeRequest;
4444
import org.elasticsearch.action.admin.indices.shrink.ResizeType;
4545
import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest;
46-
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest;
4746
import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest;
4847
import org.elasticsearch.client.indices.CreateIndexRequest;
4948
import org.elasticsearch.client.indices.FreezeIndexRequest;
5049
import org.elasticsearch.client.indices.GetIndexTemplatesRequest;
5150
import org.elasticsearch.client.indices.GetMappingsRequest;
5251
import org.elasticsearch.client.indices.IndexTemplatesExistRequest;
52+
import org.elasticsearch.client.indices.PutIndexTemplateRequest;
5353
import org.elasticsearch.client.indices.PutMappingRequest;
5454
import org.elasticsearch.client.indices.UnfreezeIndexRequest;
5555
import org.elasticsearch.common.Strings;
@@ -416,7 +416,13 @@ static Request indexPutSettings(UpdateSettingsRequest updateSettingsRequest) thr
416416
return request;
417417
}
418418

419-
static Request putTemplate(PutIndexTemplateRequest putIndexTemplateRequest) throws IOException {
419+
/**
420+
* @deprecated This uses the old form of PutIndexTemplateRequest which uses types.
421+
* Use (@link {@link #putTemplate(PutIndexTemplateRequest)} instead
422+
*/
423+
@Deprecated
424+
static Request putTemplate(org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest putIndexTemplateRequest)
425+
throws IOException {
420426
String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_template")
421427
.addPathPart(putIndexTemplateRequest.name()).build();
422428
Request request = new Request(HttpPut.METHOD_NAME, endpoint);
@@ -433,6 +439,22 @@ static Request putTemplate(PutIndexTemplateRequest putIndexTemplateRequest) thro
433439
return request;
434440
}
435441

442+
static Request putTemplate(PutIndexTemplateRequest putIndexTemplateRequest) throws IOException {
443+
String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_template")
444+
.addPathPart(putIndexTemplateRequest.name()).build();
445+
Request request = new Request(HttpPut.METHOD_NAME, endpoint);
446+
RequestConverters.Params params = new RequestConverters.Params(request);
447+
params.withMasterTimeout(putIndexTemplateRequest.masterNodeTimeout());
448+
if (putIndexTemplateRequest.create()) {
449+
params.putParam("create", Boolean.TRUE.toString());
450+
}
451+
if (Strings.hasText(putIndexTemplateRequest.cause())) {
452+
params.putParam("cause", putIndexTemplateRequest.cause());
453+
}
454+
request.setEntity(RequestConverters.createEntity(putIndexTemplateRequest, RequestConverters.REQUEST_BODY_CONTENT_TYPE));
455+
return request;
456+
}
457+
436458
static Request validateQuery(ValidateQueryRequest validateQueryRequest) throws IOException {
437459
String[] indices = validateQueryRequest.indices() == null ? Strings.EMPTY_ARRAY : validateQueryRequest.indices();
438460
String[] types = validateQueryRequest.types() == null || indices.length <= 0 ? Strings.EMPTY_ARRAY : validateQueryRequest.types();
@@ -458,7 +480,16 @@ static Request getAlias(GetAliasesRequest getAliasesRequest) {
458480
return request;
459481
}
460482

483+
@Deprecated
484+
static Request getTemplatesWithDocumentTypes(GetIndexTemplatesRequest getIndexTemplatesRequest) {
485+
return getTemplates(getIndexTemplatesRequest, true);
486+
}
487+
461488
static Request getTemplates(GetIndexTemplatesRequest getIndexTemplatesRequest) {
489+
return getTemplates(getIndexTemplatesRequest, false);
490+
}
491+
492+
private static Request getTemplates(GetIndexTemplatesRequest getIndexTemplatesRequest, boolean includeTypeName) {
462493
final String endpoint = new RequestConverters.EndpointBuilder()
463494
.addPathPartAsIs("_template")
464495
.addCommaSeparatedPathParts(getIndexTemplatesRequest.names())
@@ -467,9 +498,11 @@ static Request getTemplates(GetIndexTemplatesRequest getIndexTemplatesRequest) {
467498
final RequestConverters.Params params = new RequestConverters.Params(request);
468499
params.withLocal(getIndexTemplatesRequest.isLocal());
469500
params.withMasterTimeout(getIndexTemplatesRequest.getMasterNodeTimeout());
470-
params.putParam(INCLUDE_TYPE_NAME_PARAMETER, "true");
501+
if (includeTypeName) {
502+
params.putParam(INCLUDE_TYPE_NAME_PARAMETER, "true");
503+
}
471504
return request;
472-
}
505+
}
473506

474507
static Request templatesExist(IndexTemplatesExistRequest indexTemplatesExistRequest) {
475508
final String endpoint = new RequestConverters.EndpointBuilder()
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.client.indices;
20+
21+
import org.elasticsearch.common.xcontent.XContentParser;
22+
23+
import java.io.IOException;
24+
import java.util.ArrayList;
25+
import java.util.Comparator;
26+
import java.util.List;
27+
import java.util.Objects;
28+
29+
30+
public class GetIndexTemplatesResponse {
31+
32+
@Override
33+
public String toString() {
34+
List<IndexTemplateMetaData> thisList = new ArrayList<>(this.indexTemplates);
35+
thisList.sort(Comparator.comparing(IndexTemplateMetaData::name));
36+
return "GetIndexTemplatesResponse [indexTemplates=" + thisList + "]";
37+
}
38+
39+
private final List<IndexTemplateMetaData> indexTemplates;
40+
41+
GetIndexTemplatesResponse() {
42+
indexTemplates = new ArrayList<>();
43+
}
44+
45+
GetIndexTemplatesResponse(List<IndexTemplateMetaData> indexTemplates) {
46+
this.indexTemplates = indexTemplates;
47+
}
48+
49+
public List<IndexTemplateMetaData> getIndexTemplates() {
50+
return indexTemplates;
51+
}
52+
53+
54+
public static GetIndexTemplatesResponse fromXContent(XContentParser parser) throws IOException {
55+
final List<IndexTemplateMetaData> templates = new ArrayList<>();
56+
for (XContentParser.Token token = parser.nextToken(); token != XContentParser.Token.END_OBJECT; token = parser.nextToken()) {
57+
if (token == XContentParser.Token.FIELD_NAME) {
58+
final IndexTemplateMetaData templateMetaData = IndexTemplateMetaData.Builder.fromXContent(parser, parser.currentName());
59+
templates.add(templateMetaData);
60+
}
61+
}
62+
return new GetIndexTemplatesResponse(templates);
63+
}
64+
65+
@Override
66+
public int hashCode() {
67+
List<IndexTemplateMetaData> sortedList = new ArrayList<>(this.indexTemplates);
68+
sortedList.sort(Comparator.comparing(IndexTemplateMetaData::name));
69+
return Objects.hash(sortedList);
70+
}
71+
72+
@Override
73+
public boolean equals(Object obj) {
74+
if (this == obj)
75+
return true;
76+
if (obj == null)
77+
return false;
78+
if (getClass() != obj.getClass())
79+
return false;
80+
// To compare results we need to make sure the templates are listed in the same order
81+
GetIndexTemplatesResponse other = (GetIndexTemplatesResponse) obj;
82+
List<IndexTemplateMetaData> thisList = new ArrayList<>(this.indexTemplates);
83+
List<IndexTemplateMetaData> otherList = new ArrayList<>(other.indexTemplates);
84+
thisList.sort(Comparator.comparing(IndexTemplateMetaData::name));
85+
otherList.sort(Comparator.comparing(IndexTemplateMetaData::name));
86+
return Objects.equals(thisList, otherList);
87+
}
88+
89+
90+
}

0 commit comments

Comments
 (0)