From a7a7123d74948d2c932db4723047a5409c3d2812 Mon Sep 17 00:00:00 2001 From: Adrien Grand Date: Wed, 17 Aug 2016 16:47:03 +0200 Subject: [PATCH] Simplify inclusion in `_all`. #20028 Currently, when you set `include_in_all` on an object, it will propagate the information to its sub mappers immediately. This is annoying because this is done using a different mechanism than regular mapping updates. This PR changes object fields to propagate the information at document parsing time rather than when `include_an_all` is updated. While moving this cost to document parsing time rather than mapping update time is probably a bad trade-off, I am confident that this cost is very low and think this new way makes things simpler. --- .../index/mapper/AllFieldMapper.java | 22 ------ .../index/mapper/DateFieldMapper.java | 43 ++--------- .../index/mapper/DocumentParser.java | 6 ++ .../index/mapper/FieldMapper.java | 10 +-- .../index/mapper/IpFieldMapper.java | 42 ++--------- .../index/mapper/KeywordFieldMapper.java | 43 ++--------- .../index/mapper/LegacyByteFieldMapper.java | 9 +-- .../index/mapper/LegacyDateFieldMapper.java | 10 +-- .../index/mapper/LegacyDoubleFieldMapper.java | 9 +-- .../index/mapper/LegacyFloatFieldMapper.java | 9 +-- .../mapper/LegacyIntegerFieldMapper.java | 9 +-- .../index/mapper/LegacyIpFieldMapper.java | 9 +-- .../index/mapper/LegacyLongFieldMapper.java | 10 +-- .../index/mapper/LegacyNumberFieldMapper.java | 40 +--------- .../index/mapper/LegacyShortFieldMapper.java | 10 +-- .../mapper/LegacyTokenCountFieldMapper.java | 9 +-- .../index/mapper/NumberFieldMapper.java | 43 ++--------- .../index/mapper/ObjectMapper.java | 73 ++++--------------- .../index/mapper/ParseContext.java | 25 ++++++- .../index/mapper/RootObjectMapper.java | 9 ++- .../index/mapper/ScaledFloatFieldMapper.java | 43 ++--------- .../index/mapper/StringFieldMapper.java | 43 ++--------- .../index/mapper/TextFieldMapper.java | 43 ++--------- .../index/mapper/DocumentParserTests.java | 50 +++++++++++++ 24 files changed, 173 insertions(+), 446 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/index/mapper/AllFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/AllFieldMapper.java index 05d94ff919d..f52123784d1 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/AllFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/AllFieldMapper.java @@ -47,28 +47,6 @@ import static org.elasticsearch.index.mapper.TypeParsers.parseTextField; */ public class AllFieldMapper extends MetadataFieldMapper { - public interface IncludeInAll { - - /** - * If {@code includeInAll} is not null then return a copy of this mapper - * that will include values in the _all field according to {@code includeInAll}. - */ - Mapper includeInAll(Boolean includeInAll); - - /** - * If {@code includeInAll} is not null and not set on this mapper yet, then - * return a copy of this mapper that will include values in the _all field - * according to {@code includeInAll}. - */ - Mapper includeInAllIfNotSet(Boolean includeInAll); - - /** - * If {@code includeInAll} was already set on this mapper then return a copy - * of this mapper that has {@code includeInAll} not set. - */ - Mapper unsetIncludeInAll(); - } - public static final String NAME = "_all"; public static final String CONTENT_TYPE = "_all"; diff --git a/core/src/main/java/org/elasticsearch/index/mapper/DateFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/DateFieldMapper.java index f3d1fbd53f3..717f0361552 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/DateFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/DateFieldMapper.java @@ -37,7 +37,6 @@ import org.elasticsearch.common.joda.DateMathParser; import org.elasticsearch.common.joda.FormatDateTimeFormatter; import org.elasticsearch.common.joda.Joda; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.common.util.LocaleUtils; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.index.fielddata.IndexFieldData; @@ -60,7 +59,7 @@ import java.util.concurrent.Callable; import static org.elasticsearch.index.mapper.TypeParsers.parseDateTimeFormatter; /** A {@link FieldMapper} for ip addresses. */ -public class DateFieldMapper extends FieldMapper implements AllFieldMapper.IncludeInAll { +public class DateFieldMapper extends FieldMapper { public static final String CONTENT_TYPE = "date"; public static final FormatDateTimeFormatter DEFAULT_DATE_TIME_FORMATTER = Joda.forPattern( @@ -119,9 +118,8 @@ public class DateFieldMapper extends FieldMapper implements AllFieldMapper.Inclu @Override public DateFieldMapper build(BuilderContext context) { setupFieldType(context); - DateFieldMapper fieldMapper = new DateFieldMapper(name, fieldType, defaultFieldType, ignoreMalformed(context), - context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); - return (DateFieldMapper) fieldMapper.includeInAll(includeInAll); + return new DateFieldMapper(name, fieldType, defaultFieldType, ignoreMalformed(context), + includeInAll, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); } } @@ -473,11 +471,13 @@ public class DateFieldMapper extends FieldMapper implements AllFieldMapper.Inclu MappedFieldType fieldType, MappedFieldType defaultFieldType, Explicit ignoreMalformed, + Boolean includeInAll, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { super(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, copyTo); this.ignoreMalformed = ignoreMalformed; + this.includeInAll = includeInAll; } @Override @@ -495,39 +495,6 @@ public class DateFieldMapper extends FieldMapper implements AllFieldMapper.Inclu return (DateFieldMapper) super.clone(); } - @Override - public Mapper includeInAll(Boolean includeInAll) { - if (includeInAll != null) { - DateFieldMapper clone = clone(); - clone.includeInAll = includeInAll; - return clone; - } else { - return this; - } - } - - @Override - public Mapper includeInAllIfNotSet(Boolean includeInAll) { - if (includeInAll != null && this.includeInAll == null) { - DateFieldMapper clone = clone(); - clone.includeInAll = includeInAll; - return clone; - } else { - return this; - } - } - - @Override - public Mapper unsetIncludeInAll() { - if (includeInAll != null) { - DateFieldMapper clone = clone(); - clone.includeInAll = null; - return clone; - } else { - return this; - } - } - @Override protected void parseCreateField(ParseContext context, List fields) throws IOException { String dateAsString; 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 80d59ec39f6..60ca0c22cca 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/DocumentParser.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/DocumentParser.java @@ -343,6 +343,12 @@ final class DocumentParser { context = nestedContext(context, mapper); } + // update the default value of include_in_all if necessary + Boolean includeInAll = mapper.includeInAll(); + if (includeInAll != null) { + context = context.setIncludeInAllDefault(includeInAll); + } + // if we are at the end of the previous object, advance if (token == XContentParser.Token.END_OBJECT) { token = parser.nextToken(); 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 e3f88b2231a..ee54a473998 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java @@ -537,11 +537,7 @@ public abstract class FieldMapper extends Mapper implements Cloneable { ImmutableOpenMap.Builder builder = new ImmutableOpenMap.Builder<>(); // we disable the all in multi-field mappers for (ObjectObjectCursor cursor : mappers) { - FieldMapper mapper = cursor.value; - if (mapper instanceof AllFieldMapper.IncludeInAll) { - mapper = (FieldMapper) ((AllFieldMapper.IncludeInAll) mapper).unsetIncludeInAll(); - } - builder.put(cursor.key, mapper); + builder.put(cursor.key, cursor.value); } this.mappers = builder.build(); } @@ -568,10 +564,6 @@ public abstract class FieldMapper extends Mapper implements Cloneable { FieldMapper mergeWithMapper = cursor.value; FieldMapper mergeIntoMapper = mappers.get(mergeWithMapper.simpleName()); if (mergeIntoMapper == null) { - // we disable the all in multi-field mappers - if (mergeWithMapper instanceof AllFieldMapper.IncludeInAll) { - mergeWithMapper = (FieldMapper) ((AllFieldMapper.IncludeInAll) mergeWithMapper).unsetIncludeInAll(); - } newMappersBuilder.put(mergeWithMapper.simpleName(), mergeWithMapper); } else { FieldMapper merged = mergeIntoMapper.merge(mergeWithMapper, false); diff --git a/core/src/main/java/org/elasticsearch/index/mapper/IpFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/IpFieldMapper.java index c5e83657608..69a8e06f859 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/IpFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/IpFieldMapper.java @@ -50,7 +50,7 @@ import java.util.List; import java.util.Map; /** A {@link FieldMapper} for ip addresses. */ -public class IpFieldMapper extends FieldMapper implements AllFieldMapper.IncludeInAll { +public class IpFieldMapper extends FieldMapper { public static final String CONTENT_TYPE = "ip"; @@ -81,9 +81,8 @@ public class IpFieldMapper extends FieldMapper implements AllFieldMapper.Include @Override public IpFieldMapper build(BuilderContext context) { setupFieldType(context); - IpFieldMapper fieldMapper = new IpFieldMapper(name, fieldType, defaultFieldType, ignoreMalformed(context), - context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); - return (IpFieldMapper) fieldMapper.includeInAll(includeInAll); + return new IpFieldMapper(name, fieldType, defaultFieldType, ignoreMalformed(context), + includeInAll, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); } } @@ -261,11 +260,13 @@ public class IpFieldMapper extends FieldMapper implements AllFieldMapper.Include MappedFieldType fieldType, MappedFieldType defaultFieldType, Explicit ignoreMalformed, + Boolean includeInAll, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { super(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, copyTo); this.ignoreMalformed = ignoreMalformed; + this.includeInAll = includeInAll; } @Override @@ -283,39 +284,6 @@ public class IpFieldMapper extends FieldMapper implements AllFieldMapper.Include return (IpFieldMapper) super.clone(); } - @Override - public Mapper includeInAll(Boolean includeInAll) { - if (includeInAll != null) { - IpFieldMapper clone = clone(); - clone.includeInAll = includeInAll; - return clone; - } else { - return this; - } - } - - @Override - public Mapper includeInAllIfNotSet(Boolean includeInAll) { - if (includeInAll != null && this.includeInAll == null) { - IpFieldMapper clone = clone(); - clone.includeInAll = includeInAll; - return clone; - } else { - return this; - } - } - - @Override - public Mapper unsetIncludeInAll() { - if (includeInAll != null) { - IpFieldMapper clone = clone(); - clone.includeInAll = null; - return clone; - } else { - return this; - } - } - @Override protected void parseCreateField(ParseContext context, List fields) throws IOException { Object addressAsObject; diff --git a/core/src/main/java/org/elasticsearch/index/mapper/KeywordFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/KeywordFieldMapper.java index 1874c86ac77..56c55e1475b 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/KeywordFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/KeywordFieldMapper.java @@ -41,7 +41,7 @@ import static org.elasticsearch.index.mapper.TypeParsers.parseField; /** * A field mapper for keywords. This mapper accepts strings and indexes them as-is. */ -public final class KeywordFieldMapper extends FieldMapper implements AllFieldMapper.IncludeInAll { +public final class KeywordFieldMapper extends FieldMapper { public static final String CONTENT_TYPE = "keyword"; @@ -94,10 +94,9 @@ public final class KeywordFieldMapper extends FieldMapper implements AllFieldMap @Override public KeywordFieldMapper build(BuilderContext context) { setupFieldType(context); - KeywordFieldMapper fieldMapper = new KeywordFieldMapper( - name, fieldType, defaultFieldType, ignoreAbove, + return new KeywordFieldMapper( + name, fieldType, defaultFieldType, ignoreAbove, includeInAll, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); - return fieldMapper.includeInAll(includeInAll); } } @@ -177,10 +176,11 @@ public final class KeywordFieldMapper extends FieldMapper implements AllFieldMap private int ignoreAbove; protected KeywordFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, - int ignoreAbove, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { + int ignoreAbove, Boolean includeInAll, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { super(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, copyTo); assert fieldType.indexOptions().compareTo(IndexOptions.DOCS_AND_FREQS) <= 0; this.ignoreAbove = ignoreAbove; + this.includeInAll = includeInAll; } /** Values that have more chars than the return value of this method will @@ -200,39 +200,6 @@ public final class KeywordFieldMapper extends FieldMapper implements AllFieldMap return includeInAll; } - @Override - public KeywordFieldMapper includeInAll(Boolean includeInAll) { - if (includeInAll != null) { - KeywordFieldMapper clone = clone(); - clone.includeInAll = includeInAll; - return clone; - } else { - return this; - } - } - - @Override - public KeywordFieldMapper includeInAllIfNotSet(Boolean includeInAll) { - if (includeInAll != null && this.includeInAll == null) { - KeywordFieldMapper clone = clone(); - clone.includeInAll = includeInAll; - return clone; - } else { - return this; - } - } - - @Override - public KeywordFieldMapper unsetIncludeInAll() { - if (includeInAll != null) { - KeywordFieldMapper clone = clone(); - clone.includeInAll = null; - return clone; - } else { - return this; - } - } - @Override protected void parseCreateField(ParseContext context, List fields) throws IOException { final String value; diff --git a/core/src/main/java/org/elasticsearch/index/mapper/LegacyByteFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/LegacyByteFieldMapper.java index a7f8f85f8f5..2c63806ebbe 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/LegacyByteFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/LegacyByteFieldMapper.java @@ -75,9 +75,8 @@ public class LegacyByteFieldMapper extends LegacyNumberFieldMapper { throw new IllegalStateException("Cannot use legacy numeric types after 5.0"); } setupFieldType(context); - LegacyByteFieldMapper fieldMapper = new LegacyByteFieldMapper(name, fieldType, defaultFieldType, ignoreMalformed(context), - coerce(context), context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); - return (LegacyByteFieldMapper) fieldMapper.includeInAll(includeInAll); + return new LegacyByteFieldMapper(name, fieldType, defaultFieldType, ignoreMalformed(context), + coerce(context), includeInAll, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); } @Override @@ -176,9 +175,9 @@ public class LegacyByteFieldMapper extends LegacyNumberFieldMapper { } protected LegacyByteFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, - Explicit ignoreMalformed, Explicit coerce, + Explicit ignoreMalformed, Explicit coerce, Boolean includeInAll, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { - super(simpleName, fieldType, defaultFieldType, ignoreMalformed, coerce, indexSettings, multiFields, copyTo); + super(simpleName, fieldType, defaultFieldType, ignoreMalformed, coerce, includeInAll, indexSettings, multiFields, copyTo); } @Override diff --git a/core/src/main/java/org/elasticsearch/index/mapper/LegacyDateFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/LegacyDateFieldMapper.java index 58fbe2895dd..29689d06dff 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/LegacyDateFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/LegacyDateFieldMapper.java @@ -36,7 +36,6 @@ import org.elasticsearch.common.joda.DateMathParser; import org.elasticsearch.common.joda.FormatDateTimeFormatter; import org.elasticsearch.common.joda.Joda; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.common.util.LocaleUtils; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; @@ -116,9 +115,8 @@ public class LegacyDateFieldMapper extends LegacyNumberFieldMapper { } setupFieldType(context); fieldType.setNullValue(nullValue); - LegacyDateFieldMapper fieldMapper = new LegacyDateFieldMapper(name, fieldType, defaultFieldType, ignoreMalformed(context), - coerce(context), context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); - return (LegacyDateFieldMapper) fieldMapper.includeInAll(includeInAll); + return new LegacyDateFieldMapper(name, fieldType, defaultFieldType, ignoreMalformed(context), + coerce(context), includeInAll, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); } @Override @@ -467,8 +465,8 @@ public class LegacyDateFieldMapper extends LegacyNumberFieldMapper { } protected LegacyDateFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, Explicit ignoreMalformed,Explicit coerce, - Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { - super(simpleName, fieldType, defaultFieldType, ignoreMalformed, coerce, indexSettings, multiFields, copyTo); + Boolean includeInAll, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { + super(simpleName, fieldType, defaultFieldType, ignoreMalformed, coerce, includeInAll, indexSettings, multiFields, copyTo); } @Override diff --git a/core/src/main/java/org/elasticsearch/index/mapper/LegacyDoubleFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/LegacyDoubleFieldMapper.java index 0e727161191..07e459e8ea9 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/LegacyDoubleFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/LegacyDoubleFieldMapper.java @@ -78,9 +78,8 @@ public class LegacyDoubleFieldMapper extends LegacyNumberFieldMapper { throw new IllegalStateException("Cannot use legacy numeric types after 5.0"); } setupFieldType(context); - LegacyDoubleFieldMapper fieldMapper = new LegacyDoubleFieldMapper(name, fieldType, defaultFieldType, ignoreMalformed(context), coerce(context), - context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); - return (LegacyDoubleFieldMapper) fieldMapper.includeInAll(includeInAll); + return new LegacyDoubleFieldMapper(name, fieldType, defaultFieldType, ignoreMalformed(context), coerce(context), + includeInAll, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); } @Override @@ -187,8 +186,8 @@ public class LegacyDoubleFieldMapper extends LegacyNumberFieldMapper { } protected LegacyDoubleFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, Explicit ignoreMalformed, - Explicit coerce, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { - super(simpleName, fieldType, defaultFieldType, ignoreMalformed, coerce, indexSettings, multiFields, copyTo); + Explicit coerce, Boolean includeInAll, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { + super(simpleName, fieldType, defaultFieldType, ignoreMalformed, coerce, includeInAll, indexSettings, multiFields, copyTo); } @Override diff --git a/core/src/main/java/org/elasticsearch/index/mapper/LegacyFloatFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/LegacyFloatFieldMapper.java index 831ac973dee..3fbc639ea67 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/LegacyFloatFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/LegacyFloatFieldMapper.java @@ -77,9 +77,8 @@ public class LegacyFloatFieldMapper extends LegacyNumberFieldMapper { throw new IllegalStateException("Cannot use legacy numeric types after 5.0"); } setupFieldType(context); - LegacyFloatFieldMapper fieldMapper = new LegacyFloatFieldMapper(name, fieldType, defaultFieldType, ignoreMalformed(context), coerce(context), - context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); - return (LegacyFloatFieldMapper) fieldMapper.includeInAll(includeInAll); + return new LegacyFloatFieldMapper(name, fieldType, defaultFieldType, ignoreMalformed(context), coerce(context), + includeInAll, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); } @Override @@ -171,9 +170,9 @@ public class LegacyFloatFieldMapper extends LegacyNumberFieldMapper { } protected LegacyFloatFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, - Explicit ignoreMalformed, Explicit coerce, + Explicit ignoreMalformed, Explicit coerce, Boolean includeInAll, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { - super(simpleName, fieldType, defaultFieldType, ignoreMalformed, coerce, indexSettings, multiFields, copyTo); + super(simpleName, fieldType, defaultFieldType, ignoreMalformed, coerce, includeInAll, indexSettings, multiFields, copyTo); } @Override diff --git a/core/src/main/java/org/elasticsearch/index/mapper/LegacyIntegerFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/LegacyIntegerFieldMapper.java index 6a840813abe..65b9b65eaf9 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/LegacyIntegerFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/LegacyIntegerFieldMapper.java @@ -81,10 +81,9 @@ public class LegacyIntegerFieldMapper extends LegacyNumberFieldMapper { throw new IllegalStateException("Cannot use legacy numeric types after 5.0"); } setupFieldType(context); - LegacyIntegerFieldMapper fieldMapper = new LegacyIntegerFieldMapper(name, fieldType, defaultFieldType, - ignoreMalformed(context), coerce(context), + return new LegacyIntegerFieldMapper(name, fieldType, defaultFieldType, + ignoreMalformed(context), coerce(context), includeInAll, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); - return (LegacyIntegerFieldMapper) fieldMapper.includeInAll(includeInAll); } @Override protected int maxPrecisionStep() { @@ -175,9 +174,9 @@ public class LegacyIntegerFieldMapper extends LegacyNumberFieldMapper { } protected LegacyIntegerFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, - Explicit ignoreMalformed, Explicit coerce, + Explicit ignoreMalformed, Explicit coerce, Boolean includeInAll, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { - super(simpleName, fieldType, defaultFieldType, ignoreMalformed, coerce, indexSettings, multiFields, copyTo); + super(simpleName, fieldType, defaultFieldType, ignoreMalformed, coerce, includeInAll, indexSettings, multiFields, copyTo); } @Override diff --git a/core/src/main/java/org/elasticsearch/index/mapper/LegacyIpFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/LegacyIpFieldMapper.java index 4c76e3ec82a..699124a4c05 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/LegacyIpFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/LegacyIpFieldMapper.java @@ -117,9 +117,8 @@ public class LegacyIpFieldMapper extends LegacyNumberFieldMapper { throw new IllegalStateException("Cannot use legacy numeric types after 5.0"); } setupFieldType(context); - LegacyIpFieldMapper fieldMapper = new LegacyIpFieldMapper(name, fieldType, defaultFieldType, ignoreMalformed(context), - coerce(context), context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); - return (LegacyIpFieldMapper) fieldMapper.includeInAll(includeInAll); + return new LegacyIpFieldMapper(name, fieldType, defaultFieldType, ignoreMalformed(context), + coerce(context), includeInAll, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); } @Override @@ -267,9 +266,9 @@ public class LegacyIpFieldMapper extends LegacyNumberFieldMapper { } protected LegacyIpFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, - Explicit ignoreMalformed, Explicit coerce, + Explicit ignoreMalformed, Explicit coerce, Boolean includeInAll, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { - super(simpleName, fieldType, defaultFieldType, ignoreMalformed, coerce, indexSettings, multiFields, copyTo); + super(simpleName, fieldType, defaultFieldType, ignoreMalformed, coerce, includeInAll, indexSettings, multiFields, copyTo); } private static long parseValue(Object value) { diff --git a/core/src/main/java/org/elasticsearch/index/mapper/LegacyLongFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/LegacyLongFieldMapper.java index b1e156e263e..4661d1cd365 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/LegacyLongFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/LegacyLongFieldMapper.java @@ -81,9 +81,9 @@ public class LegacyLongFieldMapper extends LegacyNumberFieldMapper { throw new IllegalStateException("Cannot use legacy numeric types after 5.0"); } setupFieldType(context); - LegacyLongFieldMapper fieldMapper = new LegacyLongFieldMapper(name, fieldType, defaultFieldType, - ignoreMalformed(context), coerce(context), context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); - return (LegacyLongFieldMapper) fieldMapper.includeInAll(includeInAll); + return new LegacyLongFieldMapper(name, fieldType, defaultFieldType, + ignoreMalformed(context), coerce(context), includeInAll, context.indexSettings(), + multiFieldsBuilder.build(this, context), copyTo); } @Override @@ -175,9 +175,9 @@ public class LegacyLongFieldMapper extends LegacyNumberFieldMapper { } protected LegacyLongFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, - Explicit ignoreMalformed, Explicit coerce, + Explicit ignoreMalformed, Explicit coerce, Boolean includeInAll, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { - super(simpleName, fieldType, defaultFieldType, ignoreMalformed, coerce, indexSettings, multiFields, copyTo); + super(simpleName, fieldType, defaultFieldType, ignoreMalformed, coerce, includeInAll, indexSettings, multiFields, copyTo); } @Override diff --git a/core/src/main/java/org/elasticsearch/index/mapper/LegacyNumberFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/LegacyNumberFieldMapper.java index b1d3ead8464..f377883aa24 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/LegacyNumberFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/LegacyNumberFieldMapper.java @@ -40,7 +40,7 @@ import org.joda.time.DateTimeZone; /** * */ -public abstract class LegacyNumberFieldMapper extends FieldMapper implements AllFieldMapper.IncludeInAll { +public abstract class LegacyNumberFieldMapper extends FieldMapper { // this is private since it has a different default private static final Setting COERCE_SETTING = Setting.boolSetting("index.mapping.coerce", true, Property.IndexScope); @@ -158,12 +158,13 @@ public abstract class LegacyNumberFieldMapper extends FieldMapper implements All protected Explicit coerce; protected LegacyNumberFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, - Explicit ignoreMalformed, Explicit coerce, Settings indexSettings, - MultiFields multiFields, CopyTo copyTo) { + Explicit ignoreMalformed, Explicit coerce, Boolean includeInAll, + Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { super(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, copyTo); assert fieldType.tokenized() == false; this.ignoreMalformed = ignoreMalformed; this.coerce = coerce; + this.includeInAll = includeInAll; } @Override @@ -171,39 +172,6 @@ public abstract class LegacyNumberFieldMapper extends FieldMapper implements All return (LegacyNumberFieldMapper) super.clone(); } - @Override - public Mapper includeInAll(Boolean includeInAll) { - if (includeInAll != null) { - LegacyNumberFieldMapper clone = clone(); - clone.includeInAll = includeInAll; - return clone; - } else { - return this; - } - } - - @Override - public Mapper includeInAllIfNotSet(Boolean includeInAll) { - if (includeInAll != null && this.includeInAll == null) { - LegacyNumberFieldMapper clone = clone(); - clone.includeInAll = includeInAll; - return clone; - } else { - return this; - } - } - - @Override - public Mapper unsetIncludeInAll() { - if (includeInAll != null) { - LegacyNumberFieldMapper clone = clone(); - clone.includeInAll = null; - return clone; - } else { - return this; - } - } - @Override protected void parseCreateField(ParseContext context, List fields) throws IOException { RuntimeException e = null; diff --git a/core/src/main/java/org/elasticsearch/index/mapper/LegacyShortFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/LegacyShortFieldMapper.java index 39e00801789..b42ec620aea 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/LegacyShortFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/LegacyShortFieldMapper.java @@ -34,7 +34,6 @@ import org.elasticsearch.Version; import org.elasticsearch.action.fieldstats.FieldStats; import org.elasticsearch.common.Explicit; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.fielddata.IndexFieldData; @@ -78,10 +77,9 @@ public class LegacyShortFieldMapper extends LegacyNumberFieldMapper { throw new IllegalStateException("Cannot use legacy numeric types after 5.0"); } setupFieldType(context); - LegacyShortFieldMapper fieldMapper = new LegacyShortFieldMapper(name, fieldType, defaultFieldType, - ignoreMalformed(context), coerce(context), + return new LegacyShortFieldMapper(name, fieldType, defaultFieldType, + ignoreMalformed(context), coerce(context), includeInAll, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); - return (LegacyShortFieldMapper) fieldMapper.includeInAll(includeInAll); } @Override @@ -181,9 +179,9 @@ public class LegacyShortFieldMapper extends LegacyNumberFieldMapper { } protected LegacyShortFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, - Explicit ignoreMalformed, Explicit coerce, + Explicit ignoreMalformed, Explicit coerce, Boolean includeInAll, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { - super(simpleName, fieldType, defaultFieldType, ignoreMalformed, coerce, indexSettings, multiFields, copyTo); + super(simpleName, fieldType, defaultFieldType, ignoreMalformed, coerce, includeInAll, indexSettings, multiFields, copyTo); } @Override diff --git a/core/src/main/java/org/elasticsearch/index/mapper/LegacyTokenCountFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/LegacyTokenCountFieldMapper.java index 48244ffb377..fd9f6632fe0 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/LegacyTokenCountFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/LegacyTokenCountFieldMapper.java @@ -73,10 +73,9 @@ public class LegacyTokenCountFieldMapper extends LegacyIntegerFieldMapper { throw new IllegalStateException("Cannot use legacy numeric types after 5.0"); } setupFieldType(context); - LegacyTokenCountFieldMapper fieldMapper = new LegacyTokenCountFieldMapper(name, fieldType, defaultFieldType, - ignoreMalformed(context), coerce(context), context.indexSettings(), + return new LegacyTokenCountFieldMapper(name, fieldType, defaultFieldType, + ignoreMalformed(context), coerce(context), includeInAll, context.indexSettings(), analyzer, multiFieldsBuilder.build(this, context), copyTo); - return (LegacyTokenCountFieldMapper) fieldMapper.includeInAll(includeInAll); } @Override @@ -117,8 +116,8 @@ public class LegacyTokenCountFieldMapper extends LegacyIntegerFieldMapper { private NamedAnalyzer analyzer; protected LegacyTokenCountFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, Explicit ignoreMalformed, - Explicit coerce, Settings indexSettings, NamedAnalyzer analyzer, MultiFields multiFields, CopyTo copyTo) { - super(simpleName, fieldType, defaultFieldType, ignoreMalformed, coerce, indexSettings, multiFields, copyTo); + Explicit coerce, Boolean includeInAll, Settings indexSettings, NamedAnalyzer analyzer, MultiFields multiFields, CopyTo copyTo) { + super(simpleName, fieldType, defaultFieldType, ignoreMalformed, coerce, includeInAll, indexSettings, multiFields, copyTo); this.analyzer = analyzer; } diff --git a/core/src/main/java/org/elasticsearch/index/mapper/NumberFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/NumberFieldMapper.java index 6eddf07b979..be6ed23c508 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/NumberFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/NumberFieldMapper.java @@ -60,7 +60,7 @@ import java.util.Map; import java.util.Objects; /** A {@link FieldMapper} for numeric types: byte, short, int, long, float and double. */ -public class NumberFieldMapper extends FieldMapper implements AllFieldMapper.IncludeInAll { +public class NumberFieldMapper extends FieldMapper { // this is private since it has a different default static final Setting COERCE_SETTING = @@ -114,10 +114,8 @@ public class NumberFieldMapper extends FieldMapper implements AllFieldMapper.Inc @Override public NumberFieldMapper build(BuilderContext context) { setupFieldType(context); - NumberFieldMapper fieldMapper = - new NumberFieldMapper(name, fieldType, defaultFieldType, ignoreMalformed(context), - coerce(context), context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); - return (NumberFieldMapper) fieldMapper.includeInAll(includeInAll); + return new NumberFieldMapper(name, fieldType, defaultFieldType, ignoreMalformed(context), + coerce(context), includeInAll, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); } } @@ -871,12 +869,14 @@ public class NumberFieldMapper extends FieldMapper implements AllFieldMapper.Inc MappedFieldType defaultFieldType, Explicit ignoreMalformed, Explicit coerce, + Boolean includeInAll, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { super(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, copyTo); this.ignoreMalformed = ignoreMalformed; this.coerce = coerce; + this.includeInAll = includeInAll; } @Override @@ -894,39 +894,6 @@ public class NumberFieldMapper extends FieldMapper implements AllFieldMapper.Inc return (NumberFieldMapper) super.clone(); } - @Override - public Mapper includeInAll(Boolean includeInAll) { - if (includeInAll != null) { - NumberFieldMapper clone = clone(); - clone.includeInAll = includeInAll; - return clone; - } else { - return this; - } - } - - @Override - public Mapper includeInAllIfNotSet(Boolean includeInAll) { - if (includeInAll != null && this.includeInAll == null) { - NumberFieldMapper clone = clone(); - clone.includeInAll = includeInAll; - return clone; - } else { - return this; - } - } - - @Override - public Mapper unsetIncludeInAll() { - if (includeInAll != null) { - NumberFieldMapper clone = clone(); - clone.includeInAll = null; - return clone; - } else { - return this; - } - } - @Override protected void parseCreateField(ParseContext context, List fields) throws IOException { XContentParser parser = context.parser(); diff --git a/core/src/main/java/org/elasticsearch/index/mapper/ObjectMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/ObjectMapper.java index 9d6dcafc0c0..f3941f9b25f 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/ObjectMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/ObjectMapper.java @@ -25,7 +25,6 @@ import org.apache.lucene.search.TermQuery; import org.apache.lucene.util.BytesRef; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.Nullable; -import org.elasticsearch.common.Strings; import org.elasticsearch.common.collect.CopyOnWriteHashMap; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.ToXContent; @@ -47,7 +46,7 @@ import static org.elasticsearch.common.xcontent.support.XContentMapValues.lenien /** * */ -public class ObjectMapper extends Mapper implements AllFieldMapper.IncludeInAll, Cloneable { +public class ObjectMapper extends Mapper implements Cloneable { public static final String CONTENT_TYPE = "object"; public static final String NESTED_CONTENT_TYPE = "nested"; @@ -154,14 +153,15 @@ public class ObjectMapper extends Mapper implements AllFieldMapper.IncludeInAll, } context.path().remove(); - ObjectMapper objectMapper = createMapper(name, context.path().pathAsText(name), enabled, nested, dynamic, mappers, context.indexSettings()); - objectMapper = objectMapper.includeInAllIfNotSet(includeInAll); + ObjectMapper objectMapper = createMapper(name, context.path().pathAsText(name), enabled, nested, dynamic, + includeInAll, mappers, context.indexSettings()); return (Y) objectMapper; } - protected ObjectMapper createMapper(String name, String fullPath, boolean enabled, Nested nested, Dynamic dynamic, Map mappers, @Nullable Settings settings) { - return new ObjectMapper(name, fullPath, enabled, nested, dynamic, mappers); + protected ObjectMapper createMapper(String name, String fullPath, boolean enabled, Nested nested, Dynamic dynamic, + Boolean includeInAll, Map mappers, @Nullable Settings settings) { + return new ObjectMapper(name, fullPath, enabled, nested, dynamic, includeInAll, mappers); } } @@ -322,12 +322,14 @@ public class ObjectMapper extends Mapper implements AllFieldMapper.IncludeInAll, private volatile CopyOnWriteHashMap mappers; - ObjectMapper(String name, String fullPath, boolean enabled, Nested nested, Dynamic dynamic, Map mappers) { + ObjectMapper(String name, String fullPath, boolean enabled, Nested nested, Dynamic dynamic, + Boolean includeInAll, Map mappers) { super(name); this.fullPath = fullPath; this.enabled = enabled; this.nested = nested; this.dynamic = dynamic; + this.includeInAll = includeInAll; if (mappers == null) { this.mappers = new CopyOnWriteHashMap<>(); } else { @@ -373,68 +375,19 @@ public class ObjectMapper extends Mapper implements AllFieldMapper.IncludeInAll, return mappers.get(field); } - @Override - public ObjectMapper includeInAll(Boolean includeInAll) { - if (includeInAll == null) { - return this; - } - - ObjectMapper clone = clone(); - clone.includeInAll = includeInAll; - // when called from outside, apply this on all the inner mappers - for (Mapper mapper : clone.mappers.values()) { - if (mapper instanceof AllFieldMapper.IncludeInAll) { - clone.putMapper(((AllFieldMapper.IncludeInAll) mapper).includeInAll(includeInAll)); - } - } - return clone; - } - - @Override - public ObjectMapper includeInAllIfNotSet(Boolean includeInAll) { - if (includeInAll == null || this.includeInAll != null) { - return this; - } - - ObjectMapper clone = clone(); - clone.includeInAll = includeInAll; - // when called from outside, apply this on all the inner mappers - for (Mapper mapper : clone.mappers.values()) { - if (mapper instanceof AllFieldMapper.IncludeInAll) { - clone.putMapper(((AllFieldMapper.IncludeInAll) mapper).includeInAllIfNotSet(includeInAll)); - } - } - return clone; - } - - @Override - public ObjectMapper unsetIncludeInAll() { - if (includeInAll == null) { - return this; - } - ObjectMapper clone = clone(); - clone.includeInAll = null; - // when called from outside, apply this on all the inner mappers - for (Mapper mapper : mappers.values()) { - if (mapper instanceof AllFieldMapper.IncludeInAll) { - clone.putMapper(((AllFieldMapper.IncludeInAll) mapper).unsetIncludeInAll()); - } - } - return clone; - } - public Nested nested() { return this.nested; } + public Boolean includeInAll() { + return includeInAll; + } + public Query nestedTypeFilter() { return this.nestedTypeFilter; } protected void putMapper(Mapper mapper) { - if (mapper instanceof AllFieldMapper.IncludeInAll) { - mapper = ((AllFieldMapper.IncludeInAll) mapper).includeInAllIfNotSet(includeInAll); - } mappers = mappers.copyAndPut(mapper.simpleName(), mapper); } diff --git a/core/src/main/java/org/elasticsearch/index/mapper/ParseContext.java b/core/src/main/java/org/elasticsearch/index/mapper/ParseContext.java index 477f88cc53c..8a2aca97e68 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/ParseContext.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/ParseContext.java @@ -423,6 +423,22 @@ public abstract class ParseContext { public abstract DocumentMapperParser docMapperParser(); + /** Return a view of this {@link ParseContext} that changes the return + * value of {@link #getIncludeInAllDefault()}. */ + public final ParseContext setIncludeInAllDefault(boolean includeInAll) { + return new FilterParseContext(this) { + @Override + public Boolean getIncludeInAllDefault() { + return includeInAll; + } + }; + } + + /** Whether field values should be added to the _all field by default. */ + public Boolean getIncludeInAllDefault() { + return null; + } + /** * Return a new context that will be within a copy-to operation. */ @@ -526,7 +542,7 @@ public abstract class ParseContext { * is false. If its enabled, then will return true only if the specific flag is null or * its actual value (so, if not set, defaults to "true") and the field is indexed. */ - private boolean includeInAll(Boolean specificIncludeInAll, boolean indexed) { + private boolean includeInAll(Boolean includeInAll, boolean indexed) { if (isWithinCopyTo()) { return false; } @@ -536,11 +552,14 @@ public abstract class ParseContext { if (!docMapper().allFieldMapper().enabled()) { return false; } + if (includeInAll == null) { + includeInAll = getIncludeInAllDefault(); + } // not explicitly set - if (specificIncludeInAll == null) { + if (includeInAll == null) { return indexed; } - return specificIncludeInAll; + return includeInAll; } public abstract AllEntries allEntries(); diff --git a/core/src/main/java/org/elasticsearch/index/mapper/RootObjectMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/RootObjectMapper.java index f419d525e1a..eed1ac0e8d1 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/RootObjectMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/RootObjectMapper.java @@ -100,7 +100,8 @@ public class RootObjectMapper extends ObjectMapper { @Override - protected ObjectMapper createMapper(String name, String fullPath, boolean enabled, Nested nested, Dynamic dynamic, Map mappers, @Nullable Settings settings) { + protected ObjectMapper createMapper(String name, String fullPath, boolean enabled, Nested nested, Dynamic dynamic, + Boolean includeInAll, Map mappers, @Nullable Settings settings) { assert !nested.isNested(); FormatDateTimeFormatter[] dates = null; if (dynamicDateTimeFormatters == null) { @@ -111,7 +112,7 @@ public class RootObjectMapper extends ObjectMapper { } else { dates = dynamicDateTimeFormatters.toArray(new FormatDateTimeFormatter[dynamicDateTimeFormatters.size()]); } - return new RootObjectMapper(name, enabled, dynamic, mappers, + return new RootObjectMapper(name, enabled, dynamic, includeInAll, mappers, dates, dynamicTemplates.toArray(new DynamicTemplate[dynamicTemplates.size()]), dateDetection, numericDetection); @@ -207,9 +208,9 @@ public class RootObjectMapper extends ObjectMapper { private volatile DynamicTemplate dynamicTemplates[]; - RootObjectMapper(String name, boolean enabled, Dynamic dynamic, Map mappers, + RootObjectMapper(String name, boolean enabled, Dynamic dynamic, Boolean includeInAll, Map mappers, FormatDateTimeFormatter[] dynamicDateTimeFormatters, DynamicTemplate dynamicTemplates[], boolean dateDetection, boolean numericDetection) { - super(name, name, enabled, Nested.NO, dynamic, mappers); + super(name, name, enabled, Nested.NO, dynamic, includeInAll, mappers); this.dynamicTemplates = dynamicTemplates; this.dynamicDateTimeFormatters = dynamicDateTimeFormatters; this.dateDetection = dateDetection; diff --git a/core/src/main/java/org/elasticsearch/index/mapper/ScaledFloatFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/ScaledFloatFieldMapper.java index 8c50e868020..b7276e4e114 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/ScaledFloatFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/ScaledFloatFieldMapper.java @@ -65,7 +65,7 @@ import java.util.Map; /** A {@link FieldMapper} for scaled floats. Values are internally multiplied * by a scaling factor and rounded to the closest long. */ -public class ScaledFloatFieldMapper extends FieldMapper implements AllFieldMapper.IncludeInAll { +public class ScaledFloatFieldMapper extends FieldMapper { public static final String CONTENT_TYPE = "scaled_float"; // use the same default as numbers @@ -124,10 +124,8 @@ public class ScaledFloatFieldMapper extends FieldMapper implements AllFieldMappe throw new IllegalArgumentException("Field [" + name + "] misses required parameter [scaling_factor]"); } setupFieldType(context); - ScaledFloatFieldMapper fieldMapper = - new ScaledFloatFieldMapper(name, fieldType, defaultFieldType, ignoreMalformed(context), - coerce(context), context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); - return (ScaledFloatFieldMapper) fieldMapper.includeInAll(includeInAll); + return new ScaledFloatFieldMapper(name, fieldType, defaultFieldType, ignoreMalformed(context), + coerce(context), includeInAll, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); } } @@ -336,6 +334,7 @@ public class ScaledFloatFieldMapper extends FieldMapper implements AllFieldMappe MappedFieldType defaultFieldType, Explicit ignoreMalformed, Explicit coerce, + Boolean includeInAll, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { @@ -346,6 +345,7 @@ public class ScaledFloatFieldMapper extends FieldMapper implements AllFieldMappe } this.ignoreMalformed = ignoreMalformed; this.coerce = coerce; + this.includeInAll = includeInAll; } @Override @@ -363,39 +363,6 @@ public class ScaledFloatFieldMapper extends FieldMapper implements AllFieldMappe return (ScaledFloatFieldMapper) super.clone(); } - @Override - public Mapper includeInAll(Boolean includeInAll) { - if (includeInAll != null) { - ScaledFloatFieldMapper clone = clone(); - clone.includeInAll = includeInAll; - return clone; - } else { - return this; - } - } - - @Override - public Mapper includeInAllIfNotSet(Boolean includeInAll) { - if (includeInAll != null && this.includeInAll == null) { - ScaledFloatFieldMapper clone = clone(); - clone.includeInAll = includeInAll; - return clone; - } else { - return this; - } - } - - @Override - public Mapper unsetIncludeInAll() { - if (includeInAll != null) { - ScaledFloatFieldMapper clone = clone(); - clone.includeInAll = null; - return clone; - } else { - return this; - } - } - @Override protected void parseCreateField(ParseContext context, List fields) throws IOException { XContentParser parser = context.parser(); diff --git a/core/src/main/java/org/elasticsearch/index/mapper/StringFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/StringFieldMapper.java index 887a40fe70b..5dc6efb28f7 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/StringFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/StringFieldMapper.java @@ -49,7 +49,7 @@ import java.util.Set; import static org.apache.lucene.index.IndexOptions.NONE; import static org.elasticsearch.index.mapper.TypeParsers.parseTextField; -public class StringFieldMapper extends FieldMapper implements AllFieldMapper.IncludeInAll { +public class StringFieldMapper extends FieldMapper { public static final String CONTENT_TYPE = "string"; private static final int POSITION_INCREMENT_GAP_USE_ANALYZER = -1; @@ -177,10 +177,9 @@ public class StringFieldMapper extends FieldMapper implements AllFieldMapper.Inc fieldType.setSearchQuoteAnalyzer(new NamedAnalyzer(fieldType.searchQuoteAnalyzer(), positionIncrementGap)); } setupFieldType(context); - StringFieldMapper fieldMapper = new StringFieldMapper( - name, fieldType(), defaultFieldType, positionIncrementGap, ignoreAbove, + return new StringFieldMapper( + name, fieldType(), defaultFieldType, positionIncrementGap, ignoreAbove, includeInAll, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); - return fieldMapper.includeInAll(includeInAll); } } @@ -488,7 +487,7 @@ public class StringFieldMapper extends FieldMapper implements AllFieldMapper.Inc private int ignoreAbove; protected StringFieldMapper(String simpleName, StringFieldType fieldType, MappedFieldType defaultFieldType, - int positionIncrementGap, int ignoreAbove, + int positionIncrementGap, int ignoreAbove, Boolean includeInAll, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { super(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, copyTo); if (Version.indexCreated(indexSettings).onOrAfter(Version.V_5_0_0_alpha1)) { @@ -506,6 +505,7 @@ public class StringFieldMapper extends FieldMapper implements AllFieldMapper.Inc } this.positionIncrementGap = positionIncrementGap; this.ignoreAbove = ignoreAbove; + this.includeInAll = includeInAll; } @Override @@ -513,39 +513,6 @@ public class StringFieldMapper extends FieldMapper implements AllFieldMapper.Inc return (StringFieldMapper) super.clone(); } - @Override - public StringFieldMapper includeInAll(Boolean includeInAll) { - if (includeInAll != null) { - StringFieldMapper clone = clone(); - clone.includeInAll = includeInAll; - return clone; - } else { - return this; - } - } - - @Override - public StringFieldMapper includeInAllIfNotSet(Boolean includeInAll) { - if (includeInAll != null && this.includeInAll == null) { - StringFieldMapper clone = clone(); - clone.includeInAll = includeInAll; - return clone; - } else { - return this; - } - } - - @Override - public StringFieldMapper unsetIncludeInAll() { - if (includeInAll != null) { - StringFieldMapper clone = clone(); - clone.includeInAll = null; - return clone; - } else { - return this; - } - } - @Override protected boolean customBoost() { return true; diff --git a/core/src/main/java/org/elasticsearch/index/mapper/TextFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/TextFieldMapper.java index 987586db721..6d677d50786 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/TextFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/TextFieldMapper.java @@ -38,7 +38,7 @@ import java.util.Objects; import static org.elasticsearch.index.mapper.TypeParsers.parseTextField; /** A {@link FieldMapper} for full-text fields. */ -public class TextFieldMapper extends FieldMapper implements AllFieldMapper.IncludeInAll { +public class TextFieldMapper extends FieldMapper { public static final String CONTENT_TYPE = "text"; private static final int POSITION_INCREMENT_GAP_USE_ANALYZER = -1; @@ -120,10 +120,9 @@ public class TextFieldMapper extends FieldMapper implements AllFieldMapper.Inclu fieldType.setSearchQuoteAnalyzer(new NamedAnalyzer(fieldType.searchQuoteAnalyzer(), positionIncrementGap)); } setupFieldType(context); - TextFieldMapper fieldMapper = new TextFieldMapper( - name, fieldType, defaultFieldType, positionIncrementGap, + return new TextFieldMapper( + name, fieldType, defaultFieldType, positionIncrementGap, includeInAll, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); - return fieldMapper.includeInAll(includeInAll); } } @@ -297,7 +296,7 @@ public class TextFieldMapper extends FieldMapper implements AllFieldMapper.Inclu private int positionIncrementGap; protected TextFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, - int positionIncrementGap, + int positionIncrementGap, Boolean includeInAll, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { super(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, copyTo); assert fieldType.tokenized(); @@ -306,6 +305,7 @@ public class TextFieldMapper extends FieldMapper implements AllFieldMapper.Inclu throw new IllegalArgumentException("Cannot enable fielddata on a [text] field that is not indexed: [" + name() + "]"); } this.positionIncrementGap = positionIncrementGap; + this.includeInAll = includeInAll; } @Override @@ -318,39 +318,6 @@ public class TextFieldMapper extends FieldMapper implements AllFieldMapper.Inclu return includeInAll; } - @Override - public TextFieldMapper includeInAll(Boolean includeInAll) { - if (includeInAll != null) { - TextFieldMapper clone = clone(); - clone.includeInAll = includeInAll; - return clone; - } else { - return this; - } - } - - @Override - public TextFieldMapper includeInAllIfNotSet(Boolean includeInAll) { - if (includeInAll != null && this.includeInAll == null) { - TextFieldMapper clone = clone(); - clone.includeInAll = includeInAll; - return clone; - } else { - return this; - } - } - - @Override - public TextFieldMapper unsetIncludeInAll() { - if (includeInAll != null) { - TextFieldMapper clone = clone(); - clone.includeInAll = null; - return clone; - } else { - return this; - } - } - public int getPositionIncrementGap() { return this.positionIncrementGap; } diff --git a/core/src/test/java/org/elasticsearch/index/mapper/DocumentParserTests.java b/core/src/test/java/org/elasticsearch/index/mapper/DocumentParserTests.java index 3320b90a991..d650692e8ea 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/DocumentParserTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/DocumentParserTests.java @@ -19,14 +19,20 @@ package org.elasticsearch.index.mapper; +import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; +import java.util.HashSet; import java.util.List; +import java.util.Set; +import org.apache.lucene.index.IndexableField; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.compress.CompressedXContent; +import org.elasticsearch.common.lucene.all.AllField; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.index.IndexService; @@ -1154,4 +1160,48 @@ public class DocumentParserTests extends ESSingleNodeTestCase { assertThat(doc.rootDoc().get("type.test2"), equalTo("value2")); assertThat(doc.rootDoc().get("type.inner.inner_field"), equalTo("inner_value")); } + + public void testIncludeInAllPropagation() throws IOException { + String defaultMapping = XContentFactory.jsonBuilder().startObject() + .startObject("type") + .field("dynamic", "strict") + .startObject("properties") + .startObject("a") + .field("type", "keyword") + .endObject() + .startObject("o") + .field("include_in_all", false) + .startObject("properties") + .startObject("a") + .field("type", "keyword") + .endObject() + .startObject("o") + .field("include_in_all", true) + .startObject("properties") + .startObject("a") + .field("type", "keyword") + .endObject() + .endObject() + .endObject() + .endObject() + .endObject() + .endObject() + .endObject().endObject().string(); + DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser().parse("type", new CompressedXContent(defaultMapping)); + ParsedDocument doc = defaultMapper.parse("test", "type", "1", XContentFactory.jsonBuilder() + .startObject() + .field("a", "b") + .startObject("o") + .field("a", "c") + .startObject("o") + .field("a", "d") + .endObject() + .endObject() + .endObject().bytes()); + Set values = new HashSet<>(); + for (IndexableField f : doc.rootDoc().getFields("_all")) { + values.add(f.stringValue()); + } + assertEquals(new HashSet<>(Arrays.asList("b", "d")), values); + } }