Skip to content

Commit 1f2cd9f

Browse files
author
Christoph Büscher
authored
Add typless client side GetIndexRequest calls and response class (#38422)
The HLRC client currently uses `org.elasticsearch.action.admin.indices.get.GetIndexRequest` and `org.elasticsearch.action.admin.indices.get.GetIndexResponse` in its get index calls. Both request and response are designed for the typed APIs, including some return types e.g. for `getMappings()` which in the maps it returns still use a level including the type name. In order to change this without breaking existing users of the HLRC API, this PR introduces two new request and response objects in the `org.elasticsearch.client.indices` client package. These are used by the IndicesClient#get and IndicesClient#exists calls now by default and support the type-less API. The old request and response objects are still kept for use in similarly named, but deprecated methods. The newly introduced client side classes are simplified versions of the server side request/response classes since they don't need to support wire serialization, and only the response needs fromXContent parsing (but no xContent-serialization, since this is the responsibility of the server-side class). Also changing the return type of `GetIndexResponse#getMapping` to `Map<String, MappingMetaData> getMappings()`, while it previously was returning another map keyed by the type-name. Similar getters return simple Maps instead of the ImmutableOpenMaps that the server side response objects return. Backport for #37778 Relates to #35190
1 parent 39c7acb commit 1f2cd9f

File tree

16 files changed

+936
-84
lines changed

16 files changed

+936
-84
lines changed

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

Lines changed: 91 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@
3434
import org.elasticsearch.action.admin.indices.flush.SyncedFlushRequest;
3535
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest;
3636
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeResponse;
37-
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
38-
import org.elasticsearch.action.admin.indices.get.GetIndexResponse;
39-
import org.elasticsearch.client.indices.GetFieldMappingsRequest;
40-
import org.elasticsearch.client.indices.GetFieldMappingsResponse;
4137
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
4238
import org.elasticsearch.action.admin.indices.open.OpenIndexResponse;
4339
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
@@ -59,6 +55,10 @@
5955
import org.elasticsearch.client.indices.CreateIndexRequest;
6056
import org.elasticsearch.client.indices.CreateIndexResponse;
6157
import org.elasticsearch.client.indices.FreezeIndexRequest;
58+
import org.elasticsearch.client.indices.GetFieldMappingsRequest;
59+
import org.elasticsearch.client.indices.GetFieldMappingsResponse;
60+
import org.elasticsearch.client.indices.GetIndexRequest;
61+
import org.elasticsearch.client.indices.GetIndexResponse;
6262
import org.elasticsearch.client.indices.GetIndexTemplatesRequest;
6363
import org.elasticsearch.client.indices.GetMappingsRequest;
6464
import org.elasticsearch.client.indices.GetMappingsResponse;
@@ -901,6 +901,41 @@ public void getAsync(GetIndexRequest getIndexRequest, RequestOptions options,
901901
GetIndexResponse::fromXContent, listener, emptySet());
902902
}
903903

904+
/**
905+
* Retrieve information about one or more indexes
906+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-index.html">
907+
* Indices Get Index API on elastic.co</a>
908+
* @param getIndexRequest the request
909+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
910+
* @return the response
911+
* @throws IOException in case there is a problem sending the request or parsing back the response
912+
* @deprecated This method uses an old request object which still refers to types, a deprecated feature. The method
913+
* {@link #get(GetIndexRequest, RequestOptions)} should be used instead, which accepts a new request object.
914+
*/
915+
@Deprecated
916+
public org.elasticsearch.action.admin.indices.get.GetIndexResponse get(
917+
org.elasticsearch.action.admin.indices.get.GetIndexRequest getIndexRequest, RequestOptions options) throws IOException {
918+
return restHighLevelClient.performRequestAndParseEntity(getIndexRequest, IndicesRequestConverters::getIndex, options,
919+
org.elasticsearch.action.admin.indices.get.GetIndexResponse::fromXContent, emptySet());
920+
}
921+
922+
/**
923+
* Retrieve information about one or more indexes
924+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-index.html">
925+
* Indices Get Index API on elastic.co</a>
926+
* @param getIndexRequest the request
927+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
928+
* @param listener the listener to be notified upon request completion
929+
* @deprecated This method uses an old request object which still refers to types, a deprecated feature. The method
930+
* {@link #getAsync(GetIndexRequest, RequestOptions, ActionListener)} should be used instead, which accepts a new request object.
931+
*/
932+
@Deprecated
933+
public void getAsync(org.elasticsearch.action.admin.indices.get.GetIndexRequest getIndexRequest, RequestOptions options,
934+
ActionListener<org.elasticsearch.action.admin.indices.get.GetIndexResponse> listener) {
935+
restHighLevelClient.performRequestAsyncAndParseEntity(getIndexRequest, IndicesRequestConverters::getIndex, options,
936+
org.elasticsearch.action.admin.indices.get.GetIndexResponse::fromXContent, listener, emptySet());
937+
}
938+
904939
/**
905940
* Force merge one or more indices using the Force Merge API.
906941
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-forcemerge.html">
@@ -1058,15 +1093,38 @@ public boolean exists(GetIndexRequest request, RequestOptions options) throws IO
10581093
);
10591094
}
10601095

1096+
/**
1097+
* Checks if the index (indices) exists or not.
1098+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-exists.html">
1099+
* Indices Exists API on elastic.co</a>
1100+
* @param request the request
1101+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
1102+
* @return the response
1103+
* @throws IOException in case there is a problem sending the request
1104+
* @deprecated This method uses an old request object which still refers to types, a deprecated feature. The method
1105+
* {@link #exists(GetIndexRequest, RequestOptions)} should be used instead, which accepts a new request object.
1106+
*/
1107+
@Deprecated
1108+
public boolean exists(org.elasticsearch.action.admin.indices.get.GetIndexRequest request, RequestOptions options) throws IOException {
1109+
return restHighLevelClient.performRequest(
1110+
request,
1111+
IndicesRequestConverters::indicesExist,
1112+
options,
1113+
RestHighLevelClient::convertExistsResponse,
1114+
Collections.emptySet()
1115+
);
1116+
}
1117+
10611118
/**
10621119
* Checks if the index (indices) exists or not.
10631120
* <p>
10641121
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-exists.html">
10651122
* Indices Exists API on elastic.co</a>
1066-
* @deprecated Prefer {@link #exists(GetIndexRequest, RequestOptions)}
1123+
* @deprecated This method uses an old request object which still refers to types, a deprecated feature. The method
1124+
* {@link #exists(GetIndexRequest, RequestOptions)} should be used instead, which accepts a new request object.
10671125
*/
10681126
@Deprecated
1069-
public boolean exists(GetIndexRequest request, Header... headers) throws IOException {
1127+
public boolean exists(org.elasticsearch.action.admin.indices.get.GetIndexRequest request, Header... headers) throws IOException {
10701128
return restHighLevelClient.performRequest(
10711129
request,
10721130
IndicesRequestConverters::indicesExist,
@@ -1095,15 +1153,40 @@ public void existsAsync(GetIndexRequest request, RequestOptions options, ActionL
10951153
);
10961154
}
10971155

1156+
/**
1157+
* Asynchronously checks if the index (indices) exists or not.
1158+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-exists.html">
1159+
* Indices Exists API on elastic.co</a>
1160+
* @param request the request
1161+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
1162+
* @param listener the listener to be notified upon request completion
1163+
* @deprecated This method uses an old request object which still refers to types, a deprecated feature. The method
1164+
* {@link #existsAsync(GetIndexRequest, RequestOptions, ActionListener)} should be used instead, which accepts a new request object.
1165+
*/
1166+
@Deprecated
1167+
public void existsAsync(org.elasticsearch.action.admin.indices.get.GetIndexRequest request, RequestOptions options,
1168+
ActionListener<Boolean> listener) {
1169+
restHighLevelClient.performRequestAsync(
1170+
request,
1171+
IndicesRequestConverters::indicesExist,
1172+
options,
1173+
RestHighLevelClient::convertExistsResponse,
1174+
listener,
1175+
Collections.emptySet()
1176+
);
1177+
}
1178+
10981179
/**
10991180
* Asynchronously checks if the index (indices) exists or not.
11001181
* <p>
11011182
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-exists.html">
11021183
* Indices Exists API on elastic.co</a>
1103-
* @deprecated Prefer {@link #existsAsync(GetIndexRequest, RequestOptions, ActionListener)}
1184+
* @deprecated This method uses an old request object which still refers to types, a deprecated feature. The method
1185+
* {@link #existsAsync(GetIndexRequest, RequestOptions, ActionListener)} should be used instead, which accepts a new request object.
11041186
*/
11051187
@Deprecated
1106-
public void existsAsync(GetIndexRequest request, ActionListener<Boolean> listener, Header... headers) {
1188+
public void existsAsync(org.elasticsearch.action.admin.indices.get.GetIndexRequest request, ActionListener<Boolean> listener,
1189+
Header... headers) {
11071190
restHighLevelClient.performRequestAsync(
11081191
request,
11091192
IndicesRequestConverters::indicesExist,

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

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
import org.elasticsearch.action.admin.indices.flush.FlushRequest;
3434
import org.elasticsearch.action.admin.indices.flush.SyncedFlushRequest;
3535
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest;
36-
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
37-
import org.elasticsearch.client.indices.GetFieldMappingsRequest;
3836
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
3937
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
4038
import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
@@ -48,6 +46,8 @@
4846
import org.elasticsearch.action.support.ActiveShardCount;
4947
import org.elasticsearch.client.indices.CreateIndexRequest;
5048
import org.elasticsearch.client.indices.FreezeIndexRequest;
49+
import org.elasticsearch.client.indices.GetFieldMappingsRequest;
50+
import org.elasticsearch.client.indices.GetIndexRequest;
5151
import org.elasticsearch.client.indices.GetIndexTemplatesRequest;
5252
import org.elasticsearch.client.indices.GetMappingsRequest;
5353
import org.elasticsearch.client.indices.IndexTemplatesExistRequest;
@@ -151,6 +151,10 @@ static Request putMapping(PutMappingRequest putMappingRequest) throws IOExceptio
151151
return request;
152152
}
153153

154+
/**
155+
* converter for the legacy server-side {@link org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest} that still supports
156+
* types
157+
*/
154158
@Deprecated
155159
static Request putMapping(org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest putMappingRequest) throws IOException {
156160
// The concreteIndex is an internal concept, not applicable to requests made over the REST API.
@@ -375,6 +379,28 @@ static Request getSettings(GetSettingsRequest getSettingsRequest) {
375379
return request;
376380
}
377381

382+
/**
383+
* converter for the legacy server-side {@link org.elasticsearch.action.admin.indices.get.GetIndexRequest} that
384+
* still supports types
385+
*/
386+
@Deprecated
387+
static Request getIndex(org.elasticsearch.action.admin.indices.get.GetIndexRequest getIndexRequest) {
388+
String[] indices = getIndexRequest.indices() == null ? Strings.EMPTY_ARRAY : getIndexRequest.indices();
389+
390+
String endpoint = RequestConverters.endpoint(indices);
391+
Request request = new Request(HttpGet.METHOD_NAME, endpoint);
392+
393+
RequestConverters.Params params = new RequestConverters.Params(request);
394+
params.withIndicesOptions(getIndexRequest.indicesOptions());
395+
params.withLocal(getIndexRequest.local());
396+
params.withIncludeDefaults(getIndexRequest.includeDefaults());
397+
params.withHuman(getIndexRequest.humanReadable());
398+
params.withMasterTimeout(getIndexRequest.masterNodeTimeout());
399+
params.putParam(INCLUDE_TYPE_NAME_PARAMETER, Boolean.TRUE.toString());
400+
401+
return request;
402+
}
403+
378404
static Request getIndex(GetIndexRequest getIndexRequest) {
379405
String[] indices = getIndexRequest.indices() == null ? Strings.EMPTY_ARRAY : getIndexRequest.indices();
380406

@@ -388,11 +414,33 @@ static Request getIndex(GetIndexRequest getIndexRequest) {
388414
params.withHuman(getIndexRequest.humanReadable());
389415
params.withMasterTimeout(getIndexRequest.masterNodeTimeout());
390416
// Force "include_type_name" parameter since responses need to be compatible when coming from 7.0 nodes
391-
params.withIncludeTypeName(true);
417+
params.putParam(INCLUDE_TYPE_NAME_PARAMETER, Boolean.FALSE.toString());
392418

393419
return request;
394420
}
395421

422+
/**
423+
* converter for the legacy server-side {@link org.elasticsearch.action.admin.indices.get.GetIndexRequest} that
424+
* still supports types
425+
*/
426+
@Deprecated
427+
static Request indicesExist(org.elasticsearch.action.admin.indices.get.GetIndexRequest getIndexRequest) {
428+
// this can be called with no indices as argument by transport client, not via REST though
429+
if (getIndexRequest.indices() == null || getIndexRequest.indices().length == 0) {
430+
throw new IllegalArgumentException("indices are mandatory");
431+
}
432+
String endpoint = RequestConverters.endpoint(getIndexRequest.indices(), "");
433+
Request request = new Request(HttpHead.METHOD_NAME, endpoint);
434+
435+
RequestConverters.Params params = new RequestConverters.Params(request);
436+
params.withLocal(getIndexRequest.local());
437+
params.withHuman(getIndexRequest.humanReadable());
438+
params.withIndicesOptions(getIndexRequest.indicesOptions());
439+
params.withIncludeDefaults(getIndexRequest.includeDefaults());
440+
params.putParam(INCLUDE_TYPE_NAME_PARAMETER, Boolean.TRUE.toString());
441+
return request;
442+
}
443+
396444
static Request indicesExist(GetIndexRequest getIndexRequest) {
397445
// this can be called with no indices as argument by transport client, not via REST though
398446
if (getIndexRequest.indices() == null || getIndexRequest.indices().length == 0) {
@@ -406,6 +454,7 @@ static Request indicesExist(GetIndexRequest getIndexRequest) {
406454
params.withHuman(getIndexRequest.humanReadable());
407455
params.withIndicesOptions(getIndexRequest.indicesOptions());
408456
params.withIncludeDefaults(getIndexRequest.includeDefaults());
457+
params.putParam(INCLUDE_TYPE_NAME_PARAMETER, Boolean.FALSE.toString());
409458
return request;
410459
}
411460

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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.action.support.IndicesOptions;
23+
import org.elasticsearch.client.TimedRequest;
24+
import org.elasticsearch.common.util.ArrayUtils;
25+
26+
/**
27+
* A request to retrieve information about an index.
28+
*/
29+
public class GetIndexRequest extends TimedRequest {
30+
31+
public enum Feature {
32+
ALIASES,
33+
MAPPINGS,
34+
SETTINGS;
35+
}
36+
37+
static final Feature[] DEFAULT_FEATURES = new Feature[] { Feature.ALIASES, Feature.MAPPINGS, Feature.SETTINGS };
38+
private Feature[] features = DEFAULT_FEATURES;
39+
private boolean humanReadable = false;
40+
private transient boolean includeDefaults = false;
41+
42+
private final String[] indices;
43+
private IndicesOptions indicesOptions = IndicesOptions.fromOptions(false, false, true, true);
44+
private boolean local = false;
45+
46+
public GetIndexRequest(String... indices) {
47+
this.indices = indices;
48+
}
49+
50+
/**
51+
* The indices into which the mappings will be put.
52+
*/
53+
public String[] indices() {
54+
return indices;
55+
}
56+
57+
public IndicesOptions indicesOptions() {
58+
return indicesOptions;
59+
}
60+
61+
public GetIndexRequest indicesOptions(IndicesOptions indicesOptions) {
62+
this.indicesOptions = indicesOptions;
63+
return this;
64+
}
65+
66+
public final GetIndexRequest local(boolean local) {
67+
this.local = local;
68+
return this;
69+
}
70+
71+
/**
72+
* Return local information, do not retrieve the state from master node (default: false).
73+
* @return <code>true</code> if local information is to be returned;
74+
* <code>false</code> if information is to be retrieved from master node (default).
75+
*/
76+
public final boolean local() {
77+
return local;
78+
}
79+
80+
public GetIndexRequest features(Feature... features) {
81+
if (features == null) {
82+
throw new IllegalArgumentException("features cannot be null");
83+
} else {
84+
this.features = features;
85+
}
86+
return this;
87+
}
88+
89+
public GetIndexRequest addFeatures(Feature... features) {
90+
if (this.features == DEFAULT_FEATURES) {
91+
return features(features);
92+
} else {
93+
return features(ArrayUtils.concat(features(), features, Feature.class));
94+
}
95+
}
96+
97+
public Feature[] features() {
98+
return features;
99+
}
100+
101+
public GetIndexRequest humanReadable(boolean humanReadable) {
102+
this.humanReadable = humanReadable;
103+
return this;
104+
}
105+
106+
public boolean humanReadable() {
107+
return humanReadable;
108+
}
109+
110+
/**
111+
* Sets the value of "include_defaults".
112+
*
113+
* @param includeDefaults value of "include_defaults" to be set.
114+
* @return this request
115+
*/
116+
public GetIndexRequest includeDefaults(boolean includeDefaults) {
117+
this.includeDefaults = includeDefaults;
118+
return this;
119+
}
120+
121+
/**
122+
* Whether to return all default settings for each of the indices.
123+
*
124+
* @return <code>true</code> if defaults settings for each of the indices need to returned;
125+
* <code>false</code> otherwise.
126+
*/
127+
public boolean includeDefaults() {
128+
return includeDefaults;
129+
}
130+
131+
132+
}

0 commit comments

Comments
 (0)