Skip to content

Commit acf286f

Browse files
committed
Support both typed and typeless 'get mapping' requests in the HLRC.
1 parent 49b8b07 commit acf286f

File tree

14 files changed

+480
-115
lines changed

14 files changed

+480
-115
lines changed

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

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@
3939
import org.elasticsearch.action.admin.indices.get.GetIndexResponse;
4040
import org.elasticsearch.client.indices.GetFieldMappingsRequest;
4141
import org.elasticsearch.client.indices.GetFieldMappingsResponse;
42-
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest;
43-
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse;
4442
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
4543
import org.elasticsearch.action.admin.indices.open.OpenIndexResponse;
4644
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
@@ -61,6 +59,8 @@
6159
import org.elasticsearch.client.core.ShardsAcknowledgedResponse;
6260
import org.elasticsearch.client.indices.FreezeIndexRequest;
6361
import org.elasticsearch.client.indices.GetIndexTemplatesRequest;
62+
import org.elasticsearch.client.indices.GetMappingsRequest;
63+
import org.elasticsearch.client.indices.GetMappingsResponse;
6464
import org.elasticsearch.client.indices.IndexTemplatesExistRequest;
6565
import org.elasticsearch.client.indices.PutMappingRequest;
6666
import org.elasticsearch.client.indices.UnfreezeIndexRequest;
@@ -215,8 +215,11 @@ public void putMappingAsync(org.elasticsearch.action.admin.indices.mapping.put.P
215215
* @throws IOException in case there is a problem sending the request or parsing back the response
216216
*/
217217
public GetMappingsResponse getMapping(GetMappingsRequest getMappingsRequest, RequestOptions options) throws IOException {
218-
return restHighLevelClient.performRequestAndParseEntity(getMappingsRequest, IndicesRequestConverters::getMappings, options,
219-
GetMappingsResponse::fromXContent, emptySet());
218+
return restHighLevelClient.performRequestAndParseEntity(getMappingsRequest,
219+
IndicesRequestConverters::getMappings,
220+
options,
221+
GetMappingsResponse::fromXContent,
222+
emptySet());
220223
}
221224

222225
/**
@@ -229,8 +232,60 @@ public GetMappingsResponse getMapping(GetMappingsRequest getMappingsRequest, Req
229232
*/
230233
public void getMappingAsync(GetMappingsRequest getMappingsRequest, RequestOptions options,
231234
ActionListener<GetMappingsResponse> listener) {
232-
restHighLevelClient.performRequestAsyncAndParseEntity(getMappingsRequest, IndicesRequestConverters::getMappings, options,
233-
GetMappingsResponse::fromXContent, listener, emptySet());
235+
restHighLevelClient.performRequestAsyncAndParseEntity(getMappingsRequest,
236+
IndicesRequestConverters::getMappings,
237+
options,
238+
GetMappingsResponse::fromXContent,
239+
listener,
240+
emptySet());
241+
}
242+
243+
/**
244+
* Retrieves the mappings on an index or indices using the Get Mapping API.
245+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-mapping.html">
246+
* Get Mapping API on elastic.co</a>
247+
* @param getMappingsRequest the request
248+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
249+
* @return the response
250+
* @throws IOException in case there is a problem sending the request or parsing back the response
251+
*
252+
* @deprecated This method uses an old request and response objects which still refer to types, a deprecated
253+
* feature. The method {@link #getMapping(GetMappingsRequest, RequestOptions)} should be used instead, which
254+
* accepts a new request object.
255+
*/
256+
@Deprecated
257+
public org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse getMapping(
258+
org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest getMappingsRequest,
259+
RequestOptions options) throws IOException {
260+
return restHighLevelClient.performRequestAndParseEntity(getMappingsRequest,
261+
IndicesRequestConverters::getMappings,
262+
options,
263+
org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse::fromXContent,
264+
emptySet());
265+
}
266+
267+
/**
268+
* Asynchronously retrieves the mappings on an index on indices using the Get Mapping API.
269+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-mapping.html">
270+
* Get Mapping API on elastic.co</a>
271+
* @param getMappingsRequest the request
272+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
273+
* @param listener the listener to be notified upon request completion
274+
*
275+
* @deprecated This method uses old request and response objects which still refer to types, a deprecated feature.
276+
* The method {@link #getMapping(GetMappingsRequest, RequestOptions)} should be used instead, which accepts a new
277+
* request object.
278+
*/
279+
@Deprecated
280+
public void getMappingAsync(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest getMappingsRequest,
281+
RequestOptions options,
282+
ActionListener<org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse> listener) {
283+
restHighLevelClient.performRequestAsyncAndParseEntity(getMappingsRequest,
284+
IndicesRequestConverters::getMappings,
285+
options,
286+
org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse::fromXContent,
287+
listener,
288+
emptySet());
234289
}
235290

236291
/**
@@ -242,8 +297,9 @@ public void getMappingAsync(GetMappingsRequest getMappingsRequest, RequestOption
242297
* @return the response
243298
* @throws IOException in case there is a problem sending the request or parsing back the response
244299
*
245-
* @deprecated This method uses an old request object which still refers to types, a deprecated feature. The method
246-
* {@link #getFieldMapping(GetFieldMappingsRequest, RequestOptions)} should be used instead, which accepts a new request object.
300+
* @deprecated This method uses old request and response objects which still refer to types, a deprecated feature.
301+
* The method {@link #getFieldMapping(GetFieldMappingsRequest, RequestOptions)} should be used instead, which
302+
* accepts a new request object.
247303
*/
248304
@Deprecated
249305
public org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse getFieldMapping(
@@ -261,9 +317,9 @@ public org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsRespon
261317
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
262318
* @param listener the listener to be notified upon request completion
263319
*
264-
* @deprecated This method uses an old request object which still refers to types, a deprecated feature. The
265-
* method {@link #getFieldMappingAsync(GetFieldMappingsRequest, RequestOptions, ActionListener)} should be used instead,
266-
* which accepts a new request object.
320+
* @deprecated This method uses an request and response objects which still refers to types, a deprecated feature.
321+
* The method {@link #getFieldMappingAsync(GetFieldMappingsRequest, RequestOptions, ActionListener)} should be
322+
* used instead, which accepts a new request object.
267323
*/
268324
@Deprecated
269325
public void getFieldMappingAsync(org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsRequest getFieldMappingsRequest,

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest;
3737
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
3838
import org.elasticsearch.client.indices.GetFieldMappingsRequest;
39-
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest;
4039
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
4140
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
4241
import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
@@ -49,6 +48,7 @@
4948
import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest;
5049
import org.elasticsearch.client.indices.FreezeIndexRequest;
5150
import org.elasticsearch.client.indices.GetIndexTemplatesRequest;
51+
import org.elasticsearch.client.indices.GetMappingsRequest;
5252
import org.elasticsearch.client.indices.IndexTemplatesExistRequest;
5353
import org.elasticsearch.client.indices.PutMappingRequest;
5454
import org.elasticsearch.client.indices.UnfreezeIndexRequest;
@@ -151,7 +151,20 @@ static Request putMapping(org.elasticsearch.action.admin.indices.mapping.put.Put
151151
return request;
152152
}
153153

154-
static Request getMappings(GetMappingsRequest getMappingsRequest) throws IOException {
154+
static Request getMappings(GetMappingsRequest getMappingsRequest) {
155+
String[] indices = getMappingsRequest.indices() == null ? Strings.EMPTY_ARRAY : getMappingsRequest.indices();
156+
157+
Request request = new Request(HttpGet.METHOD_NAME, RequestConverters.endpoint(indices, "_mapping"));
158+
159+
RequestConverters.Params parameters = new RequestConverters.Params(request);
160+
parameters.withMasterTimeout(getMappingsRequest.masterNodeTimeout());
161+
parameters.withIndicesOptions(getMappingsRequest.indicesOptions());
162+
parameters.withLocal(getMappingsRequest.local());
163+
164+
return request;
165+
}
166+
167+
static Request getMappings(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest getMappingsRequest) {
155168
String[] indices = getMappingsRequest.indices() == null ? Strings.EMPTY_ARRAY : getMappingsRequest.indices();
156169
String[] types = getMappingsRequest.types() == null ? Strings.EMPTY_ARRAY : getMappingsRequest.types();
157170

@@ -161,6 +174,7 @@ static Request getMappings(GetMappingsRequest getMappingsRequest) throws IOExcep
161174
parameters.withMasterTimeout(getMappingsRequest.masterNodeTimeout());
162175
parameters.withIndicesOptions(getMappingsRequest.indicesOptions());
163176
parameters.withLocal(getMappingsRequest.local());
177+
parameters.putParam(INCLUDE_TYPE_NAME_PARAMETER, "true");
164178

165179
return request;
166180
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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.client.Validatable;
25+
import org.elasticsearch.common.Strings;
26+
27+
public class GetMappingsRequest extends TimedRequest implements Validatable {
28+
29+
private boolean local = false;
30+
private boolean includeDefaults = false;
31+
private String[] indices = Strings.EMPTY_ARRAY;
32+
private IndicesOptions indicesOptions = IndicesOptions.strictExpandOpen();
33+
34+
/**
35+
* Indicates whether the receiving node should operate based on local index information or
36+
* forward requests, where needed, to other nodes. If running locally, request will not
37+
* raise errors if local index information is missing.
38+
*/
39+
public GetMappingsRequest local(boolean local) {
40+
this.local = local;
41+
return this;
42+
}
43+
44+
public boolean local() {
45+
return local;
46+
}
47+
48+
public GetMappingsRequest indices(String... indices) {
49+
this.indices = indices;
50+
return this;
51+
}
52+
53+
public GetMappingsRequest indicesOptions(IndicesOptions indicesOptions) {
54+
this.indicesOptions = indicesOptions;
55+
return this;
56+
}
57+
58+
public String[] indices() {
59+
return indices;
60+
}
61+
62+
public IndicesOptions indicesOptions() {
63+
return indicesOptions;
64+
}
65+
66+
public boolean includeDefaults() {
67+
return includeDefaults;
68+
}
69+
70+
/** Indicates whether default mapping settings should be returned */
71+
public GetMappingsRequest includeDefaults(boolean includeDefaults) {
72+
this.includeDefaults = includeDefaults;
73+
return this;
74+
}
75+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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.cluster.metadata.MappingMetaData;
23+
import org.elasticsearch.common.ParseField;
24+
import org.elasticsearch.common.xcontent.XContentParser;
25+
import org.elasticsearch.common.xcontent.XContentParserUtils;
26+
import org.elasticsearch.index.mapper.MapperService;
27+
28+
import java.io.IOException;
29+
import java.util.HashMap;
30+
import java.util.Map;
31+
import java.util.Objects;
32+
33+
public class GetMappingsResponse {
34+
35+
static final ParseField MAPPINGS = new ParseField("mappings");
36+
37+
private Map<String, MappingMetaData> mappings;
38+
39+
public GetMappingsResponse(Map<String, MappingMetaData> mappings) {
40+
this.mappings = mappings;
41+
}
42+
43+
public Map<String, MappingMetaData> mappings() {
44+
return mappings;
45+
}
46+
47+
public static GetMappingsResponse fromXContent(XContentParser parser) throws IOException {
48+
if (parser.currentToken() == null) {
49+
parser.nextToken();
50+
}
51+
52+
XContentParserUtils.ensureExpectedToken(parser.currentToken(),
53+
XContentParser.Token.START_OBJECT,
54+
parser::getTokenLocation);
55+
56+
Map<String, Object> parts = parser.map();
57+
58+
Map<String, MappingMetaData> mappings = new HashMap<>();
59+
for (Map.Entry<String, Object> entry : parts.entrySet()) {
60+
String indexName = entry.getKey();
61+
assert entry.getValue() instanceof Map : "expected a map as type mapping, but got: " + entry.getValue().getClass();
62+
63+
@SuppressWarnings("unchecked")
64+
final Map<String, Object> fieldMappings = (Map<String, Object>) ((Map<String, ?>) entry.getValue())
65+
.get(MAPPINGS.getPreferredName());
66+
67+
mappings.put(indexName, new MappingMetaData(MapperService.SINGLE_MAPPING_NAME, fieldMappings));
68+
}
69+
70+
return new GetMappingsResponse(mappings);
71+
}
72+
73+
@Override
74+
public boolean equals(Object o) {
75+
if (this == o) return true;
76+
if (o == null || getClass() != o.getClass()) return false;
77+
GetMappingsResponse that = (GetMappingsResponse) o;
78+
return Objects.equals(mappings, that.mappings);
79+
}
80+
81+
@Override
82+
public int hashCode() {
83+
return Objects.hash(mappings);
84+
}
85+
}

0 commit comments

Comments
 (0)