mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-17 10:25:15 +00:00
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:
parent
f2f4b72a12
commit
2341825358
@ -275,27 +275,19 @@ public class DocumentMapperParser extends AbstractIndexComponent {
|
|||||||
|
|
||||||
@SuppressWarnings({"unchecked"})
|
@SuppressWarnings({"unchecked"})
|
||||||
private Tuple<String, Map<String, Object>> extractMapping(String type, Map<String, Object> root) throws MapperParsingException {
|
private Tuple<String, Map<String, Object>> extractMapping(String type, Map<String, Object> root) throws MapperParsingException {
|
||||||
int size = root.size();
|
if (root.size() == 0) {
|
||||||
switch (size) {
|
// if we don't have any keys throw an exception
|
||||||
case 0:
|
throw new MapperParsingException("malformed mapping no root object found");
|
||||||
// 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");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String rootName = root.keySet().iterator().next();
|
String rootName = root.keySet().iterator().next();
|
||||||
if (type == null) {
|
Tuple<String, Map<String, Object>> mapping;
|
||||||
type = rootName;
|
if (type == null || type.equals(rootName)) {
|
||||||
} else if (!type.equals(rootName)) {
|
mapping = new Tuple<String, Map<String, Object>>(rootName, (Map<String, Object>) root.get(rootName));
|
||||||
// we always assume the first and single key is the mapping type root
|
} else {
|
||||||
throw new MapperParsingException("mapping must have the `type` as the root object. Got [" + rootName + "], expected [" + type + "]");
|
mapping = new Tuple<String, Map<String, Object>>(type, root);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return mapping;
|
||||||
return new Tuple<String, Map<String, Object>>(type, (Map<String, Object>) root.get(rootName));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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)
|
@Test(expected = MergeMappingException.class)
|
||||||
public void updateMappingWithConflicts() throws Exception {
|
public void updateMappingWithConflicts() throws Exception {
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user