From 59d1e6912951884df56da24bf06bd4cc33b12476 Mon Sep 17 00:00:00 2001 From: Suraj Singh <79435743+dreamer-89@users.noreply.github.com> Date: Mon, 21 Mar 2022 16:49:18 -0700 Subject: [PATCH] [Remove] Types from PutIndexTemplateRequest and builder to reduce mapping to a string (#2510) Signed-off-by: Suraj Singh --- .../cluster/SimpleClusterStateIT.java | 10 +-- .../gateway/RecoveryFromGatewayIT.java | 5 +- .../template/IndexTemplateBlocksIT.java | 5 +- .../template/SimpleIndexTemplateIT.java | 64 +++----------- .../snapshots/RestoreSnapshotIT.java | 5 +- .../SnapshotCustomPluginStateIT.java | 5 +- .../template/put/PutIndexTemplateRequest.java | 86 ++++++++++--------- .../put/PutIndexTemplateRequestBuilder.java | 21 +---- .../MetadataIndexTemplateService.java | 17 ++-- .../put/PutIndexTemplateRequestTests.java | 16 ++-- .../MetadataIndexTemplateServiceTests.java | 11 +-- 11 files changed, 85 insertions(+), 160 deletions(-) diff --git a/server/src/internalClusterTest/java/org/opensearch/cluster/SimpleClusterStateIT.java b/server/src/internalClusterTest/java/org/opensearch/cluster/SimpleClusterStateIT.java index fcf5dcf3891ce..19f7b0b4c630c 100644 --- a/server/src/internalClusterTest/java/org/opensearch/cluster/SimpleClusterStateIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/cluster/SimpleClusterStateIT.java @@ -166,11 +166,9 @@ public void testIndexTemplates() throws Exception { .preparePutTemplate("foo_template") .setPatterns(Collections.singletonList("te*")) .setOrder(0) - .addMapping( - "type1", + .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("type1") .startObject("properties") .startObject("field1") .field("type", "text") @@ -182,7 +180,6 @@ public void testIndexTemplates() throws Exception { .endObject() .endObject() .endObject() - .endObject() ) .get(); @@ -191,11 +188,9 @@ public void testIndexTemplates() throws Exception { .preparePutTemplate("fuu_template") .setPatterns(Collections.singletonList("test*")) .setOrder(1) - .addMapping( - "type1", + .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("type1") .startObject("properties") .startObject("field2") .field("type", "text") @@ -203,7 +198,6 @@ public void testIndexTemplates() throws Exception { .endObject() .endObject() .endObject() - .endObject() ) .get(); diff --git a/server/src/internalClusterTest/java/org/opensearch/gateway/RecoveryFromGatewayIT.java b/server/src/internalClusterTest/java/org/opensearch/gateway/RecoveryFromGatewayIT.java index d76e21ea712f3..92d8dc6a0bb5e 100644 --- a/server/src/internalClusterTest/java/org/opensearch/gateway/RecoveryFromGatewayIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/gateway/RecoveryFromGatewayIT.java @@ -461,11 +461,9 @@ public void testLatestVersionLoaded() throws Exception { .preparePutTemplate("template_1") .setPatterns(Collections.singletonList("te*")) .setOrder(0) - .addMapping( - "type1", + .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("type1") .startObject("properties") .startObject("field1") .field("type", "text") @@ -477,7 +475,6 @@ public void testLatestVersionLoaded() throws Exception { .endObject() .endObject() .endObject() - .endObject() ) .execute() .actionGet(); diff --git a/server/src/internalClusterTest/java/org/opensearch/indices/template/IndexTemplateBlocksIT.java b/server/src/internalClusterTest/java/org/opensearch/indices/template/IndexTemplateBlocksIT.java index 721e05a867137..21e3e58d4d091 100644 --- a/server/src/internalClusterTest/java/org/opensearch/indices/template/IndexTemplateBlocksIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/indices/template/IndexTemplateBlocksIT.java @@ -54,11 +54,9 @@ public void testIndexTemplatesWithBlocks() throws IOException { .preparePutTemplate("template_blocks") .setPatterns(Collections.singletonList("te*")) .setOrder(0) - .addMapping( - "type1", + .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("type1") .startObject("properties") .startObject("field1") .field("type", "text") @@ -70,7 +68,6 @@ public void testIndexTemplatesWithBlocks() throws IOException { .endObject() .endObject() .endObject() - .endObject() ) .execute() .actionGet(); diff --git a/server/src/internalClusterTest/java/org/opensearch/indices/template/SimpleIndexTemplateIT.java b/server/src/internalClusterTest/java/org/opensearch/indices/template/SimpleIndexTemplateIT.java index 090cf81de60a3..0e15a0c895432 100644 --- a/server/src/internalClusterTest/java/org/opensearch/indices/template/SimpleIndexTemplateIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/indices/template/SimpleIndexTemplateIT.java @@ -110,11 +110,9 @@ public void testSimpleIndexTemplateTests() throws Exception { .setPatterns(Collections.singletonList("te*")) .setSettings(indexSettings()) .setOrder(0) - .addMapping( - "type1", + .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("type1") .startObject("properties") .startObject("field1") .field("type", "text") @@ -126,7 +124,6 @@ public void testSimpleIndexTemplateTests() throws Exception { .endObject() .endObject() .endObject() - .endObject() ) .get(); @@ -136,11 +133,9 @@ public void testSimpleIndexTemplateTests() throws Exception { .setPatterns(Collections.singletonList("test*")) .setSettings(indexSettings()) .setOrder(1) - .addMapping( - "type1", + .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("type1") .startObject("properties") .startObject("field2") .field("type", "text") @@ -148,7 +143,6 @@ public void testSimpleIndexTemplateTests() throws Exception { .endObject() .endObject() .endObject() - .endObject() ) .get(); @@ -161,11 +155,9 @@ public void testSimpleIndexTemplateTests() throws Exception { .setSettings(indexSettings()) .setCreate(true) .setOrder(1) - .addMapping( - "type1", + .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("type1") .startObject("properties") .startObject("field2") .field("type", "text") @@ -173,7 +165,6 @@ public void testSimpleIndexTemplateTests() throws Exception { .endObject() .endObject() .endObject() - .endObject() ), IllegalArgumentException.class ); @@ -223,11 +214,9 @@ public void testDeleteIndexTemplate() throws Exception { .preparePutTemplate("template_1") .setPatterns(Collections.singletonList("te*")) .setOrder(0) - .addMapping( - "type1", + .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("type1") .startObject("properties") .startObject("field1") .field("type", "text") @@ -239,7 +228,6 @@ public void testDeleteIndexTemplate() throws Exception { .endObject() .endObject() .endObject() - .endObject() ) .execute() .actionGet(); @@ -249,11 +237,9 @@ public void testDeleteIndexTemplate() throws Exception { .preparePutTemplate("template_2") .setPatterns(Collections.singletonList("test*")) .setOrder(1) - .addMapping( - "type1", + .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("type1") .startObject("properties") .startObject("field2") .field("type", "text") @@ -261,7 +247,6 @@ public void testDeleteIndexTemplate() throws Exception { .endObject() .endObject() .endObject() - .endObject() ) .execute() .actionGet(); @@ -281,11 +266,9 @@ public void testDeleteIndexTemplate() throws Exception { .preparePutTemplate("template_1") .setPatterns(Collections.singletonList("te*")) .setOrder(0) - .addMapping( - "type1", + .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("type1") .startObject("properties") .startObject("field1") .field("type", "text") @@ -297,7 +280,6 @@ public void testDeleteIndexTemplate() throws Exception { .endObject() .endObject() .endObject() - .endObject() ) .execute() .actionGet(); @@ -322,11 +304,9 @@ public void testThatGetIndexTemplatesWorks() throws Exception { .setPatterns(Collections.singletonList("te*")) .setOrder(0) .setVersion(123) - .addMapping( - "type1", + .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("type1") .startObject("properties") .startObject("field1") .field("type", "text") @@ -338,7 +318,6 @@ public void testThatGetIndexTemplatesWorks() throws Exception { .endObject() .endObject() .endObject() - .endObject() ) .execute() .actionGet(); @@ -367,11 +346,9 @@ public void testThatGetIndexTemplatesWithSimpleRegexWorks() throws Exception { .preparePutTemplate("template_1") .setPatterns(Collections.singletonList("te*")) .setOrder(0) - .addMapping( - "type1", + .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("type1") .startObject("properties") .startObject("field1") .field("type", "text") @@ -383,7 +360,6 @@ public void testThatGetIndexTemplatesWithSimpleRegexWorks() throws Exception { .endObject() .endObject() .endObject() - .endObject() ) .execute() .actionGet(); @@ -394,11 +370,9 @@ public void testThatGetIndexTemplatesWithSimpleRegexWorks() throws Exception { .preparePutTemplate("template_2") .setPatterns(Collections.singletonList("te*")) .setOrder(0) - .addMapping( - "type1", + .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("type1") .startObject("properties") .startObject("field1") .field("type", "text") @@ -410,7 +384,6 @@ public void testThatGetIndexTemplatesWithSimpleRegexWorks() throws Exception { .endObject() .endObject() .endObject() - .endObject() ) .execute() .actionGet(); @@ -421,11 +394,9 @@ public void testThatGetIndexTemplatesWithSimpleRegexWorks() throws Exception { .preparePutTemplate("template3") .setPatterns(Collections.singletonList("te*")) .setOrder(0) - .addMapping( - "type1", + .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("type1") .startObject("properties") .startObject("field1") .field("type", "text") @@ -437,7 +408,6 @@ public void testThatGetIndexTemplatesWithSimpleRegexWorks() throws Exception { .endObject() .endObject() .endObject() - .endObject() ) .execute() .actionGet(); @@ -507,7 +477,7 @@ public void testBrokenMapping() throws Exception { .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 ")); @@ -896,11 +866,9 @@ public void testCombineTemplates() throws Exception { .setPatterns(Collections.singletonList("test*")) .setCreate(true) .setOrder(1) - .addMapping( - "type1", + .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("type1") .startObject("properties") .startObject("field2") .field("type", "text") @@ -908,7 +876,6 @@ public void testCombineTemplates() throws Exception { .endObject() .endObject() .endObject() - .endObject() ) .get() ); @@ -946,11 +913,9 @@ public void testMultipleTemplate() throws IOException { .preparePutTemplate("template_1") .setPatterns(Arrays.asList("a*", "b*")) .setSettings(indexSettings()) - .addMapping( - "type1", + .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("type1") .startObject("properties") .startObject("field1") .field("type", "text") @@ -962,7 +927,6 @@ public void testMultipleTemplate() throws IOException { .endObject() .endObject() .endObject() - .endObject() ) .get(); @@ -1027,7 +991,7 @@ public void testPartitionedTemplate() throws Exception { .indices() .preparePutTemplate("template_2") .setPatterns(Collections.singletonList("te*")) - .addMapping("type", "{\"type\":{\"_routing\":{\"required\":false}}}", XContentType.JSON) + .setMapping("{\"_routing\":{\"required\":false}}", XContentType.JSON) .setSettings(Settings.builder().put("index.number_of_shards", "6").put("index.routing_partition_size", "3")) .get() ); diff --git a/server/src/internalClusterTest/java/org/opensearch/snapshots/RestoreSnapshotIT.java b/server/src/internalClusterTest/java/org/opensearch/snapshots/RestoreSnapshotIT.java index 643a301c025c3..3a7fdd4093657 100644 --- a/server/src/internalClusterTest/java/org/opensearch/snapshots/RestoreSnapshotIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/snapshots/RestoreSnapshotIT.java @@ -424,11 +424,9 @@ public void testRestoreTemplates() throws Exception { .indices() .preparePutTemplate("test-template") .setPatterns(Collections.singletonList("te*")) - .addMapping( - "_doc", + .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("_doc") .startObject("properties") .startObject("field1") .field("type", "text") @@ -440,7 +438,6 @@ public void testRestoreTemplates() throws Exception { .endObject() .endObject() .endObject() - .endObject() ) .get() .isAcknowledged(), diff --git a/server/src/internalClusterTest/java/org/opensearch/snapshots/SnapshotCustomPluginStateIT.java b/server/src/internalClusterTest/java/org/opensearch/snapshots/SnapshotCustomPluginStateIT.java index 90ec4940a95c5..ea1635b1d8053 100644 --- a/server/src/internalClusterTest/java/org/opensearch/snapshots/SnapshotCustomPluginStateIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/snapshots/SnapshotCustomPluginStateIT.java @@ -81,11 +81,9 @@ public void testIncludeGlobalState() throws Exception { .indices() .preparePutTemplate("test-template") .setPatterns(Collections.singletonList("te*")) - .addMapping( - "_doc", + .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("_doc") .startObject("properties") .startObject("field1") .field("type", "text") @@ -97,7 +95,6 @@ public void testIncludeGlobalState() throws Exception { .endObject() .endObject() .endObject() - .endObject() ) .get() .isAcknowledged(), diff --git a/server/src/main/java/org/opensearch/action/admin/indices/template/put/PutIndexTemplateRequest.java b/server/src/main/java/org/opensearch/action/admin/indices/template/put/PutIndexTemplateRequest.java index d443d14f3f463..608e3da699318 100644 --- a/server/src/main/java/org/opensearch/action/admin/indices/template/put/PutIndexTemplateRequest.java +++ b/server/src/main/java/org/opensearch/action/admin/indices/template/put/PutIndexTemplateRequest.java @@ -33,16 +33,17 @@ import org.opensearch.OpenSearchGenerationException; import org.opensearch.OpenSearchParseException; +import org.opensearch.Version; import org.opensearch.action.ActionRequestValidationException; import org.opensearch.action.IndicesRequest; import org.opensearch.action.admin.indices.alias.Alias; import org.opensearch.action.admin.indices.mapping.put.PutMappingRequest; import org.opensearch.action.support.IndicesOptions; import org.opensearch.action.support.master.MasterNodeRequest; +import org.opensearch.common.Nullable; import org.opensearch.common.Strings; import org.opensearch.common.bytes.BytesArray; import org.opensearch.common.bytes.BytesReference; -import org.opensearch.common.collect.MapBuilder; import org.opensearch.common.io.stream.StreamInput; import org.opensearch.common.io.stream.StreamOutput; import org.opensearch.common.logging.DeprecationLogger; @@ -62,7 +63,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; @@ -94,7 +94,8 @@ public class PutIndexTemplateRequest extends MasterNodeRequest mappings = new HashMap<>(); + @Nullable + private String mappings; private final Set aliases = new HashSet<>(); @@ -108,11 +109,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_2_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++) { @@ -233,17 +237,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. */ @@ -259,41 +252,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 OpenSearchGenerationException("Failed to generate [" + source + "]", e); @@ -305,11 +304,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; } @@ -372,7 +371,7 @@ public PutIndexTemplateRequest source(Map templateSource) { + "], 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()); @@ -489,10 +488,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_2_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) { @@ -516,20 +519,19 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.endObject(); builder.startObject("mappings"); - for (Map.Entry entry : mappings.entrySet()) { - builder.field(entry.getKey()); + if (mappings != null) { + builder.field(MapperService.SINGLE_MAPPING_NAME); try ( XContentParser parser = JsonXContent.jsonXContent.createParser( NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - entry.getValue() + mappings ) ) { builder.copyCurrentStructure(parser); } } builder.endObject(); - builder.startObject("aliases"); for (Alias alias : aliases) { alias.toXContent(builder, params); diff --git a/server/src/main/java/org/opensearch/action/admin/indices/template/put/PutIndexTemplateRequestBuilder.java b/server/src/main/java/org/opensearch/action/admin/indices/template/put/PutIndexTemplateRequestBuilder.java index df3b5f6417576..e5a02acb4a6e9 100644 --- a/server/src/main/java/org/opensearch/action/admin/indices/template/put/PutIndexTemplateRequestBuilder.java +++ b/server/src/main/java/org/opensearch/action/admin/indices/template/put/PutIndexTemplateRequestBuilder.java @@ -124,12 +124,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; } @@ -196,22 +195,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/opensearch/cluster/metadata/MetadataIndexTemplateService.java b/server/src/main/java/org/opensearch/cluster/metadata/MetadataIndexTemplateService.java index 22cd5c1dbbbe2..896679206aec5 100644 --- a/server/src/main/java/org/opensearch/cluster/metadata/MetadataIndexTemplateService.java +++ b/server/src/main/java/org/opensearch/cluster/metadata/MetadataIndexTemplateService.java @@ -915,11 +915,11 @@ static ClusterState innerPutTemplate( templateBuilder.patterns(request.indexPatterns); templateBuilder.settings(request.settings); - 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); } } @@ -1511,7 +1511,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; @@ -1541,8 +1541,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; } @@ -1551,11 +1551,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/opensearch/action/admin/indices/template/put/PutIndexTemplateRequestTests.java b/server/src/test/java/org/opensearch/action/admin/indices/template/put/PutIndexTemplateRequestTests.java index db174aeca9476..f4cdb778a3169 100644 --- a/server/src/test/java/org/opensearch/action/admin/indices/template/put/PutIndexTemplateRequestTests.java +++ b/server/src/test/java/org/opensearch/action/admin/indices/template/put/PutIndexTemplateRequestTests.java @@ -39,6 +39,7 @@ import org.opensearch.common.xcontent.XContentFactory; import org.opensearch.common.xcontent.XContentParser; import org.opensearch.common.xcontent.XContentType; +import org.opensearch.index.mapper.MapperService; import org.opensearch.test.AbstractXContentTestCase; import java.io.IOException; @@ -90,10 +91,9 @@ public void testMappingKeyedByType() throws IOException { .endObject() .endObject() .endObject(); - request1.mapping("type1", builder); + request1.mapping(builder); builder = XContentFactory.contentBuilder(randomFrom(XContentType.values())); builder.startObject() - .startObject("type1") .startObject("properties") .startObject("field1") .field("type", "text") @@ -106,17 +106,16 @@ public void testMappingKeyedByType() throws IOException { .endObject() .endObject() .endObject() - .endObject() .endObject(); - request2.mapping("type1", builder); + 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(nakedMapping, XContentType.JSON); assertEquals(request1.mappings(), request2.mappings()); } { @@ -130,8 +129,8 @@ public void testMappingKeyedByType() throws IOException { .map() ) .map(); - request1.mapping("type3", nakedMapping); - request2.mapping("type3", MapBuilder.newMapBuilder().put("type3", nakedMapping).map()); + request1.mapping(nakedMapping); + request2.mapping(MapBuilder.newMapBuilder().put(MapperService.SINGLE_MAPPING_NAME, nakedMapping).map()); assertEquals(request1.mappings(), request2.mappings()); } } @@ -163,7 +162,6 @@ protected PutIndexTemplateRequest createTestInstance() { if (randomBoolean()) { try { request.mapping( - "doc", XContentFactory.jsonBuilder() .startObject() .startObject("doc") diff --git a/server/src/test/java/org/opensearch/cluster/metadata/MetadataIndexTemplateServiceTests.java b/server/src/test/java/org/opensearch/cluster/metadata/MetadataIndexTemplateServiceTests.java index 19e73422c5362..c3a16a1e25bc8 100644 --- a/server/src/test/java/org/opensearch/cluster/metadata/MetadataIndexTemplateServiceTests.java +++ b/server/src/test/java/org/opensearch/cluster/metadata/MetadataIndexTemplateServiceTests.java @@ -185,12 +185,10 @@ public void testIndexTemplateWithAliasNameEqualToTemplatePattern() { public void testIndexTemplateWithValidateMapping() throws Exception { PutRequest request = new PutRequest("api", "validate_template"); request.patterns(singletonList("te*")); - request.putMapping( - "type1", + request.mappings( Strings.toString( XContentFactory.jsonBuilder() .startObject() - .startObject("type1") .startObject("properties") .startObject("field2") .field("type", "text") @@ -198,7 +196,6 @@ public void testIndexTemplateWithValidateMapping() throws Exception { .endObject() .endObject() .endObject() - .endObject() ) ); @@ -211,7 +208,7 @@ 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)); @@ -223,7 +220,7 @@ 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); @@ -2073,7 +2070,7 @@ public void testLegacyNoopUpdate() { pr.settings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 3).build()); } if (randomBoolean()) { - pr.mappings(Collections.emptyMap()); + pr.mappings("{}"); } if (randomBoolean()) { pr.aliases(Collections.singleton(new Alias("alias")));