From 46045dc88803120792d48725ee64a61484a1b2bb Mon Sep 17 00:00:00 2001 From: Alan Woodward Date: Mon, 20 Jan 2020 15:05:52 +0000 Subject: [PATCH] PutIndexTemplateRequest contains a single mapping (#50899) 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 #41059 --- .../indices/PutIndexTemplateRequestTests.java | 2 +- .../template/put/PutIndexTemplateRequest.java | 79 ++++++++++--------- .../put/PutIndexTemplateRequestBuilder.java | 21 +---- .../MetaDataIndexTemplateService.java | 28 ++----- .../MetaDataIndexTemplateServiceTests.java | 8 +- .../put/PutIndexTemplateRequestTests.java | 67 ++++++++-------- .../cluster/SimpleClusterStateIT.java | 8 +- .../gateway/RecoveryFromGatewayIT.java | 2 +- .../template/IndexTemplateBlocksIT.java | 2 +- .../template/SimpleIndexTemplateIT.java | 32 ++++---- .../SharedClusterSnapshotRestoreIT.java | 6 +- .../support/SecurityIndexManager.java | 3 +- .../support/SecurityIndexManagerTests.java | 26 +++--- .../persistence/TransformInternalIndex.java | 4 +- 14 files changed, 132 insertions(+), 156 deletions(-) diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/PutIndexTemplateRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/PutIndexTemplateRequestTests.java index 9118690201e5e..b10decb15b5a9 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/PutIndexTemplateRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/PutIndexTemplateRequestTests.java @@ -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())); } diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequest.java b/server/src/main/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequest.java index 3e6b4f5bc45d2..1ad5cedb9332c 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequest.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequest.java @@ -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; @@ -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; @@ -79,7 +79,8 @@ public class PutIndexTemplateRequest extends MasterNodeRequest mappings = new HashMap<>(); + @Nullable + private String mappings; private final Set aliases = new HashSet<>(); @@ -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++) { @@ -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. */ @@ -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 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 source) { - // wrap it in a type map if its not - if (source.size() != 1 || !source.containsKey(type)) { - source = MapBuilder.newMapBuilder().put(type, source).map(); + public PutIndexTemplateRequest mapping(Map 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); @@ -297,11 +296,11 @@ public PutIndexTemplateRequest mapping(String type, Map 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 mappings() { + public String mappings() { return this.mappings; } @@ -353,7 +352,7 @@ public PutIndexTemplateRequest source(Map templateSource) { "Malformed [mappings] section for type [" + entry1.getKey() + "], should include an inner object describing the mapping"); } - mapping(entry1.getKey(), (Map) entry1.getValue()); + mapping((Map) entry1.getValue()); } } else if (name.equals("aliases")) { aliases((Map) entry.getValue()); @@ -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 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) { diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequestBuilder.java b/server/src/main/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequestBuilder.java index b4d998c6e6770..33b7c13d204bf 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequestBuilder.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequestBuilder.java @@ -109,12 +109,11 @@ public PutIndexTemplateRequestBuilder setSettings(Map 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; } @@ -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 source) { - request.mapping(type, source); + public PutIndexTemplateRequestBuilder setMapping(XContentBuilder source) { + request.mapping(source); return this; } diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/MetaDataIndexTemplateService.java b/server/src/main/java/org/elasticsearch/cluster/metadata/MetaDataIndexTemplateService.java index 55e03a604ec55..f47dd8f7dec31 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/MetaDataIndexTemplateService.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/MetaDataIndexTemplateService.java @@ -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; @@ -285,19 +283,14 @@ private static void validateAndAddTemplate(final PutRequest request, IndexTempla templateBuilder.patterns(request.indexPatterns); templateBuilder.settings(request.settings); - Map> mappingsForValidation = new HashMap<>(); - for (Map.Entry 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 { @@ -390,7 +383,7 @@ public static class PutRequest { Integer version; List indexPatterns; Settings settings = Settings.Builder.EMPTY_SETTINGS; - Map mappings = new HashMap<>(); + String mappings = null; List aliases = new ArrayList<>(); TimeValue masterTimeout = MasterNodeRequest.DEFAULT_MASTER_NODE_TIMEOUT; @@ -420,8 +413,8 @@ public PutRequest settings(Settings settings) { return this; } - public PutRequest mappings(Map mappings) { - this.mappings.putAll(mappings); + public PutRequest mappings(String mappings) { + this.mappings = mappings; return this; } @@ -430,11 +423,6 @@ public PutRequest aliases(Set 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; diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/template/put/MetaDataIndexTemplateServiceTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/template/put/MetaDataIndexTemplateServiceTests.java index 31add95f55401..d31e87f9ca09f 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/template/put/MetaDataIndexTemplateServiceTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/template/put/MetaDataIndexTemplateServiceTests.java @@ -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())); @@ -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 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 aliases = new HashSet<>(); aliases.add(new Alias("invalid_alias").filter("abcde")); request.aliases(aliases); diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequestTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequestTests.java index 6dd3dcb1f14f5..4f68777f9bf0d 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequestTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequestTests.java @@ -34,7 +34,6 @@ import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.hasKey; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.core.Is.is; @@ -64,56 +63,56 @@ public void testMappingKeyedByType() throws IOException { XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values())); builder.startObject().startObject("properties") .startObject("field1") - .field("type", "text") + .field("type", "text") .endObject() .startObject("field2") - .startObject("properties") - .startObject("field21") - .field("type", "keyword") - .endObject() - .endObject() + .startObject("properties") + .startObject("field21") + .field("type", "keyword") .endObject() - .endObject().endObject(); - request1.mapping("type1", builder); + .endObject() + .endObject() + .endObject().endObject(); + request1.mapping(builder); builder = XContentFactory.contentBuilder(randomFrom(XContentType.values())); - builder.startObject().startObject("type1") + builder.startObject().startObject("_doc") .startObject("properties") - .startObject("field1") - .field("type", "text") - .endObject() - .startObject("field2") - .startObject("properties") - .startObject("field21") - .field("type", "keyword") - .endObject() - .endObject() - .endObject() + .startObject("field1") + .field("type", "text") + .endObject() + .startObject("field2") + .startObject("properties") + .startObject("field21") + .field("type", "keyword") + .endObject() + .endObject() + .endObject() .endObject() - .endObject().endObject(); - request2.mapping("type1", builder); + .endObject().endObject(); + request2.mapping(builder); assertEquals(request1.mappings(), request2.mappings()); } { request1 = new PutIndexTemplateRequest("foo"); request2 = new PutIndexTemplateRequest("bar"); String nakedMapping = "{\"properties\": {\"foo\": {\"type\": \"integer\"}}}"; - request1.mapping("type2", nakedMapping, XContentType.JSON); - request2.mapping("type2", "{\"type2\": " + nakedMapping + "}", XContentType.JSON); + request1.mapping(nakedMapping, XContentType.JSON); + request2.mapping("{\"_doc\": " + nakedMapping + "}", XContentType.JSON); assertEquals(request1.mappings(), request2.mappings()); } { request1 = new PutIndexTemplateRequest("foo"); request2 = new PutIndexTemplateRequest("bar"); - Map nakedMapping = MapBuilder.newMapBuilder() - .put("properties", MapBuilder.newMapBuilder() - .put("bar", MapBuilder.newMapBuilder() - .put("type", "scaled_float") - .put("scaling_factor", 100) - .map()) + Map nakedMapping = MapBuilder.newMapBuilder() + .put("properties", MapBuilder.newMapBuilder() + .put("bar", MapBuilder.newMapBuilder() + .put("type", "scaled_float") + .put("scaling_factor", 100) + .map()) .map()) - .map(); - request1.mapping("type3", nakedMapping); - request2.mapping("type3", MapBuilder.newMapBuilder().put("type3", nakedMapping).map()); + .map(); + request1.mapping(nakedMapping); + request2.mapping(Map.of("_doc", nakedMapping)); assertEquals(request1.mappings(), request2.mappings()); } } @@ -154,7 +153,7 @@ public void testSourceParsing() throws IOException { .build(); assertThat(request.settings(), equalTo(settings)); - assertThat(request.mappings(), hasKey("_doc")); + assertThat(request.mappings(), containsString("field")); Alias alias = new Alias("my-alias"); assertThat(request.aliases().size(), equalTo(1)); diff --git a/server/src/test/java/org/elasticsearch/cluster/SimpleClusterStateIT.java b/server/src/test/java/org/elasticsearch/cluster/SimpleClusterStateIT.java index 01bef1d292597..fde1f1b54d4fc 100644 --- a/server/src/test/java/org/elasticsearch/cluster/SimpleClusterStateIT.java +++ b/server/src/test/java/org/elasticsearch/cluster/SimpleClusterStateIT.java @@ -140,9 +140,9 @@ public void testIndexTemplates() throws Exception { client().admin().indices().preparePutTemplate("foo_template") .setPatterns(Collections.singletonList("te*")) .setOrder(0) - .addMapping("type1", XContentFactory.jsonBuilder() + .setMapping(XContentFactory.jsonBuilder() .startObject() - .startObject("type1") + .startObject("_doc") .startObject("properties") .startObject("field1") .field("type", "text") @@ -160,9 +160,9 @@ public void testIndexTemplates() throws Exception { client().admin().indices().preparePutTemplate("fuu_template") .setPatterns(Collections.singletonList("test*")) .setOrder(1) - .addMapping("type1", XContentFactory.jsonBuilder() + .setMapping(XContentFactory.jsonBuilder() .startObject() - .startObject("type1") + .startObject("_doc") .startObject("properties") .startObject("field2") .field("type", "text") diff --git a/server/src/test/java/org/elasticsearch/gateway/RecoveryFromGatewayIT.java b/server/src/test/java/org/elasticsearch/gateway/RecoveryFromGatewayIT.java index ee55545fe34ea..4e7ff217c9c5f 100644 --- a/server/src/test/java/org/elasticsearch/gateway/RecoveryFromGatewayIT.java +++ b/server/src/test/java/org/elasticsearch/gateway/RecoveryFromGatewayIT.java @@ -383,7 +383,7 @@ public void testLatestVersionLoaded() throws Exception { client().admin().indices().preparePutTemplate("template_1") .setPatterns(Collections.singletonList("te*")) .setOrder(0) - .addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties") + .setMapping(XContentFactory.jsonBuilder().startObject().startObject("_doc").startObject("properties") .startObject("field1").field("type", "text").field("store", true).endObject() .startObject("field2").field("type", "keyword").field("store", true).endObject() .endObject().endObject().endObject()) diff --git a/server/src/test/java/org/elasticsearch/indices/template/IndexTemplateBlocksIT.java b/server/src/test/java/org/elasticsearch/indices/template/IndexTemplateBlocksIT.java index 3e38a6df187f8..10154ea5c12b5 100644 --- a/server/src/test/java/org/elasticsearch/indices/template/IndexTemplateBlocksIT.java +++ b/server/src/test/java/org/elasticsearch/indices/template/IndexTemplateBlocksIT.java @@ -38,7 +38,7 @@ public void testIndexTemplatesWithBlocks() throws IOException { client().admin().indices().preparePutTemplate("template_blocks") .setPatterns(Collections.singletonList("te*")) .setOrder(0) - .addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties") + .setMapping(XContentFactory.jsonBuilder().startObject().startObject("_doc").startObject("properties") .startObject("field1").field("type", "text").field("store", true).endObject() .startObject("field2").field("type", "keyword").field("store", true).endObject() .endObject().endObject().endObject()) diff --git a/server/src/test/java/org/elasticsearch/indices/template/SimpleIndexTemplateIT.java b/server/src/test/java/org/elasticsearch/indices/template/SimpleIndexTemplateIT.java index 8367791d4748d..38bd2e0199306 100644 --- a/server/src/test/java/org/elasticsearch/indices/template/SimpleIndexTemplateIT.java +++ b/server/src/test/java/org/elasticsearch/indices/template/SimpleIndexTemplateIT.java @@ -93,7 +93,7 @@ public void testSimpleIndexTemplateTests() throws Exception { .setPatterns(Collections.singletonList("te*")) .setSettings(indexSettings()) .setOrder(0) - .addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties") + .setMapping(XContentFactory.jsonBuilder().startObject().startObject("_doc").startObject("properties") .startObject("field1").field("type", "text").field("store", true).endObject() .startObject("field2").field("type", "keyword").field("store", true).endObject() .endObject().endObject().endObject()) @@ -103,7 +103,7 @@ public void testSimpleIndexTemplateTests() throws Exception { .setPatterns(Collections.singletonList("test*")) .setSettings(indexSettings()) .setOrder(1) - .addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties") + .setMapping(XContentFactory.jsonBuilder().startObject().startObject("_doc").startObject("properties") .startObject("field2").field("type", "text").field("store", false).endObject() .endObject().endObject().endObject()) .get(); @@ -114,7 +114,7 @@ public void testSimpleIndexTemplateTests() throws Exception { .setSettings(indexSettings()) .setCreate(true) .setOrder(1) - .addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties") + .setMapping(XContentFactory.jsonBuilder().startObject().startObject("_doc").startObject("properties") .startObject("field2").field("type", "text").field("store", false).endObject() .endObject().endObject().endObject()) , IllegalArgumentException.class @@ -164,9 +164,9 @@ public void testDeleteIndexTemplate() throws Exception { client().admin().indices().preparePutTemplate("template_1") .setPatterns(Collections.singletonList("te*")) .setOrder(0) - .addMapping("type1", XContentFactory.jsonBuilder() + .setMapping(XContentFactory.jsonBuilder() .startObject() - .startObject("type1") + .startObject("_doc") .startObject("properties") .startObject("field1") .field("type", "text") @@ -184,9 +184,9 @@ public void testDeleteIndexTemplate() throws Exception { client().admin().indices().preparePutTemplate("template_2") .setPatterns(Collections.singletonList("test*")) .setOrder(1) - .addMapping("type1", XContentFactory.jsonBuilder() + .setMapping(XContentFactory.jsonBuilder() .startObject() - .startObject("type1") + .startObject("_doc") .startObject("properties") .startObject("field2") .field("type", "text") @@ -211,7 +211,7 @@ public void testDeleteIndexTemplate() throws Exception { client().admin().indices().preparePutTemplate("template_1") .setPatterns(Collections.singletonList("te*")) .setOrder(0) - .addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties") + .setMapping(XContentFactory.jsonBuilder().startObject().startObject("_doc").startObject("properties") .startObject("field1").field("type", "text").field("store", true).endObject() .startObject("field2").field("type", "keyword").field("store", true).endObject() .endObject().endObject().endObject()) @@ -234,7 +234,7 @@ public void testThatGetIndexTemplatesWorks() throws Exception { .setPatterns(Collections.singletonList("te*")) .setOrder(0) .setVersion(123) - .addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties") + .setMapping(XContentFactory.jsonBuilder().startObject().startObject("_doc").startObject("properties") .startObject("field1").field("type", "text").field("store", true).endObject() .startObject("field2").field("type", "keyword").field("store", true).endObject() .endObject().endObject().endObject()) @@ -259,7 +259,7 @@ public void testThatGetIndexTemplatesWithSimpleRegexWorks() throws Exception { client().admin().indices().preparePutTemplate("template_1") .setPatterns(Collections.singletonList("te*")) .setOrder(0) - .addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties") + .setMapping(XContentFactory.jsonBuilder().startObject().startObject("_doc").startObject("properties") .startObject("field1").field("type", "text").field("store", true).endObject() .startObject("field2").field("type", "keyword").field("store", true).endObject() .endObject().endObject().endObject()) @@ -269,7 +269,7 @@ public void testThatGetIndexTemplatesWithSimpleRegexWorks() throws Exception { client().admin().indices().preparePutTemplate("template_2") .setPatterns(Collections.singletonList("te*")) .setOrder(0) - .addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties") + .setMapping(XContentFactory.jsonBuilder().startObject().startObject("_doc").startObject("properties") .startObject("field1").field("type", "text").field("store", true).endObject() .startObject("field2").field("type", "keyword").field("store", true).endObject() .endObject().endObject().endObject()) @@ -279,7 +279,7 @@ public void testThatGetIndexTemplatesWithSimpleRegexWorks() throws Exception { client().admin().indices().preparePutTemplate("template3") .setPatterns(Collections.singletonList("te*")) .setOrder(0) - .addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties") + .setMapping(XContentFactory.jsonBuilder().startObject().startObject("_doc").startObject("properties") .startObject("field1").field("type", "text").field("store", true).endObject() .startObject("field2").field("type", "keyword").field("store", true).endObject() .endObject().endObject().endObject()) @@ -345,7 +345,7 @@ public void testBrokenMapping() throws Exception { MapperParsingException e = expectThrows( MapperParsingException.class, () -> client().admin().indices().preparePutTemplate("template_1") .setPatterns(Collections.singletonList("te*")) - .addMapping("type1", "{\"foo\": \"abcde\"}", XContentType.JSON) + .setMapping("{\"foo\": \"abcde\"}", XContentType.JSON) .get()); assertThat(e.getMessage(), containsString("Failed to parse mapping")); @@ -687,7 +687,7 @@ public void testCombineTemplates() throws Exception{ .setPatterns(Collections.singletonList("test*")) .setCreate(true) .setOrder(1) - .addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties") + .setMapping(XContentFactory.jsonBuilder().startObject().startObject("_doc").startObject("properties") .startObject("field2").field("type", "text").field("analyzer", "custom_1").endObject() .endObject().endObject().endObject()) .get()); @@ -719,7 +719,7 @@ public void testMultipleTemplate() throws IOException { client().admin().indices().preparePutTemplate("template_1") .setPatterns(Arrays.asList("a*", "b*")) .setSettings(indexSettings()) - .addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties") + .setMapping(XContentFactory.jsonBuilder().startObject().startObject("_doc").startObject("properties") .startObject("field1").field("type", "text").field("store", true).endObject() .startObject("field2").field("type", "keyword").field("store", false).endObject() .endObject().endObject().endObject()) @@ -781,7 +781,7 @@ public void testPartitionedTemplate() throws Exception { IllegalArgumentException eBadMapping = expectThrows(IllegalArgumentException.class, () -> client().admin().indices().preparePutTemplate("template_2") .setPatterns(Collections.singletonList("te*")) - .addMapping("type", "{\"type\":{\"_routing\":{\"required\":false}}}", XContentType.JSON) + .setMapping("{\"_doc\":{\"_routing\":{\"required\":false}}}", XContentType.JSON) .setSettings(Settings.builder() .put("index.number_of_shards", "6") .put("index.routing_partition_size", "3")) diff --git a/server/src/test/java/org/elasticsearch/snapshots/SharedClusterSnapshotRestoreIT.java b/server/src/test/java/org/elasticsearch/snapshots/SharedClusterSnapshotRestoreIT.java index b54359e516d00..d5f6c98711cf4 100644 --- a/server/src/test/java/org/elasticsearch/snapshots/SharedClusterSnapshotRestoreIT.java +++ b/server/src/test/java/org/elasticsearch/snapshots/SharedClusterSnapshotRestoreIT.java @@ -553,9 +553,9 @@ public void testRestoreTemplates() throws Exception { assertThat(client.admin().indices() .preparePutTemplate("test-template") .setPatterns(Collections.singletonList("te*")) - .addMapping("test-mapping", XContentFactory.jsonBuilder() + .setMapping(XContentFactory.jsonBuilder() .startObject() - .startObject("test-mapping") + .startObject("_doc") .startObject("properties") .startObject("field1") .field("type", "text") @@ -611,7 +611,7 @@ public void testIncludeGlobalState() throws Exception { assertThat(client.admin().indices() .preparePutTemplate("test-template") .setPatterns(Collections.singletonList("te*")) - .addMapping("_doc", XContentFactory.jsonBuilder() + .setMapping(XContentFactory.jsonBuilder() .startObject() .startObject("_doc") .startObject("properties") diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/SecurityIndexManager.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/SecurityIndexManager.java index 635294ff7e4d9..b5305abd826c3 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/SecurityIndexManager.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/SecurityIndexManager.java @@ -47,7 +47,6 @@ import org.elasticsearch.gateway.GatewayService; import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexNotFoundException; -import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.indices.IndexClosedException; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.xpack.core.security.index.RestrictedIndicesNames; @@ -429,7 +428,7 @@ private static byte[] readTemplateAsBytes(String templateName) { private static Tuple parseMappingAndSettingsFromTemplateBytes(byte[] template) throws IOException { final PutIndexTemplateRequest request = new PutIndexTemplateRequest("name_is_not_important").source(template, XContentType.JSON); - final String mappingSource = request.mappings().get(MapperService.SINGLE_MAPPING_NAME); + final String mappingSource = request.mappings(); try (XContentParser parser = XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, mappingSource)) { ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation); // { diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/support/SecurityIndexManagerTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/support/SecurityIndexManagerTests.java index b785f24f7c6a7..b6ac14041bb9b 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/support/SecurityIndexManagerTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/support/SecurityIndexManagerTests.java @@ -38,6 +38,7 @@ import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.gateway.GatewayService; import org.elasticsearch.index.Index; +import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.test.ESTestCase; @@ -406,8 +407,7 @@ private static ClusterState state() { } private static IndexMetaData.Builder getIndexMetadata(String indexName, String aliasName, String templateName, int format, - IndexMetaData.State state) - throws IOException { + IndexMetaData.State state) { IndexMetaData.Builder indexMetaData = IndexMetaData.builder(indexName); indexMetaData.settings(Settings.builder() .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) @@ -417,25 +417,25 @@ private static IndexMetaData.Builder getIndexMetadata(String indexName, String a .build()); indexMetaData.putAlias(AliasMetaData.builder(aliasName).build()); indexMetaData.state(state); - final Map mappings = getTemplateMappings(templateName); - for (Map.Entry entry : mappings.entrySet()) { - indexMetaData.putMapping(entry.getValue()); + final String mappings = getTemplateMappings(templateName); + if (mappings != null) { + indexMetaData.putMapping(mappings); } return indexMetaData; } private static IndexTemplateMetaData.Builder getIndexTemplateMetaData(String templateName) throws IOException { - final Map mappings = getTemplateMappings(templateName); + final String mappings = getTemplateMappings(templateName); IndexTemplateMetaData.Builder templateBuilder = IndexTemplateMetaData.builder(TEMPLATE_NAME) .patterns(Arrays.asList(generateRandomStringArray(10, 100, false, false))); - for (Map.Entry entry : mappings.entrySet()) { - templateBuilder.putMapping(entry.getKey(), entry.getValue()); + if (mappings != null) { + templateBuilder.putMapping(MapperService.SINGLE_MAPPING_NAME, mappings); } return templateBuilder; } - private static Map getTemplateMappings(String templateName) { + private static String getTemplateMappings(String templateName) { String template = loadTemplate(templateName); PutIndexTemplateRequest request = new PutIndexTemplateRequest(); request.source(template, XContentType.JSON); @@ -534,8 +534,8 @@ private static IndexMetaData.Builder createIndexMetadata(String indexName, Strin .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1) .build()); - for (Map.Entry entry : request.mappings().entrySet()) { - indexMetaData.putMapping(entry.getValue()); + if (request.mappings() != null) { + indexMetaData.putMapping(request.mappings()); } return indexMetaData; } @@ -568,8 +568,8 @@ private static IndexTemplateMetaData.Builder getIndexTemplateMetaData(String tem request.source(template, XContentType.JSON); IndexTemplateMetaData.Builder templateBuilder = IndexTemplateMetaData.builder(templateName) .patterns(Arrays.asList(generateRandomStringArray(10, 100, false, false))); - for (Map.Entry entry : request.mappings().entrySet()) { - templateBuilder.putMapping(entry.getKey(), entry.getValue()); + if (request.mappings() != null) { + templateBuilder.putMapping(MapperService.SINGLE_MAPPING_NAME, request.mappings()); } return templateBuilder; } diff --git a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/persistence/TransformInternalIndex.java b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/persistence/TransformInternalIndex.java index 2951ade8f47fb..df205e8e7a928 100644 --- a/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/persistence/TransformInternalIndex.java +++ b/x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/persistence/TransformInternalIndex.java @@ -367,7 +367,7 @@ protected static void installLatestVersionedIndexTemplateIfRequired( .patterns(indexTemplateMetaData.patterns()) .version(indexTemplateMetaData.version()) .settings(indexTemplateMetaData.settings()) - .mapping(SINGLE_MAPPING_NAME, XContentHelper.convertToMap(jsonMappings, true, XContentType.JSON).v2()); + .mapping(XContentHelper.convertToMap(jsonMappings, true, XContentType.JSON).v2()); ActionListener innerListener = ActionListener.wrap(r -> listener.onResponse(null), listener::onFailure); executeAsyncWithOrigin( client.threadPool().getThreadContext(), @@ -402,7 +402,7 @@ protected static void installLatestAuditIndexTemplateIfRequired( ) .version(indexTemplateMetaData.version()) .settings(indexTemplateMetaData.settings()) - .mapping(SINGLE_MAPPING_NAME, XContentHelper.convertToMap(jsonMappings, true, XContentType.JSON).v2()); + .mapping(XContentHelper.convertToMap(jsonMappings, true, XContentType.JSON).v2()); ActionListener innerListener = ActionListener.wrap(r -> listener.onResponse(null), listener::onFailure); executeAsyncWithOrigin( client.threadPool().getThreadContext(),