Skip to content

Commit f2ec6e7

Browse files
committed
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 elastic#4483
1 parent cb3b653 commit f2ec6e7

File tree

2 files changed

+52
-17
lines changed

2 files changed

+52
-17
lines changed

src/main/java/org/elasticsearch/index/mapper/DocumentMapperParser.java

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -276,27 +276,19 @@ private Tuple<String, Map<String, Object>> extractMapping(String type, String so
276276

277277
@SuppressWarnings({"unchecked"})
278278
private Tuple<String, Map<String, Object>> extractMapping(String type, Map<String, Object> root) throws MapperParsingException {
279-
int size = root.size();
280-
switch (size) {
281-
case 0:
282-
// if we don't have any keys throw an exception
283-
throw new MapperParsingException("malformed mapping no root object found");
284-
case 1:
285-
break;
286-
default:
287-
// we always assume the first and single key is the mapping type root
288-
throw new MapperParsingException("mapping must have the `type` as the root object");
279+
if (root.size() == 0) {
280+
// if we don't have any keys throw an exception
281+
throw new MapperParsingException("malformed mapping no root object found");
289282
}
290283

291284
String rootName = root.keySet().iterator().next();
292-
if (type == null) {
293-
type = rootName;
294-
} else if (!type.equals(rootName)) {
295-
// we always assume the first and single key is the mapping type root
296-
throw new MapperParsingException("mapping must have the `type` as the root object. Got [" + rootName + "], expected [" + type + "]");
285+
Tuple<String, Map<String, Object>> mapping;
286+
if (type == null || type.equals(rootName)) {
287+
mapping = new Tuple<String, Map<String, Object>>(rootName, (Map<String, Object>) root.get(rootName));
288+
} else {
289+
mapping = new Tuple<String, Map<String, Object>>(type, root);
297290
}
298291

299-
300-
return new Tuple<String, Map<String, Object>>(type, (Map<String, Object>) root.get(rootName));
292+
return mapping;
301293
}
302294
}

src/test/java/org/elasticsearch/indices/mapping/UpdateMappingTests.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,49 @@ public boolean apply(Object input) {
120120
}
121121
}
122122

123+
@Test
124+
public void updateMappingWithoutType() throws Exception {
125+
client().admin().indices().prepareCreate("test")
126+
.setSettings(
127+
ImmutableSettings.settingsBuilder()
128+
.put("index.number_of_shards", 1)
129+
.put("index.number_of_replicas", 0)
130+
).addMapping("doc", "{\"doc\":{\"properties\":{\"body\":{\"type\":\"string\"}}}}")
131+
.execute().actionGet();
132+
client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet();
133+
134+
PutMappingResponse putMappingResponse = client().admin().indices().preparePutMapping("test").setType("doc")
135+
.setSource("{\"properties\":{\"date\":{\"type\":\"integer\"}}}")
136+
.execute().actionGet();
137+
138+
assertThat(putMappingResponse.isAcknowledged(), equalTo(true));
139+
140+
GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("test").execute().actionGet();
141+
assertThat(getMappingsResponse.mappings().get("test").get("doc").source().toString(),
142+
equalTo("{\"doc\":{\"properties\":{\"body\":{\"type\":\"string\"},\"date\":{\"type\":\"integer\"}}}}"));
143+
}
144+
145+
@Test
146+
public void updateMappingWithoutTypeMultiObjects() throws Exception {
147+
client().admin().indices().prepareCreate("test")
148+
.setSettings(
149+
ImmutableSettings.settingsBuilder()
150+
.put("index.number_of_shards", 1)
151+
.put("index.number_of_replicas", 0)
152+
).execute().actionGet();
153+
client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet();
154+
155+
PutMappingResponse putMappingResponse = client().admin().indices().preparePutMapping("test").setType("doc")
156+
.setSource("{\"_source\":{\"enabled\":false},\"properties\":{\"date\":{\"type\":\"integer\"}}}")
157+
.execute().actionGet();
158+
159+
assertThat(putMappingResponse.isAcknowledged(), equalTo(true));
160+
161+
GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("test").execute().actionGet();
162+
assertThat(getMappingsResponse.mappings().get("test").get("doc").source().toString(),
163+
equalTo("{\"doc\":{\"_source\":{\"enabled\":false},\"properties\":{\"date\":{\"type\":\"integer\"}}}}"));
164+
}
165+
123166
@Test(expected = MergeMappingException.class)
124167
public void updateMappingWithConflicts() throws Exception {
125168

0 commit comments

Comments
 (0)