From 598370b6c722f8b1f40b20b7fa5f21e3c0c0a85a Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Wed, 3 Aug 2011 12:36:05 +0300 Subject: [PATCH] Allow creation of empty docs, closes #1195. --- .../elasticsearch/index/mapper/DocumentMapper.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java index 3d57b4ae5a9..62a24e49585 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java @@ -519,11 +519,15 @@ public class DocumentMapper implements ToXContent { if (token != XContentParser.Token.START_OBJECT) { throw new MapperParsingException("Malformed content, must start with an object"); } + boolean emptyDoc = false; token = parser.nextToken(); - if (token != XContentParser.Token.FIELD_NAME) { + if (token == XContentParser.Token.END_OBJECT) { + // empty doc, we can handle it... + emptyDoc = true; + } else if (token != XContentParser.Token.FIELD_NAME) { throw new MapperParsingException("Malformed content, after first object, either the type field or the actual properties should exist"); } - if (parser.currentName().equals(type)) { + if (type.equals(parser.currentName())) { // first field is the same as the type, this might be because the type is provided, and the object exists within it // or because there is a valid field that by chance is named as the type @@ -558,7 +562,9 @@ public class DocumentMapper implements ToXContent { indexFieldMapper.parse(context); - rootObjectMapper.parse(context); + if (!emptyDoc) { + rootObjectMapper.parse(context); + } for (int i = 0; i < countDownTokens; i++) { parser.nextToken();