Skip to content

Commit 2341825

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 #4483
1 parent f2f4b72 commit 2341825

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

+9-17
Original file line numberDiff line numberDiff line change
@@ -275,27 +275,19 @@ private Tuple<String, Map<String, Object>> extractMapping(String type, String so
275275

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

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

298-
299-
return new Tuple<String, Map<String, Object>>(type, (Map<String, Object>) root.get(rootName));
291+
return mapping;
300292
}
301293
}

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

+43
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)