Make type wrapping optional for PUT Mapping API request

Put mapping now supports either of these formats:

POST foo/doc/_mapping
{
  "doc": {
    "_routing": {"required": true},
    "properties": {
      "body": {"type": "string"}
    }
  }
}

or

POST foo/doc/_mapping
{
  "_routing": {"required": true},
  "properties": {
    "body": {"type": "string"}
  }
}

Closes #4483
This commit is contained in:
Lee Hinman 2014-01-08 17:06:28 -07:00
parent f2f4b72a12
commit 2341825358
2 changed files with 52 additions and 17 deletions

View File

@ -275,27 +275,19 @@ public class DocumentMapperParser extends AbstractIndexComponent {
@SuppressWarnings({"unchecked"})
private Tuple<String, Map<String, Object>> extractMapping(String type, Map<String, Object> 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<String, Map<String, Object>> mapping;
if (type == null || type.equals(rootName)) {
mapping = new Tuple<String, Map<String, Object>>(rootName, (Map<String, Object>) root.get(rootName));
} else {
mapping = new Tuple<String, Map<String, Object>>(type, root);
}
return new Tuple<String, Map<String, Object>>(type, (Map<String, Object>) root.get(rootName));
return mapping;
}
}

View File

@ -120,6 +120,49 @@ public class UpdateMappingTests extends ElasticsearchIntegrationTest {
}
}
@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 {