parent/child: Make sure that no _parent#null gets introduces as default _parent mapping.

Instead it should just be `_parent` field.

Also added more tests regarding the join doc values field being added.

Closes #19389
This commit is contained in:
Martijn van Groningen 2016-07-18 12:34:38 +02:00
parent 16812cc032
commit 82e7f1fc43
2 changed files with 47 additions and 15 deletions

View File

@ -58,9 +58,6 @@ import java.util.Objects;
import static org.elasticsearch.common.xcontent.support.XContentMapValues.nodeMapValue;
/**
*
*/
public class ParentFieldMapper extends MetadataFieldMapper {
public static final String NAME = "_parent";
@ -98,7 +95,7 @@ public class ParentFieldMapper extends MetadataFieldMapper {
}
public Builder eagerGlobalOrdinals(boolean eagerGlobalOrdinals) {
((ParentFieldType) fieldType()).setEagerGlobalOrdinals(eagerGlobalOrdinals);
fieldType().setEagerGlobalOrdinals(eagerGlobalOrdinals);
return builder;
}
@ -143,8 +140,8 @@ public class ParentFieldMapper extends MetadataFieldMapper {
@Override
public MetadataFieldMapper getDefault(Settings indexSettings, MappedFieldType fieldType, String typeName) {
KeywordFieldMapper parentJoinField = createParentJoinFieldMapper(typeName, new BuilderContext(indexSettings, new ContentPath(0)));
MappedFieldType childJoinFieldType = Defaults.FIELD_TYPE.clone();
childJoinFieldType.setName(joinField(null));
MappedFieldType childJoinFieldType = new ParentFieldType(Defaults.FIELD_TYPE, typeName);
childJoinFieldType.setName(ParentFieldMapper.NAME);
return new ParentFieldMapper(parentJoinField, childJoinFieldType, null, indexSettings);
}
}

View File

@ -18,17 +18,22 @@
*/
package org.elasticsearch.index.mapper.parent;
import org.apache.lucene.index.IndexableField;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.IndexService;
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.analysis.AnalysisService;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.MapperParsingException;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.MapperService.MergeReason;
import org.elasticsearch.index.mapper.ParseContext;
import org.elasticsearch.index.mapper.ParsedDocument;
import org.elasticsearch.index.mapper.SourceToParse;
import org.elasticsearch.index.similarity.SimilarityService;
@ -59,23 +64,42 @@ public class ParentMappingTests extends ESSingleNodeTestCase {
}
}
public void testParentSet() throws Exception {
public void testJoinFieldSet() throws Exception {
String parentMapping = XContentFactory.jsonBuilder().startObject().startObject("parent_type")
.endObject().endObject().string();
String childMapping = XContentFactory.jsonBuilder().startObject().startObject("child_type")
.startObject("_parent").field("type", "parent_type").endObject()
.endObject().endObject().string();
IndexService indexService = createIndex("test");
indexService.mapperService().merge("parent_type", new CompressedXContent(parentMapping), MergeReason.MAPPING_UPDATE, false);
indexService.mapperService().merge("child_type", new CompressedXContent(childMapping), MergeReason.MAPPING_UPDATE, false);
// Indexing parent doc:
DocumentMapper parentDocMapper = indexService.mapperService().documentMapper("parent_type");
ParsedDocument doc = parentDocMapper.parse(SourceToParse.source("test", "parent_type", "1122", new BytesArray("{}")));
assertEquals(1, getNumberOfFieldWithParentPrefix(doc.rootDoc()));
assertEquals("1122", doc.rootDoc().getBinaryValue("_parent#parent_type").utf8ToString());
// Indexing child doc:
DocumentMapper childDocMapper = indexService.mapperService().documentMapper("child_type");
doc = childDocMapper.parse(SourceToParse.source("test", "child_type", "1", new BytesArray("{}")).parent("1122"));
assertEquals(1, getNumberOfFieldWithParentPrefix(doc.rootDoc()));
assertEquals("1122", doc.rootDoc().getBinaryValue("_parent#parent_type").utf8ToString());
}
public void testJoinFieldNotSet() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("_parent").field("type", "p_type").endObject()
.endObject().endObject().string();
DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping));
ParsedDocument doc = docMapper.parse(SourceToParse.source("test", "type", "1", XContentFactory.jsonBuilder()
.startObject()
.field("x_field", "x_value")
.endObject()
.bytes()).parent("1122"));
assertEquals("1122", doc.rootDoc().getBinaryValue("_parent#p_type").utf8ToString());
.bytes()));
assertEquals(0, getNumberOfFieldWithParentPrefix(doc.rootDoc()));
}
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/19389")
public void testNoParentNullFieldCreatedIfNoParentSpecified() throws Exception {
Index index = new Index("_index", "testUUID");
IndexSettings indexSettings = IndexSettingsModule.newIndexSettings(index, Settings.EMPTY);
@ -88,8 +112,19 @@ public class ParentMappingTests extends ESSingleNodeTestCase {
.startObject("properties")
.endObject()
.endObject().endObject();
mapperService.merge("some_type", new CompressedXContent(mappingSource.string()), MapperService.MergeReason.MAPPING_UPDATE, false);
mapperService.merge("some_type", new CompressedXContent(mappingSource.string()), MergeReason.MAPPING_UPDATE, false);
Set<String> allFields = new HashSet<>(mapperService.simpleMatchToIndexNames("*"));
assertTrue(allFields.contains("_parent"));
assertFalse(allFields.contains("_parent#null"));
}
private static int getNumberOfFieldWithParentPrefix(ParseContext.Document doc) {
int numFieldWithParentPrefix = 0;
for (IndexableField field : doc) {
if (field.name().startsWith("_parent")) {
numFieldWithParentPrefix++;
}
}
return numFieldWithParentPrefix;
}
}