From 6f0774ea06e4eea5d71a8033286762492531851f Mon Sep 17 00:00:00 2001 From: Suraj Singh Date: Thu, 17 Mar 2022 09:08:43 -0700 Subject: [PATCH 1/3] [Remove] PutIndexTemplateRequest and builder contains a single mapping Signed-off-by: Suraj Singh --- .../cluster/SimpleClusterStateIT.java | 10 +- .../gateway/RecoveryFromGatewayIT.java | 5 +- .../template/IndexTemplateBlocksIT.java | 5 +- .../template/SimpleIndexTemplateIT.java | 62 +++++------- .../snapshots/RestoreSnapshotIT.java | 5 +- .../SnapshotCustomPluginStateIT.java | 5 +- .../template/put/PutIndexTemplateRequest.java | 98 +++++++++---------- .../put/PutIndexTemplateRequestBuilder.java | 21 +--- .../MetadataIndexTemplateService.java | 17 ++-- .../put/PutIndexTemplateRequestTests.java | 13 ++- .../MetadataIndexTemplateServiceTests.java | 11 +-- 11 files changed, 101 insertions(+), 151 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..62f6a93118a2a 100644 --- a/server/src/internalClusterTest/java/org/opensearch/cluster/SimpleClusterStateIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/cluster/SimpleClusterStateIT.java @@ -166,11 +166,10 @@ public void testIndexTemplates() throws Exception { .preparePutTemplate("foo_template") .setPatterns(Collections.singletonList("te*")) .setOrder(0) - .addMapping( - "type1", + .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("type1") + .startObject("_doc") .startObject("properties") .startObject("field1") .field("type", "text") @@ -191,11 +190,10 @@ public void testIndexTemplates() throws Exception { .preparePutTemplate("fuu_template") .setPatterns(Collections.singletonList("test*")) .setOrder(1) - .addMapping( - "type1", + .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("type1") + .startObject("_doc") .startObject("properties") .startObject("field2") .field("type", "text") diff --git a/server/src/internalClusterTest/java/org/opensearch/gateway/RecoveryFromGatewayIT.java b/server/src/internalClusterTest/java/org/opensearch/gateway/RecoveryFromGatewayIT.java index 3c5f2828ff94f..38f0de6426a86 100644 --- a/server/src/internalClusterTest/java/org/opensearch/gateway/RecoveryFromGatewayIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/gateway/RecoveryFromGatewayIT.java @@ -461,11 +461,10 @@ public void testLatestVersionLoaded() throws Exception { .preparePutTemplate("template_1") .setPatterns(Collections.singletonList("te*")) .setOrder(0) - .addMapping( - "type1", + .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("type1") + .startObject("_doc") .startObject("properties") .startObject("field1") .field("type", "text") 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..dd011edf587e2 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,10 @@ public void testSimpleIndexTemplateTests() throws Exception { .setPatterns(Collections.singletonList("te*")) .setSettings(indexSettings()) .setOrder(0) - .addMapping( - "type1", + .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("type1") + .startObject("_doc") .startObject("properties") .startObject("field1") .field("type", "text") @@ -136,11 +135,10 @@ public void testSimpleIndexTemplateTests() throws Exception { .setPatterns(Collections.singletonList("test*")) .setSettings(indexSettings()) .setOrder(1) - .addMapping( - "type1", + .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("type1") + .startObject("_doc") .startObject("properties") .startObject("field2") .field("type", "text") @@ -161,11 +159,10 @@ public void testSimpleIndexTemplateTests() throws Exception { .setSettings(indexSettings()) .setCreate(true) .setOrder(1) - .addMapping( - "type1", + .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("type1") + .startObject("_doc") .startObject("properties") .startObject("field2") .field("type", "text") @@ -223,11 +220,10 @@ public void testDeleteIndexTemplate() throws Exception { .preparePutTemplate("template_1") .setPatterns(Collections.singletonList("te*")) .setOrder(0) - .addMapping( - "type1", + .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("type1") + .startObject("_doc") .startObject("properties") .startObject("field1") .field("type", "text") @@ -249,11 +245,10 @@ public void testDeleteIndexTemplate() throws Exception { .preparePutTemplate("template_2") .setPatterns(Collections.singletonList("test*")) .setOrder(1) - .addMapping( - "type1", + .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("type1") + .startObject("_doc") .startObject("properties") .startObject("field2") .field("type", "text") @@ -281,8 +276,7 @@ public void testDeleteIndexTemplate() throws Exception { .preparePutTemplate("template_1") .setPatterns(Collections.singletonList("te*")) .setOrder(0) - .addMapping( - "type1", + .setMapping( XContentFactory.jsonBuilder() .startObject() .startObject("type1") @@ -322,11 +316,10 @@ public void testThatGetIndexTemplatesWorks() throws Exception { .setPatterns(Collections.singletonList("te*")) .setOrder(0) .setVersion(123) - .addMapping( - "type1", + .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("type1") + .startObject("_doc") .startObject("properties") .startObject("field1") .field("type", "text") @@ -367,11 +360,10 @@ public void testThatGetIndexTemplatesWithSimpleRegexWorks() throws Exception { .preparePutTemplate("template_1") .setPatterns(Collections.singletonList("te*")) .setOrder(0) - .addMapping( - "type1", + .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("type1") + .startObject("_doc") .startObject("properties") .startObject("field1") .field("type", "text") @@ -394,11 +386,10 @@ public void testThatGetIndexTemplatesWithSimpleRegexWorks() throws Exception { .preparePutTemplate("template_2") .setPatterns(Collections.singletonList("te*")) .setOrder(0) - .addMapping( - "type1", + .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("type1") + .startObject("_doc") .startObject("properties") .startObject("field1") .field("type", "text") @@ -421,11 +412,10 @@ public void testThatGetIndexTemplatesWithSimpleRegexWorks() throws Exception { .preparePutTemplate("template3") .setPatterns(Collections.singletonList("te*")) .setOrder(0) - .addMapping( - "type1", + .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("type1") + .startObject("_doc") .startObject("properties") .startObject("field1") .field("type", "text") @@ -507,7 +497,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 +886,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 +896,6 @@ public void testCombineTemplates() throws Exception { .endObject() .endObject() .endObject() - .endObject() ) .get() ); @@ -946,11 +933,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 +947,6 @@ public void testMultipleTemplate() throws IOException { .endObject() .endObject() .endObject() - .endObject() ) .get(); @@ -1027,7 +1011,7 @@ public void testPartitionedTemplate() throws Exception { .indices() .preparePutTemplate("template_2") .setPatterns(Collections.singletonList("te*")) - .addMapping("type", "{\"type\":{\"_routing\":{\"required\":false}}}", XContentType.JSON) + .setMapping("{\"type\":{\"_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..cb6d4f6ef7506 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,17 +519,14 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.endObject(); builder.startObject("mappings"); - for (Map.Entry entry : mappings.entrySet()) { - builder.field(entry.getKey()); - try ( - XContentParser parser = JsonXContent.jsonXContent.createParser( - NamedXContentRegistry.EMPTY, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - entry.getValue() - ) - ) { - builder.copyCurrentStructure(parser); - } + try ( + XContentParser parser = JsonXContent.jsonXContent.createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + mappings + ) + ) { + builder.copyCurrentStructure(parser); } builder.endObject(); 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..1f47639d8c572 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 @@ -90,7 +90,7 @@ 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") @@ -108,15 +108,15 @@ public void testMappingKeyedByType() throws IOException { .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("{\"type2\": " + nakedMapping + "}", XContentType.JSON); assertEquals(request1.mappings(), request2.mappings()); } { @@ -130,8 +130,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("type3", nakedMapping).map()); assertEquals(request1.mappings(), request2.mappings()); } } @@ -163,7 +163,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..c4f850fef34d8 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"))); From 956e8ed74ce1e781022268fd45df3091c484af08 Mon Sep 17 00:00:00 2001 From: Suraj Singh Date: Fri, 18 Mar 2022 11:23:08 -0700 Subject: [PATCH 2/3] formatting via spotlessApply Signed-off-by: Suraj Singh --- .../cluster/metadata/MetadataIndexTemplateServiceTests.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 c4f850fef34d8..c3a16a1e25bc8 100644 --- a/server/src/test/java/org/opensearch/cluster/metadata/MetadataIndexTemplateServiceTests.java +++ b/server/src/test/java/org/opensearch/cluster/metadata/MetadataIndexTemplateServiceTests.java @@ -2070,7 +2070,7 @@ public void testLegacyNoopUpdate() { pr.settings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 3).build()); } if (randomBoolean()) { - pr.mappings("{}"); + pr.mappings("{}"); } if (randomBoolean()) { pr.aliases(Collections.singleton(new Alias("alias"))); From 0fa6b434c03ffed129b29e73f5ad35dc9a28e66f Mon Sep 17 00:00:00 2001 From: Suraj Singh Date: Fri, 18 Mar 2022 12:07:47 -0700 Subject: [PATCH 3/3] Address review comment and add missing field in toXContent method Signed-off-by: Suraj Singh --- .../cluster/SimpleClusterStateIT.java | 4 ---- .../gateway/RecoveryFromGatewayIT.java | 2 -- .../template/SimpleIndexTemplateIT.java | 22 +------------------ .../template/put/PutIndexTemplateRequest.java | 20 +++++++++-------- .../put/PutIndexTemplateRequestTests.java | 7 +++--- 5 files changed, 15 insertions(+), 40 deletions(-) diff --git a/server/src/internalClusterTest/java/org/opensearch/cluster/SimpleClusterStateIT.java b/server/src/internalClusterTest/java/org/opensearch/cluster/SimpleClusterStateIT.java index 62f6a93118a2a..19f7b0b4c630c 100644 --- a/server/src/internalClusterTest/java/org/opensearch/cluster/SimpleClusterStateIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/cluster/SimpleClusterStateIT.java @@ -169,7 +169,6 @@ public void testIndexTemplates() throws Exception { .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("_doc") .startObject("properties") .startObject("field1") .field("type", "text") @@ -181,7 +180,6 @@ public void testIndexTemplates() throws Exception { .endObject() .endObject() .endObject() - .endObject() ) .get(); @@ -193,7 +191,6 @@ public void testIndexTemplates() throws Exception { .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("_doc") .startObject("properties") .startObject("field2") .field("type", "text") @@ -201,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 38f0de6426a86..40cbd480c2dec 100644 --- a/server/src/internalClusterTest/java/org/opensearch/gateway/RecoveryFromGatewayIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/gateway/RecoveryFromGatewayIT.java @@ -464,7 +464,6 @@ public void testLatestVersionLoaded() throws Exception { .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("_doc") .startObject("properties") .startObject("field1") .field("type", "text") @@ -476,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/SimpleIndexTemplateIT.java b/server/src/internalClusterTest/java/org/opensearch/indices/template/SimpleIndexTemplateIT.java index dd011edf587e2..0e15a0c895432 100644 --- a/server/src/internalClusterTest/java/org/opensearch/indices/template/SimpleIndexTemplateIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/indices/template/SimpleIndexTemplateIT.java @@ -113,7 +113,6 @@ public void testSimpleIndexTemplateTests() throws Exception { .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("_doc") .startObject("properties") .startObject("field1") .field("type", "text") @@ -125,7 +124,6 @@ public void testSimpleIndexTemplateTests() throws Exception { .endObject() .endObject() .endObject() - .endObject() ) .get(); @@ -138,7 +136,6 @@ public void testSimpleIndexTemplateTests() throws Exception { .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("_doc") .startObject("properties") .startObject("field2") .field("type", "text") @@ -146,7 +143,6 @@ public void testSimpleIndexTemplateTests() throws Exception { .endObject() .endObject() .endObject() - .endObject() ) .get(); @@ -162,7 +158,6 @@ public void testSimpleIndexTemplateTests() throws Exception { .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("_doc") .startObject("properties") .startObject("field2") .field("type", "text") @@ -170,7 +165,6 @@ public void testSimpleIndexTemplateTests() throws Exception { .endObject() .endObject() .endObject() - .endObject() ), IllegalArgumentException.class ); @@ -223,7 +217,6 @@ public void testDeleteIndexTemplate() throws Exception { .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("_doc") .startObject("properties") .startObject("field1") .field("type", "text") @@ -235,7 +228,6 @@ public void testDeleteIndexTemplate() throws Exception { .endObject() .endObject() .endObject() - .endObject() ) .execute() .actionGet(); @@ -248,7 +240,6 @@ public void testDeleteIndexTemplate() throws Exception { .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("_doc") .startObject("properties") .startObject("field2") .field("type", "text") @@ -256,7 +247,6 @@ public void testDeleteIndexTemplate() throws Exception { .endObject() .endObject() .endObject() - .endObject() ) .execute() .actionGet(); @@ -279,7 +269,6 @@ public void testDeleteIndexTemplate() throws Exception { .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("type1") .startObject("properties") .startObject("field1") .field("type", "text") @@ -291,7 +280,6 @@ public void testDeleteIndexTemplate() throws Exception { .endObject() .endObject() .endObject() - .endObject() ) .execute() .actionGet(); @@ -319,7 +307,6 @@ public void testThatGetIndexTemplatesWorks() throws Exception { .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("_doc") .startObject("properties") .startObject("field1") .field("type", "text") @@ -331,7 +318,6 @@ public void testThatGetIndexTemplatesWorks() throws Exception { .endObject() .endObject() .endObject() - .endObject() ) .execute() .actionGet(); @@ -363,7 +349,6 @@ public void testThatGetIndexTemplatesWithSimpleRegexWorks() throws Exception { .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("_doc") .startObject("properties") .startObject("field1") .field("type", "text") @@ -375,7 +360,6 @@ public void testThatGetIndexTemplatesWithSimpleRegexWorks() throws Exception { .endObject() .endObject() .endObject() - .endObject() ) .execute() .actionGet(); @@ -389,7 +373,6 @@ public void testThatGetIndexTemplatesWithSimpleRegexWorks() throws Exception { .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("_doc") .startObject("properties") .startObject("field1") .field("type", "text") @@ -401,7 +384,6 @@ public void testThatGetIndexTemplatesWithSimpleRegexWorks() throws Exception { .endObject() .endObject() .endObject() - .endObject() ) .execute() .actionGet(); @@ -415,7 +397,6 @@ public void testThatGetIndexTemplatesWithSimpleRegexWorks() throws Exception { .setMapping( XContentFactory.jsonBuilder() .startObject() - .startObject("_doc") .startObject("properties") .startObject("field1") .field("type", "text") @@ -427,7 +408,6 @@ public void testThatGetIndexTemplatesWithSimpleRegexWorks() throws Exception { .endObject() .endObject() .endObject() - .endObject() ) .execute() .actionGet(); @@ -1011,7 +991,7 @@ public void testPartitionedTemplate() throws Exception { .indices() .preparePutTemplate("template_2") .setPatterns(Collections.singletonList("te*")) - .setMapping("{\"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/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 cb6d4f6ef7506..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 @@ -519,17 +519,19 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.endObject(); builder.startObject("mappings"); - try ( - XContentParser parser = JsonXContent.jsonXContent.createParser( - NamedXContentRegistry.EMPTY, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - mappings - ) - ) { - builder.copyCurrentStructure(parser); + if (mappings != null) { + builder.field(MapperService.SINGLE_MAPPING_NAME); + try ( + XContentParser parser = JsonXContent.jsonXContent.createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + mappings + ) + ) { + builder.copyCurrentStructure(parser); + } } builder.endObject(); - builder.startObject("aliases"); for (Alias alias : aliases) { alias.toXContent(builder, params); 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 1f47639d8c572..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; @@ -93,7 +94,6 @@ public void testMappingKeyedByType() throws IOException { request1.mapping(builder); builder = XContentFactory.contentBuilder(randomFrom(XContentType.values())); builder.startObject() - .startObject("type1") .startObject("properties") .startObject("field1") .field("type", "text") @@ -106,7 +106,6 @@ public void testMappingKeyedByType() throws IOException { .endObject() .endObject() .endObject() - .endObject() .endObject(); request2.mapping(builder); assertEquals(request1.mappings(), request2.mappings()); @@ -116,7 +115,7 @@ public void testMappingKeyedByType() throws IOException { request2 = new PutIndexTemplateRequest("bar"); String nakedMapping = "{\"properties\": {\"foo\": {\"type\": \"integer\"}}}"; request1.mapping(nakedMapping, XContentType.JSON); - request2.mapping("{\"type2\": " + nakedMapping + "}", XContentType.JSON); + request2.mapping(nakedMapping, XContentType.JSON); assertEquals(request1.mappings(), request2.mappings()); } { @@ -131,7 +130,7 @@ public void testMappingKeyedByType() throws IOException { ) .map(); request1.mapping(nakedMapping); - request2.mapping(MapBuilder.newMapBuilder().put("type3", nakedMapping).map()); + request2.mapping(MapBuilder.newMapBuilder().put(MapperService.SINGLE_MAPPING_NAME, nakedMapping).map()); assertEquals(request1.mappings(), request2.mappings()); } }