Mappings: Fix doc parser to still pre/post process metadata fields on disabled type

closes #13017
This commit is contained in:
Ryan Ernst 2015-08-26 20:20:14 -07:00
parent 9bd4a7b72e
commit a5bae3bdd8
2 changed files with 21 additions and 20 deletions

View File

@ -104,8 +104,9 @@ class DocumentParser implements Closeable {
if (token != XContentParser.Token.START_OBJECT) { if (token != XContentParser.Token.START_OBJECT) {
throw new MapperParsingException("Malformed content, must start with an object"); throw new MapperParsingException("Malformed content, must start with an object");
} }
if (mapping.root.isEnabled()) {
boolean emptyDoc = false; boolean emptyDoc = false;
if (mapping.root.isEnabled()) {
token = parser.nextToken(); token = parser.nextToken();
if (token == XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.END_OBJECT) {
// empty doc, we can handle it... // empty doc, we can handle it...
@ -113,25 +114,26 @@ class DocumentParser implements Closeable {
} else if (token != XContentParser.Token.FIELD_NAME) { } 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"); throw new MapperParsingException("Malformed content, after first object, either the type field or the actual properties should exist");
} }
}
for (MetadataFieldMapper metadataMapper : mapping.metadataMappers) { for (MetadataFieldMapper metadataMapper : mapping.metadataMappers) {
metadataMapper.preParse(context); metadataMapper.preParse(context);
} }
if (emptyDoc == false) {
if (mapping.root.isEnabled() == false) {
// entire type is disabled
parser.skipChildren();
} else if (emptyDoc == false) {
Mapper update = parseObject(context, mapping.root); Mapper update = parseObject(context, mapping.root);
if (update != null) { if (update != null) {
context.addDynamicMappingsUpdate(update); context.addDynamicMappingsUpdate(update);
} }
} }
for (MetadataFieldMapper metadataMapper : mapping.metadataMappers) { for (MetadataFieldMapper metadataMapper : mapping.metadataMappers) {
metadataMapper.postParse(context); metadataMapper.postParse(context);
} }
} else {
// entire type is disabled
parser.skipChildren();
}
// try to parse the next token, this should be null if the object is ended properly // try to parse the next token, this should be null if the object is ended properly
// but will throw a JSON exception if the extra tokens is not valid JSON (this will be handled by the catch) // but will throw a JSON exception if the extra tokens is not valid JSON (this will be handled by the catch)
if (Version.indexCreated(indexSettings).onOrAfter(Version.V_2_0_0_beta1) if (Version.indexCreated(indexSettings).onOrAfter(Version.V_2_0_0_beta1)

View File

@ -19,12 +19,9 @@
package org.elasticsearch.index.mapper; package org.elasticsearch.index.mapper;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.mapper.internal.UidFieldMapper;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.common.xcontent.json.JsonXContentParser;
import org.elasticsearch.test.ESSingleNodeTestCase; import org.elasticsearch.test.ESSingleNodeTestCase;
// TODO: make this a real unit test // TODO: make this a real unit test
@ -37,11 +34,12 @@ public class DocumentParserTests extends ESSingleNodeTestCase {
DocumentMapper mapper = mapperParser.parse(mapping); DocumentMapper mapper = mapperParser.parse(mapping);
BytesReference bytes = XContentFactory.jsonBuilder() BytesReference bytes = XContentFactory.jsonBuilder()
.startObject() .startObject().startObject("foo")
.field("field", "1234") .field("field", "1234")
.endObject().bytes(); .endObject().endObject().bytes();
ParsedDocument doc = mapper.parse("test", "type", "1", bytes); ParsedDocument doc = mapper.parse("test", "type", "1", bytes);
assertNull(doc.rootDoc().getField("field")); assertNull(doc.rootDoc().getField("field"));
assertNotNull(doc.rootDoc().getField(UidFieldMapper.NAME));
} }
public void testFieldDisabled() throws Exception { public void testFieldDisabled() throws Exception {
@ -60,5 +58,6 @@ public class DocumentParserTests extends ESSingleNodeTestCase {
ParsedDocument doc = mapper.parse("test", "type", "1", bytes); ParsedDocument doc = mapper.parse("test", "type", "1", bytes);
assertNull(doc.rootDoc().getField("foo")); assertNull(doc.rootDoc().getField("foo"));
assertNotNull(doc.rootDoc().getField("bar")); assertNotNull(doc.rootDoc().getField("bar"));
assertNotNull(doc.rootDoc().getField(UidFieldMapper.NAME));
} }
} }