diff --git a/core/src/main/java/org/elasticsearch/index/mapper/DocumentParser.java b/core/src/main/java/org/elasticsearch/index/mapper/DocumentParser.java index 227edc23d7d..85367723c54 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/DocumentParser.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/DocumentParser.java @@ -473,9 +473,7 @@ final class DocumentParser { if (update != null) { context.addDynamicMapper(update); } - if (fieldMapper.copyTo() != null) { - parseCopyFields(context, fieldMapper.copyTo().copyToFields()); - } + parseCopyFields(context, fieldMapper.copyTo().copyToFields()); } } diff --git a/core/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java index b8c958f41f3..c2af9f715dd 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java @@ -43,6 +43,7 @@ import java.util.Comparator; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.stream.StreamSupport; public abstract class FieldMapper extends Mapper implements Cloneable { @@ -60,7 +61,7 @@ public abstract class FieldMapper extends Mapper implements Cloneable { protected boolean indexOptionsSet = false; protected boolean docValuesSet = false; protected final MultiFields.Builder multiFieldsBuilder; - protected CopyTo copyTo; + protected CopyTo copyTo = CopyTo.empty(); protected Builder(String name, MappedFieldType fieldType, MappedFieldType defaultFieldType) { super(name); @@ -256,7 +257,7 @@ public abstract class FieldMapper extends Mapper implements Cloneable { defaultFieldType.freeze(); this.defaultFieldType = defaultFieldType; this.multiFields = multiFields; - this.copyTo = copyTo; + this.copyTo = Objects.requireNonNull(copyTo); } @Override @@ -407,10 +408,7 @@ public abstract class FieldMapper extends Mapper implements Cloneable { } multiFields.toXContent(builder, params); - - if (copyTo != null) { - copyTo.toXContent(builder, params); - } + copyTo.toXContent(builder, params); } protected final void doXContentAnalyzers(XContentBuilder builder, boolean includeDefaults) throws IOException { @@ -617,6 +615,12 @@ public abstract class FieldMapper extends Mapper implements Cloneable { */ public static class CopyTo { + private static final CopyTo EMPTY = new CopyTo(Collections.emptyList()); + + public static CopyTo empty() { + return EMPTY; + } + private final List copyToFields; private CopyTo(List copyToFields) { @@ -643,6 +647,9 @@ public abstract class FieldMapper extends Mapper implements Cloneable { } public CopyTo build() { + if (copyToBuilders.isEmpty()) { + return EMPTY; + } return new CopyTo(Collections.unmodifiableList(copyToBuilders)); } } diff --git a/core/src/main/java/org/elasticsearch/index/mapper/MetadataFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/MetadataFieldMapper.java index ec84631e041..0833e8f33f3 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/MetadataFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/MetadataFieldMapper.java @@ -55,7 +55,7 @@ public abstract class MetadataFieldMapper extends FieldMapper { } protected MetadataFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, Settings indexSettings) { - super(simpleName, fieldType, defaultFieldType, indexSettings, MultiFields.empty(), null); + super(simpleName, fieldType, defaultFieldType, indexSettings, MultiFields.empty(), CopyTo.empty()); } /** diff --git a/core/src/test/java/org/elasticsearch/index/mapper/DocumentFieldMapperTests.java b/core/src/test/java/org/elasticsearch/index/mapper/DocumentFieldMapperTests.java index 27b90801ecc..398708d75f9 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/DocumentFieldMapperTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/DocumentFieldMapperTests.java @@ -95,7 +95,7 @@ public class DocumentFieldMapperTests extends LuceneTestCase { private static final Settings SETTINGS = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).build(); FakeFieldMapper(String simpleName, MappedFieldType fieldType) { - super(simpleName, fieldType.clone(), fieldType.clone(), SETTINGS, null, null); + super(simpleName, fieldType.clone(), fieldType.clone(), SETTINGS, MultiFields.empty(), CopyTo.empty()); } @Override diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/mapper/MetaJoinFieldMapper.java b/modules/parent-join/src/main/java/org/elasticsearch/join/mapper/MetaJoinFieldMapper.java index db9fae9b47d..2b02f41636b 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/mapper/MetaJoinFieldMapper.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/mapper/MetaJoinFieldMapper.java @@ -108,7 +108,7 @@ public class MetaJoinFieldMapper extends FieldMapper { } MetaJoinFieldMapper(String name, MappedFieldType fieldType, Settings indexSettings) { - super(name, fieldType, ParentIdFieldMapper.Defaults.FIELD_TYPE, indexSettings, MultiFields.empty(), null); + super(name, fieldType, ParentIdFieldMapper.Defaults.FIELD_TYPE, indexSettings, MultiFields.empty(), CopyTo.empty()); } void setFieldMapper(ParentJoinFieldMapper mapper) { diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/mapper/ParentIdFieldMapper.java b/modules/parent-join/src/main/java/org/elasticsearch/join/mapper/ParentIdFieldMapper.java index cc2815c373a..91adfc5fdf9 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/mapper/ParentIdFieldMapper.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/mapper/ParentIdFieldMapper.java @@ -134,7 +134,7 @@ public final class ParentIdFieldMapper extends FieldMapper { Set children, MappedFieldType fieldType, Settings indexSettings) { - super(simpleName, fieldType, Defaults.FIELD_TYPE, indexSettings, MultiFields.empty(), null); + super(simpleName, fieldType, Defaults.FIELD_TYPE, indexSettings, MultiFields.empty(), CopyTo.empty()); this.parentName = parentName; this.children = children; } diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/mapper/ParentJoinFieldMapper.java b/modules/parent-join/src/main/java/org/elasticsearch/join/mapper/ParentJoinFieldMapper.java index 0b3a3885fab..7a17d9ed7fe 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/mapper/ParentJoinFieldMapper.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/mapper/ParentJoinFieldMapper.java @@ -249,7 +249,7 @@ public final class ParentJoinFieldMapper extends FieldMapper { MetaJoinFieldMapper uniqueFieldMapper, List parentIdFields, boolean eagerGlobalOrdinals) { - super(simpleName, fieldType, Defaults.FIELD_TYPE, indexSettings, MultiFields.empty(), null); + super(simpleName, fieldType, Defaults.FIELD_TYPE, indexSettings, MultiFields.empty(), CopyTo.empty()); this.parentIdFields = parentIdFields; this.uniqueFieldMapper = uniqueFieldMapper; this.uniqueFieldMapper.setFieldMapper(this);