Skip to content

Commit

Permalink
Make type wrapping optional for PUT Mapping API request
Browse files Browse the repository at this point in the history
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 elastic#4483
  • Loading branch information
dakrone authored and brusic committed Jan 19, 2014
1 parent f75ffcc commit 6290a10
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -275,27 +275,19 @@ private Tuple<String, Map<String, Object>> extractMapping(String type, String so

@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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down

0 comments on commit 6290a10

Please sign in to comment.