diff --git a/src/main/java/org/elasticsearch/index/mapper/DocumentMapperParser.java b/src/main/java/org/elasticsearch/index/mapper/DocumentMapperParser.java index eda8b668a03ea..c62149879949d 100644 --- a/src/main/java/org/elasticsearch/index/mapper/DocumentMapperParser.java +++ b/src/main/java/org/elasticsearch/index/mapper/DocumentMapperParser.java @@ -276,27 +276,19 @@ private Tuple> extractMapping(String type, String so @SuppressWarnings({"unchecked"}) private Tuple> extractMapping(String type, Map root) throws MapperParsingException { - int size = root.size(); - switch (size) { - case 0: - // if we don't have any keys throw an exception - throw new MapperParsingException("malformed mapping no root object found"); - case 1: - break; - default: - // we always assume the first and single key is the mapping type root - throw new MapperParsingException("mapping must have the `type` as the root object"); + if (root.size() == 0) { + // if we don't have any keys throw an exception + throw new MapperParsingException("malformed mapping no root object found"); } String rootName = root.keySet().iterator().next(); - if (type == null) { - type = rootName; - } else if (!type.equals(rootName)) { - // we always assume the first and single key is the mapping type root - throw new MapperParsingException("mapping must have the `type` as the root object. Got [" + rootName + "], expected [" + type + "]"); + Tuple> mapping; + if (type == null || type.equals(rootName)) { + mapping = new Tuple>(rootName, (Map) root.get(rootName)); + } else { + mapping = new Tuple>(type, root); } - - return new Tuple>(type, (Map) root.get(rootName)); + return mapping; } } diff --git a/src/test/java/org/elasticsearch/indices/mapping/UpdateMappingTests.java b/src/test/java/org/elasticsearch/indices/mapping/UpdateMappingTests.java index 4a9c86661a90e..81ed364e80b3a 100644 --- a/src/test/java/org/elasticsearch/indices/mapping/UpdateMappingTests.java +++ b/src/test/java/org/elasticsearch/indices/mapping/UpdateMappingTests.java @@ -120,6 +120,49 @@ public boolean apply(Object input) { } } + @Test + public void updateMappingWithoutType() throws Exception { + client().admin().indices().prepareCreate("test") + .setSettings( + ImmutableSettings.settingsBuilder() + .put("index.number_of_shards", 1) + .put("index.number_of_replicas", 0) + ).addMapping("doc", "{\"doc\":{\"properties\":{\"body\":{\"type\":\"string\"}}}}") + .execute().actionGet(); + client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet(); + + PutMappingResponse putMappingResponse = client().admin().indices().preparePutMapping("test").setType("doc") + .setSource("{\"properties\":{\"date\":{\"type\":\"integer\"}}}") + .execute().actionGet(); + + assertThat(putMappingResponse.isAcknowledged(), equalTo(true)); + + GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("test").execute().actionGet(); + assertThat(getMappingsResponse.mappings().get("test").get("doc").source().toString(), + equalTo("{\"doc\":{\"properties\":{\"body\":{\"type\":\"string\"},\"date\":{\"type\":\"integer\"}}}}")); + } + + @Test + public void updateMappingWithoutTypeMultiObjects() throws Exception { + client().admin().indices().prepareCreate("test") + .setSettings( + ImmutableSettings.settingsBuilder() + .put("index.number_of_shards", 1) + .put("index.number_of_replicas", 0) + ).execute().actionGet(); + client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet(); + + PutMappingResponse putMappingResponse = client().admin().indices().preparePutMapping("test").setType("doc") + .setSource("{\"_source\":{\"enabled\":false},\"properties\":{\"date\":{\"type\":\"integer\"}}}") + .execute().actionGet(); + + assertThat(putMappingResponse.isAcknowledged(), equalTo(true)); + + GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("test").execute().actionGet(); + assertThat(getMappingsResponse.mappings().get("test").get("doc").source().toString(), + equalTo("{\"doc\":{\"_source\":{\"enabled\":false},\"properties\":{\"date\":{\"type\":\"integer\"}}}}")); + } + @Test(expected = MergeMappingException.class) public void updateMappingWithConflicts() throws Exception {