Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 11 commits into from
Jan 28, 2019
Merged
Next Next commit
Support both typed and typeless 'get mapping' requests in the HLRC.
  • Loading branch information
jtibshirani committed Jan 24, 2019
commit acf286f9106e14bdf81837c1694703fa1189aba3
Original file line number Diff line number Diff line change
@@ -39,8 +39,6 @@
import org.elasticsearch.action.admin.indices.get.GetIndexResponse;
import org.elasticsearch.client.indices.GetFieldMappingsRequest;
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.OpenIndexResponse;
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
@@ -61,6 +59,8 @@
import org.elasticsearch.client.core.ShardsAcknowledgedResponse;
import org.elasticsearch.client.indices.FreezeIndexRequest;
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.PutMappingRequest;
import org.elasticsearch.client.indices.UnfreezeIndexRequest;
@@ -215,8 +215,11 @@ public void putMappingAsync(org.elasticsearch.action.admin.indices.mapping.put.P
* @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 {
return restHighLevelClient.performRequestAndParseEntity(getMappingsRequest, IndicesRequestConverters::getMappings, options,
GetMappingsResponse::fromXContent, emptySet());
return restHighLevelClient.performRequestAndParseEntity(getMappingsRequest,
IndicesRequestConverters::getMappings,
options,
GetMappingsResponse::fromXContent,
emptySet());
}

/**
@@ -229,8 +232,60 @@ public GetMappingsResponse getMapping(GetMappingsRequest getMappingsRequest, Req
*/
public void getMappingAsync(GetMappingsRequest getMappingsRequest, RequestOptions options,
ActionListener<GetMappingsResponse> listener) {
restHighLevelClient.performRequestAsyncAndParseEntity(getMappingsRequest, IndicesRequestConverters::getMappings, options,
GetMappingsResponse::fromXContent, listener, emptySet());
restHighLevelClient.performRequestAsyncAndParseEntity(getMappingsRequest,
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 an 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());
}

/**
@@ -242,8 +297,9 @@ public void getMappingAsync(GetMappingsRequest getMappingsRequest, RequestOption
* @return 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
* {@link #getFieldMapping(GetFieldMappingsRequest, RequestOptions)} should be used instead, which accepts a new request object.
* @deprecated This method uses old request and response objects which still refer to types, a deprecated feature.
* The method {@link #getFieldMapping(GetFieldMappingsRequest, RequestOptions)} should be used instead, which
* accepts a new request object.
*/
@Deprecated
public org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse getFieldMapping(
@@ -261,9 +317,9 @@ public org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsRespon
* @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 an old request object which still refers to types, a deprecated feature. The
* method {@link #getFieldMappingAsync(GetFieldMappingsRequest, RequestOptions, ActionListener)} should be used instead,
* which accepts a new request object.
* @deprecated This method uses an request and response objects which still refers to types, a deprecated feature.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: s/an/a

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, thanks!

* The method {@link #getFieldMappingAsync(GetFieldMappingsRequest, RequestOptions, ActionListener)} should be
* used instead, which accepts a new request object.
*/
@Deprecated
public void getFieldMappingAsync(org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsRequest getFieldMappingsRequest,
Original file line number Diff line number Diff line change
@@ -36,7 +36,6 @@
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
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.refresh.RefreshRequest;
import org.elasticsearch.action.admin.indices.rollover.RolloverRequest;
@@ -49,6 +48,7 @@
import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest;
import org.elasticsearch.client.indices.FreezeIndexRequest;
import org.elasticsearch.client.indices.GetIndexTemplatesRequest;
import org.elasticsearch.client.indices.GetMappingsRequest;
import org.elasticsearch.client.indices.IndexTemplatesExistRequest;
import org.elasticsearch.client.indices.PutMappingRequest;
import org.elasticsearch.client.indices.UnfreezeIndexRequest;
@@ -151,7 +151,20 @@ static Request putMapping(org.elasticsearch.action.admin.indices.mapping.put.Put
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;
}

static Request getMappings(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest getMappingsRequest) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add @deprecated even if we only use this internally. It makes it more obvious and serves as a grep-able reminder that we can remove this in 8.0.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

String[] indices = getMappingsRequest.indices() == null ? Strings.EMPTY_ARRAY : getMappingsRequest.indices();
String[] types = getMappingsRequest.types() == null ? Strings.EMPTY_ARRAY : getMappingsRequest.types();

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

return request;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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.client.Validatable;
import org.elasticsearch.common.Strings;

public class GetMappingsRequest extends TimedRequest implements Validatable {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since TimedRequest implement Validatable, we don't need it here again.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* 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;
import java.util.Objects;

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);
}

@Override
public boolean equals(Object o) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although implementing equals/hashcode never hurts I think, it puts some additional burden on us in testing and maintaining it. I think I opted for starting with not implementing it as long as we don't need it, but I'd be interested in your opinion on this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added these methods to support the serialization/ deserialization tests in GetMappingsResponseTests. But I agree in this instance I should probably remove this and just define assertEqualInstances in the test -- because MappingMetaData stores the mappings as xContent, general-purpose equals/ hashCode methods are not so useful and potentially misleading.

if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
GetMappingsResponse that = (GetMappingsResponse) o;
return Objects.equals(mappings, that.mappings);
}

@Override
public int hashCode() {
return Objects.hash(mappings);
}
}
Loading