Skip to content

Commit b8ca70b

Browse files
authored
HLRC support for Index Templates V2 (#54838)
* HLRC support for Index Templates V2 This change adds High Level Rest Client support for Index Templates V2. Relates to #53101
1 parent 99b9a18 commit b8ca70b

File tree

13 files changed

+757
-28
lines changed

13 files changed

+757
-28
lines changed

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

+126-1
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,22 @@
4848
import org.elasticsearch.client.indices.CreateIndexRequest;
4949
import org.elasticsearch.client.indices.CreateIndexResponse;
5050
import org.elasticsearch.client.indices.DeleteAliasRequest;
51+
import org.elasticsearch.client.indices.DeleteIndexTemplateV2Request;
5152
import org.elasticsearch.client.indices.FreezeIndexRequest;
5253
import org.elasticsearch.client.indices.GetFieldMappingsRequest;
5354
import org.elasticsearch.client.indices.GetFieldMappingsResponse;
5455
import org.elasticsearch.client.indices.GetIndexRequest;
5556
import org.elasticsearch.client.indices.GetIndexResponse;
5657
import org.elasticsearch.client.indices.GetIndexTemplatesRequest;
5758
import org.elasticsearch.client.indices.GetIndexTemplatesResponse;
59+
import org.elasticsearch.client.indices.GetIndexTemplateV2Request;
60+
import org.elasticsearch.client.indices.GetIndexTemplatesV2Response;
5861
import org.elasticsearch.client.indices.GetMappingsRequest;
5962
import org.elasticsearch.client.indices.GetMappingsResponse;
63+
import org.elasticsearch.client.indices.IndexTemplateV2ExistRequest;
6064
import org.elasticsearch.client.indices.IndexTemplatesExistRequest;
6165
import org.elasticsearch.client.indices.PutIndexTemplateRequest;
66+
import org.elasticsearch.client.indices.PutIndexTemplateV2Request;
6267
import org.elasticsearch.client.indices.PutMappingRequest;
6368
import org.elasticsearch.client.indices.ReloadAnalyzersRequest;
6469
import org.elasticsearch.client.indices.ReloadAnalyzersResponse;
@@ -909,6 +914,36 @@ public Cancellable putTemplateAsync(PutIndexTemplateRequest putIndexTemplateRequ
909914
AcknowledgedResponse::fromXContent, listener, emptySet());
910915
}
911916

917+
/**
918+
* Puts an index template using the Index Templates API.
919+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html"> Index Templates API
920+
* on elastic.co</a>
921+
* @param putIndexTemplateRequest the request
922+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
923+
* @return the response
924+
* @throws IOException in case there is a problem sending the request or parsing back the response
925+
*/
926+
public AcknowledgedResponse putIndexTemplate(PutIndexTemplateV2Request putIndexTemplateRequest, RequestOptions options)
927+
throws IOException {
928+
return restHighLevelClient.performRequestAndParseEntity(putIndexTemplateRequest, IndicesRequestConverters::putIndexTemplate,
929+
options, AcknowledgedResponse::fromXContent, emptySet());
930+
}
931+
932+
/**
933+
* Asynchronously puts an index template using the Index Templates API.
934+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html"> Index Templates API
935+
* on elastic.co</a>
936+
* @param putIndexTemplateRequest the request
937+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
938+
* @param listener the listener to be notified upon request completion
939+
* @return cancellable that may be used to cancel the request
940+
*/
941+
public Cancellable putIndexTemplateAsync(PutIndexTemplateV2Request putIndexTemplateRequest,
942+
RequestOptions options, ActionListener<AcknowledgedResponse> listener) {
943+
return restHighLevelClient.performRequestAsyncAndParseEntity(putIndexTemplateRequest, IndicesRequestConverters::putIndexTemplate,
944+
options, AcknowledgedResponse::fromXContent, listener, emptySet());
945+
}
946+
912947
/**
913948
* Validate a potentially expensive query without executing it.
914949
* <p>
@@ -943,6 +978,36 @@ public Cancellable validateQueryAsync(ValidateQueryRequest validateQueryRequest,
943978
ValidateQueryResponse::fromXContent, listener, emptySet());
944979
}
945980

981+
/**
982+
* Gets index templates using the Index Templates API
983+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html"> Index Templates API
984+
* on elastic.co</a>
985+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
986+
* @param getIndexTemplatesRequest the request
987+
* @return the response
988+
* @throws IOException in case there is a problem sending the request or parsing back the response
989+
*/
990+
public GetIndexTemplatesV2Response getIndexTemplate(GetIndexTemplateV2Request getIndexTemplatesRequest,
991+
RequestOptions options) throws IOException {
992+
return restHighLevelClient.performRequestAndParseEntity(getIndexTemplatesRequest, IndicesRequestConverters::getIndexTemplates,
993+
options, GetIndexTemplatesV2Response::fromXContent, emptySet());
994+
}
995+
996+
/**
997+
* Asynchronously gets index templates using the Index Templates API
998+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html"> Index Templates API
999+
* on elastic.co</a>
1000+
* @param getIndexTemplatesRequest the request
1001+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
1002+
* @param listener the listener to be notified upon request completion
1003+
* @return cancellable that may be used to cancel the request
1004+
*/
1005+
public Cancellable getIndexTemplateAsync(GetIndexTemplateV2Request getIndexTemplatesRequest, RequestOptions options,
1006+
ActionListener<GetIndexTemplatesV2Response> listener) {
1007+
return restHighLevelClient.performRequestAsyncAndParseEntity(getIndexTemplatesRequest,
1008+
IndicesRequestConverters::getIndexTemplates, options, GetIndexTemplatesV2Response::fromXContent, listener, emptySet());
1009+
}
1010+
9461011
/**
9471012
* Gets index templates using the Index Templates API
9481013
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html"> Index Templates API
@@ -953,7 +1018,7 @@ public Cancellable validateQueryAsync(ValidateQueryRequest validateQueryRequest,
9531018
* @throws IOException in case there is a problem sending the request or parsing back the response
9541019
*/
9551020
public GetIndexTemplatesResponse getIndexTemplate(GetIndexTemplatesRequest getIndexTemplatesRequest,
956-
RequestOptions options) throws IOException {
1021+
RequestOptions options) throws IOException {
9571022
return restHighLevelClient.performRequestAndParseEntity(getIndexTemplatesRequest,
9581023
IndicesRequestConverters::getTemplates,
9591024
options, GetIndexTemplatesResponse::fromXContent, emptySet());
@@ -1006,6 +1071,37 @@ public Cancellable existsTemplateAsync(IndexTemplatesExistRequest indexTemplates
10061071
RestHighLevelClient::convertExistsResponse, listener, emptySet());
10071072
}
10081073

1074+
/**
1075+
* Uses the Index Templates API to determine if index templates exist
1076+
*
1077+
* @param indexTemplatesRequest the request
1078+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
1079+
* @return true if any index templates in the request exist, false otherwise
1080+
* @throws IOException in case there is a problem sending the request or parsing back the response
1081+
*/
1082+
public boolean existsIndexTemplate(IndexTemplateV2ExistRequest indexTemplatesRequest,
1083+
RequestOptions options) throws IOException {
1084+
return restHighLevelClient.performRequest(indexTemplatesRequest,
1085+
IndicesRequestConverters::templatesExist, options,
1086+
RestHighLevelClient::convertExistsResponse, emptySet());
1087+
}
1088+
1089+
/**
1090+
* Uses the Index Templates API to determine if index templates exist
1091+
* @param indexTemplatesExistRequest the request
1092+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
1093+
* @param listener the listener to be notified upon request completion. The listener will be called with the value {@code true}
1094+
* @return cancellable that may be used to cancel the request
1095+
*/
1096+
public Cancellable existsIndexTemplateAsync(IndexTemplateV2ExistRequest indexTemplatesExistRequest,
1097+
RequestOptions options,
1098+
ActionListener<Boolean> listener) {
1099+
1100+
return restHighLevelClient.performRequestAsync(indexTemplatesExistRequest,
1101+
IndicesRequestConverters::templatesExist, options,
1102+
RestHighLevelClient::convertExistsResponse, listener, emptySet());
1103+
}
1104+
10091105
/**
10101106
* Calls the analyze API
10111107
*
@@ -1112,6 +1208,35 @@ public Cancellable deleteTemplateAsync(DeleteIndexTemplateRequest request, Reque
11121208
options, AcknowledgedResponse::fromXContent, listener, emptySet());
11131209
}
11141210

1211+
/**
1212+
* Delete an index template using the Index Templates API
1213+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html"> Index Templates API
1214+
* on elastic.co</a>
1215+
*
1216+
* @param request the request
1217+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
1218+
* @throws IOException in case there is a problem sending the request or parsing back the response
1219+
*/
1220+
public AcknowledgedResponse deleteIndexTemplate(DeleteIndexTemplateV2Request request, RequestOptions options) throws IOException {
1221+
return restHighLevelClient.performRequestAndParseEntity(request, IndicesRequestConverters::deleteIndexTemplate,
1222+
options, AcknowledgedResponse::fromXContent, emptySet());
1223+
}
1224+
1225+
/**
1226+
* Asynchronously delete an index template using the Index Templates API
1227+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html"> Index Templates API
1228+
* on elastic.co</a>
1229+
* @param request the request
1230+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
1231+
* @param listener the listener to be notified upon request completion
1232+
* @return cancellable that may be used to cancel the request
1233+
*/
1234+
public Cancellable deleteIndexTemplateAsync(DeleteIndexTemplateV2Request request, RequestOptions options,
1235+
ActionListener<AcknowledgedResponse> listener) {
1236+
return restHighLevelClient.performRequestAsyncAndParseEntity(request, IndicesRequestConverters::deleteIndexTemplate,
1237+
options, AcknowledgedResponse::fromXContent, listener, emptySet());
1238+
}
1239+
11151240
/**
11161241
* Synchronously calls the _reload_search_analyzers API
11171242
*

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

+57
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,17 @@
4141
import org.elasticsearch.client.indices.CloseIndexRequest;
4242
import org.elasticsearch.client.indices.CreateIndexRequest;
4343
import org.elasticsearch.client.indices.DeleteAliasRequest;
44+
import org.elasticsearch.client.indices.DeleteIndexTemplateV2Request;
4445
import org.elasticsearch.client.indices.FreezeIndexRequest;
4546
import org.elasticsearch.client.indices.GetFieldMappingsRequest;
4647
import org.elasticsearch.client.indices.GetIndexRequest;
4748
import org.elasticsearch.client.indices.GetIndexTemplatesRequest;
49+
import org.elasticsearch.client.indices.GetIndexTemplateV2Request;
4850
import org.elasticsearch.client.indices.GetMappingsRequest;
51+
import org.elasticsearch.client.indices.IndexTemplateV2ExistRequest;
4952
import org.elasticsearch.client.indices.IndexTemplatesExistRequest;
5053
import org.elasticsearch.client.indices.PutIndexTemplateRequest;
54+
import org.elasticsearch.client.indices.PutIndexTemplateV2Request;
5155
import org.elasticsearch.client.indices.PutMappingRequest;
5256
import org.elasticsearch.client.indices.ReloadAnalyzersRequest;
5357
import org.elasticsearch.client.indices.ResizeRequest;
@@ -403,6 +407,23 @@ static Request putTemplate(PutIndexTemplateRequest putIndexTemplateRequest) thro
403407
return request;
404408
}
405409

410+
static Request putIndexTemplate(PutIndexTemplateV2Request putIndexTemplateRequest) throws IOException {
411+
String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_index_template")
412+
.addPathPart(putIndexTemplateRequest.name()).build();
413+
Request request = new Request(HttpPut.METHOD_NAME, endpoint);
414+
RequestConverters.Params params = new RequestConverters.Params();
415+
params.withMasterTimeout(putIndexTemplateRequest.masterNodeTimeout());
416+
if (putIndexTemplateRequest.create()) {
417+
params.putParam("create", Boolean.TRUE.toString());
418+
}
419+
if (Strings.hasText(putIndexTemplateRequest.cause())) {
420+
params.putParam("cause", putIndexTemplateRequest.cause());
421+
}
422+
request.addParameters(params.asMap());
423+
request.setEntity(RequestConverters.createEntity(putIndexTemplateRequest, RequestConverters.REQUEST_BODY_CONTENT_TYPE));
424+
return request;
425+
}
426+
406427
static Request validateQuery(ValidateQueryRequest validateQueryRequest) throws IOException {
407428
String[] indices = validateQueryRequest.indices() == null ? Strings.EMPTY_ARRAY : validateQueryRequest.indices();
408429
String endpoint = RequestConverters.endpoint(indices, "_validate/query");
@@ -442,6 +463,19 @@ static Request getTemplates(GetIndexTemplatesRequest getIndexTemplatesRequest) {
442463
return request;
443464
}
444465

466+
static Request getIndexTemplates(GetIndexTemplateV2Request getIndexTemplatesRequest) {
467+
final String endpoint = new RequestConverters.EndpointBuilder()
468+
.addPathPartAsIs("_index_template")
469+
.addPathPart(getIndexTemplatesRequest.name())
470+
.build();
471+
final Request request = new Request(HttpGet.METHOD_NAME, endpoint);
472+
final RequestConverters.Params params = new RequestConverters.Params();
473+
params.withLocal(getIndexTemplatesRequest.isLocal());
474+
params.withMasterTimeout(getIndexTemplatesRequest.getMasterNodeTimeout());
475+
request.addParameters(params.asMap());
476+
return request;
477+
}
478+
445479
static Request templatesExist(IndexTemplatesExistRequest indexTemplatesExistRequest) {
446480
final String endpoint = new RequestConverters.EndpointBuilder()
447481
.addPathPartAsIs("_template")
@@ -455,6 +489,19 @@ static Request templatesExist(IndexTemplatesExistRequest indexTemplatesExistRequ
455489
return request;
456490
}
457491

492+
static Request templatesExist(IndexTemplateV2ExistRequest indexTemplatesExistRequest) {
493+
final String endpoint = new RequestConverters.EndpointBuilder()
494+
.addPathPartAsIs("_index_template")
495+
.addPathPart(indexTemplatesExistRequest.name())
496+
.build();
497+
final Request request = new Request(HttpHead.METHOD_NAME, endpoint);
498+
final RequestConverters.Params params = new RequestConverters.Params();
499+
params.withLocal(indexTemplatesExistRequest.isLocal());
500+
params.withMasterTimeout(indexTemplatesExistRequest.getMasterNodeTimeout());
501+
request.addParameters(params.asMap());
502+
return request;
503+
}
504+
458505
static Request analyze(AnalyzeRequest request) throws IOException {
459506
RequestConverters.EndpointBuilder builder = new RequestConverters.EndpointBuilder();
460507
String index = request.index();
@@ -501,6 +548,16 @@ static Request deleteTemplate(DeleteIndexTemplateRequest deleteIndexTemplateRequ
501548
return request;
502549
}
503550

551+
static Request deleteIndexTemplate(DeleteIndexTemplateV2Request deleteIndexTemplateRequest) {
552+
String name = deleteIndexTemplateRequest.getName();
553+
String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_index_template").addPathPart(name).build();
554+
Request request = new Request(HttpDelete.METHOD_NAME, endpoint);
555+
RequestConverters.Params params = new RequestConverters.Params();
556+
params.withMasterTimeout(deleteIndexTemplateRequest.masterNodeTimeout());
557+
request.addParameters(params.asMap());
558+
return request;
559+
}
560+
504561
static Request reloadAnalyzers(ReloadAnalyzersRequest reloadAnalyzersRequest) {
505562
String endpoint = RequestConverters.endpoint(reloadAnalyzersRequest.getIndices(), "_reload_search_analyzers");
506563
Request request = new Request(HttpPost.METHOD_NAME, endpoint);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
20+
package org.elasticsearch.client.indices;
21+
22+
import org.elasticsearch.client.TimedRequest;
23+
24+
public class DeleteIndexTemplateV2Request extends TimedRequest {
25+
26+
private final String name;
27+
28+
public DeleteIndexTemplateV2Request(String name) {
29+
this.name = name;
30+
}
31+
32+
public String getName() {
33+
return name;
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
20+
package org.elasticsearch.client.indices;
21+
22+
import org.elasticsearch.client.TimedRequest;
23+
import org.elasticsearch.client.Validatable;
24+
import org.elasticsearch.common.Nullable;
25+
import org.elasticsearch.common.unit.TimeValue;
26+
27+
/**
28+
* A request to read the content of index templates
29+
*/
30+
public class GetIndexTemplateV2Request implements Validatable {
31+
32+
private final String name;
33+
34+
private TimeValue masterNodeTimeout = TimedRequest.DEFAULT_MASTER_NODE_TIMEOUT;
35+
private boolean local = false;
36+
37+
/**
38+
* Create a request to read the content of index template. If no template name is provided, all templates will
39+
* be read
40+
*
41+
* @param name the name of template to read
42+
*/
43+
public GetIndexTemplateV2Request(String name) {
44+
this.name = name;
45+
}
46+
47+
/**
48+
* @return the name of index template this request is requesting
49+
*/
50+
public String name() {
51+
return name;
52+
}
53+
54+
/**
55+
* @return the timeout for waiting for the master node to respond
56+
*/
57+
public TimeValue getMasterNodeTimeout() {
58+
return masterNodeTimeout;
59+
}
60+
61+
public void setMasterNodeTimeout(@Nullable TimeValue masterNodeTimeout) {
62+
this.masterNodeTimeout = masterNodeTimeout;
63+
}
64+
65+
public void setMasterNodeTimeout(String masterNodeTimeout) {
66+
final TimeValue timeValue = TimeValue.parseTimeValue(masterNodeTimeout, getClass().getSimpleName() + ".masterNodeTimeout");
67+
setMasterNodeTimeout(timeValue);
68+
}
69+
70+
/**
71+
* @return true if this request is to read from the local cluster state, rather than the master node - false otherwise
72+
*/
73+
public boolean isLocal() {
74+
return local;
75+
}
76+
77+
public void setLocal(boolean local) {
78+
this.local = local;
79+
}
80+
}

0 commit comments

Comments
 (0)