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