Skip to content

Commit

Permalink
PutIndexTemplateRequest contains a single mapping (elastic#50899)
Browse files Browse the repository at this point in the history
New index templates can only contain a single mapping, as we no longer support
multiple types. This commit changes the internal classes used to represent a
PutIndexTemplateRequest from holding a map of types to mappings, to instead
holding a single mapping string.

Relates to elastic#41059
  • Loading branch information
romseygeek authored and SivagurunathanV committed Jan 21, 2020
1 parent d804ff0 commit 46045dc
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 156 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ protected void assertInstances(org.elasticsearch.action.admin.indices.template.p
throw new UncheckedIOException(e);
}
}
assertThat(serverInstance.mappings().get("_doc"), equalTo(mapping));
assertThat(serverInstance.mappings(), equalTo(mapping));
assertThat(serverInstance.settings(), equalTo(clientTestInstance.settings()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@
import org.apache.logging.log4j.LogManager;
import org.elasticsearch.ElasticsearchGenerationException;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.Version;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.IndicesRequest;
import org.elasticsearch.action.admin.indices.alias.Alias;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.master.MasterNodeRequest;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.collect.MapBuilder;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.logging.DeprecationLogger;
Expand All @@ -47,7 +48,6 @@

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -79,7 +79,8 @@ public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateR

private Settings settings = EMPTY_SETTINGS;

private Map<String, String> mappings = new HashMap<>();
@Nullable
private String mappings;

private final Set<Alias> aliases = new HashSet<>();

Expand All @@ -93,11 +94,14 @@ public PutIndexTemplateRequest(StreamInput in) throws IOException {
order = in.readInt();
create = in.readBoolean();
settings = readSettingsFromStream(in);
int size = in.readVInt();
for (int i = 0; i < size; i++) {
final String type = in.readString();
String mappingSource = in.readString();
mappings.put(type, mappingSource);
if (in.getVersion().before(Version.V_8_0_0)) {
int size = in.readVInt();
for (int i = 0; i < size; i++) {
in.readString(); // type - cannot assert on _doc because 7x allows arbitrary type names
this.mappings = in.readString();
}
} else {
this.mappings = in.readOptionalString();
}
int aliasesSize = in.readVInt();
for (int i = 0; i < aliasesSize; i++) {
Expand Down Expand Up @@ -225,17 +229,6 @@ public Settings settings() {
return this.settings;
}

/**
* Adds mapping that will be added when the index gets created.
*
* @param type The mapping type
* @param source The mapping source
* @param xContentType The type of content contained within the source
*/
public PutIndexTemplateRequest mapping(String type, String source, XContentType xContentType) {
return mapping(type, new BytesArray(source), xContentType);
}

/**
* The cause for this index template creation.
*/
Expand All @@ -251,41 +244,47 @@ public String cause() {
/**
* Adds mapping that will be added when the index gets created.
*
* @param type The mapping type
* @param source The mapping source
* @param xContentType The type of content contained within the source
*/
public PutIndexTemplateRequest mapping(String type, XContentBuilder source) {
return mapping(type, BytesReference.bytes(source), source.contentType());
public PutIndexTemplateRequest mapping(String source, XContentType xContentType) {
return mapping(new BytesArray(source), xContentType);
}

/**
* Adds mapping that will be added when the index gets created.
*
* @param source The mapping source
*/
public PutIndexTemplateRequest mapping(XContentBuilder source) {
return mapping(BytesReference.bytes(source), source.contentType());
}

/**
* Adds mapping that will be added when the index gets created.
*
* @param type The mapping type
* @param source The mapping source
* @param xContentType the source content type
*/
public PutIndexTemplateRequest mapping(String type, BytesReference source, XContentType xContentType) {
public PutIndexTemplateRequest mapping(BytesReference source, XContentType xContentType) {
Objects.requireNonNull(xContentType);
Map<String, Object> mappingAsMap = XContentHelper.convertToMap(source, false, xContentType).v2();
return mapping(type, mappingAsMap);
return mapping(mappingAsMap);
}

/**
* Adds mapping that will be added when the index gets created.
*
* @param type The mapping type
* @param source The mapping source
*/
public PutIndexTemplateRequest mapping(String type, Map<String, Object> source) {
// wrap it in a type map if its not
if (source.size() != 1 || !source.containsKey(type)) {
source = MapBuilder.<String, Object>newMapBuilder().put(type, source).map();
public PutIndexTemplateRequest mapping(Map<String, Object> source) {
if (source.size() != 1 || source.containsKey(MapperService.SINGLE_MAPPING_NAME) == false) {
source = Map.of(MapperService.SINGLE_MAPPING_NAME, source);
}
try {
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.map(source);
mappings.put(type, Strings.toString(builder));
mappings = Strings.toString(builder);
return this;
} catch (IOException e) {
throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e);
Expand All @@ -297,11 +296,11 @@ public PutIndexTemplateRequest mapping(String type, Map<String, Object> source)
* ("field1", "type=string,store=true").
*/
public PutIndexTemplateRequest mapping(String... source) {
mapping(MapperService.SINGLE_MAPPING_NAME, PutMappingRequest.simpleMapping(source));
mapping(PutMappingRequest.simpleMapping(source));
return this;
}

public Map<String, String> mappings() {
public String mappings() {
return this.mappings;
}

Expand Down Expand Up @@ -353,7 +352,7 @@ public PutIndexTemplateRequest source(Map<String, Object> templateSource) {
"Malformed [mappings] section for type [" + entry1.getKey() +
"], should include an inner object describing the mapping");
}
mapping(entry1.getKey(), (Map<String, Object>) entry1.getValue());
mapping((Map<String, Object>) entry1.getValue());
}
} else if (name.equals("aliases")) {
aliases((Map<String, Object>) entry.getValue());
Expand Down Expand Up @@ -471,10 +470,14 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeInt(order);
out.writeBoolean(create);
writeSettingsToStream(settings, out);
out.writeVInt(mappings.size());
for (Map.Entry<String, String> entry : mappings.entrySet()) {
out.writeString(entry.getKey());
out.writeString(entry.getValue());
if (out.getVersion().before(Version.V_8_0_0)) {
out.writeVInt(mappings == null ? 0 : 1);
if (mappings != null) {
out.writeString(MapperService.SINGLE_MAPPING_NAME);
out.writeString(mappings);
}
} else {
out.writeOptionalString(mappings);
}
out.writeVInt(aliases.size());
for (Alias alias : aliases) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,11 @@ public PutIndexTemplateRequestBuilder setSettings(Map<String, Object> source) {
/**
* Adds mapping that will be added when the index template gets created.
*
* @param type The mapping type
* @param source The mapping source
* @param xContentType The type/format of the source
*/
public PutIndexTemplateRequestBuilder addMapping(String type, String source, XContentType xContentType) {
request.mapping(type, source, xContentType);
public PutIndexTemplateRequestBuilder setMapping(String source, XContentType xContentType) {
request.mapping(source, xContentType);
return this;
}

Expand Down Expand Up @@ -181,22 +180,10 @@ public PutIndexTemplateRequestBuilder cause(String cause) {
/**
* Adds mapping that will be added when the index template gets created.
*
* @param type The mapping type
* @param source The mapping source
*/
public PutIndexTemplateRequestBuilder addMapping(String type, XContentBuilder source) {
request.mapping(type, source);
return this;
}

/**
* Adds mapping that will be added when the index gets created.
*
* @param type The mapping type
* @param source The mapping source
*/
public PutIndexTemplateRequestBuilder addMapping(String type, Map<String, Object> source) {
request.mapping(type, source);
public PutIndexTemplateRequestBuilder setMapping(XContentBuilder source) {
request.mapping(source);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,9 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
Expand Down Expand Up @@ -285,19 +283,14 @@ private static void validateAndAddTemplate(final PutRequest request, IndexTempla
templateBuilder.patterns(request.indexPatterns);
templateBuilder.settings(request.settings);

Map<String, Map<String, Object>> mappingsForValidation = new HashMap<>();
for (Map.Entry<String, String> entry : request.mappings.entrySet()) {
if (request.mappings != null) {
try {
templateBuilder.putMapping(entry.getKey(), entry.getValue());
templateBuilder.putMapping(MapperService.SINGLE_MAPPING_NAME, request.mappings);
} catch (Exception e) {
throw new MapperParsingException("Failed to parse mapping [{}]: {}", e, entry.getKey(), e.getMessage());
throw new MapperParsingException("Failed to parse mapping: {}", e, request.mappings);
}
mappingsForValidation.put(entry.getKey(), MapperService.parseMapping(xContentRegistry, entry.getValue()));
}
if (mappingsForValidation.isEmpty() == false) {
assert mappingsForValidation.size() == 1;
String type = mappingsForValidation.keySet().iterator().next();
dummyIndexService.mapperService().merge(type, mappingsForValidation.get(type), MergeReason.MAPPING_UPDATE);
dummyIndexService.mapperService().merge(MapperService.SINGLE_MAPPING_NAME,
MapperService.parseMapping(xContentRegistry, request.mappings), MergeReason.MAPPING_UPDATE);
}

} finally {
Expand Down Expand Up @@ -390,7 +383,7 @@ public static class PutRequest {
Integer version;
List<String> indexPatterns;
Settings settings = Settings.Builder.EMPTY_SETTINGS;
Map<String, String> mappings = new HashMap<>();
String mappings = null;
List<Alias> aliases = new ArrayList<>();

TimeValue masterTimeout = MasterNodeRequest.DEFAULT_MASTER_NODE_TIMEOUT;
Expand Down Expand Up @@ -420,8 +413,8 @@ public PutRequest settings(Settings settings) {
return this;
}

public PutRequest mappings(Map<String, String> mappings) {
this.mappings.putAll(mappings);
public PutRequest mappings(String mappings) {
this.mappings = mappings;
return this;
}

Expand All @@ -430,11 +423,6 @@ public PutRequest aliases(Set<Alias> aliases) {
return this;
}

public PutRequest putMapping(String mappingType, String mappingSource) {
mappings.put(mappingType, mappingSource);
return this;
}

public PutRequest masterTimeout(TimeValue masterTimeout) {
this.masterTimeout = masterTimeout;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void testIndexTemplateWithAliasNameEqualToTemplatePattern() {
public void testIndexTemplateWithValidateMapping() throws Exception {
PutRequest request = new PutRequest("api", "validate_template");
request.patterns(singletonList("te*"));
request.putMapping("type1", Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type1")
request.mappings(Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("_doc")
.startObject("properties").startObject("field2").field("type", "text").field("analyzer", "custom_1").endObject()
.endObject().endObject().endObject()));

Expand All @@ -118,19 +118,19 @@ public void testIndexTemplateWithValidateMapping() throws Exception {
public void testBrokenMapping() throws Exception {
PutRequest request = new PutRequest("api", "broken_mapping");
request.patterns(singletonList("te*"));
request.putMapping("type1", "abcde");
request.mappings("abcde");

List<Throwable> errors = putTemplateDetail(request);
assertThat(errors.size(), equalTo(1));
assertThat(errors.get(0), instanceOf(MapperParsingException.class));
assertThat(errors.get(0).getMessage(), containsString("Failed to parse mapping "));
assertThat(errors.get(0).getMessage(), containsString("Failed to parse mapping"));
}

public void testAliasInvalidFilterInvalidJson() throws Exception {
//invalid json: put index template fails
PutRequest request = new PutRequest("api", "blank_mapping");
request.patterns(singletonList("te*"));
request.putMapping("type1", "{}");
request.mappings("{}");
Set<Alias> aliases = new HashSet<>();
aliases.add(new Alias("invalid_alias").filter("abcde"));
request.aliases(aliases);
Expand Down
Loading

0 comments on commit 46045dc

Please sign in to comment.