Support both typed and typeless 'get mapping' requests in the HLRC. (#37796)

From previous PRs, we've already added support for include_type_name to
the get mapping API. We had also taken an approach to the HLRC where the
server-side `GetMappingResponse#fromXContent` could only handle typeless
input.

This PR updates the HLRC for 'get mapping' to be in line with our new approach:

* Add a typeless 'get mappings' method to the Java HLRC, that accepts new
client-side request and response objects. This new response only handles
typeless mapping definitions.
* Switch the old version of `GetMappingResponse` back to expecting typed
mappings, and deprecate the corresponding method on the HLRC.

Finally, the PR also does some small, related clean-up around 'get field mappings'.
This commit is contained in:
Julie Tibshirani 2019-01-27 16:02:22 -08:00 committed by GitHub
parent 66ddd8d2f7
commit b1735aa93b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 494 additions and 136 deletions

View File

@ -37,8 +37,6 @@ import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexResponse; import org.elasticsearch.action.admin.indices.get.GetIndexResponse;
import org.elasticsearch.client.indices.GetFieldMappingsRequest; import org.elasticsearch.client.indices.GetFieldMappingsRequest;
import org.elasticsearch.client.indices.GetFieldMappingsResponse; import org.elasticsearch.client.indices.GetFieldMappingsResponse;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse;
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest; import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
import org.elasticsearch.action.admin.indices.open.OpenIndexResponse; import org.elasticsearch.action.admin.indices.open.OpenIndexResponse;
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest; import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
@ -61,6 +59,8 @@ import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse; import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.FreezeIndexRequest; import org.elasticsearch.client.indices.FreezeIndexRequest;
import org.elasticsearch.client.indices.GetIndexTemplatesRequest; import org.elasticsearch.client.indices.GetIndexTemplatesRequest;
import org.elasticsearch.client.indices.GetMappingsRequest;
import org.elasticsearch.client.indices.GetMappingsResponse;
import org.elasticsearch.client.indices.IndexTemplatesExistRequest; import org.elasticsearch.client.indices.IndexTemplatesExistRequest;
import org.elasticsearch.client.indices.PutMappingRequest; import org.elasticsearch.client.indices.PutMappingRequest;
import org.elasticsearch.client.indices.UnfreezeIndexRequest; import org.elasticsearch.client.indices.UnfreezeIndexRequest;
@ -261,8 +261,11 @@ public final class IndicesClient {
* @throws IOException in case there is a problem sending the request or parsing back the response * @throws IOException in case there is a problem sending the request or parsing back the response
*/ */
public GetMappingsResponse getMapping(GetMappingsRequest getMappingsRequest, RequestOptions options) throws IOException { public GetMappingsResponse getMapping(GetMappingsRequest getMappingsRequest, RequestOptions options) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(getMappingsRequest, IndicesRequestConverters::getMappings, options, return restHighLevelClient.performRequestAndParseEntity(getMappingsRequest,
GetMappingsResponse::fromXContent, emptySet()); IndicesRequestConverters::getMappings,
options,
GetMappingsResponse::fromXContent,
emptySet());
} }
/** /**
@ -275,8 +278,60 @@ public final class IndicesClient {
*/ */
public void getMappingAsync(GetMappingsRequest getMappingsRequest, RequestOptions options, public void getMappingAsync(GetMappingsRequest getMappingsRequest, RequestOptions options,
ActionListener<GetMappingsResponse> listener) { ActionListener<GetMappingsResponse> listener) {
restHighLevelClient.performRequestAsyncAndParseEntity(getMappingsRequest, IndicesRequestConverters::getMappings, options, restHighLevelClient.performRequestAsyncAndParseEntity(getMappingsRequest,
GetMappingsResponse::fromXContent, listener, emptySet()); IndicesRequestConverters::getMappings,
options,
GetMappingsResponse::fromXContent,
listener,
emptySet());
}
/**
* Retrieves the mappings on an index or indices using the Get Mapping API.
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-mapping.html">
* Get Mapping API on elastic.co</a>
* @param getMappingsRequest the request
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @return the response
* @throws IOException in case there is a problem sending the request or parsing back the response
*
* @deprecated This method uses old request and response objects which still refer to types, a deprecated
* feature. The method {@link #getMapping(GetMappingsRequest, RequestOptions)} should be used instead, which
* accepts a new request object.
*/
@Deprecated
public org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse getMapping(
org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest getMappingsRequest,
RequestOptions options) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(getMappingsRequest,
IndicesRequestConverters::getMappings,
options,
org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse::fromXContent,
emptySet());
}
/**
* Asynchronously retrieves the mappings on an index on indices using the Get Mapping API.
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-mapping.html">
* Get Mapping API on elastic.co</a>
* @param getMappingsRequest the request
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @param listener the listener to be notified upon request completion
*
* @deprecated This method uses old request and response objects which still refer to types, a deprecated feature.
* The method {@link #getMapping(GetMappingsRequest, RequestOptions)} should be used instead, which accepts a new
* request object.
*/
@Deprecated
public void getMappingAsync(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest getMappingsRequest,
RequestOptions options,
ActionListener<org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse> listener) {
restHighLevelClient.performRequestAsyncAndParseEntity(getMappingsRequest,
IndicesRequestConverters::getMappings,
options,
org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse::fromXContent,
listener,
emptySet());
} }
/** /**
@ -288,8 +343,9 @@ public final class IndicesClient {
* @return the response * @return the response
* @throws IOException in case there is a problem sending the request or parsing back the response * @throws IOException in case there is a problem sending the request or parsing back the response
* *
* @deprecated This method uses an old request object which still refers to types, a deprecated feature. The method * @deprecated This method uses old request and response objects which still refer to types, a deprecated feature.
* {@link #getFieldMapping(GetFieldMappingsRequest, RequestOptions)} should be used instead, which accepts a new request object. * The method {@link #getFieldMapping(GetFieldMappingsRequest, RequestOptions)} should be used instead, which
* accepts a new request object.
*/ */
@Deprecated @Deprecated
public org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse getFieldMapping( public org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse getFieldMapping(
@ -307,9 +363,9 @@ public final class IndicesClient {
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @param listener the listener to be notified upon request completion * @param listener the listener to be notified upon request completion
* *
* @deprecated This method uses an old request object which still refers to types, a deprecated feature. The * @deprecated This method uses old request and response objects which still refer to types, a deprecated feature.
* method {@link #getFieldMappingAsync(GetFieldMappingsRequest, RequestOptions, ActionListener)} should be used instead, * The method {@link #getFieldMappingAsync(GetFieldMappingsRequest, RequestOptions, ActionListener)} should be
* which accepts a new request object. * used instead, which accepts a new request object.
*/ */
@Deprecated @Deprecated
public void getFieldMappingAsync(org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsRequest getFieldMappingsRequest, public void getFieldMappingAsync(org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsRequest getFieldMappingsRequest,

View File

@ -35,7 +35,6 @@ import org.elasticsearch.action.admin.indices.flush.SyncedFlushRequest;
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest; import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest; import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.client.indices.GetFieldMappingsRequest; import org.elasticsearch.client.indices.GetFieldMappingsRequest;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest;
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest; import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest; import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
import org.elasticsearch.action.admin.indices.rollover.RolloverRequest; import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
@ -49,6 +48,7 @@ import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryReques
import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.FreezeIndexRequest; import org.elasticsearch.client.indices.FreezeIndexRequest;
import org.elasticsearch.client.indices.GetIndexTemplatesRequest; import org.elasticsearch.client.indices.GetIndexTemplatesRequest;
import org.elasticsearch.client.indices.GetMappingsRequest;
import org.elasticsearch.client.indices.IndexTemplatesExistRequest; import org.elasticsearch.client.indices.IndexTemplatesExistRequest;
import org.elasticsearch.client.indices.PutMappingRequest; import org.elasticsearch.client.indices.PutMappingRequest;
import org.elasticsearch.client.indices.UnfreezeIndexRequest; import org.elasticsearch.client.indices.UnfreezeIndexRequest;
@ -148,6 +148,7 @@ final class IndicesRequestConverters {
return request; return request;
} }
@Deprecated
static Request putMapping(org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest putMappingRequest) throws IOException { static Request putMapping(org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest putMappingRequest) throws IOException {
// The concreteIndex is an internal concept, not applicable to requests made over the REST API. // The concreteIndex is an internal concept, not applicable to requests made over the REST API.
if (putMappingRequest.getConcreteIndex() != null) { if (putMappingRequest.getConcreteIndex() != null) {
@ -166,7 +167,21 @@ final class IndicesRequestConverters {
return request; return request;
} }
static Request getMappings(GetMappingsRequest getMappingsRequest) throws IOException { static Request getMappings(GetMappingsRequest getMappingsRequest) {
String[] indices = getMappingsRequest.indices() == null ? Strings.EMPTY_ARRAY : getMappingsRequest.indices();
Request request = new Request(HttpGet.METHOD_NAME, RequestConverters.endpoint(indices, "_mapping"));
RequestConverters.Params parameters = new RequestConverters.Params(request);
parameters.withMasterTimeout(getMappingsRequest.masterNodeTimeout());
parameters.withIndicesOptions(getMappingsRequest.indicesOptions());
parameters.withLocal(getMappingsRequest.local());
return request;
}
@Deprecated
static Request getMappings(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest getMappingsRequest) {
String[] indices = getMappingsRequest.indices() == null ? Strings.EMPTY_ARRAY : getMappingsRequest.indices(); String[] indices = getMappingsRequest.indices() == null ? Strings.EMPTY_ARRAY : getMappingsRequest.indices();
String[] types = getMappingsRequest.types() == null ? Strings.EMPTY_ARRAY : getMappingsRequest.types(); String[] types = getMappingsRequest.types() == null ? Strings.EMPTY_ARRAY : getMappingsRequest.types();
@ -176,6 +191,7 @@ final class IndicesRequestConverters {
parameters.withMasterTimeout(getMappingsRequest.masterNodeTimeout()); parameters.withMasterTimeout(getMappingsRequest.masterNodeTimeout());
parameters.withIndicesOptions(getMappingsRequest.indicesOptions()); parameters.withIndicesOptions(getMappingsRequest.indicesOptions());
parameters.withLocal(getMappingsRequest.local()); parameters.withLocal(getMappingsRequest.local());
parameters.putParam(INCLUDE_TYPE_NAME_PARAMETER, "true");
return request; return request;
} }
@ -201,6 +217,7 @@ final class IndicesRequestConverters {
return request; return request;
} }
@Deprecated
static Request getFieldMapping(org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsRequest getFieldMappingsRequest) { static Request getFieldMapping(org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsRequest getFieldMappingsRequest) {
String[] indices = getFieldMappingsRequest.indices() == null ? Strings.EMPTY_ARRAY : getFieldMappingsRequest.indices(); String[] indices = getFieldMappingsRequest.indices() == null ? Strings.EMPTY_ARRAY : getFieldMappingsRequest.indices();
String[] types = getFieldMappingsRequest.types() == null ? Strings.EMPTY_ARRAY : getFieldMappingsRequest.types(); String[] types = getFieldMappingsRequest.types() == null ? Strings.EMPTY_ARRAY : getFieldMappingsRequest.types();

View File

@ -0,0 +1,74 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.client.indices;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.TimedRequest;
import org.elasticsearch.common.Strings;
public class GetMappingsRequest extends TimedRequest {
private boolean local = false;
private boolean includeDefaults = false;
private String[] indices = Strings.EMPTY_ARRAY;
private IndicesOptions indicesOptions = IndicesOptions.strictExpandOpen();
/**
* Indicates whether the receiving node should operate based on local index information or
* forward requests, where needed, to other nodes. If running locally, request will not
* raise errors if local index information is missing.
*/
public GetMappingsRequest local(boolean local) {
this.local = local;
return this;
}
public boolean local() {
return local;
}
public GetMappingsRequest indices(String... indices) {
this.indices = indices;
return this;
}
public GetMappingsRequest indicesOptions(IndicesOptions indicesOptions) {
this.indicesOptions = indicesOptions;
return this;
}
public String[] indices() {
return indices;
}
public IndicesOptions indicesOptions() {
return indicesOptions;
}
public boolean includeDefaults() {
return includeDefaults;
}
/** Indicates whether default mapping settings should be returned */
public GetMappingsRequest includeDefaults(boolean includeDefaults) {
this.includeDefaults = includeDefaults;
return this;
}
}

View File

@ -0,0 +1,71 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.client.indices;
import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentParserUtils;
import org.elasticsearch.index.mapper.MapperService;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class GetMappingsResponse {
static final ParseField MAPPINGS = new ParseField("mappings");
private Map<String, MappingMetaData> mappings;
public GetMappingsResponse(Map<String, MappingMetaData> mappings) {
this.mappings = mappings;
}
public Map<String, MappingMetaData> mappings() {
return mappings;
}
public static GetMappingsResponse fromXContent(XContentParser parser) throws IOException {
if (parser.currentToken() == null) {
parser.nextToken();
}
XContentParserUtils.ensureExpectedToken(parser.currentToken(),
XContentParser.Token.START_OBJECT,
parser::getTokenLocation);
Map<String, Object> parts = parser.map();
Map<String, MappingMetaData> mappings = new HashMap<>();
for (Map.Entry<String, Object> entry : parts.entrySet()) {
String indexName = entry.getKey();
assert entry.getValue() instanceof Map : "expected a map as type mapping, but got: " + entry.getValue().getClass();
@SuppressWarnings("unchecked")
final Map<String, Object> fieldMappings = (Map<String, Object>) ((Map<String, ?>) entry.getValue())
.get(MAPPINGS.getPreferredName());
mappings.put(indexName, new MappingMetaData(MapperService.SINGLE_MAPPING_NAME, fieldMappings));
}
return new GetMappingsResponse(mappings);
}
}

View File

@ -41,8 +41,6 @@ import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest;
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeResponse; import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeResponse;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest; import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexResponse; import org.elasticsearch.action.admin.indices.get.GetIndexResponse;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse;
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest; import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
import org.elasticsearch.action.admin.indices.open.OpenIndexResponse; import org.elasticsearch.action.admin.indices.open.OpenIndexResponse;
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest; import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
@ -72,6 +70,8 @@ import org.elasticsearch.client.indices.FreezeIndexRequest;
import org.elasticsearch.client.indices.GetFieldMappingsRequest; import org.elasticsearch.client.indices.GetFieldMappingsRequest;
import org.elasticsearch.client.indices.GetFieldMappingsResponse; import org.elasticsearch.client.indices.GetFieldMappingsResponse;
import org.elasticsearch.client.indices.GetIndexTemplatesRequest; import org.elasticsearch.client.indices.GetIndexTemplatesRequest;
import org.elasticsearch.client.indices.GetMappingsRequest;
import org.elasticsearch.client.indices.GetMappingsResponse;
import org.elasticsearch.client.indices.IndexTemplatesExistRequest; import org.elasticsearch.client.indices.IndexTemplatesExistRequest;
import org.elasticsearch.client.indices.PutMappingRequest; import org.elasticsearch.client.indices.PutMappingRequest;
import org.elasticsearch.client.indices.UnfreezeIndexRequest; import org.elasticsearch.client.indices.UnfreezeIndexRequest;
@ -95,6 +95,7 @@ import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.rest.action.admin.indices.RestCreateIndexAction; import org.elasticsearch.rest.action.admin.indices.RestCreateIndexAction;
import org.elasticsearch.rest.action.admin.indices.RestGetFieldMappingAction; import org.elasticsearch.rest.action.admin.indices.RestGetFieldMappingAction;
import org.elasticsearch.rest.action.admin.indices.RestGetMappingAction;
import org.elasticsearch.rest.action.admin.indices.RestPutMappingAction; import org.elasticsearch.rest.action.admin.indices.RestPutMappingAction;
import java.io.IOException; import java.io.IOException;
@ -527,8 +528,9 @@ public class IndicesClientIT extends ESRestHighLevelClientTestCase {
mappingBuilder.endObject().endObject().endObject(); mappingBuilder.endObject().endObject().endObject();
putMappingRequest.source(mappingBuilder); putMappingRequest.source(mappingBuilder);
AcknowledgedResponse putMappingResponse = AcknowledgedResponse putMappingResponse = execute(putMappingRequest,
execute(putMappingRequest, highLevelClient().indices()::putMapping, highLevelClient().indices()::putMappingAsync); highLevelClient().indices()::putMapping,
highLevelClient().indices()::putMappingAsync);
assertTrue(putMappingResponse.isAcknowledged()); assertTrue(putMappingResponse.isAcknowledged());
Map<String, Object> getIndexResponse = getAsMap(indexName); Map<String, Object> getIndexResponse = getAsMap(indexName);
@ -536,8 +538,48 @@ public class IndicesClientIT extends ESRestHighLevelClientTestCase {
GetMappingsRequest request = new GetMappingsRequest().indices(indexName); GetMappingsRequest request = new GetMappingsRequest().indices(indexName);
GetMappingsResponse getMappingsResponse = GetMappingsResponse getMappingsResponse = execute(
execute(request, highLevelClient().indices()::getMapping, highLevelClient().indices()::getMappingAsync); request,
highLevelClient().indices()::getMapping,
highLevelClient().indices()::getMappingAsync);
Map<String, Object> mappings = getMappingsResponse.mappings().get(indexName).sourceAsMap();
Map<String, String> type = new HashMap<>();
type.put("type", "text");
Map<String, Object> field = new HashMap<>();
field.put("field", type);
Map<String, Object> expected = new HashMap<>();
expected.put("properties", field);
assertThat(mappings, equalTo(expected));
}
public void testGetMappingWithTypes() throws IOException {
String indexName = "test";
createIndex(indexName, Settings.EMPTY);
PutMappingRequest putMappingRequest = new PutMappingRequest(indexName);
XContentBuilder mappingBuilder = JsonXContent.contentBuilder();
mappingBuilder.startObject().startObject("properties").startObject("field");
mappingBuilder.field("type", "text");
mappingBuilder.endObject().endObject().endObject();
putMappingRequest.source(mappingBuilder);
AcknowledgedResponse putMappingResponse = execute(putMappingRequest,
highLevelClient().indices()::putMapping,
highLevelClient().indices()::putMappingAsync);
assertTrue(putMappingResponse.isAcknowledged());
Map<String, Object> getIndexResponse = getAsMap(indexName);
assertEquals("text", XContentMapValues.extractValue(indexName + ".mappings.properties.field.type", getIndexResponse));
org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest request =
new org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest().indices(indexName);
org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse getMappingsResponse = execute(
request,
highLevelClient().indices()::getMapping,
highLevelClient().indices()::getMappingAsync,
expectWarnings(RestGetMappingAction.TYPES_DEPRECATION_MESSAGE));
Map<String, Object> mappings = getMappingsResponse.getMappings().get(indexName).get("_doc").sourceAsMap(); Map<String, Object> mappings = getMappingsResponse.getMappings().get(indexName).get("_doc").sourceAsMap();
Map<String, String> type = new HashMap<>(); Map<String, String> type = new HashMap<>();

View File

@ -37,7 +37,6 @@ import org.elasticsearch.action.admin.indices.flush.FlushRequest;
import org.elasticsearch.action.admin.indices.flush.SyncedFlushRequest; import org.elasticsearch.action.admin.indices.flush.SyncedFlushRequest;
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest; import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest; import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest;
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest; import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest; import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
import org.elasticsearch.action.admin.indices.rollover.RolloverRequest; import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
@ -50,11 +49,12 @@ import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateReque
import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest; import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest;
import org.elasticsearch.action.support.master.AcknowledgedRequest; import org.elasticsearch.action.support.master.AcknowledgedRequest;
import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.RandomCreateIndexGenerator; import org.elasticsearch.client.indices.GetFieldMappingsRequest;
import org.elasticsearch.client.indices.GetIndexTemplatesRequest; import org.elasticsearch.client.indices.GetIndexTemplatesRequest;
import org.elasticsearch.client.indices.GetMappingsRequest;
import org.elasticsearch.client.indices.IndexTemplatesExistRequest; import org.elasticsearch.client.indices.IndexTemplatesExistRequest;
import org.elasticsearch.client.indices.PutMappingRequest; import org.elasticsearch.client.indices.PutMappingRequest;
import org.elasticsearch.client.indices.GetFieldMappingsRequest; import org.elasticsearch.client.indices.RandomCreateIndexGenerator;
import org.elasticsearch.common.CheckedFunction; import org.elasticsearch.common.CheckedFunction;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
@ -231,22 +231,53 @@ public class IndicesRequestConvertersTests extends ESTestCase {
RequestConvertersTests.assertToXContentBody(putMappingRequest, request.getEntity()); RequestConvertersTests.assertToXContentBody(putMappingRequest, request.getEntity());
} }
public void testGetMapping() throws IOException { public void testGetMapping() {
GetMappingsRequest getMappingRequest = new GetMappingsRequest(); GetMappingsRequest getMappingRequest = new GetMappingsRequest();
String[] indices = Strings.EMPTY_ARRAY; String[] indices = Strings.EMPTY_ARRAY;
if (ESTestCase.randomBoolean()) { if (randomBoolean()) {
indices = RequestConvertersTests.randomIndicesNames(0, 5); indices = RequestConvertersTests.randomIndicesNames(0, 5);
getMappingRequest.indices(indices); getMappingRequest.indices(indices);
} else if (ESTestCase.randomBoolean()) { } else if (randomBoolean()) {
getMappingRequest.indices((String[]) null);
}
Map<String, String> expectedParams = new HashMap<>();
RequestConvertersTests.setRandomIndicesOptions(getMappingRequest::indicesOptions,
getMappingRequest::indicesOptions, expectedParams);
RequestConvertersTests.setRandomMasterTimeout(getMappingRequest, expectedParams);
RequestConvertersTests.setRandomLocal(getMappingRequest::local, expectedParams);
Request request = IndicesRequestConverters.getMappings(getMappingRequest);
StringJoiner endpoint = new StringJoiner("/", "/", "");
String index = String.join(",", indices);
if (Strings.hasLength(index)) {
endpoint.add(index);
}
endpoint.add("_mapping");
Assert.assertThat(endpoint.toString(), equalTo(request.getEndpoint()));
Assert.assertThat(expectedParams, equalTo(request.getParameters()));
Assert.assertThat(HttpGet.METHOD_NAME, equalTo(request.getMethod()));
}
public void testGetMappingWithTypes() {
org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest getMappingRequest =
new org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest();
String[] indices = Strings.EMPTY_ARRAY;
if (randomBoolean()) {
indices = RequestConvertersTests.randomIndicesNames(0, 5);
getMappingRequest.indices(indices);
} else if (randomBoolean()) {
getMappingRequest.indices((String[]) null); getMappingRequest.indices((String[]) null);
} }
String type = null; String type = null;
if (ESTestCase.randomBoolean()) { if (randomBoolean()) {
type = ESTestCase.randomAlphaOfLengthBetween(3, 10); type = randomAlphaOfLengthBetween(3, 10);
getMappingRequest.types(type); getMappingRequest.types(type);
} else if (ESTestCase.randomBoolean()) { } else if (randomBoolean()) {
getMappingRequest.types((String[]) null); getMappingRequest.types((String[]) null);
} }
@ -256,6 +287,7 @@ public class IndicesRequestConvertersTests extends ESTestCase {
getMappingRequest::indicesOptions, expectedParams); getMappingRequest::indicesOptions, expectedParams);
RequestConvertersTests.setRandomMasterTimeout(getMappingRequest, expectedParams); RequestConvertersTests.setRandomMasterTimeout(getMappingRequest, expectedParams);
RequestConvertersTests.setRandomLocal(getMappingRequest, expectedParams); RequestConvertersTests.setRandomLocal(getMappingRequest, expectedParams);
expectedParams.put(INCLUDE_TYPE_NAME_PARAMETER, "true");
Request request = IndicesRequestConverters.getMappings(getMappingRequest); Request request = IndicesRequestConverters.getMappings(getMappingRequest);
StringJoiner endpoint = new StringJoiner("/", "/", ""); StringJoiner endpoint = new StringJoiner("/", "/", "");
@ -277,21 +309,21 @@ public class IndicesRequestConvertersTests extends ESTestCase {
GetFieldMappingsRequest getFieldMappingsRequest = new GetFieldMappingsRequest(); GetFieldMappingsRequest getFieldMappingsRequest = new GetFieldMappingsRequest();
String[] indices = Strings.EMPTY_ARRAY; String[] indices = Strings.EMPTY_ARRAY;
if (ESTestCase.randomBoolean()) { if (randomBoolean()) {
indices = RequestConvertersTests.randomIndicesNames(0, 5); indices = RequestConvertersTests.randomIndicesNames(0, 5);
getFieldMappingsRequest.indices(indices); getFieldMappingsRequest.indices(indices);
} else if (ESTestCase.randomBoolean()) { } else if (randomBoolean()) {
getFieldMappingsRequest.indices((String[]) null); getFieldMappingsRequest.indices((String[]) null);
} }
String[] fields = null; String[] fields = null;
if (ESTestCase.randomBoolean()) { if (randomBoolean()) {
fields = new String[ESTestCase.randomIntBetween(1, 5)]; fields = new String[randomIntBetween(1, 5)];
for (int i = 0; i < fields.length; i++) { for (int i = 0; i < fields.length; i++) {
fields[i] = ESTestCase.randomAlphaOfLengthBetween(3, 10); fields[i] = randomAlphaOfLengthBetween(3, 10);
} }
getFieldMappingsRequest.fields(fields); getFieldMappingsRequest.fields(fields);
} else if (ESTestCase.randomBoolean()) { } else if (randomBoolean()) {
getFieldMappingsRequest.fields((String[]) null); getFieldMappingsRequest.fields((String[]) null);
} }
@ -321,29 +353,29 @@ public class IndicesRequestConvertersTests extends ESTestCase {
new org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsRequest(); new org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsRequest();
String[] indices = Strings.EMPTY_ARRAY; String[] indices = Strings.EMPTY_ARRAY;
if (ESTestCase.randomBoolean()) { if (randomBoolean()) {
indices = RequestConvertersTests.randomIndicesNames(0, 5); indices = RequestConvertersTests.randomIndicesNames(0, 5);
getFieldMappingsRequest.indices(indices); getFieldMappingsRequest.indices(indices);
} else if (ESTestCase.randomBoolean()) { } else if (randomBoolean()) {
getFieldMappingsRequest.indices((String[]) null); getFieldMappingsRequest.indices((String[]) null);
} }
String type = null; String type = null;
if (ESTestCase.randomBoolean()) { if (randomBoolean()) {
type = ESTestCase.randomAlphaOfLengthBetween(3, 10); type = randomAlphaOfLengthBetween(3, 10);
getFieldMappingsRequest.types(type); getFieldMappingsRequest.types(type);
} else if (ESTestCase.randomBoolean()) { } else if (randomBoolean()) {
getFieldMappingsRequest.types((String[]) null); getFieldMappingsRequest.types((String[]) null);
} }
String[] fields = null; String[] fields = null;
if (ESTestCase.randomBoolean()) { if (randomBoolean()) {
fields = new String[ESTestCase.randomIntBetween(1, 5)]; fields = new String[randomIntBetween(1, 5)];
for (int i = 0; i < fields.length; i++) { for (int i = 0; i < fields.length; i++) {
fields[i] = ESTestCase.randomAlphaOfLengthBetween(3, 10); fields[i] = randomAlphaOfLengthBetween(3, 10);
} }
getFieldMappingsRequest.fields(fields); getFieldMappingsRequest.fields(fields);
} else if (ESTestCase.randomBoolean()) { } else if (randomBoolean()) {
getFieldMappingsRequest.fields((String[]) null); getFieldMappingsRequest.fields((String[]) null);
} }

View File

@ -42,8 +42,6 @@ import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexResponse; import org.elasticsearch.action.admin.indices.get.GetIndexResponse;
import org.elasticsearch.client.indices.GetFieldMappingsRequest; import org.elasticsearch.client.indices.GetFieldMappingsRequest;
import org.elasticsearch.client.indices.GetFieldMappingsResponse; import org.elasticsearch.client.indices.GetFieldMappingsResponse;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse;
import org.elasticsearch.action.admin.indices.open.OpenIndexRequest; import org.elasticsearch.action.admin.indices.open.OpenIndexRequest;
import org.elasticsearch.action.admin.indices.open.OpenIndexResponse; import org.elasticsearch.action.admin.indices.open.OpenIndexResponse;
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest; import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
@ -76,6 +74,8 @@ import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse; import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.FreezeIndexRequest; import org.elasticsearch.client.indices.FreezeIndexRequest;
import org.elasticsearch.client.indices.GetIndexTemplatesRequest; import org.elasticsearch.client.indices.GetIndexTemplatesRequest;
import org.elasticsearch.client.indices.GetMappingsRequest;
import org.elasticsearch.client.indices.GetMappingsResponse;
import org.elasticsearch.client.indices.IndexTemplatesExistRequest; import org.elasticsearch.client.indices.IndexTemplatesExistRequest;
import org.elasticsearch.client.indices.PutMappingRequest; import org.elasticsearch.client.indices.PutMappingRequest;
import org.elasticsearch.client.indices.UnfreezeIndexRequest; import org.elasticsearch.client.indices.UnfreezeIndexRequest;
@ -591,8 +591,7 @@ public class IndicesClientDocumentationIT extends ESRestHighLevelClientTestCase
// end::get-mappings-request // end::get-mappings-request
// tag::get-mappings-request-masterTimeout // tag::get-mappings-request-masterTimeout
request.masterNodeTimeout(TimeValue.timeValueMinutes(1)); // <1> request.setMasterTimeout(TimeValue.timeValueMinutes(1)); // <1>
request.masterNodeTimeout("1m"); // <2>
// end::get-mappings-request-masterTimeout // end::get-mappings-request-masterTimeout
// tag::get-mappings-request-indicesOptions // tag::get-mappings-request-indicesOptions
@ -604,9 +603,9 @@ public class IndicesClientDocumentationIT extends ESRestHighLevelClientTestCase
// end::get-mappings-execute // end::get-mappings-execute
// tag::get-mappings-response // tag::get-mappings-response
ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> allMappings = getMappingResponse.mappings(); // <1> Map<String, MappingMetaData> allMappings = getMappingResponse.mappings(); // <1>
MappingMetaData typeMapping = allMappings.get("twitter").get("_doc"); // <2> MappingMetaData indexMapping = allMappings.get("twitter"); // <2>
Map<String, Object> mapping = typeMapping.sourceAsMap(); // <3> Map<String, Object> mapping = indexMapping.sourceAsMap(); // <3>
// end::get-mappings-response // end::get-mappings-response
Map<String, String> type = new HashMap<>(); Map<String, String> type = new HashMap<>();
@ -662,9 +661,9 @@ public class IndicesClientDocumentationIT extends ESRestHighLevelClientTestCase
final CountDownLatch latch = new CountDownLatch(1); final CountDownLatch latch = new CountDownLatch(1);
final ActionListener<GetMappingsResponse> latchListener = new LatchedActionListener<>(listener, latch); final ActionListener<GetMappingsResponse> latchListener = new LatchedActionListener<>(listener, latch);
listener = ActionListener.wrap(r -> { listener = ActionListener.wrap(r -> {
ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> allMappings = r.mappings(); Map<String, MappingMetaData> allMappings = r.mappings();
MappingMetaData typeMapping = allMappings.get("twitter").get("_doc"); MappingMetaData indexMapping = allMappings.get("twitter");
Map<String, Object> mapping = typeMapping.sourceAsMap(); Map<String, Object> mapping = indexMapping.sourceAsMap();
Map<String, String> type = new HashMap<>(); Map<String, String> type = new HashMap<>();
type.put("type", "text"); type.put("type", "text");

View File

@ -44,14 +44,14 @@ public class GetFieldMappingsResponseTests extends ESTestCase {
.test(); .test();
} }
Predicate<String> getRandomFieldsExcludeFilter() { private Predicate<String> getRandomFieldsExcludeFilter() {
// allow random fields at the level of `index` and `index.mappings.field` // allow random fields at the level of `index` and `index.mappings.field`
// otherwise random field could be evaluated as index name or type name // otherwise random field could be evaluated as index name or type name
return s -> false == (s.matches("(?<index>[^.]+)") return s -> false == (s.matches("(?<index>[^.]+)")
|| s.matches("(?<index>[^.]+)\\.mappings\\.(?<field>[^.]+)")); || s.matches("(?<index>[^.]+)\\.mappings\\.(?<field>[^.]+)"));
} }
static GetFieldMappingsResponse createTestInstance() { private static GetFieldMappingsResponse createTestInstance() {
Map<String, Map<String, FieldMappingMetaData>> mappings = new HashMap<>(); Map<String, Map<String, FieldMappingMetaData>> mappings = new HashMap<>();
// if mappings is empty, means that fields are not found // if mappings is empty, means that fields are not found
if (randomBoolean()) { if (randomBoolean()) {
@ -72,12 +72,11 @@ public class GetFieldMappingsResponseTests extends ESTestCase {
} }
// As the client class GetFieldMappingsResponse doesn't have toXContent method, adding this method here only for the test // As the client class GetFieldMappingsResponse doesn't have toXContent method, adding this method here only for the test
static void toXContent(GetFieldMappingsResponse response, XContentBuilder builder) throws IOException { private static void toXContent(GetFieldMappingsResponse response, XContentBuilder builder) throws IOException {
builder.startObject(); builder.startObject();
for (Map.Entry<String, Map<String, FieldMappingMetaData>> indexEntry : response.mappings().entrySet()) { for (Map.Entry<String, Map<String, FieldMappingMetaData>> indexEntry : response.mappings().entrySet()) {
builder.startObject(indexEntry.getKey()); builder.startObject(indexEntry.getKey());
builder.startObject("mappings"); builder.startObject("mappings");
Map<String, FieldMappingMetaData> mappings = null;
for (Map.Entry<String, FieldMappingMetaData> fieldEntry : indexEntry.getValue().entrySet()) { for (Map.Entry<String, FieldMappingMetaData> fieldEntry : indexEntry.getValue().entrySet()) {
builder.startObject(fieldEntry.getKey()); builder.startObject(fieldEntry.getKey());
builder.field("full_name", fieldEntry.getValue().fullName()); builder.field("full_name", fieldEntry.getValue().fullName());

View File

@ -0,0 +1,120 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.client.indices;
import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.ToXContent.Params;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.test.ESTestCase;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Predicate;
import static org.elasticsearch.client.indices.GetMappingsResponse.MAPPINGS;
import static org.elasticsearch.test.AbstractXContentTestCase.xContentTester;
public class GetMappingsResponseTests extends ESTestCase {
// Because the client-side class does not have a toXContent method, we test xContent serialization by creating
// a random client object, converting it to a server object then serializing it to xContent, and finally
// parsing it back as a client object. We check equality between the original client object, and the parsed one.
public void testFromXContent() throws IOException {
xContentTester(
this::createParser,
GetMappingsResponseTests::createTestInstance,
GetMappingsResponseTests::toXContent,
GetMappingsResponse::fromXContent)
.supportsUnknownFields(true)
.assertEqualsConsumer(GetMappingsResponseTests::assertEqualInstances)
.randomFieldsExcludeFilter(randomFieldsExcludeFilter())
.test();
}
private static GetMappingsResponse createTestInstance() {
Map<String, MappingMetaData> mappings = Collections.singletonMap(
"index-" + randomAlphaOfLength(5), randomMappingMetaData());
return new GetMappingsResponse(mappings);
}
private static void assertEqualInstances(GetMappingsResponse expected, GetMappingsResponse actual) {
assertEquals(expected.mappings(), actual.mappings());
}
private Predicate<String> randomFieldsExcludeFilter() {
return field -> !field.equals(MAPPINGS.getPreferredName());
}
public static MappingMetaData randomMappingMetaData() {
Map<String, Object> mappings = new HashMap<>();
if (frequently()) { // rarely have no fields
mappings.put("field1", randomFieldMapping());
if (randomBoolean()) {
mappings.put("field2", randomFieldMapping());
}
}
try {
return new MappingMetaData(MapperService.SINGLE_MAPPING_NAME, mappings);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static Map<String, Object> randomFieldMapping() {
Map<String, Object> mappings = new HashMap<>();
if (randomBoolean()) {
mappings.put("type", randomFrom("text", "keyword"));
mappings.put("index", "analyzed");
mappings.put("analyzer", "english");
} else {
mappings.put("type", randomFrom("integer", "float", "long", "double"));
mappings.put("index", Objects.toString(randomBoolean()));
}
return mappings;
}
private static void toXContent(GetMappingsResponse response, XContentBuilder builder) throws IOException {
Params params = new ToXContent.MapParams(
Collections.singletonMap(BaseRestHandler.INCLUDE_TYPE_NAME_PARAMETER, "false"));
ImmutableOpenMap.Builder<String, ImmutableOpenMap<String, MappingMetaData>> allMappings = ImmutableOpenMap.builder();
for (Map.Entry<String, MappingMetaData> indexEntry : response.mappings().entrySet()) {
ImmutableOpenMap.Builder<String, MappingMetaData> mappings = ImmutableOpenMap.builder();
mappings.put(MapperService.SINGLE_MAPPING_NAME, indexEntry.getValue());
allMappings.put(indexEntry.getKey(), mappings.build());
}
org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse serverResponse =
new org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse(allMappings.build());
builder.startObject();
serverResponse.toXContent(builder, params);
builder.endObject();
}
}

View File

@ -10,13 +10,13 @@
[id="{upid}-{api}-request"] [id="{upid}-{api}-request"]
==== Get Mappings Request ==== Get Mappings Request
A +{request}+ can have an optional list of indices and optional list of types: A +{request}+ can have an optional list of indices:
["source","java",subs="attributes,callouts,macros"] ["source","java",subs="attributes,callouts,macros"]
-------------------------------------------------- --------------------------------------------------
include-tagged::{doc-tests-file}[{api}-request] include-tagged::{doc-tests-file}[{api}-request]
-------------------------------------------------- --------------------------------------------------
<1> An empty request that will return all indices and types <1> An empty request that will return all indices
<2> Setting the indices to fetch mapping for <2> Setting the indices to fetch mapping for
==== Optional arguments ==== Optional arguments
@ -27,7 +27,6 @@ The following arguments can also optionally be provided:
include-tagged::{doc-tests-file}[{api}-request-masterTimeout] include-tagged::{doc-tests-file}[{api}-request-masterTimeout]
-------------------------------------------------- --------------------------------------------------
<1> Timeout to connect to the master node as a `TimeValue` <1> Timeout to connect to the master node as a `TimeValue`
<2> Timeout to connect to the master node as a `String`
["source","java",subs="attributes,callouts,macros"] ["source","java",subs="attributes,callouts,macros"]
-------------------------------------------------- --------------------------------------------------
@ -48,5 +47,5 @@ executed operation as follows:
include-tagged::{doc-tests-file}[{api}-response] include-tagged::{doc-tests-file}[{api}-response]
-------------------------------------------------- --------------------------------------------------
<1> Returning all indices' mappings <1> Returning all indices' mappings
<2> Retrieving the mappings for a particular index and type <2> Retrieving the mappings for a particular index
<3> Getting the mappings as a Java Map <3> Getting the mappings as a Java Map

View File

@ -20,7 +20,6 @@
package org.elasticsearch.action.admin.indices.mapping.get; package org.elasticsearch.action.admin.indices.mapping.get;
import com.carrotsearch.hppc.cursors.ObjectObjectCursor; import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.cluster.metadata.MappingMetaData; import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.common.ParseField; import org.elasticsearch.common.ParseField;
@ -31,7 +30,6 @@ import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ToXContentFragment; import org.elasticsearch.common.xcontent.ToXContentFragment;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.BaseRestHandler;
import java.io.IOException; import java.io.IOException;
@ -45,7 +43,7 @@ public class GetMappingsResponse extends ActionResponse implements ToXContentFra
private ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappings = ImmutableOpenMap.of(); private ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappings = ImmutableOpenMap.of();
GetMappingsResponse(ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappings) { public GetMappingsResponse(ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappings) {
this.mappings = mappings; this.mappings = mappings;
} }
@ -102,17 +100,20 @@ public class GetMappingsResponse extends ActionResponse implements ToXContentFra
for (Map.Entry<String, Object> entry : parts.entrySet()) { for (Map.Entry<String, Object> entry : parts.entrySet()) {
final String indexName = entry.getKey(); final String indexName = entry.getKey();
assert entry.getValue() instanceof Map : "expected a map as type mapping, but got: " + entry.getValue().getClass(); assert entry.getValue() instanceof Map : "expected a map as type mapping, but got: " + entry.getValue().getClass();
final Map<String, Object> mapping = (Map<String, Object>) ((Map) entry.getValue()).get(MAPPINGS.getPreferredName());
ImmutableOpenMap.Builder<String, MappingMetaData> typeBuilder = new ImmutableOpenMap.Builder<>(); ImmutableOpenMap.Builder<String, MappingMetaData> typeBuilder = new ImmutableOpenMap.Builder<>();
@SuppressWarnings("unchecked") for (Map.Entry<String, Object> typeEntry : mapping.entrySet()) {
final Map<String, Object> fieldMappings = (Map<String, Object>) ((Map<String, ?>) entry.getValue()) final String typeName = typeEntry.getKey();
.get(MAPPINGS.getPreferredName()); assert typeEntry.getValue() instanceof Map : "expected a map as inner type mapping, but got: " +
if (fieldMappings.isEmpty() == false) { typeEntry.getValue().getClass();
assert fieldMappings instanceof Map : "expected a map as inner type mapping, but got: " + fieldMappings.getClass(); final Map<String, Object> fieldMappings = (Map<String, Object>) typeEntry.getValue();
MappingMetaData mmd = new MappingMetaData(MapperService.SINGLE_MAPPING_NAME, fieldMappings); MappingMetaData mmd = new MappingMetaData(typeName, fieldMappings);
typeBuilder.put(MapperService.SINGLE_MAPPING_NAME, mmd); typeBuilder.put(typeName, mmd);
} }
builder.put(indexName, typeBuilder.build()); builder.put(indexName, typeBuilder.build());
} }
return new GetMappingsResponse(builder.build()); return new GetMappingsResponse(builder.build());
} }

View File

@ -60,8 +60,8 @@ import static org.elasticsearch.rest.RestRequest.Method.HEAD;
public class RestGetMappingAction extends BaseRestHandler { public class RestGetMappingAction extends BaseRestHandler {
private static final Logger logger = LogManager.getLogger(RestGetMappingAction.class); private static final Logger logger = LogManager.getLogger(RestGetMappingAction.class);
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(logger); private static final DeprecationLogger deprecationLogger = new DeprecationLogger(logger);
static final String TYPES_DEPRECATION_MESSAGE = "[types removal] Using include_type_name in get mapping requests is deprecated. " public static final String TYPES_DEPRECATION_MESSAGE = "[types removal] Using include_type_name in get" +
+ "The parameter will be removed in the next major version."; " mapping requests is deprecated. The parameter will be removed in the next major version.";
public RestGetMappingAction(final Settings settings, final RestController controller) { public RestGetMappingAction(final Settings settings, final RestController controller) {
super(settings); super(settings);

View File

@ -124,8 +124,8 @@ public class GetFieldMappingsResponseTests extends AbstractStreamableXContentTes
} }
/** /**
* For now, we only unit test the legacy typed responses. This will soon no longer be the case, * For xContent roundtrip testing we force the xContent output to still contain types because the parser
* as we introduce support for typeless xContent parsing in {@link GetFieldMappingsResponse}. * still expects them. The new typeless parsing is implemented in the client side GetFieldMappingsResponse.
*/ */
@Override @Override
protected ToXContent.Params getToXContentParams() { protected ToXContent.Params getToXContentParams() {

View File

@ -20,13 +20,11 @@
package org.elasticsearch.action.admin.indices.mapping.get; package org.elasticsearch.action.admin.indices.mapping.get;
import com.carrotsearch.hppc.cursors.ObjectCursor; import com.carrotsearch.hppc.cursors.ObjectCursor;
import org.elasticsearch.cluster.metadata.MappingMetaData; import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.common.collect.ImmutableOpenMap; import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.ToXContent.Params; import org.elasticsearch.common.xcontent.ToXContent.Params;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.test.AbstractStreamableXContentTestCase; import org.elasticsearch.test.AbstractStreamableXContentTestCase;
@ -41,8 +39,6 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import static org.elasticsearch.test.AbstractXContentTestCase.xContentTester;
public class GetMappingsResponseTests extends AbstractStreamableXContentTestCase<GetMappingsResponse> { public class GetMappingsResponseTests extends AbstractStreamableXContentTestCase<GetMappingsResponse> {
@Override @Override
@ -117,15 +113,20 @@ public class GetMappingsResponseTests extends AbstractStreamableXContentTestCase
return typeBuilder.build(); return typeBuilder.build();
} }
/**
* For xContent roundtrip testing we force the xContent output to still contain types because the parser
* still expects them. The new typeless parsing is implemented in the client side GetMappingsResponse.
*/
@Override @Override
protected GetMappingsResponse createTestInstance() { protected Params getToXContentParams() {
return createTestInstance(true); return new ToXContent.MapParams(Collections.singletonMap(BaseRestHandler.INCLUDE_TYPE_NAME_PARAMETER, "true"));
} }
private GetMappingsResponse createTestInstance(boolean randomTypeNames) { @Override
protected GetMappingsResponse createTestInstance() {
ImmutableOpenMap.Builder<String, ImmutableOpenMap<String, MappingMetaData>> indexBuilder = ImmutableOpenMap.builder(); ImmutableOpenMap.Builder<String, ImmutableOpenMap<String, MappingMetaData>> indexBuilder = ImmutableOpenMap.builder();
int typeCount = rarely() ? 0 : 1; int typeCount = rarely() ? 0 : 1;
indexBuilder.put("index-" + randomAlphaOfLength(5), createMappingsForIndex(typeCount, randomTypeNames)); indexBuilder.put("index-" + randomAlphaOfLength(5), createMappingsForIndex(typeCount, randomBoolean()));
GetMappingsResponse resp = new GetMappingsResponse(indexBuilder.build()); GetMappingsResponse resp = new GetMappingsResponse(indexBuilder.build());
logger.debug("--> created: {}", resp); logger.debug("--> created: {}", resp);
return resp; return resp;
@ -163,57 +164,4 @@ public class GetMappingsResponseTests extends AbstractStreamableXContentTestCase
} }
return mappings; return mappings;
} }
@Override
protected GetMappingsResponse createXContextTestInstance(XContentType xContentType) {
// don't use random type names for XContent roundtrip tests because we cannot parse them back anymore
return createTestInstance(false);
}
/**
* check that the "old" legacy response format with types works as expected
*/
public void testToXContentWithTypes() throws IOException {
Params params = new ToXContent.MapParams(Collections.singletonMap(BaseRestHandler.INCLUDE_TYPE_NAME_PARAMETER, "true"));
xContentTester(this::createParser, t -> createTestInstance(), params, this::fromXContentWithTypes)
.numberOfTestRuns(NUMBER_OF_TEST_RUNS)
.supportsUnknownFields(supportsUnknownFields())
.shuffleFieldsExceptions(getShuffleFieldsExceptions())
.randomFieldsExcludeFilter(getRandomFieldsExcludeFilter())
.assertEqualsConsumer(this::assertEqualInstances)
.assertToXContentEquivalence(true)
.test();
}
/**
* including the pre-7.0 parsing code here to test that older HLRC clients using this can parse the responses that are
* returned when "include_type_name=true"
*/
private GetMappingsResponse fromXContentWithTypes(XContentParser parser) throws IOException {
if (parser.currentToken() == null) {
parser.nextToken();
}
assert parser.currentToken() == XContentParser.Token.START_OBJECT;
Map<String, Object> parts = parser.map();
ImmutableOpenMap.Builder<String, ImmutableOpenMap<String, MappingMetaData>> builder = new ImmutableOpenMap.Builder<>();
for (Map.Entry<String, Object> entry : parts.entrySet()) {
final String indexName = entry.getKey();
assert entry.getValue() instanceof Map : "expected a map as type mapping, but got: " + entry.getValue().getClass();
final Map<String, Object> mapping = (Map<String, Object>) ((Map) entry.getValue()).get("mappings");
ImmutableOpenMap.Builder<String, MappingMetaData> typeBuilder = new ImmutableOpenMap.Builder<>();
for (Map.Entry<String, Object> typeEntry : mapping.entrySet()) {
final String typeName = typeEntry.getKey();
assert typeEntry.getValue() instanceof Map : "expected a map as inner type mapping, but got: "
+ typeEntry.getValue().getClass();
final Map<String, Object> fieldMappings = (Map<String, Object>) typeEntry.getValue();
MappingMetaData mmd = new MappingMetaData(typeName, fieldMappings);
typeBuilder.put(typeName, mmd);
}
builder.put(indexName, typeBuilder.build());
}
return new GetMappingsResponse(builder.build());
}
} }