diff --git a/modules/aggs-matrix-stats/src/test/java/org/elasticsearch/search/aggregations/matrix/stats/MatrixStatsAggregatorTests.java b/modules/aggs-matrix-stats/src/test/java/org/elasticsearch/search/aggregations/matrix/stats/MatrixStatsAggregatorTests.java index d2170a13dad12..c6f33aa15db1d 100644 --- a/modules/aggs-matrix-stats/src/test/java/org/elasticsearch/search/aggregations/matrix/stats/MatrixStatsAggregatorTests.java +++ b/modules/aggs-matrix-stats/src/test/java/org/elasticsearch/search/aggregations/matrix/stats/MatrixStatsAggregatorTests.java @@ -42,8 +42,7 @@ public class MatrixStatsAggregatorTests extends AggregatorTestCase { public void testNoData() throws Exception { MappedFieldType ft = - new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE); - ft.setName("field"); + new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.DOUBLE); try (Directory directory = newDirectory(); RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) { @@ -62,9 +61,7 @@ public void testNoData() throws Exception { } public void testUnmapped() throws Exception { - MappedFieldType ft = - new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE); - ft.setName("field"); + MappedFieldType ft = new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.DOUBLE); try (Directory directory = newDirectory(); RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) { @@ -84,11 +81,9 @@ public void testUnmapped() throws Exception { public void testTwoFields() throws Exception { String fieldA = "a"; - MappedFieldType ftA = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE); - ftA.setName(fieldA); + MappedFieldType ftA = new NumberFieldMapper.NumberFieldType(fieldA, NumberFieldMapper.NumberType.DOUBLE); String fieldB = "b"; - MappedFieldType ftB = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE); - ftB.setName(fieldB); + MappedFieldType ftB = new NumberFieldMapper.NumberFieldType(fieldB, NumberFieldMapper.NumberType.DOUBLE); try (Directory directory = newDirectory(); RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) { @@ -123,11 +118,9 @@ public void testTwoFields() throws Exception { public void testTwoFieldsReduce() throws Exception { String fieldA = "a"; - MappedFieldType ftA = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE); - ftA.setName(fieldA); + MappedFieldType ftA = new NumberFieldMapper.NumberFieldType(fieldA, NumberFieldMapper.NumberType.DOUBLE); String fieldB = "b"; - MappedFieldType ftB = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE); - ftB.setName(fieldB); + MappedFieldType ftB = new NumberFieldMapper.NumberFieldType(fieldB, NumberFieldMapper.NumberType.DOUBLE); try (Directory directory = newDirectory(); RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) { diff --git a/modules/lang-expression/src/test/java/org/elasticsearch/script/expression/ExpressionFieldScriptTests.java b/modules/lang-expression/src/test/java/org/elasticsearch/script/expression/ExpressionFieldScriptTests.java index 03aeca782b53a..aa4daf3179adb 100644 --- a/modules/lang-expression/src/test/java/org/elasticsearch/script/expression/ExpressionFieldScriptTests.java +++ b/modules/lang-expression/src/test/java/org/elasticsearch/script/expression/ExpressionFieldScriptTests.java @@ -47,7 +47,7 @@ public class ExpressionFieldScriptTests extends ESTestCase { public void setUp() throws Exception { super.setUp(); - NumberFieldMapper.NumberFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE); + NumberFieldMapper.NumberFieldType fieldType = new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.DOUBLE); MapperService mapperService = mock(MapperService.class); when(mapperService.fieldType("field")).thenReturn(fieldType); when(mapperService.fieldType("alias")).thenReturn(fieldType); diff --git a/modules/lang-expression/src/test/java/org/elasticsearch/script/expression/ExpressionNumberSortScriptTests.java b/modules/lang-expression/src/test/java/org/elasticsearch/script/expression/ExpressionNumberSortScriptTests.java index 057ebdb3af5a1..007f4d608c82d 100644 --- a/modules/lang-expression/src/test/java/org/elasticsearch/script/expression/ExpressionNumberSortScriptTests.java +++ b/modules/lang-expression/src/test/java/org/elasticsearch/script/expression/ExpressionNumberSortScriptTests.java @@ -47,7 +47,7 @@ public class ExpressionNumberSortScriptTests extends ESTestCase { public void setUp() throws Exception { super.setUp(); - NumberFieldType fieldType = new NumberFieldType(NumberType.DOUBLE); + NumberFieldType fieldType = new NumberFieldType("field", NumberType.DOUBLE); MapperService mapperService = mock(MapperService.class); when(mapperService.fieldType("field")).thenReturn(fieldType); when(mapperService.fieldType("alias")).thenReturn(fieldType); diff --git a/modules/lang-expression/src/test/java/org/elasticsearch/script/expression/ExpressionTermsSetQueryTests.java b/modules/lang-expression/src/test/java/org/elasticsearch/script/expression/ExpressionTermsSetQueryTests.java index 43a3e26259b97..fd279a9fa14e9 100644 --- a/modules/lang-expression/src/test/java/org/elasticsearch/script/expression/ExpressionTermsSetQueryTests.java +++ b/modules/lang-expression/src/test/java/org/elasticsearch/script/expression/ExpressionTermsSetQueryTests.java @@ -47,7 +47,7 @@ public class ExpressionTermsSetQueryTests extends ESTestCase { public void setUp() throws Exception { super.setUp(); - NumberFieldType fieldType = new NumberFieldType(NumberType.DOUBLE); + NumberFieldType fieldType = new NumberFieldType("field", NumberType.DOUBLE); MapperService mapperService = mock(MapperService.class); when(mapperService.fieldType("field")).thenReturn(fieldType); when(mapperService.fieldType("alias")).thenReturn(fieldType); diff --git a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/RankFeatureFieldMapper.java b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/RankFeatureFieldMapper.java index 73900547ecf1c..0b878d610f759 100644 --- a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/RankFeatureFieldMapper.java +++ b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/RankFeatureFieldMapper.java @@ -20,6 +20,7 @@ package org.elasticsearch.index.mapper; import org.apache.lucene.document.FeatureField; +import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.Term; import org.apache.lucene.search.Query; @@ -36,7 +37,6 @@ import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Objects; /** * A {@link FieldMapper} that exposes Lucene's {@link FeatureField}. @@ -46,12 +46,11 @@ public class RankFeatureFieldMapper extends FieldMapper { public static final String CONTENT_TYPE = "rank_feature"; public static class Defaults { - public static final MappedFieldType FIELD_TYPE = new RankFeatureFieldType(); + public static final FieldType FIELD_TYPE = new FieldType(); static { FIELD_TYPE.setTokenized(false); FIELD_TYPE.setIndexOptions(IndexOptions.NONE); - FIELD_TYPE.setHasDocValues(false); FIELD_TYPE.setOmitNorms(true); FIELD_TYPE.freeze(); } @@ -59,27 +58,22 @@ public static class Defaults { public static class Builder extends FieldMapper.Builder { + private boolean positiveScoreImpact = true; + public Builder(String name) { - super(name, Defaults.FIELD_TYPE, Defaults.FIELD_TYPE); + super(name, Defaults.FIELD_TYPE); builder = this; } - @Override - public RankFeatureFieldType fieldType() { - return (RankFeatureFieldType) super.fieldType(); - } - public Builder positiveScoreImpact(boolean v) { - fieldType().setPositiveScoreImpact(v); + this.positiveScoreImpact = v; return builder; } @Override public RankFeatureFieldMapper build(BuilderContext context) { - setupFieldType(context); - return new RankFeatureFieldMapper( - name, fieldType, defaultFieldType, - context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); + return new RankFeatureFieldMapper(name, fieldType, new RankFeatureFieldType(buildFullName(context), meta, positiveScoreImpact), + context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo, positiveScoreImpact); } } @@ -102,9 +96,11 @@ public Mapper.Builder parse(String name, Map node, ParserCont public static final class RankFeatureFieldType extends MappedFieldType { - private boolean positiveScoreImpact = true; + private final boolean positiveScoreImpact; - public RankFeatureFieldType() { + public RankFeatureFieldType(String name, Map meta, boolean positiveScoreImpact) { + super(name, true, false, meta); + this.positiveScoreImpact = positiveScoreImpact; setIndexAnalyzer(Lucene.KEYWORD_ANALYZER); setSearchAnalyzer(Lucene.KEYWORD_ANALYZER); } @@ -118,22 +114,6 @@ public RankFeatureFieldType clone() { return new RankFeatureFieldType(this); } - @Override - public boolean equals(Object o) { - if (super.equals(o) == false) { - return false; - } - RankFeatureFieldType other = (RankFeatureFieldType) o; - return Objects.equals(positiveScoreImpact, other.positiveScoreImpact); - } - - @Override - public int hashCode() { - int h = super.hashCode(); - h = 31 * h + Objects.hashCode(positiveScoreImpact); - return h; - } - @Override public String typeName() { return CONTENT_TYPE; @@ -143,11 +123,6 @@ public boolean positiveScoreImpact() { return positiveScoreImpact; } - public void setPositiveScoreImpact(boolean positiveScoreImpact) { - checkIfFrozen(); - this.positiveScoreImpact = positiveScoreImpact; - } - @Override public Query existsQuery(QueryShardContext context) { return new TermQuery(new Term("_feature", name())); @@ -164,10 +139,14 @@ public Query termQuery(Object value, QueryShardContext context) { } } - private RankFeatureFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, - Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { - super(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, copyTo); + private final boolean positiveScoreImpact; + + private RankFeatureFieldMapper(String simpleName, FieldType fieldType, MappedFieldType mappedFieldType, + Settings indexSettings, MultiFields multiFields, CopyTo copyTo, + boolean positiveScoreImpact) { + super(simpleName, fieldType, mappedFieldType, indexSettings, multiFields, copyTo); assert fieldType.indexOptions().compareTo(IndexOptions.DOCS_AND_FREQS) <= 0; + this.positiveScoreImpact = positiveScoreImpact; } @Override @@ -202,7 +181,7 @@ protected void parseCreateField(ParseContext context) throws IOException { name() + "] in the same document"); } - if (fieldType().positiveScoreImpact() == false) { + if (positiveScoreImpact == false) { value = 1 / value; } @@ -218,15 +197,19 @@ protected String contentType() { protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException { super.doXContentBody(builder, includeDefaults, params); - if (includeDefaults || fieldType().positiveScoreImpact() == false) { - builder.field("positive_score_impact", fieldType().positiveScoreImpact()); + if (includeDefaults || positiveScoreImpact == false) { + builder.field("positive_score_impact", positiveScoreImpact); } } + @Override + protected boolean docValuesByDefault() { + return false; + } + @Override protected void mergeOptions(FieldMapper other, List conflicts) { - RankFeatureFieldType ft = (RankFeatureFieldType) other.fieldType(); - if (fieldType().positiveScoreImpact != ft.positiveScoreImpact()) { + if (positiveScoreImpact != ((RankFeatureFieldMapper)other).positiveScoreImpact) { conflicts.add("mapper [" + name() + "] has different [positive_score_impact] values"); } } diff --git a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/RankFeatureMetaFieldMapper.java b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/RankFeatureMetaFieldMapper.java index 9a30956c5802f..4ceff0c7b58ed 100644 --- a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/RankFeatureMetaFieldMapper.java +++ b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/RankFeatureMetaFieldMapper.java @@ -19,14 +19,15 @@ package org.elasticsearch.index.mapper; +import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.search.Query; -import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.index.query.QueryShardContext; import java.io.IOException; +import java.util.Collections; import java.util.Map; /** @@ -41,30 +42,26 @@ public class RankFeatureMetaFieldMapper extends MetadataFieldMapper { public static final String CONTENT_TYPE = "_feature"; public static class Defaults { - public static final MappedFieldType FIELD_TYPE = new RankFeatureMetaFieldType(); + public static final FieldType FIELD_TYPE = new FieldType(); static { FIELD_TYPE.setIndexOptions(IndexOptions.DOCS_AND_FREQS); FIELD_TYPE.setTokenized(true); FIELD_TYPE.setStored(false); FIELD_TYPE.setOmitNorms(true); - FIELD_TYPE.setIndexAnalyzer(Lucene.KEYWORD_ANALYZER); - FIELD_TYPE.setSearchAnalyzer(Lucene.KEYWORD_ANALYZER); - FIELD_TYPE.setName(NAME); FIELD_TYPE.freeze(); } } public static class Builder extends MetadataFieldMapper.Builder { - public Builder(MappedFieldType existing) { - super(NAME, existing == null ? Defaults.FIELD_TYPE : existing, Defaults.FIELD_TYPE); + public Builder() { + super(NAME, Defaults.FIELD_TYPE); } @Override public RankFeatureMetaFieldMapper build(BuilderContext context) { - setupFieldType(context); - return new RankFeatureMetaFieldMapper(fieldType, context.indexSettings()); + return new RankFeatureMetaFieldMapper(context.indexSettings()); } } @@ -72,19 +69,22 @@ public static class TypeParser implements MetadataFieldMapper.TypeParser { @Override public MetadataFieldMapper.Builder parse(String name, Map node, ParserContext parserContext) throws MapperParsingException { - return new Builder(parserContext.mapperService().fieldType(NAME)); + return new Builder(); } @Override public MetadataFieldMapper getDefault(ParserContext context) { final Settings indexSettings = context.mapperService().getIndexSettings().getSettings(); - return new RankFeatureMetaFieldMapper(indexSettings, Defaults.FIELD_TYPE); + return new RankFeatureMetaFieldMapper(indexSettings); } } public static final class RankFeatureMetaFieldType extends MappedFieldType { - public RankFeatureMetaFieldType() { + public static final RankFeatureMetaFieldType INSTANCE = new RankFeatureMetaFieldType(); + + private RankFeatureMetaFieldType() { + super(NAME, false, false, Collections.emptyMap()); } protected RankFeatureMetaFieldType(RankFeatureMetaFieldType ref) { @@ -112,12 +112,8 @@ public Query termQuery(Object value, QueryShardContext context) { } } - private RankFeatureMetaFieldMapper(Settings indexSettings, MappedFieldType existing) { - this(existing.clone(), indexSettings); - } - - private RankFeatureMetaFieldMapper(MappedFieldType fieldType, Settings indexSettings) { - super(NAME, fieldType, Defaults.FIELD_TYPE, indexSettings); + private RankFeatureMetaFieldMapper(Settings indexSettings) { + super(Defaults.FIELD_TYPE, RankFeatureMetaFieldType.INSTANCE, indexSettings); } @Override diff --git a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/RankFeaturesFieldMapper.java b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/RankFeaturesFieldMapper.java index c6d9303fcaf58..0726df272ddf2 100644 --- a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/RankFeaturesFieldMapper.java +++ b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/RankFeaturesFieldMapper.java @@ -20,6 +20,7 @@ package org.elasticsearch.index.mapper; import org.apache.lucene.document.FeatureField; +import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.search.Query; import org.elasticsearch.common.lucene.Lucene; @@ -41,12 +42,11 @@ public class RankFeaturesFieldMapper extends FieldMapper { public static final String CONTENT_TYPE = "rank_features"; public static class Defaults { - public static final MappedFieldType FIELD_TYPE = new RankFeaturesFieldType(); + public static final FieldType FIELD_TYPE = new FieldType(); static { FIELD_TYPE.setTokenized(false); FIELD_TYPE.setIndexOptions(IndexOptions.NONE); - FIELD_TYPE.setHasDocValues(false); FIELD_TYPE.setOmitNorms(true); FIELD_TYPE.freeze(); } @@ -55,20 +55,14 @@ public static class Defaults { public static class Builder extends FieldMapper.Builder { public Builder(String name) { - super(name, Defaults.FIELD_TYPE, Defaults.FIELD_TYPE); + super(name, Defaults.FIELD_TYPE); builder = this; } - @Override - public RankFeaturesFieldType fieldType() { - return (RankFeaturesFieldType) super.fieldType(); - } - @Override public RankFeaturesFieldMapper build(BuilderContext context) { - setupFieldType(context); return new RankFeaturesFieldMapper( - name, fieldType, defaultFieldType, + name, fieldType, new RankFeaturesFieldType(buildFullName(context), meta), context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); } } @@ -76,14 +70,14 @@ public RankFeaturesFieldMapper build(BuilderContext context) { public static class TypeParser implements Mapper.TypeParser { @Override public Mapper.Builder parse(String name, Map node, ParserContext parserContext) throws MapperParsingException { - RankFeaturesFieldMapper.Builder builder = new RankFeaturesFieldMapper.Builder(name); - return builder; + return new Builder(name); } } public static final class RankFeaturesFieldType extends MappedFieldType { - public RankFeaturesFieldType() { + public RankFeaturesFieldType(String name, Map meta) { + super(name, false, false, meta); setIndexAnalyzer(Lucene.KEYWORD_ANALYZER); setSearchAnalyzer(Lucene.KEYWORD_ANALYZER); } @@ -117,9 +111,9 @@ public Query termQuery(Object value, QueryShardContext context) { } } - private RankFeaturesFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, + private RankFeaturesFieldMapper(String simpleName, FieldType fieldType, MappedFieldType mappedFieldType, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { - super(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, copyTo); + super(simpleName, fieldType, mappedFieldType, indexSettings, multiFields, copyTo); assert fieldType.indexOptions().compareTo(IndexOptions.DOCS_AND_FREQS) <= 0; } @@ -175,6 +169,16 @@ protected void parseCreateField(ParseContext context) throws IOException { throw new AssertionError("parse is implemented directly"); } + @Override + protected boolean indexedByDefault() { + return false; + } + + @Override + protected boolean docValuesByDefault() { + return false; + } + @Override protected String contentType() { return CONTENT_TYPE; diff --git a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/ScaledFloatFieldMapper.java b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/ScaledFloatFieldMapper.java index 7f2214f903acb..705c3a5beb99d 100644 --- a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/ScaledFloatFieldMapper.java +++ b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/ScaledFloatFieldMapper.java @@ -20,6 +20,7 @@ package org.elasticsearch.index.mapper; import org.apache.lucene.document.Field; +import org.apache.lucene.document.FieldType; import org.apache.lucene.index.DocValues; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.LeafReaderContext; @@ -76,11 +77,13 @@ public class ScaledFloatFieldMapper extends FieldMapper { public static class Builder extends FieldMapper.Builder { private boolean scalingFactorSet = false; + private double scalingFactor; private Boolean ignoreMalformed; private Boolean coerce; + private Double nullValue; public Builder(String name) { - super(name, new ScaledFloatFieldType(), new ScaledFloatFieldType()); + super(name, new FieldType()); builder = this; } @@ -92,7 +95,7 @@ public Builder ignoreMalformed(boolean ignoreMalformed) { @Override public Builder indexOptions(IndexOptions indexOptions) { throw new MapperParsingException( - "index_options not allowed in field [" + name + "] of type [" + builder.fieldType().typeName() + "]"); + "index_options not allowed in field [" + name + "] of type [" + CONTENT_TYPE + "]"); } protected Explicit ignoreMalformed(BuilderContext context) { @@ -111,11 +114,16 @@ public Builder coerce(boolean coerce) { } public Builder scalingFactor(double scalingFactor) { - ((ScaledFloatFieldType) fieldType).setScalingFactor(scalingFactor); + this.scalingFactor = scalingFactor; scalingFactorSet = true; return this; } + public Builder nullValue(Double nullValue) { + this.nullValue = nullValue; + return this; + } + protected Explicit coerce(BuilderContext context) { if (coerce != null) { return new Explicit<>(coerce, true); @@ -131,9 +139,9 @@ public ScaledFloatFieldMapper build(BuilderContext context) { if (scalingFactorSet == false) { throw new IllegalArgumentException("Field [" + name + "] misses required parameter [scaling_factor]"); } - setupFieldType(context); - return new ScaledFloatFieldMapper(name, fieldType, defaultFieldType, ignoreMalformed(context), - coerce(context), context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); + ScaledFloatFieldType type = new ScaledFloatFieldType(buildFullName(context), indexed, hasDocValues, meta, scalingFactor); + return new ScaledFloatFieldMapper(name, fieldType, type, ignoreMalformed(context), + coerce(context), context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo, nullValue); } } @@ -171,13 +179,15 @@ public Mapper.Builder parse(String name, Map node, public static final class ScaledFloatFieldType extends SimpleMappedFieldType { - private double scalingFactor; + private final double scalingFactor; - public ScaledFloatFieldType() { - super(); - setTokenized(false); - setHasDocValues(true); - setOmitNorms(true); + public ScaledFloatFieldType(String name, boolean indexed, boolean hasDocValues, Map meta, double scalingFactor) { + super(name, indexed, hasDocValues, meta); + this.scalingFactor = scalingFactor; + } + + public ScaledFloatFieldType(String name, double scalingFactor) { + this(name, true, true, Collections.emptyMap(), scalingFactor); } ScaledFloatFieldType(ScaledFloatFieldType other) { @@ -189,11 +199,6 @@ public double getScalingFactor() { return scalingFactor; } - public void setScalingFactor(double scalingFactor) { - checkIfFrozen(); - this.scalingFactor = scalingFactor; - } - @Override public MappedFieldType clone() { return new ScaledFloatFieldType(this); @@ -333,17 +338,22 @@ private double scale(Object input) { private Explicit coerce; + private final Double nullValue; + private final double scalingFactor; + private ScaledFloatFieldMapper( String simpleName, - MappedFieldType fieldType, - MappedFieldType defaultFieldType, + FieldType fieldType, + ScaledFloatFieldType mappedFieldType, Explicit ignoreMalformed, Explicit coerce, Settings indexSettings, MultiFields multiFields, - CopyTo copyTo) { - super(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, copyTo); - final double scalingFactor = fieldType().getScalingFactor(); + CopyTo copyTo, + Double nullValue) { + super(simpleName, fieldType, mappedFieldType, indexSettings, multiFields, copyTo); + this.scalingFactor = mappedFieldType.scalingFactor; + this.nullValue = nullValue; if (Double.isFinite(scalingFactor) == false || scalingFactor <= 0) { throw new IllegalArgumentException("[scaling_factor] must be a positive number, got [" + scalingFactor + "]"); } @@ -358,7 +368,7 @@ public ScaledFloatFieldType fieldType() { @Override protected String contentType() { - return fieldType.typeName(); + return CONTENT_TYPE; } @Override @@ -394,7 +404,7 @@ protected void parseCreateField(ParseContext context) throws IOException { } if (value == null) { - value = fieldType().nullValue(); + value = nullValue; } if (value == null) { @@ -414,11 +424,11 @@ protected void parseCreateField(ParseContext context) throws IOException { throw new IllegalArgumentException("[scaled_float] only supports finite values, but got [" + doubleValue + "]"); } } - long scaledValue = Math.round(doubleValue * fieldType().getScalingFactor()); + long scaledValue = Math.round(doubleValue * scalingFactor); - boolean indexed = fieldType().indexOptions() != IndexOptions.NONE; + boolean indexed = fieldType().isSearchable(); boolean docValued = fieldType().hasDocValues(); - boolean stored = fieldType().stored(); + boolean stored = fieldType.stored(); List fields = NumberFieldMapper.NumberType.LONG.createFields(fieldType().name(), scaledValue, indexed, docValued, stored); context.doc().addAll(fields); @@ -431,7 +441,7 @@ protected void parseCreateField(ParseContext context) throws IOException { protected void mergeOptions(FieldMapper other, List conflicts) { ScaledFloatFieldMapper mergeWith = (ScaledFloatFieldMapper) other; ScaledFloatFieldType ft = (ScaledFloatFieldType) other.fieldType(); - if (fieldType().scalingFactor != ft.getScalingFactor()) { + if (fieldType().scalingFactor != ft.scalingFactor) { conflicts.add("mapper [" + name() + "] has different [scaling_factor] values"); } if (mergeWith.ignoreMalformed.explicit()) { @@ -446,7 +456,7 @@ protected void mergeOptions(FieldMapper other, List conflicts) { protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException { super.doXContentBody(builder, includeDefaults, params); - builder.field("scaling_factor", fieldType().getScalingFactor()); + builder.field("scaling_factor", scalingFactor); if (includeDefaults || ignoreMalformed.explicit()) { builder.field("ignore_malformed", ignoreMalformed.value()); @@ -454,9 +464,8 @@ protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, if (includeDefaults || coerce.explicit()) { builder.field("coerce", coerce.value()); } - - if (includeDefaults || fieldType().nullValue() != null) { - builder.field("null_value", fieldType().nullValue()); + if (nullValue != null) { + builder.field("null_value", nullValue); } } diff --git a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldMapper.java b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldMapper.java index d47793bd09929..cb1809609fdd8 100644 --- a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldMapper.java +++ b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldMapper.java @@ -28,8 +28,8 @@ import org.apache.lucene.analysis.shingle.FixedShingleFilter; import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute; import org.apache.lucene.document.Field; +import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexOptions; -import org.apache.lucene.index.IndexableField; import org.apache.lucene.index.Term; import org.apache.lucene.search.AutomatonQuery; import org.apache.lucene.search.BooleanClause; @@ -57,6 +57,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -93,7 +94,7 @@ public static class Defaults { public static final int MAX_GRAM = 20; public static final int MAX_SHINGLE_SIZE = 3; - public static final MappedFieldType FIELD_TYPE = new SearchAsYouTypeFieldType(); + public static final FieldType FIELD_TYPE = new FieldType(); static { FIELD_TYPE.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS); @@ -110,9 +111,9 @@ public Mapper.Builder parse(String name, final Builder builder = new Builder(name); - builder.fieldType().setIndexAnalyzer(parserContext.getIndexAnalyzers().getDefaultIndexAnalyzer()); - builder.fieldType().setSearchAnalyzer(parserContext.getIndexAnalyzers().getDefaultSearchAnalyzer()); - builder.fieldType().setSearchQuoteAnalyzer(parserContext.getIndexAnalyzers().getDefaultSearchQuoteAnalyzer()); + builder.indexAnalyzer(parserContext.getIndexAnalyzers().getDefaultIndexAnalyzer()); + builder.searchAnalyzer(parserContext.getIndexAnalyzers().getDefaultSearchAnalyzer()); + builder.searchQuoteAnalyzer(parserContext.getIndexAnalyzers().getDefaultSearchQuoteAnalyzer()); parseTextField(builder, name, node, parserContext); for (Iterator> iterator = node.entrySet().iterator(); iterator.hasNext();) { final Map.Entry entry = iterator.next(); @@ -133,7 +134,7 @@ public static class Builder extends FieldMapper.Builder { private int maxShingleSize = Defaults.MAX_SHINGLE_SIZE; public Builder(String name) { - super(name, Defaults.FIELD_TYPE, Defaults.FIELD_TYPE); + super(name, Defaults.FIELD_TYPE); this.builder = this; } @@ -147,23 +148,26 @@ public Builder maxShingleSize(int maxShingleSize) { } @Override - public SearchAsYouTypeFieldType fieldType() { - return (SearchAsYouTypeFieldType) this.fieldType; + public Builder docValues(boolean docValues) { + if (docValues) { + throw new IllegalArgumentException("mapper [" + name() + "] of type [search_as_you_type] does not support doc values"); + } + return this; } @Override public SearchAsYouTypeFieldMapper build(Mapper.BuilderContext context) { - setupFieldType(context); - final NamedAnalyzer indexAnalyzer = fieldType().indexAnalyzer(); - final NamedAnalyzer searchAnalyzer = fieldType().searchAnalyzer(); - final NamedAnalyzer searchQuoteAnalyzer = fieldType().searchQuoteAnalyzer(); + boolean hasNorms = fieldType.omitNorms() == false; + SearchAsYouTypeFieldType ft = new SearchAsYouTypeFieldType(buildFullName(context), indexed, meta, hasNorms); + ft.setIndexAnalyzer(indexAnalyzer); + ft.setSearchAnalyzer(searchAnalyzer); + ft.setSearchQuoteAnalyzer(searchQuoteAnalyzer); + ft.setSimilarity(similarity); // set up the prefix field final String fullName = buildFullName(context); - final String prefixFieldName = fullName + PREFIX_FIELD_SUFFIX; - final PrefixFieldType prefixFieldType = new PrefixFieldType(fullName, prefixFieldName, Defaults.MIN_GRAM, Defaults.MAX_GRAM); - prefixFieldType.setIndexOptions(fieldType().indexOptions()); + final PrefixFieldType prefixFieldType = new PrefixFieldType(fullName, Defaults.MIN_GRAM, Defaults.MAX_GRAM); // wrap the root field's index analyzer with shingles and edge ngrams final SearchAsYouTypeAnalyzer prefixIndexWrapper = SearchAsYouTypeAnalyzer.withShingleAndPrefix(indexAnalyzer.analyzer(), maxShingleSize); @@ -173,15 +177,19 @@ public SearchAsYouTypeFieldMapper build(Mapper.BuilderContext context) { // don't wrap the root field's search quote analyzer as prefix field doesn't support phrase queries prefixFieldType.setIndexAnalyzer(new NamedAnalyzer(indexAnalyzer.name(), AnalyzerScope.INDEX, prefixIndexWrapper)); prefixFieldType.setSearchAnalyzer(new NamedAnalyzer(searchAnalyzer.name(), AnalyzerScope.INDEX, prefixSearchWrapper)); - final PrefixFieldMapper prefixFieldMapper = new PrefixFieldMapper(prefixFieldType, context.indexSettings()); + FieldType prefixft = new FieldType(fieldType); + prefixft.setStoreTermVectors(false); + prefixft.setOmitNorms(true); + prefixft.setStored(false); + final PrefixFieldMapper prefixFieldMapper = new PrefixFieldMapper(prefixft, prefixFieldType, context.indexSettings()); // set up the shingle fields final ShingleFieldMapper[] shingleFieldMappers = new ShingleFieldMapper[maxShingleSize - 1]; final ShingleFieldType[] shingleFieldTypes = new ShingleFieldType[maxShingleSize - 1]; for (int i = 0; i < shingleFieldMappers.length; i++) { final int shingleSize = i + 2; - final ShingleFieldType shingleFieldType = new ShingleFieldType(fieldType(), shingleSize); - shingleFieldType.setName(getShingleFieldName(buildFullName(context), shingleSize)); + String fieldName = getShingleFieldName(buildFullName(context), shingleSize); + final ShingleFieldType shingleFieldType = new ShingleFieldType(fieldName, shingleSize, hasNorms); // wrap the root field's index, search, and search quote analyzers with shingles final SearchAsYouTypeAnalyzer shingleIndexWrapper = SearchAsYouTypeAnalyzer.withShingle(indexAnalyzer.analyzer(), shingleSize); @@ -195,11 +203,13 @@ public SearchAsYouTypeFieldMapper build(Mapper.BuilderContext context) { new NamedAnalyzer(searchQuoteAnalyzer.name(), AnalyzerScope.INDEX, shingleSearchQuoteWrapper)); shingleFieldType.setPrefixFieldType(prefixFieldType); shingleFieldTypes[i] = shingleFieldType; - shingleFieldMappers[i] = new ShingleFieldMapper(shingleFieldType, context.indexSettings()); + FieldType shingleft = new FieldType(fieldType); + shingleft.setStored(false); + shingleFieldMappers[i] = new ShingleFieldMapper(shingleft, shingleFieldType, context.indexSettings()); } - fieldType().setPrefixField(prefixFieldType); - fieldType().setShingleFields(shingleFieldTypes); - return new SearchAsYouTypeFieldMapper(name, fieldType(), context.indexSettings(), copyTo, + ft.setPrefixField(prefixFieldType); + ft.setShingleFields(shingleFieldTypes); + return new SearchAsYouTypeFieldMapper(name, fieldType, ft, context.indexSettings(), copyTo, maxShingleSize, prefixFieldMapper, shingleFieldMappers); } } @@ -226,9 +236,12 @@ static class SearchAsYouTypeFieldType extends StringFieldType { PrefixFieldType prefixField; ShingleFieldType[] shingleFields = new ShingleFieldType[0]; + final boolean hasNorms; - SearchAsYouTypeFieldType() { - setTokenized(true); + SearchAsYouTypeFieldType(String name, boolean indexed, Map meta, boolean hasNorms) { + super(name, indexed, false, meta); + this.hasNorms = hasNorms; + this.hasPositions = true; } SearchAsYouTypeFieldType(SearchAsYouTypeFieldType other) { @@ -245,15 +258,14 @@ static class SearchAsYouTypeFieldType extends StringFieldType { } } } + this.hasNorms = other.hasNorms; } public void setPrefixField(PrefixFieldType prefixField) { - checkIfFrozen(); this.prefixField = prefixField; } public void setShingleFields(ShingleFieldType[] shingleFields) { - checkIfFrozen(); this.shingleFields = shingleFields; } @@ -274,7 +286,7 @@ private ShingleFieldType shingleFieldForPositions(int positions) { @Override public Query existsQuery(QueryShardContext context) { - if (omitNorms()) { + if (hasNorms == false) { return new TermQuery(new Term(FieldNamesFieldMapper.NAME, name())); } else { return new NormsFieldExistsQuery(name()); @@ -375,14 +387,12 @@ static final class PrefixFieldType extends StringFieldType { final int maxChars; final String parentField; - PrefixFieldType(String parentField, String name, int minChars, int maxChars) { - setTokenized(true); - setOmitNorms(true); - setStored(false); - setName(name); + PrefixFieldType(String parentField, int minChars, int maxChars) { + super(parentField + PREFIX_FIELD_SUFFIX, true, false, Collections.emptyMap()); this.minChars = minChars; this.maxChars = maxChars; this.parentField = parentField; + this.hasPositions = true; } PrefixFieldType(PrefixFieldType other) { @@ -390,6 +400,7 @@ static final class PrefixFieldType extends StringFieldType { this.minChars = other.minChars; this.maxChars = other.maxChars; this.parentField = other.parentField; + this.hasPositions = other.hasPositions; } boolean termLengthWithinBounds(int length) { @@ -459,8 +470,8 @@ public int hashCode() { static final class PrefixFieldMapper extends FieldMapper { - PrefixFieldMapper(PrefixFieldType fieldType, Settings indexSettings) { - super(fieldType.name(), fieldType, fieldType, indexSettings, MultiFields.empty(), CopyTo.empty()); + PrefixFieldMapper(FieldType fieldType, PrefixFieldType mappedFieldType, Settings indexSettings) { + super(mappedFieldType.name(), fieldType, mappedFieldType, indexSettings, MultiFields.empty(), CopyTo.empty()); } @Override @@ -468,6 +479,10 @@ public PrefixFieldType fieldType() { return (PrefixFieldType) super.fieldType(); } + FieldType getLuceneFieldType() { + return fieldType; + } + @Override protected void parseCreateField(ParseContext context) { throw new UnsupportedOperationException(); @@ -491,8 +506,12 @@ public String toString() { static final class ShingleFieldMapper extends FieldMapper { - ShingleFieldMapper(ShingleFieldType fieldType, Settings indexSettings) { - super(fieldType.name(), fieldType, fieldType, indexSettings, MultiFields.empty(), CopyTo.empty()); + ShingleFieldMapper(FieldType fieldType, ShingleFieldType mappedFieldtype, Settings indexSettings) { + super(mappedFieldtype.name(), fieldType, mappedFieldtype, indexSettings, MultiFields.empty(), CopyTo.empty()); + } + + FieldType getLuceneFieldType() { + return fieldType; } @Override @@ -521,24 +540,27 @@ protected String contentType() { */ static class ShingleFieldType extends StringFieldType { final int shingleSize; + final boolean hasNorms; PrefixFieldType prefixFieldType; - ShingleFieldType(MappedFieldType other, int shingleSize) { - super(other); + ShingleFieldType(String name, int shingleSize, boolean hasNorms) { + super(name, true, false, Collections.emptyMap()); this.shingleSize = shingleSize; - this.setStored(false); + this.hasNorms = hasNorms; + this.hasPositions = true; } ShingleFieldType(ShingleFieldType other) { super(other); this.shingleSize = other.shingleSize; + this.hasNorms = other.hasNorms; + this.hasPositions = other.hasPositions; if (other.prefixFieldType != null) { this.prefixFieldType = other.prefixFieldType.clone(); } } void setPrefixFieldType(PrefixFieldType prefixFieldType) { - checkIfFrozen(); this.prefixFieldType = prefixFieldType; } @@ -554,7 +576,7 @@ public String typeName() { @Override public Query existsQuery(QueryShardContext context) { - if (omitNorms()) { + if (hasNorms == false) { return new TermQuery(new Term(FieldNamesFieldMapper.NAME, name())); } else { return new NormsFieldExistsQuery(name()); @@ -635,13 +657,14 @@ public int hashCode() { private final ShingleFieldMapper[] shingleFields; public SearchAsYouTypeFieldMapper(String simpleName, - SearchAsYouTypeFieldType fieldType, + FieldType fieldType, + SearchAsYouTypeFieldType mappedFieldType, Settings indexSettings, CopyTo copyTo, int maxShingleSize, PrefixFieldMapper prefixField, ShingleFieldMapper[] shingleFields) { - super(simpleName, fieldType, Defaults.FIELD_TYPE, indexSettings, MultiFields.empty(), copyTo); + super(simpleName, fieldType, mappedFieldType, indexSettings, MultiFields.empty(), copyTo); this.prefixField = prefixField; this.shingleFields = shingleFields; this.maxShingleSize = maxShingleSize; @@ -654,13 +677,12 @@ protected void parseCreateField(ParseContext context) throws IOException { return; } - List newFields = new ArrayList<>(); - context.doc().add(new Field(fieldType().name(), value, fieldType())); + context.doc().add(new Field(fieldType().name(), value, fieldType)); for (ShingleFieldMapper subFieldMapper : shingleFields) { - context.doc().add(new Field(subFieldMapper.fieldType().name(), value, subFieldMapper.fieldType())); + context.doc().add(new Field(subFieldMapper.fieldType().name(), value, subFieldMapper.getLuceneFieldType())); } - context.doc().add(new Field(prefixField.fieldType().name(), value, prefixField.fieldType())); - if (fieldType().omitNorms()) { + context.doc().add(new Field(prefixField.fieldType().name(), value, prefixField.getLuceneFieldType())); + if (fieldType.omitNorms()) { createFieldNamesField(context); } } @@ -681,6 +703,9 @@ protected void mergeOptions(FieldMapper other, List conflicts) { this.shingleFields[i] = (ShingleFieldMapper) this.shingleFields[i].merge(m.shingleFields[i]); } } + if (Objects.equals(this.fieldType().similarity(), other.fieldType().similarity()) == false) { + conflicts.add("mapper [" + name() + "] has different [similarity] settings"); + } } public static String getShingleFieldName(String parentField, int shingleSize) { diff --git a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/TokenCountFieldMapper.java b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/TokenCountFieldMapper.java index b66bcf23fada3..560cea9c16269 100644 --- a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/TokenCountFieldMapper.java +++ b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/TokenCountFieldMapper.java @@ -22,7 +22,7 @@ import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute; -import org.apache.lucene.index.IndexOptions; +import org.apache.lucene.document.FieldType; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.index.analysis.NamedAnalyzer; @@ -44,16 +44,16 @@ public class TokenCountFieldMapper extends FieldMapper { public static final String CONTENT_TYPE = "token_count"; public static class Defaults { - public static final MappedFieldType FIELD_TYPE = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); public static final boolean DEFAULT_POSITION_INCREMENTS = true; } public static class Builder extends FieldMapper.Builder { private NamedAnalyzer analyzer; + private Integer nullValue; private boolean enablePositionIncrements = Defaults.DEFAULT_POSITION_INCREMENTS; public Builder(String name) { - super(name, Defaults.FIELD_TYPE, Defaults.FIELD_TYPE); + super(name, new FieldType()); builder = this; } @@ -75,11 +75,17 @@ public boolean enablePositionIncrements() { return enablePositionIncrements; } + public Builder nullValue(Integer nullValue) { + this.nullValue = nullValue; + return this; + } + @Override public TokenCountFieldMapper build(BuilderContext context) { - setupFieldType(context); - return new TokenCountFieldMapper(name, fieldType, defaultFieldType, - context.indexSettings(), analyzer, enablePositionIncrements, multiFieldsBuilder.build(this, context), copyTo); + return new TokenCountFieldMapper(name, fieldType, + new NumberFieldMapper.NumberFieldType(buildFullName(context), NumberFieldMapper.NumberType.INTEGER), + context.indexSettings(), analyzer, enablePositionIncrements, nullValue, + multiFieldsBuilder.build(this, context), copyTo); } } @@ -115,13 +121,16 @@ public Mapper.Builder parse(String name, Map node, ParserCont } private NamedAnalyzer analyzer; - private boolean enablePositionIncrements; + private final boolean enablePositionIncrements; + private Integer nullValue; - protected TokenCountFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, - Settings indexSettings, NamedAnalyzer analyzer, boolean enablePositionIncrements, MultiFields multiFields, CopyTo copyTo) { + protected TokenCountFieldMapper(String simpleName, FieldType fieldType, MappedFieldType defaultFieldType, + Settings indexSettings, NamedAnalyzer analyzer, boolean enablePositionIncrements, Integer nullValue, + MultiFields multiFields, CopyTo copyTo) { super(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, copyTo); this.analyzer = analyzer; this.enablePositionIncrements = enablePositionIncrements; + this.nullValue = nullValue; } @Override @@ -133,20 +142,20 @@ protected void parseCreateField(ParseContext context) throws IOException { value = context.parser().textOrNull(); } - if (value == null && fieldType().nullValue() == null) { + if (value == null && nullValue == null) { return; } final int tokenCount; if (value == null) { - tokenCount = (Integer) fieldType().nullValue(); + tokenCount = nullValue; } else { tokenCount = countPositions(analyzer, name(), value, enablePositionIncrements); } - boolean indexed = fieldType().indexOptions() != IndexOptions.NONE; + boolean indexed = fieldType().isSearchable(); boolean docValued = fieldType().hasDocValues(); - boolean stored = fieldType().stored(); + boolean stored = fieldType.stored(); context.doc().addAll(NumberFieldMapper.NumberType.INTEGER.createFields(fieldType().name(), tokenCount, indexed, docValued, stored)); } @@ -202,8 +211,11 @@ protected String contentType() { @Override protected void mergeOptions(FieldMapper other, List conflicts) { - this.analyzer = ((TokenCountFieldMapper) other).analyzer; - this.enablePositionIncrements = ((TokenCountFieldMapper) other).enablePositionIncrements; + // TODO we should ban updating analyzers and null values as well + if (this.enablePositionIncrements != ((TokenCountFieldMapper)other).enablePositionIncrements) { + conflicts.add("mapper [" + name() + "] has a different [enable_position_increments] setting"); + } + this.analyzer = ((TokenCountFieldMapper)other).analyzer; } @Override diff --git a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/RankFeatureFieldMapperTests.java b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/RankFeatureFieldMapperTests.java index 09c1768f28a90..cf285c0fda999 100644 --- a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/RankFeatureFieldMapperTests.java +++ b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/RankFeatureFieldMapperTests.java @@ -36,19 +36,25 @@ import java.io.IOException; import java.util.Arrays; import java.util.Collection; +import java.util.Set; public class RankFeatureFieldMapperTests extends FieldMapperTestCase { IndexService indexService; DocumentMapperParser parser; + @Override + protected Set unsupportedProperties() { + return Set.of("analyzer", "similarity", "store", "doc_values"); + } + @Before public void setup() { indexService = createIndex("test"); parser = indexService.mapperService().documentMapperParser(); addModifier("positive_score_impact", false, (a, b) -> { - a.fieldType().setPositiveScoreImpact(true); - b.fieldType().setPositiveScoreImpact(false); + a.positiveScoreImpact(true); + b.positiveScoreImpact(false); }); } diff --git a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/RankFeatureFieldTypeTests.java b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/RankFeatureFieldTypeTests.java index 0034fb2d4c16e..85ec2ec220984 100644 --- a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/RankFeatureFieldTypeTests.java +++ b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/RankFeatureFieldTypeTests.java @@ -19,15 +19,18 @@ package org.elasticsearch.index.mapper; +import java.util.Collections; +import java.util.Map; + public class RankFeatureFieldTypeTests extends FieldTypeTestCase { @Override - protected MappedFieldType createDefaultFieldType() { - return new RankFeatureFieldMapper.RankFeatureFieldType(); + protected MappedFieldType createDefaultFieldType(String name, Map meta) { + return new RankFeatureFieldMapper.RankFeatureFieldType(name, meta, true); } public void testIsAggregatable() { - MappedFieldType fieldType = createDefaultFieldType(); + MappedFieldType fieldType = new RankFeatureFieldMapper.RankFeatureFieldType("field", Collections.emptyMap(), true); assertFalse(fieldType.isAggregatable()); } } diff --git a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/RankFeatureMetaFieldTypeTests.java b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/RankFeatureMetaFieldTypeTests.java deleted file mode 100644 index 025cace781ae2..0000000000000 --- a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/RankFeatureMetaFieldTypeTests.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.index.mapper; - -public class RankFeatureMetaFieldTypeTests extends FieldTypeTestCase { - - @Override - protected MappedFieldType createDefaultFieldType() { - return new RankFeatureMetaFieldMapper.RankFeatureMetaFieldType(); - } - -} diff --git a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/RankFeaturesFieldMapperTests.java b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/RankFeaturesFieldMapperTests.java index f6dd518191e2c..0a1f6423cc5ce 100644 --- a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/RankFeaturesFieldMapperTests.java +++ b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/RankFeaturesFieldMapperTests.java @@ -28,15 +28,20 @@ import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.IndexService; import org.elasticsearch.plugins.Plugin; -import org.elasticsearch.test.ESSingleNodeTestCase; import org.hamcrest.Matchers; import org.junit.Before; import java.io.IOException; import java.util.Arrays; import java.util.Collection; +import java.util.Set; -public class RankFeaturesFieldMapperTests extends ESSingleNodeTestCase { +public class RankFeaturesFieldMapperTests extends FieldMapperTestCase { + + @Override + protected Set unsupportedProperties() { + return Set.of("analyzer", "similarity", "store", "doc_values"); + } IndexService indexService; DocumentMapperParser parser; @@ -127,4 +132,9 @@ public void testRejectMultiValuedFields() throws MapperParsingException, IOExcep assertEquals("[rank_features] fields do not support indexing multiple values for the same rank feature [foo.field.bar] in " + "the same document", e.getCause().getMessage()); } + + @Override + protected RankFeaturesFieldMapper.Builder newBuilder() { + return new RankFeaturesFieldMapper.Builder("rf"); + } } diff --git a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/RankFeaturesFieldTypeTests.java b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/RankFeaturesFieldTypeTests.java index 2e0b0b5130075..5c02f860d8aca 100644 --- a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/RankFeaturesFieldTypeTests.java +++ b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/RankFeaturesFieldTypeTests.java @@ -19,15 +19,18 @@ package org.elasticsearch.index.mapper; +import java.util.Collections; +import java.util.Map; + public class RankFeaturesFieldTypeTests extends FieldTypeTestCase { @Override - protected MappedFieldType createDefaultFieldType() { - return new RankFeaturesFieldMapper.RankFeaturesFieldType(); + protected MappedFieldType createDefaultFieldType(String name, Map meta) { + return new RankFeaturesFieldMapper.RankFeaturesFieldType(name, meta); } public void testIsAggregatable() { - MappedFieldType fieldType = createDefaultFieldType(); + MappedFieldType fieldType = createDefaultFieldType("field", Collections.emptyMap()); assertFalse(fieldType.isAggregatable()); } } diff --git a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/ScaledFloatFieldMapperTests.java b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/ScaledFloatFieldMapperTests.java index 79e15f49d2759..7fa4a81ec5c13 100644 --- a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/ScaledFloatFieldMapperTests.java +++ b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/ScaledFloatFieldMapperTests.java @@ -37,6 +37,7 @@ import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.Set; import static org.hamcrest.Matchers.containsString; @@ -55,6 +56,11 @@ public void setup() { }); } + @Override + protected Set unsupportedProperties() { + return Set.of("analyzer", "similarity"); + } + @Override protected Collection> getPlugins() { return pluginList(InternalSettingsPlugin.class, MapperExtrasPlugin.class); diff --git a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/ScaledFloatFieldTypeTests.java b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/ScaledFloatFieldTypeTests.java index e34392f0c6d70..ff517cf4e35a2 100644 --- a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/ScaledFloatFieldTypeTests.java +++ b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/ScaledFloatFieldTypeTests.java @@ -40,29 +40,26 @@ import java.io.IOException; import java.util.Arrays; +import java.util.Map; public class ScaledFloatFieldTypeTests extends FieldTypeTestCase { @Override - protected MappedFieldType createDefaultFieldType() { - ScaledFloatFieldMapper.ScaledFloatFieldType ft = new ScaledFloatFieldMapper.ScaledFloatFieldType(); - ft.setScalingFactor(100); - return ft; + protected MappedFieldType createDefaultFieldType(String name, Map meta) { + return new ScaledFloatFieldMapper.ScaledFloatFieldType(name, true, true, meta, 100); } public void testTermQuery() { - ScaledFloatFieldMapper.ScaledFloatFieldType ft = new ScaledFloatFieldMapper.ScaledFloatFieldType(); - ft.setName("scaled_float"); - ft.setScalingFactor(0.1 + randomDouble() * 100); + ScaledFloatFieldMapper.ScaledFloatFieldType ft + = new ScaledFloatFieldMapper.ScaledFloatFieldType("scaled_float", 0.1 + randomDouble() * 100); double value = (randomDouble() * 2 - 1) * 10000; long scaledValue = Math.round(value * ft.getScalingFactor()); assertEquals(LongPoint.newExactQuery("scaled_float", scaledValue), ft.termQuery(value, null)); } public void testTermsQuery() { - ScaledFloatFieldMapper.ScaledFloatFieldType ft = new ScaledFloatFieldMapper.ScaledFloatFieldType(); - ft.setName("scaled_float"); - ft.setScalingFactor(0.1 + randomDouble() * 100); + ScaledFloatFieldMapper.ScaledFloatFieldType ft + = new ScaledFloatFieldMapper.ScaledFloatFieldType("scaled_float", 0.1 + randomDouble() * 100); double value1 = (randomDouble() * 2 - 1) * 10000; long scaledValue1 = Math.round(value1 * ft.getScalingFactor()); double value2 = (randomDouble() * 2 - 1) * 10000; @@ -76,9 +73,8 @@ public void testRangeQuery() throws IOException { // make sure the accuracy loss of scaled floats only occurs at index time // this test checks that searching scaled floats yields the same results as // searching doubles that are rounded to the closest half float - ScaledFloatFieldMapper.ScaledFloatFieldType ft = new ScaledFloatFieldMapper.ScaledFloatFieldType(); - ft.setName("scaled_float"); - ft.setScalingFactor(0.1 + randomDouble() * 100); + ScaledFloatFieldMapper.ScaledFloatFieldType ft + = new ScaledFloatFieldMapper.ScaledFloatFieldType("scaled_float", 0.1 + randomDouble() * 100); Directory dir = newDirectory(); IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(null)); final int numDocs = 1000; @@ -108,9 +104,8 @@ public void testRangeQuery() throws IOException { } public void testRoundsUpperBoundCorrectly() { - ScaledFloatFieldMapper.ScaledFloatFieldType ft = new ScaledFloatFieldMapper.ScaledFloatFieldType(); - ft.setName("scaled_float"); - ft.setScalingFactor(100.0); + ScaledFloatFieldMapper.ScaledFloatFieldType ft + = new ScaledFloatFieldMapper.ScaledFloatFieldType("scaled_float", 100); Query scaledFloatQ = ft.rangeQuery(null, 0.1, true, false, MOCK_QSC); assertEquals("scaled_float:[-9223372036854775808 TO 9]", scaledFloatQ.toString()); scaledFloatQ = ft.rangeQuery(null, 0.1, true, true, MOCK_QSC); @@ -128,9 +123,8 @@ public void testRoundsUpperBoundCorrectly() { } public void testRoundsLowerBoundCorrectly() { - ScaledFloatFieldMapper.ScaledFloatFieldType ft = new ScaledFloatFieldMapper.ScaledFloatFieldType(); - ft.setName("scaled_float"); - ft.setScalingFactor(100.0); + ScaledFloatFieldMapper.ScaledFloatFieldType ft + = new ScaledFloatFieldMapper.ScaledFloatFieldType("scaled_float", 100); Query scaledFloatQ = ft.rangeQuery(-0.1, null, false, true, MOCK_QSC); assertEquals("scaled_float:[-9 TO 9223372036854775807]", scaledFloatQ.toString()); scaledFloatQ = ft.rangeQuery(-0.1, null, true, true, MOCK_QSC); @@ -146,16 +140,14 @@ public void testRoundsLowerBoundCorrectly() { } public void testValueForSearch() { - ScaledFloatFieldMapper.ScaledFloatFieldType ft = new ScaledFloatFieldMapper.ScaledFloatFieldType(); - ft.setName("scaled_float"); - ft.setScalingFactor(0.1 + randomDouble() * 100); + ScaledFloatFieldMapper.ScaledFloatFieldType ft + = new ScaledFloatFieldMapper.ScaledFloatFieldType("scaled_float", 0.1 + randomDouble() * 100); assertNull(ft.valueForDisplay(null)); assertEquals(10/ft.getScalingFactor(), ft.valueForDisplay(10L)); } public void testFieldData() throws IOException { - ScaledFloatFieldMapper.ScaledFloatFieldType ft = new ScaledFloatFieldMapper.ScaledFloatFieldType(); - ft.setScalingFactor(0.1 + randomDouble() * 100); + double scalingFactor = 0.1 + randomDouble() * 100; Directory dir = newDirectory(); IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(null)); Document doc = new Document(); @@ -172,25 +164,27 @@ public void testFieldData() throws IOException { IndexSettings indexSettings = new IndexSettings(indexMetadata, Settings.EMPTY); // single-valued - ft.setName("scaled_float1"); - IndexNumericFieldData fielddata = (IndexNumericFieldData) ft.fielddataBuilder("index") - .build(indexSettings, ft, null, null, null); + ScaledFloatFieldMapper.ScaledFloatFieldType f1 + = new ScaledFloatFieldMapper.ScaledFloatFieldType("scaled_float1", scalingFactor); + IndexNumericFieldData fielddata = (IndexNumericFieldData) f1.fielddataBuilder("index") + .build(indexSettings, f1, null, null, null); assertEquals(fielddata.getNumericType(), IndexNumericFieldData.NumericType.DOUBLE); LeafNumericFieldData leafFieldData = fielddata.load(reader.leaves().get(0)); SortedNumericDoubleValues values = leafFieldData.getDoubleValues(); assertTrue(values.advanceExact(0)); assertEquals(1, values.docValueCount()); - assertEquals(10/ft.getScalingFactor(), values.nextValue(), 10e-5); + assertEquals(10/f1.getScalingFactor(), values.nextValue(), 10e-5); // multi-valued - ft.setName("scaled_float2"); - fielddata = (IndexNumericFieldData) ft.fielddataBuilder("index").build(indexSettings, ft, null, null, null); + ScaledFloatFieldMapper.ScaledFloatFieldType f2 + = new ScaledFloatFieldMapper.ScaledFloatFieldType("scaled_float2", scalingFactor); + fielddata = (IndexNumericFieldData) f2.fielddataBuilder("index").build(indexSettings, f2, null, null, null); leafFieldData = fielddata.load(reader.leaves().get(0)); values = leafFieldData.getDoubleValues(); assertTrue(values.advanceExact(0)); assertEquals(2, values.docValueCount()); - assertEquals(5/ft.getScalingFactor(), values.nextValue(), 10e-5); - assertEquals(12/ft.getScalingFactor(), values.nextValue(), 10e-5); + assertEquals(5/f2.getScalingFactor(), values.nextValue(), 10e-5); + assertEquals(12/f2.getScalingFactor(), values.nextValue(), 10e-5); } IOUtils.close(w, dir); } diff --git a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldMapperTests.java b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldMapperTests.java index f8c7e7b011cc2..50f50ada355f3 100644 --- a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldMapperTests.java +++ b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldMapperTests.java @@ -63,6 +63,7 @@ import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.List; @@ -72,7 +73,6 @@ import static java.util.Arrays.asList; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.hasProperty; -import static org.hamcrest.Matchers.arrayContainingInAnyOrder; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; @@ -90,6 +90,11 @@ public void addModifiers() { }); } + @Override + protected Set unsupportedProperties() { + return Set.of("doc_values"); + } + @Override protected Collection> getPlugins() { return pluginList(MapperExtrasPlugin.class); @@ -98,8 +103,9 @@ protected Collection> getPlugins() { @Override protected SearchAsYouTypeFieldMapper.Builder newBuilder() { return new SearchAsYouTypeFieldMapper.Builder("sayt") - .indexAnalyzer(new NamedAnalyzer("analyzer", AnalyzerScope.INDEX, new StandardAnalyzer())) - .searchAnalyzer(new NamedAnalyzer("analyzer", AnalyzerScope.INDEX, new StandardAnalyzer())); + .indexAnalyzer(new NamedAnalyzer("standard", AnalyzerScope.INDEX, new StandardAnalyzer())) + .searchAnalyzer(new NamedAnalyzer("standard", AnalyzerScope.INDEX, new StandardAnalyzer())) + .searchQuoteAnalyzer(new NamedAnalyzer("standard", AnalyzerScope.INDEX, new StandardAnalyzer())); } public void testIndexing() throws IOException { @@ -275,7 +281,7 @@ public void testMultiFields() throws IOException { fields.add(path); final MapperService mapperService = createIndex(index, Settings.EMPTY, mapping).mapperService(); - FieldType fieldType = mapperService.fieldType(path + "._index_prefix"); + MappedFieldType fieldType = mapperService.fieldType(path + "._index_prefix"); assertThat(fieldType, instanceOf(PrefixFieldType.class)); PrefixFieldType prefixFieldType = (PrefixFieldType) fieldType; assertEquals(path, prefixFieldType.parentField); @@ -330,7 +336,7 @@ public void testIndexOptions() throws IOException { getShingleFieldMapper(defaultMapper, "a_field._2gram"), getShingleFieldMapper(defaultMapper, "a_field._3gram") ).forEach(mapper -> assertThat("for " + mapper.name(), - mapper.fieldType().indexOptions(), equalTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS))); + mapper.fieldType.indexOptions(), equalTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS))); } public void testStore() throws IOException { @@ -351,12 +357,12 @@ public void testStore() throws IOException { .documentMapperParser() .parse("_doc", new CompressedXContent(mapping)); - assertTrue(getRootFieldMapper(defaultMapper, "a_field").fieldType().stored()); + assertTrue(getRootFieldMapper(defaultMapper, "a_field").fieldType.stored()); Stream.of( getPrefixFieldMapper(defaultMapper, "a_field._index_prefix"), getShingleFieldMapper(defaultMapper, "a_field._2gram"), getShingleFieldMapper(defaultMapper, "a_field._3gram") - ).forEach(mapper -> assertFalse("for " + mapper.name(), mapper.fieldType().stored())); + ).forEach(mapper -> assertFalse("for " + mapper.name(), mapper.fieldType.stored())); } public void testIndex() throws IOException { @@ -382,7 +388,7 @@ public void testIndex() throws IOException { getPrefixFieldMapper(defaultMapper, "a_field._index_prefix"), getShingleFieldMapper(defaultMapper, "a_field._2gram"), getShingleFieldMapper(defaultMapper, "a_field._3gram") - ).forEach(mapper -> assertThat("for " + mapper.name(), mapper.fieldType().indexOptions(), equalTo(IndexOptions.NONE))); + ).forEach(mapper -> assertThat("for " + mapper.name(), mapper.fieldType.indexOptions(), equalTo(IndexOptions.NONE))); } public void testTermVectors() throws IOException { @@ -407,10 +413,10 @@ public void testTermVectors() throws IOException { getRootFieldMapper(defaultMapper, "a_field"), getShingleFieldMapper(defaultMapper, "a_field._2gram"), getShingleFieldMapper(defaultMapper, "a_field._3gram") - ).forEach(mapper -> assertTrue("for " + mapper.name(), mapper.fieldType().storeTermVectors())); + ).forEach(mapper -> assertTrue("for " + mapper.name(), mapper.fieldType.storeTermVectors())); final PrefixFieldMapper prefixFieldMapper = getPrefixFieldMapper(defaultMapper, "a_field._index_prefix"); - assertFalse(prefixFieldMapper.fieldType().storeTermVectors()); + assertFalse(prefixFieldMapper.fieldType.storeTermVectors()); } public void testNorms() throws IOException { @@ -436,10 +442,10 @@ public void testNorms() throws IOException { getRootFieldMapper(defaultMapper, "a_field"), getShingleFieldMapper(defaultMapper, "a_field._2gram"), getShingleFieldMapper(defaultMapper, "a_field._3gram") - ).forEach(mapper -> assertFalse("for " + mapper.name(), mapper.fieldType().omitNorms())); + ).forEach(mapper -> assertFalse("for " + mapper.name(), mapper.fieldType.omitNorms())); final PrefixFieldMapper prefixFieldMapper = getPrefixFieldMapper(defaultMapper, "a_field._index_prefix"); - assertTrue(prefixFieldMapper.fieldType().omitNorms()); + assertTrue(prefixFieldMapper.fieldType.omitNorms()); } // can disable them on shingle fields @@ -466,7 +472,7 @@ public void testNorms() throws IOException { getPrefixFieldMapper(defaultMapper, "a_field._index_prefix"), getShingleFieldMapper(defaultMapper, "a_field._2gram"), getShingleFieldMapper(defaultMapper, "a_field._3gram") - ).forEach(mapper -> assertTrue("for " + mapper.name(), mapper.fieldType().omitNorms())); + ).forEach(mapper -> assertTrue("for " + mapper.name(), mapper.fieldType.omitNorms())); } } @@ -759,28 +765,13 @@ private void documentParsingTestCase(Collection values) throws IOExcepti final ParsedDocument parsedDocument = defaultMapper.parse( new SourceToParse("test", "1", BytesReference.bytes(builder), XContentType.JSON)); - - final Set> rootFieldMatchers = values.stream() - .map(value -> indexableFieldMatcher(value, SearchAsYouTypeFieldType.class)) - .collect(Collectors.toSet()); - final Set> shingleFieldMatchers = values.stream() - .map(value -> indexableFieldMatcher(value, ShingleFieldType.class)) - .collect(Collectors.toSet()); - final Set> prefixFieldMatchers = values.stream() - .map(value -> indexableFieldMatcher(value, PrefixFieldType.class)) - .collect(Collectors.toSet()); - - // the use of new ArrayList<>() here is to avoid the varargs form of arrayContainingInAnyOrder - assertThat( - parsedDocument.rootDoc().getFields("a_field"), - arrayContainingInAnyOrder(new ArrayList<>(rootFieldMatchers))); - - assertThat( - parsedDocument.rootDoc().getFields("a_field._index_prefix"), - arrayContainingInAnyOrder(new ArrayList<>(prefixFieldMatchers))); - - for (String name : asList("a_field._2gram", "a_field._3gram")) { - assertThat(parsedDocument.rootDoc().getFields(name), arrayContainingInAnyOrder(new ArrayList<>(shingleFieldMatchers))); + IndexableField[] rootFields = parsedDocument.rootDoc().getFields("a_field"); + IndexableField[] prefixFields = parsedDocument.rootDoc().getFields("a_field._index_prefix"); + IndexableField[] shingle2Fields = parsedDocument.rootDoc().getFields("a_field._2gram"); + IndexableField[] shingle3Fields = parsedDocument.rootDoc().getFields("a_field._3gram"); + for (IndexableField[] fields : new IndexableField[][]{ rootFields, prefixFields, shingle2Fields, shingle3Fields}) { + Set expectedValues = Arrays.stream(fields).map(IndexableField::stringValue).collect(Collectors.toSet()); + assertThat(values, equalTo(expectedValues)); } } diff --git a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldTypeTests.java b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldTypeTests.java index 5be83dd22a6c1..184155413e140 100644 --- a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldTypeTests.java +++ b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldTypeTests.java @@ -19,7 +19,6 @@ package org.elasticsearch.index.mapper; -import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.Term; import org.apache.lucene.search.ConstantScoreQuery; import org.apache.lucene.search.PrefixQuery; @@ -32,6 +31,9 @@ import org.elasticsearch.index.mapper.SearchAsYouTypeFieldMapper.SearchAsYouTypeFieldType; import org.elasticsearch.index.mapper.SearchAsYouTypeFieldMapper.ShingleFieldType; +import java.util.Collections; +import java.util.Map; + import static java.util.Arrays.asList; import static org.apache.lucene.search.MultiTermQuery.CONSTANT_SCORE_REWRITE; import static org.hamcrest.Matchers.equalTo; @@ -39,48 +41,44 @@ public class SearchAsYouTypeFieldTypeTests extends FieldTypeTestCase { private static final String NAME = "a_field"; - private static final String PREFIX_NAME = NAME + "._index_prefix"; @Override - protected SearchAsYouTypeFieldType createDefaultFieldType() { - final SearchAsYouTypeFieldType fieldType = new SearchAsYouTypeFieldType(); - fieldType.setName(NAME); - fieldType.setPrefixField(new PrefixFieldType(NAME, PREFIX_NAME, Defaults.MIN_GRAM, Defaults.MAX_GRAM)); - fieldType.setShingleFields(new ShingleFieldType[] { new ShingleFieldType(fieldType, 2) }); + protected SearchAsYouTypeFieldType createDefaultFieldType(String name, Map meta) { + final SearchAsYouTypeFieldType fieldType = new SearchAsYouTypeFieldType(name, true, meta, true); + fieldType.setPrefixField(new PrefixFieldType(NAME, Defaults.MIN_GRAM, Defaults.MAX_GRAM)); + fieldType.setShingleFields(new ShingleFieldType[] { new ShingleFieldType(fieldType.name(), 2, true) }); return fieldType; } public void testTermQuery() { - final MappedFieldType fieldType = createDefaultFieldType(); + final MappedFieldType fieldType = createDefaultFieldType(NAME, Collections.emptyMap()); - fieldType.setIndexOptions(IndexOptions.DOCS); assertThat(fieldType.termQuery("foo", null), equalTo(new TermQuery(new Term(NAME, "foo")))); - fieldType.setIndexOptions(IndexOptions.NONE); - final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> fieldType.termQuery("foo", null)); + SearchAsYouTypeFieldType unsearchable = new SearchAsYouTypeFieldType(NAME, false, Collections.emptyMap(), true); + final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> unsearchable.termQuery("foo", null)); assertThat(e.getMessage(), equalTo("Cannot search on field [" + NAME + "] since it is not indexed.")); } public void testTermsQuery() { - final MappedFieldType fieldType = createDefaultFieldType(); + final MappedFieldType fieldType = createDefaultFieldType(NAME, Collections.emptyMap()); - fieldType.setIndexOptions(IndexOptions.DOCS); assertThat(fieldType.termsQuery(asList("foo", "bar"), null), equalTo(new TermInSetQuery(NAME, asList(new BytesRef("foo"), new BytesRef("bar"))))); - fieldType.setIndexOptions(IndexOptions.NONE); + SearchAsYouTypeFieldType unsearchable = new SearchAsYouTypeFieldType(NAME, false, Collections.emptyMap(), true); final IllegalArgumentException e = - expectThrows(IllegalArgumentException.class, () -> fieldType.termsQuery(asList("foo", "bar"), null)); + expectThrows(IllegalArgumentException.class, () -> unsearchable.termsQuery(asList("foo", "bar"), null)); assertThat(e.getMessage(), equalTo("Cannot search on field [" + NAME + "] since it is not indexed.")); } public void testPrefixQuery() { - final SearchAsYouTypeFieldType fieldType = createDefaultFieldType(); + final SearchAsYouTypeFieldType fieldType = createDefaultFieldType(NAME, Collections.emptyMap()); // this term should be a length that can be rewriteable to a term query on the prefix field final String withinBoundsTerm = "foo"; assertThat(fieldType.prefixQuery(withinBoundsTerm, CONSTANT_SCORE_REWRITE, randomMockShardContext()), - equalTo(new ConstantScoreQuery(new TermQuery(new Term(PREFIX_NAME, withinBoundsTerm))))); + equalTo(new ConstantScoreQuery(new TermQuery(new Term(NAME + "._index_prefix", withinBoundsTerm))))); // our defaults don't allow a situation where a term can be too small 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 b259bb3d2108e..c4658987b7752 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 @@ -19,6 +19,7 @@ package org.elasticsearch.join.mapper; +import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.search.Query; import org.apache.lucene.util.BytesRef; @@ -26,13 +27,13 @@ import org.elasticsearch.index.fielddata.IndexFieldData; import org.elasticsearch.index.fielddata.plain.SortedSetOrdinalsIndexFieldData; import org.elasticsearch.index.mapper.FieldMapper; -import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.ParseContext; import org.elasticsearch.index.mapper.StringFieldType; import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.search.aggregations.support.CoreValuesSourceType; import java.io.IOException; +import java.util.Collections; import java.util.List; /** @@ -47,11 +48,10 @@ public class MetaJoinFieldMapper extends FieldMapper { static final String CONTENT_TYPE = "parent_join"; static class Defaults { - public static final MappedFieldType FIELD_TYPE = new MetaJoinFieldType(); + public static final FieldType FIELD_TYPE = new FieldType(); static { FIELD_TYPE.setStored(false); - FIELD_TYPE.setHasDocValues(false); FIELD_TYPE.setIndexOptions(IndexOptions.NONE); FIELD_TYPE.freeze(); } @@ -62,26 +62,29 @@ static class Builder extends FieldMapper.Builder { final String joinField; Builder(String joinField) { - super(NAME, Defaults.FIELD_TYPE, Defaults.FIELD_TYPE); + super(NAME, Defaults.FIELD_TYPE); builder = this; this.joinField = joinField; } @Override public MetaJoinFieldMapper build(BuilderContext context) { - fieldType.setName(NAME); - return new MetaJoinFieldMapper(name, joinField, (MetaJoinFieldType) fieldType, context.indexSettings()); + return new MetaJoinFieldMapper(name, joinField, context.indexSettings()); } } public static class MetaJoinFieldType extends StringFieldType { - private String joinField; + private final String joinField; - MetaJoinFieldType() {} + MetaJoinFieldType(String joinField) { + super(NAME, false, false, Collections.emptyMap()); + this.joinField = joinField; + } protected MetaJoinFieldType(MetaJoinFieldType ref) { super(ref); + this.joinField = ref.joinField; } public MetaJoinFieldType clone() { @@ -108,10 +111,6 @@ public Object valueForDisplay(Object value) { return binaryValue.utf8ToString(); } - public void setJoinField(String joinField) { - this.joinField = joinField; - } - public String getJoinField() { return joinField; } @@ -122,10 +121,8 @@ public Query existsQuery(QueryShardContext context) { } } - MetaJoinFieldMapper(String name, String joinField, MetaJoinFieldType fieldType, Settings indexSettings) { - super(name, fieldType, ParentIdFieldMapper.Defaults.FIELD_TYPE, indexSettings, MultiFields.empty(), CopyTo.empty()); - fieldType.setJoinField(joinField); - + MetaJoinFieldMapper(String name, String joinField, Settings indexSettings) { + super(name, Defaults.FIELD_TYPE, new MetaJoinFieldType(joinField), indexSettings, MultiFields.empty(), CopyTo.empty()); } @Override 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 cae435c2a1871..43b0472f7786a 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 @@ -20,6 +20,7 @@ package org.elasticsearch.join.mapper; import org.apache.lucene.document.Field; +import org.apache.lucene.document.FieldType; import org.apache.lucene.document.SortedDocValuesField; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.Term; @@ -44,6 +45,7 @@ import java.io.IOException; import java.util.Collection; import java.util.List; +import java.util.Map; import java.util.Set; /** @@ -54,12 +56,11 @@ public final class ParentIdFieldMapper extends FieldMapper { static final String CONTENT_TYPE = "parent"; static class Defaults { - static final MappedFieldType FIELD_TYPE = new ParentIdFieldType(); + static final FieldType FIELD_TYPE = new FieldType(); static { FIELD_TYPE.setTokenized(false); FIELD_TYPE.setOmitNorms(true); - FIELD_TYPE.setHasDocValues(true); FIELD_TYPE.setIndexOptions(IndexOptions.DOCS); FIELD_TYPE.freeze(); } @@ -70,7 +71,7 @@ static class Builder extends FieldMapper.Builder { private final Set children; Builder(String name, String parent, Set children) { - super(name, Defaults.FIELD_TYPE, Defaults.FIELD_TYPE); + super(name, Defaults.FIELD_TYPE); builder = this; this.parent = parent; this.children = children; @@ -81,21 +82,23 @@ public Set getChildren() { } public Builder eagerGlobalOrdinals(boolean eagerGlobalOrdinals) { - fieldType().setEagerGlobalOrdinals(eagerGlobalOrdinals); + this.eagerGlobalOrdinals = eagerGlobalOrdinals; return builder; } @Override public ParentIdFieldMapper build(BuilderContext context) { - fieldType.setName(name); - return new ParentIdFieldMapper(name, parent, children, fieldType, context.indexSettings()); + return new ParentIdFieldMapper(name, parent, children, fieldType, + new ParentIdFieldType(buildFullName(context), eagerGlobalOrdinals, meta), context.indexSettings()); } } public static final class ParentIdFieldType extends StringFieldType { - ParentIdFieldType() { + ParentIdFieldType(String name, boolean eagerGlobalOrdinals, Map meta) { + super(name, true, true, meta); setIndexAnalyzer(Lucene.KEYWORD_ANALYZER); setSearchAnalyzer(Lucene.KEYWORD_ANALYZER); + setEagerGlobalOrdinals(eagerGlobalOrdinals); } protected ParentIdFieldType(ParentIdFieldType ref) { @@ -138,9 +141,10 @@ public Query existsQuery(QueryShardContext context) { protected ParentIdFieldMapper(String simpleName, String parentName, Set children, - MappedFieldType fieldType, + FieldType fieldType, + MappedFieldType mappedFieldType, Settings indexSettings) { - super(simpleName, fieldType, Defaults.FIELD_TYPE, indexSettings, MultiFields.empty(), CopyTo.empty()); + super(simpleName, fieldType, mappedFieldType, indexSettings, MultiFields.empty(), CopyTo.empty()); this.parentName = parentName; this.children = children; } @@ -186,7 +190,7 @@ protected void parseCreateField(ParseContext context) throws IOException { } String refId = (String) context.externalValue(); BytesRef binaryValue = new BytesRef(refId); - Field field = new Field(fieldType().name(), binaryValue, fieldType()); + Field field = new Field(fieldType().name(), binaryValue, fieldType); context.doc().add(field); context.doc().add(new SortedDocValuesField(fieldType().name(), binaryValue)); } 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 cb3872cf1e1b6..93cd653e48f80 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 @@ -20,6 +20,7 @@ package org.elasticsearch.join.mapper; import org.apache.lucene.document.Field; +import org.apache.lucene.document.FieldType; import org.apache.lucene.document.SortedDocValuesField; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.search.DocValuesFieldExistsQuery; @@ -68,12 +69,11 @@ public final class ParentJoinFieldMapper extends FieldMapper { public static final String CONTENT_TYPE = "join"; public static class Defaults { - public static final MappedFieldType FIELD_TYPE = new JoinFieldType(); + public static final FieldType FIELD_TYPE = new FieldType(); static { FIELD_TYPE.setTokenized(false); FIELD_TYPE.setOmitNorms(true); - FIELD_TYPE.setHasDocValues(true); FIELD_TYPE.setIndexOptions(IndexOptions.DOCS); FIELD_TYPE.freeze(); } @@ -133,15 +133,10 @@ public static class Builder extends FieldMapper.Builder { boolean eagerGlobalOrdinals = true; public Builder(String name) { - super(name, Defaults.FIELD_TYPE, Defaults.FIELD_TYPE); + super(name, Defaults.FIELD_TYPE); builder = this; } - @Override - public JoinFieldType fieldType() { - return (JoinFieldType) super.fieldType(); - } - public Builder addParent(String parent, Set children) { String parentIdFieldName = getParentIdFieldName(name, parent); parentIdFieldBuilders.add(new ParentIdFieldMapper.Builder(parentIdFieldName, parent, children)); @@ -156,7 +151,6 @@ public Builder eagerGlobalOrdinals(boolean eagerGlobalOrdinals) { @Override public ParentJoinFieldMapper build(BuilderContext context) { checkObjectOrNested(context.path(), name); - fieldType.setName(name); final List parentIdFields = new ArrayList<>(); parentIdFieldBuilders.stream() .map((parentBuilder) -> { @@ -168,7 +162,7 @@ public ParentJoinFieldMapper build(BuilderContext context) { .forEach(parentIdFields::add); checkParentFields(name(), parentIdFields); MetaJoinFieldMapper unique = new MetaJoinFieldMapper.Builder(name).build(context); - return new ParentJoinFieldMapper(name, fieldType, context.indexSettings(), + return new ParentJoinFieldMapper(name, fieldType, new JoinFieldType(buildFullName(context), meta), context.indexSettings(), unique, Collections.unmodifiableList(parentIdFields), eagerGlobalOrdinals); } } @@ -211,7 +205,8 @@ public Mapper.Builder parse(String name, Map node, ParserCont } public static final class JoinFieldType extends StringFieldType { - public JoinFieldType() { + public JoinFieldType(String name, Map meta) { + super(name, true, true, meta); setIndexAnalyzer(Lucene.KEYWORD_ANALYZER); setSearchAnalyzer(Lucene.KEYWORD_ANALYZER); } @@ -256,12 +251,13 @@ public Query existsQuery(QueryShardContext context) { private boolean eagerGlobalOrdinals; protected ParentJoinFieldMapper(String simpleName, - MappedFieldType fieldType, + FieldType fieldType, + MappedFieldType mappedFieldType, Settings indexSettings, MetaJoinFieldMapper uniqueFieldMapper, List parentIdFields, boolean eagerGlobalOrdinals) { - super(simpleName, fieldType, Defaults.FIELD_TYPE, indexSettings, MultiFields.empty(), CopyTo.empty()); + super(simpleName, fieldType, mappedFieldType, indexSettings, MultiFields.empty(), CopyTo.empty()); this.parentIdFields = parentIdFields; this.uniqueFieldMapper = uniqueFieldMapper; this.eagerGlobalOrdinals = eagerGlobalOrdinals; @@ -420,7 +416,7 @@ public void parse(ParseContext context) throws IOException { } BytesRef binaryValue = new BytesRef(name); - Field field = new Field(fieldType().name(), binaryValue, fieldType()); + Field field = new Field(fieldType().name(), binaryValue, fieldType); context.doc().add(field); context.doc().add(new SortedDocValuesField(fieldType().name(), binaryValue)); context.path().remove(); diff --git a/modules/parent-join/src/test/java/org/elasticsearch/join/aggregations/ChildrenToParentAggregatorTests.java b/modules/parent-join/src/test/java/org/elasticsearch/join/aggregations/ChildrenToParentAggregatorTests.java index 0d7add41e5ac2..24278aaedf1a8 100644 --- a/modules/parent-join/src/test/java/org/elasticsearch/join/aggregations/ChildrenToParentAggregatorTests.java +++ b/modules/parent-join/src/test/java/org/elasticsearch/join/aggregations/ChildrenToParentAggregatorTests.java @@ -302,8 +302,7 @@ private void testCase(Query query, IndexSearcher indexSearcher, Consumer INDEX_MAP_UNMAPPED_FIELDS_AS_TEXT_SETTING = Setting.boolSetting( "index.percolator.map_unmapped_fields_as_text", false, Setting.Property.IndexScope); static final String CONTENT_TYPE = "percolator"; - private static final FieldType FIELD_TYPE = new FieldType(); static final byte FIELD_VALUE_SEPARATOR = 0; // nul code point static final String EXTRACTION_COMPLETE = "complete"; @@ -113,14 +112,14 @@ static class Builder extends FieldMapper.Builder { private final Supplier queryShardContext; Builder(String fieldName, Supplier queryShardContext) { - super(fieldName, FIELD_TYPE, FIELD_TYPE); + super(fieldName, new FieldType()); this.queryShardContext = queryShardContext; } @Override public PercolatorFieldMapper build(BuilderContext context) { + PercolatorFieldType fieldType = new PercolatorFieldType(buildFullName(context), meta); context.path().add(name()); - FieldType fieldType = (FieldType) this.fieldType; KeywordFieldMapper extractedTermsField = createExtractQueryFieldBuilder(EXTRACTED_TERMS_FIELD_NAME, context); fieldType.queryTermsField = extractedTermsField.fieldType(); KeywordFieldMapper extractionResultField = createExtractQueryFieldBuilder(EXTRACTION_RESULT_FIELD_NAME, context); @@ -136,10 +135,10 @@ public PercolatorFieldMapper build(BuilderContext context) { fieldType.mapUnmappedFieldsAsText = getMapUnmappedFieldAsText(context.indexSettings()); context.path().remove(); - setupFieldType(context); - return new PercolatorFieldMapper(name(), fieldType, defaultFieldType, context.indexSettings(), + return new PercolatorFieldMapper(name(), Builder.this.fieldType, fieldType, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo, queryShardContext, extractedTermsField, - extractionResultField, queryBuilderField, rangeFieldMapper, minimumShouldMatchFieldMapper); + extractionResultField, queryBuilderField, rangeFieldMapper, minimumShouldMatchFieldMapper, + getMapUnmappedFieldAsText(context.indexSettings())); } private static boolean getMapUnmappedFieldAsText(Settings indexSettings) { @@ -159,7 +158,6 @@ static BinaryFieldMapper createQueryBuilderFieldBuilder(BuilderContext context) builder.docValues(true); builder.indexOptions(IndexOptions.NONE); builder.store(false); - builder.fieldType().setDocValuesType(DocValuesType.BINARY); return builder.build(context); } @@ -176,7 +174,6 @@ static NumberFieldMapper createMinimumShouldMatchField(BuilderContext context) { builder.index(false); builder.store(false); builder.docValues(true); - builder.fieldType().setDocValuesType(DocValuesType.NUMERIC); return builder.build(context); } @@ -190,7 +187,7 @@ public Builder parse(String name, Map node, ParserContext parser } } - static class FieldType extends MappedFieldType { + static class PercolatorFieldType extends MappedFieldType { MappedFieldType queryTermsField; MappedFieldType extractionResultField; @@ -200,13 +197,11 @@ static class FieldType extends MappedFieldType { RangeFieldMapper.RangeFieldType rangeField; boolean mapUnmappedFieldsAsText; - FieldType() { - setIndexOptions(IndexOptions.NONE); - setDocValuesType(DocValuesType.NONE); - setStored(false); + PercolatorFieldType(String name, Map meta) { + super(name, false, false, meta); } - FieldType(FieldType ref) { + PercolatorFieldType(PercolatorFieldType ref) { super(ref); queryTermsField = ref.queryTermsField; extractionResultField = ref.extractionResultField; @@ -218,7 +213,7 @@ static class FieldType extends MappedFieldType { @Override public MappedFieldType clone() { - return new FieldType(this); + return new PercolatorFieldType(this); } @Override @@ -337,26 +332,27 @@ Tuple, Map>> extractTermsAndRanges(IndexRead } private final Supplier queryShardContext; - private KeywordFieldMapper queryTermsField; - private KeywordFieldMapper extractionResultField; - private BinaryFieldMapper queryBuilderField; - private NumberFieldMapper minimumShouldMatchFieldMapper; - - private RangeFieldMapper rangeFieldMapper; - - PercolatorFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, + private final KeywordFieldMapper queryTermsField; + private final KeywordFieldMapper extractionResultField; + private final BinaryFieldMapper queryBuilderField; + private final NumberFieldMapper minimumShouldMatchFieldMapper; + private final RangeFieldMapper rangeFieldMapper; + private final boolean mapUnmappedFieldsAsText; + + PercolatorFieldMapper(String simpleName, FieldType fieldType, MappedFieldType mappedFieldType, Settings indexSettings, MultiFields multiFields, CopyTo copyTo, Supplier queryShardContext, KeywordFieldMapper queryTermsField, KeywordFieldMapper extractionResultField, BinaryFieldMapper queryBuilderField, RangeFieldMapper rangeFieldMapper, - NumberFieldMapper minimumShouldMatchFieldMapper) { - super(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, copyTo); + NumberFieldMapper minimumShouldMatchFieldMapper, boolean mapUnmappedFieldsAsText) { + super(simpleName, fieldType, mappedFieldType, indexSettings, multiFields, copyTo); this.queryShardContext = queryShardContext; this.queryTermsField = queryTermsField; this.extractionResultField = extractionResultField; this.queryBuilderField = queryBuilderField; this.minimumShouldMatchFieldMapper = minimumShouldMatchFieldMapper; this.rangeFieldMapper = rangeFieldMapper; + this.mapUnmappedFieldsAsText = mapUnmappedFieldsAsText; } @Override @@ -401,14 +397,21 @@ static void createQueryBuilderField(Version indexVersion, BinaryFieldMapper qbFi } } + private static final FieldType INDEXED_KEYWORD = new FieldType(); + static { + INDEXED_KEYWORD.setTokenized(false); + INDEXED_KEYWORD.setIndexOptions(IndexOptions.DOCS); + INDEXED_KEYWORD.freeze(); + } + void processQuery(Query query, ParseContext context) { ParseContext.Document doc = context.doc(); - FieldType pft = (FieldType) this.fieldType(); + PercolatorFieldType pft = (PercolatorFieldType) this.fieldType(); QueryAnalyzer.Result result; Version indexVersion = context.mapperService().getIndexSettings().getIndexVersionCreated(); result = QueryAnalyzer.analyze(query, indexVersion); if (result == QueryAnalyzer.Result.UNKNOWN) { - doc.add(new Field(pft.extractionResultField.name(), EXTRACTION_FAILED, extractionResultField.fieldType())); + doc.add(new Field(pft.extractionResultField.name(), EXTRACTION_FAILED, INDEXED_KEYWORD)); return; } for (QueryAnalyzer.QueryExtraction extraction : result.extractions) { @@ -417,7 +420,7 @@ void processQuery(Query query, ParseContext context) { builder.append(new BytesRef(extraction.field())); builder.append(FIELD_VALUE_SEPARATOR); builder.append(extraction.bytes()); - doc.add(new Field(queryTermsField.name(), builder.toBytesRef(), queryTermsField.fieldType())); + doc.add(new Field(queryTermsField.name(), builder.toBytesRef(), INDEXED_KEYWORD)); } else if (extraction.range != null) { byte[] min = extraction.range.lowerPoint; byte[] max = extraction.range.upperPoint; @@ -426,14 +429,14 @@ void processQuery(Query query, ParseContext context) { } if (result.matchAllDocs) { - doc.add(new Field(extractionResultField.name(), EXTRACTION_FAILED, extractionResultField.fieldType())); + doc.add(new Field(extractionResultField.name(), EXTRACTION_FAILED, INDEXED_KEYWORD)); if (result.verified) { - doc.add(new Field(extractionResultField.name(), EXTRACTION_COMPLETE, extractionResultField.fieldType())); + doc.add(new Field(extractionResultField.name(), EXTRACTION_COMPLETE, INDEXED_KEYWORD)); } } else if (result.verified) { - doc.add(new Field(extractionResultField.name(), EXTRACTION_COMPLETE, extractionResultField.fieldType())); + doc.add(new Field(extractionResultField.name(), EXTRACTION_COMPLETE, INDEXED_KEYWORD)); } else { - doc.add(new Field(extractionResultField.name(), EXTRACTION_PARTIAL, extractionResultField.fieldType())); + doc.add(new Field(extractionResultField.name(), EXTRACTION_PARTIAL, INDEXED_KEYWORD)); } createFieldNamesField(context); @@ -488,7 +491,7 @@ protected String contentType() { } boolean isMapUnmappedFieldAsText() { - return ((FieldType) fieldType).mapUnmappedFieldsAsText; + return mapUnmappedFieldsAsText; } /** @@ -546,4 +549,13 @@ static byte[] encodeRange(String rangeFieldName, byte[] minEncoded, byte[] maxEn return bytes; } + @Override + protected boolean indexedByDefault() { + return false; + } + + @Override + protected boolean docValuesByDefault() { + return false; + } } diff --git a/modules/percolator/src/test/java/org/elasticsearch/percolator/CandidateQueryTests.java b/modules/percolator/src/test/java/org/elasticsearch/percolator/CandidateQueryTests.java index 64f052ba34a7e..e7c832103bd24 100644 --- a/modules/percolator/src/test/java/org/elasticsearch/percolator/CandidateQueryTests.java +++ b/modules/percolator/src/test/java/org/elasticsearch/percolator/CandidateQueryTests.java @@ -128,7 +128,7 @@ public class CandidateQueryTests extends ESSingleNodeTestCase { private MapperService mapperService; private PercolatorFieldMapper fieldMapper; - private PercolatorFieldMapper.FieldType fieldType; + private PercolatorFieldMapper.PercolatorFieldType fieldType; private List queries; private PercolateQuery.QueryStore queryStore; @@ -167,7 +167,7 @@ public void init() throws Exception { .endObject().endObject()); mapperService.merge("type", new CompressedXContent(percolatorMapper), MapperService.MergeReason.MAPPING_UPDATE); fieldMapper = (PercolatorFieldMapper) mapperService.documentMapper().mappers().getMapper(queryField); - fieldType = (PercolatorFieldMapper.FieldType) fieldMapper.fieldType(); + fieldType = (PercolatorFieldMapper.PercolatorFieldType) fieldMapper.fieldType(); queries = new ArrayList<>(); queryStore = ctx -> docId -> this.queries.get(docId); diff --git a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorFieldMapperTests.java b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorFieldMapperTests.java index 69316566e2d6e..b174be14bcdd1 100644 --- a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorFieldMapperTests.java +++ b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorFieldMapperTests.java @@ -128,7 +128,7 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase { private String fieldName; private IndexService indexService; private MapperService mapperService; - private PercolatorFieldMapper.FieldType fieldType; + private PercolatorFieldMapper.PercolatorFieldType fieldType; @Override protected Collection> getPlugins() { @@ -170,7 +170,7 @@ private void addQueryFieldMappings() throws Exception { .startObject("properties").startObject(fieldName).field("type", "percolator").endObject().endObject() .endObject().endObject()); mapperService.merge("doc", new CompressedXContent(percolatorMapper), MapperService.MergeReason.MAPPING_UPDATE); - fieldType = (PercolatorFieldMapper.FieldType) mapperService.fieldType(fieldName); + fieldType = (PercolatorFieldMapper.PercolatorFieldType) mapperService.fieldType(fieldName); } public void testExtractTerms() throws Exception { @@ -192,7 +192,7 @@ public void testExtractTerms() throws Exception { fieldMapper.processQuery(bq.build(), parseContext); ParseContext.Document document = parseContext.doc(); - PercolatorFieldMapper.FieldType fieldType = (PercolatorFieldMapper.FieldType) fieldMapper.fieldType(); + PercolatorFieldMapper.PercolatorFieldType fieldType = (PercolatorFieldMapper.PercolatorFieldType) fieldMapper.fieldType(); assertThat(document.getField(fieldType.extractionResultField.name()).stringValue(), equalTo(EXTRACTION_COMPLETE)); List fields = new ArrayList<>(Arrays.asList(document.getFields(fieldType.queryTermsField.name()))); fields.sort(Comparator.comparing(IndexableField::binaryValue)); @@ -248,7 +248,7 @@ public void testExtractRanges() throws Exception { fieldMapper.processQuery(bq.build(), parseContext); ParseContext.Document document = parseContext.doc(); - PercolatorFieldMapper.FieldType fieldType = (PercolatorFieldMapper.FieldType) fieldMapper.fieldType(); + PercolatorFieldMapper.PercolatorFieldType fieldType = (PercolatorFieldMapper.PercolatorFieldType) fieldMapper.fieldType(); assertThat(document.getField(fieldType.extractionResultField.name()).stringValue(), equalTo(EXTRACTION_PARTIAL)); List fields = new ArrayList<>(Arrays.asList(document.getFields(fieldType.rangeField.name()))); fields.sort(Comparator.comparing(IndexableField::binaryValue)); @@ -302,7 +302,7 @@ public void testExtractTermsAndRanges_failed() throws Exception { fieldMapper.processQuery(query, parseContext); ParseContext.Document document = parseContext.doc(); - PercolatorFieldMapper.FieldType fieldType = (PercolatorFieldMapper.FieldType) fieldMapper.fieldType(); + PercolatorFieldMapper.PercolatorFieldType fieldType = (PercolatorFieldMapper.PercolatorFieldType) fieldMapper.fieldType(); assertThat(document.getFields().size(), equalTo(1)); assertThat(document.getField(fieldType.extractionResultField.name()).stringValue(), equalTo(EXTRACTION_FAILED)); } @@ -321,7 +321,7 @@ public void testExtractTermsAndRanges_partial() throws Exception { fieldMapper.processQuery(phraseQuery, parseContext); ParseContext.Document document = parseContext.doc(); - PercolatorFieldMapper.FieldType fieldType = (PercolatorFieldMapper.FieldType) fieldMapper.fieldType(); + PercolatorFieldMapper.PercolatorFieldType fieldType = (PercolatorFieldMapper.PercolatorFieldType) fieldMapper.fieldType(); assertThat(document.getFields().size(), equalTo(4)); assertThat(document.getFields().get(0).binaryValue().utf8ToString(), equalTo("field\u0000term")); assertThat(document.getField(fieldType.extractionResultField.name()).stringValue(), equalTo(EXTRACTION_PARTIAL)); diff --git a/modules/percolator/src/test/java/org/elasticsearch/percolator/QueryBuilderStoreTests.java b/modules/percolator/src/test/java/org/elasticsearch/percolator/QueryBuilderStoreTests.java index ef115ec4896a0..fec3f8969ead0 100644 --- a/modules/percolator/src/test/java/org/elasticsearch/percolator/QueryBuilderStoreTests.java +++ b/modules/percolator/src/test/java/org/elasticsearch/percolator/QueryBuilderStoreTests.java @@ -98,10 +98,7 @@ public void testStoringQueryBuilders() throws IOException { .thenReturn(new BytesBinaryIndexFieldData(new Index("index", "uuid"), fieldMapper.name(), CoreValuesSourceType.BYTES)); when(queryShardContext.fieldMapper(Mockito.anyString())).thenAnswer(invocation -> { final String fieldName = (String) invocation.getArguments()[0]; - KeywordFieldMapper.KeywordFieldType ft = new KeywordFieldMapper.KeywordFieldType(); - ft.setName(fieldName); - ft.freeze(); - return ft; + return new KeywordFieldMapper.KeywordFieldType(fieldName); }); PercolateQuery.QueryStore queryStore = PercolateQueryBuilder.createStore(fieldMapper.fieldType(), queryShardContext); diff --git a/plugins/analysis-icu/src/main/java/org/elasticsearch/index/mapper/ICUCollationKeywordFieldMapper.java b/plugins/analysis-icu/src/main/java/org/elasticsearch/index/mapper/ICUCollationKeywordFieldMapper.java index dca847e3d7eb8..aff74a999896f 100644 --- a/plugins/analysis-icu/src/main/java/org/elasticsearch/index/mapper/ICUCollationKeywordFieldMapper.java +++ b/plugins/analysis-icu/src/main/java/org/elasticsearch/index/mapper/ICUCollationKeywordFieldMapper.java @@ -24,6 +24,7 @@ import com.ibm.icu.text.RuleBasedCollator; import com.ibm.icu.util.ULocale; import org.apache.lucene.document.Field; +import org.apache.lucene.document.FieldType; import org.apache.lucene.document.SortedSetDocValuesField; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.Term; @@ -49,6 +50,7 @@ import java.io.IOException; import java.time.ZoneId; +import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -59,7 +61,7 @@ public class ICUCollationKeywordFieldMapper extends FieldMapper { public static final String CONTENT_TYPE = "icu_collation_keyword"; public static class Defaults { - public static final MappedFieldType FIELD_TYPE = new CollationFieldType(); + public static final FieldType FIELD_TYPE = new FieldType(); static { FIELD_TYPE.setTokenized(false); @@ -73,11 +75,17 @@ public static class Defaults { } public static final class CollationFieldType extends StringFieldType { - private Collator collator = null; + private final Collator collator; - public CollationFieldType() { + public CollationFieldType(String name, boolean isSearchable, boolean hasDocValues, Collator collator, Map meta) { + super(name, isSearchable, hasDocValues, meta); setIndexAnalyzer(Lucene.KEYWORD_ANALYZER); setSearchAnalyzer(Lucene.KEYWORD_ANALYZER); + this.collator = collator; + } + + public CollationFieldType(String name, Collator collator) { + this(name, true, true, collator, Collections.emptyMap()); } protected CollationFieldType(CollationFieldType ref) { @@ -109,11 +117,6 @@ public Collator collator() { return collator; } - public void setCollator(Collator collator) { - checkIfFrozen(); - this.collator = collator.isFrozen() ? collator : collator.freeze(); - } - @Override public Query existsQuery(QueryShardContext context) { if (hasDocValues()) { @@ -218,17 +221,13 @@ public static class Builder extends FieldMapper.Builder { private String variableTop = null; private boolean hiraganaQuaternaryMode = false; protected int ignoreAbove = Defaults.IGNORE_ABOVE; + protected String nullValue; public Builder(String name) { - super(name, Defaults.FIELD_TYPE, Defaults.FIELD_TYPE); + super(name, Defaults.FIELD_TYPE); builder = this; } - @Override - public CollationFieldType fieldType() { - return (CollationFieldType) super.fieldType(); - } - @Override public Builder indexOptions(IndexOptions indexOptions) { if (indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS) > 0) { @@ -355,6 +354,11 @@ public Builder hiraganaQuaternaryMode(final boolean hiraganaQuaternaryMode) { return this; } + public Builder nullValue(String nullValue) { + this.nullValue = nullValue; + return this; + } + public Collator buildCollator() { Collator collator; if (rules != null) { @@ -454,11 +458,10 @@ public Collator buildCollator() { @Override public ICUCollationKeywordFieldMapper build(BuilderContext context) { final Collator collator = buildCollator(); - fieldType().setCollator(collator); - setupFieldType(context); - return new ICUCollationKeywordFieldMapper(name, fieldType, defaultFieldType, context.indexSettings(), + CollationFieldType ft = new CollationFieldType(buildFullName(context), indexed, hasDocValues, collator, meta); + return new ICUCollationKeywordFieldMapper(name, fieldType, ft, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo, rules, language, country, variant, strength, decomposition, - alternate, caseLevel, caseFirst, numeric, variableTop, hiraganaQuaternaryMode, ignoreAbove, collator); + alternate, caseLevel, caseFirst, numeric, variableTop, hiraganaQuaternaryMode, ignoreAbove, collator, nullValue); } } @@ -559,14 +562,15 @@ public Mapper.Builder parse(String name, Map node, ParserCont private final boolean hiraganaQuaternaryMode; private int ignoreAbove; private final Collator collator; + private final String nullValue; - protected ICUCollationKeywordFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, + protected ICUCollationKeywordFieldMapper(String simpleName, FieldType fieldType, MappedFieldType mappedFieldType, Settings indexSettings, MultiFields multiFields, CopyTo copyTo, String rules, String language, String country, String variant, String strength, String decomposition, String alternate, boolean caseLevel, String caseFirst, boolean numeric, String variableTop, boolean hiraganaQuaternaryMode, - int ignoreAbove, Collator collator) { - super(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, copyTo); + int ignoreAbove, Collator collator, String nullValue) { + super(simpleName, fieldType, mappedFieldType, indexSettings, multiFields, copyTo); assert collator.isFrozen(); this.rules = rules; this.language = language; @@ -582,6 +586,7 @@ protected ICUCollationKeywordFieldMapper(String simpleName, MappedFieldType fiel this.hiraganaQuaternaryMode = hiraganaQuaternaryMode; this.ignoreAbove = ignoreAbove; this.collator = collator; + this.nullValue = nullValue; } @Override @@ -654,11 +659,15 @@ protected void mergeOptions(FieldMapper other, List conflicts) { @Override protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException { super.doXContentBody(builder, includeDefaults, params); - - if (includeDefaults || fieldType().nullValue() != null) { - builder.field("null_value", fieldType().nullValue()); + if (includeDefaults || (mappedFieldType.isSearchable() && fieldType.indexOptions() != IndexOptions.DOCS)) { + builder.field("index_options", indexOptionToString(fieldType.indexOptions())); + } + if (nullValue != null) { + builder.field("null_value", nullValue); + } + if (includeDefaults || fieldType.omitNorms() != KeywordFieldMapper.Defaults.FIELD_TYPE.omitNorms()) { + builder.field("norms", fieldType.omitNorms() == false); } - if (includeDefaults || rules != null) { builder.field("rules", rules); } @@ -720,7 +729,7 @@ protected void parseCreateField(ParseContext context) throws IOException { } else { XContentParser parser = context.parser(); if (parser.currentToken() == XContentParser.Token.VALUE_NULL) { - value = fieldType().nullValueAsString(); + value = nullValue; } else { value = parser.textOrNull(); } @@ -733,14 +742,14 @@ protected void parseCreateField(ParseContext context) throws IOException { RawCollationKey key = collator.getRawCollationKey(value, null); final BytesRef binaryValue = new BytesRef(key.bytes, 0, key.size); - if (fieldType().indexOptions() != IndexOptions.NONE || fieldType().stored()) { - Field field = new Field(fieldType().name(), binaryValue, fieldType()); + if (fieldType.indexOptions() != IndexOptions.NONE || fieldType.stored()) { + Field field = new Field(mappedFieldType.name(), binaryValue, fieldType); context.doc().add(field); } if (fieldType().hasDocValues()) { context.doc().add(new SortedSetDocValuesField(fieldType().name(), binaryValue)); - } else if (fieldType().indexOptions() != IndexOptions.NONE || fieldType().stored()) { + } else if (fieldType.indexOptions() != IndexOptions.NONE || fieldType.stored()) { createFieldNamesField(context); } } diff --git a/plugins/analysis-icu/src/test/java/org/elasticsearch/index/mapper/CollationFieldTypeTests.java b/plugins/analysis-icu/src/test/java/org/elasticsearch/index/mapper/CollationFieldTypeTests.java index 3e7d9c564b615..5e91524ee8282 100644 --- a/plugins/analysis-icu/src/test/java/org/elasticsearch/index/mapper/CollationFieldTypeTests.java +++ b/plugins/analysis-icu/src/test/java/org/elasticsearch/index/mapper/CollationFieldTypeTests.java @@ -22,7 +22,6 @@ import com.ibm.icu.text.Collator; import com.ibm.icu.text.RawCollationKey; import com.ibm.icu.util.ULocale; -import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.Term; import org.apache.lucene.search.TermInSetQuery; import org.apache.lucene.search.TermQuery; @@ -36,16 +35,21 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; +import java.util.Map; public class CollationFieldTypeTests extends FieldTypeTestCase { + + private static final Collator DEFAULT_COLLATOR = Collator.getInstance(ULocale.ROOT).freeze(); + @Override - protected MappedFieldType createDefaultFieldType() { - return new CollationFieldType(); + protected MappedFieldType createDefaultFieldType(String name, Map meta) { + return new CollationFieldType(name, true, true, DEFAULT_COLLATOR, meta); } public void testIsFieldWithinQuery() throws IOException { - CollationFieldType ft = new CollationFieldType(); + CollationFieldType ft = new CollationFieldType("field", DEFAULT_COLLATOR); // current impl ignores args and shourd always return INTERSECTS assertEquals(Relation.INTERSECTS, ft.isFieldWithinQuery(null, RandomStrings.randomAsciiOfLengthBetween(random(), 0, 5), @@ -54,33 +58,25 @@ public void testIsFieldWithinQuery() throws IOException { } public void testTermQuery() { - MappedFieldType ft = createDefaultFieldType(); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); - Collator collator = Collator.getInstance(new ULocale("tr")); collator.setStrength(Collator.PRIMARY); collator.freeze(); - ((CollationFieldType) ft).setCollator(collator); + MappedFieldType ft = new CollationFieldType("field", collator); RawCollationKey key = collator.getRawCollationKey("ı will use turkish casıng", null); BytesRef expected = new BytesRef(key.bytes, 0, key.size); assertEquals(new TermQuery(new Term("field", expected)), ft.termQuery("I WİLL USE TURKİSH CASING", null)); - ft.setIndexOptions(IndexOptions.NONE); + MappedFieldType unsearchable = new CollationFieldType("field", false, true, collator, Collections.emptyMap()); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> ft.termQuery("bar", null)); + () -> unsearchable.termQuery("bar", null)); assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage()); } public void testTermsQuery() { - MappedFieldType ft = createDefaultFieldType(); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); - - Collator collator = Collator.getInstance(ULocale.ROOT).freeze(); - ((CollationFieldType) ft).setCollator(collator); + Collator collator = DEFAULT_COLLATOR; + MappedFieldType ft = new CollationFieldType("field", collator); RawCollationKey fooKey = collator.getRawCollationKey("foo", null); RawCollationKey barKey = collator.getRawCollationKey("bar", null); @@ -92,56 +88,43 @@ public void testTermsQuery() { assertEquals(new TermInSetQuery("field", terms), ft.termsQuery(Arrays.asList("foo", "bar"), null)); - ft.setIndexOptions(IndexOptions.NONE); + MappedFieldType unsearchable = new CollationFieldType("field", false, true, collator, Collections.emptyMap()); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> ft.termsQuery(Arrays.asList("foo", "bar"), null)); + () -> unsearchable.termsQuery(Arrays.asList("foo", "bar"), null)); assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage()); } public void testRegexpQuery() { - MappedFieldType ft = createDefaultFieldType(); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ft = new CollationFieldType("field", DEFAULT_COLLATOR); UnsupportedOperationException e = expectThrows(UnsupportedOperationException.class, () -> ft.regexpQuery("foo.*", 0, 10, null, randomMockShardContext())); assertEquals("[regexp] queries are not supported on [icu_collation_keyword] fields.", e.getMessage()); } public void testFuzzyQuery() { - MappedFieldType ft = createDefaultFieldType(); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ft = new CollationFieldType("field", DEFAULT_COLLATOR); UnsupportedOperationException e = expectThrows(UnsupportedOperationException.class, () -> ft.fuzzyQuery("foo", Fuzziness.fromEdits(2), 1, 50, true, randomMockShardContext())); assertEquals("[fuzzy] queries are not supported on [icu_collation_keyword] fields.", e.getMessage()); } public void testPrefixQuery() { - MappedFieldType ft = createDefaultFieldType(); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ft = new CollationFieldType("field", DEFAULT_COLLATOR); UnsupportedOperationException e = expectThrows(UnsupportedOperationException.class, () -> ft.prefixQuery("prefix", null, randomMockShardContext())); assertEquals("[prefix] queries are not supported on [icu_collation_keyword] fields.", e.getMessage()); } public void testWildcardQuery() { - MappedFieldType ft = createDefaultFieldType(); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ft = new CollationFieldType("field", DEFAULT_COLLATOR); UnsupportedOperationException e = expectThrows(UnsupportedOperationException.class, () -> ft.wildcardQuery("foo*", null, randomMockShardContext())); assertEquals("[wildcard] queries are not supported on [icu_collation_keyword] fields.", e.getMessage()); } public void testRangeQuery() { - MappedFieldType ft = createDefaultFieldType(); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); - - Collator collator = Collator.getInstance(ULocale.ROOT).freeze(); - ((CollationFieldType) ft).setCollator(collator); - + Collator collator = DEFAULT_COLLATOR; + MappedFieldType ft = new CollationFieldType("field", collator); RawCollationKey aKey = collator.getRawCollationKey("a", null); RawCollationKey bKey = collator.getRawCollationKey("b", null); @@ -155,9 +138,9 @@ public void testRangeQuery() { assertEquals("[range] queries on [text] or [keyword] fields cannot be executed when " + "'search.allow_expensive_queries' is set to false.", ee.getMessage()); - ft.setIndexOptions(IndexOptions.NONE); + MappedFieldType unsearchable = new CollationFieldType("field", false, true, collator, Collections.emptyMap()); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> ft.rangeQuery("a", "b", false, false, null, null, null, MOCK_QSC)); + () -> unsearchable.rangeQuery("a", "b", false, false, null, null, null, MOCK_QSC)); assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage()); } } diff --git a/plugins/analysis-icu/src/test/java/org/elasticsearch/index/mapper/ICUCollationKeywordFieldMapperTests.java b/plugins/analysis-icu/src/test/java/org/elasticsearch/index/mapper/ICUCollationKeywordFieldMapperTests.java index 0384ed2f6780d..21a9cce88fe54 100644 --- a/plugins/analysis-icu/src/test/java/org/elasticsearch/index/mapper/ICUCollationKeywordFieldMapperTests.java +++ b/plugins/analysis-icu/src/test/java/org/elasticsearch/index/mapper/ICUCollationKeywordFieldMapperTests.java @@ -41,6 +41,7 @@ import java.io.IOException; import java.util.Arrays; import java.util.Collection; +import java.util.Set; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; @@ -59,6 +60,11 @@ protected ICUCollationKeywordFieldMapper.Builder newBuilder() { return new ICUCollationKeywordFieldMapper.Builder("icu"); } + @Override + protected Set unsupportedProperties() { + return Set.of("analyzer", "similarity"); + } + IndexService indexService; DocumentMapperParser parser; diff --git a/plugins/mapper-annotated-text/src/main/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextFieldMapper.java b/plugins/mapper-annotated-text/src/main/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextFieldMapper.java index 095d7bbd16b2f..d30cfe86b4574 100644 --- a/plugins/mapper-annotated-text/src/main/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextFieldMapper.java +++ b/plugins/mapper-annotated-text/src/main/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextFieldMapper.java @@ -29,6 +29,7 @@ import org.apache.lucene.analysis.tokenattributes.PositionLengthAttribute; import org.apache.lucene.analysis.tokenattributes.TypeAttribute; import org.apache.lucene.document.Field; +import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexOptions; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.settings.Settings; @@ -37,7 +38,6 @@ import org.elasticsearch.index.analysis.AnalyzerScope; import org.elasticsearch.index.analysis.NamedAnalyzer; import org.elasticsearch.index.mapper.FieldMapper; -import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.Mapper; import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.index.mapper.ParseContext; @@ -79,8 +79,9 @@ public class AnnotatedTextFieldMapper extends FieldMapper { private static final int POSITION_INCREMENT_GAP_USE_ANALYZER = -1; public static class Defaults { - public static final MappedFieldType FIELD_TYPE = new AnnotatedTextFieldType(); + public static final FieldType FIELD_TYPE = new FieldType(); static { + FIELD_TYPE.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS); FIELD_TYPE.freeze(); } } @@ -90,15 +91,10 @@ public static class Builder extends FieldMapper.Builder { private int positionIncrementGap = POSITION_INCREMENT_GAP_USE_ANALYZER; public Builder(String name) { - super(name, Defaults.FIELD_TYPE, Defaults.FIELD_TYPE); + super(name, Defaults.FIELD_TYPE); builder = this; } - @Override - public AnnotatedTextFieldType fieldType() { - return (AnnotatedTextFieldType) super.fieldType(); - } - public Builder positionIncrementGap(int positionIncrementGap) { if (positionIncrementGap < 0) { throw new MapperParsingException("[positions_increment_gap] must be positive, got " + positionIncrementGap); @@ -115,32 +111,37 @@ public Builder docValues(boolean docValues) { return super.docValues(docValues); } - @Override - public AnnotatedTextFieldMapper build(BuilderContext context) { - if (fieldType().indexOptions() == IndexOptions.NONE ) { - throw new IllegalArgumentException("[" + CONTENT_TYPE + "] fields must be indexed"); - } + private AnnotatedTextFieldType buildFieldType(BuilderContext context) { + boolean hasPositions = fieldType.indexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0; + AnnotatedTextFieldType ft = new AnnotatedTextFieldType(buildFullName(context), hasPositions, meta); if (positionIncrementGap != POSITION_INCREMENT_GAP_USE_ANALYZER) { if (fieldType.indexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) < 0) { throw new IllegalArgumentException("Cannot set position_increment_gap on field [" + name + "] without positions enabled"); } - fieldType.setIndexAnalyzer(new NamedAnalyzer(fieldType.indexAnalyzer(), positionIncrementGap)); - fieldType.setSearchAnalyzer(new NamedAnalyzer(fieldType.searchAnalyzer(), positionIncrementGap)); - fieldType.setSearchQuoteAnalyzer(new NamedAnalyzer(fieldType.searchQuoteAnalyzer(), positionIncrementGap)); + ft.setIndexAnalyzer(indexAnalyzer, positionIncrementGap); + ft.setSearchAnalyzer(new NamedAnalyzer(searchAnalyzer, positionIncrementGap)); + ft.setSearchQuoteAnalyzer(new NamedAnalyzer(searchQuoteAnalyzer, positionIncrementGap)); } else { //Using the analyzer's default BUT need to do the same thing AnalysisRegistry.processAnalyzerFactory // does to splice in new default of posIncGap=100 by wrapping the analyzer if (fieldType.indexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0) { int overrideInc = TextFieldMapper.Defaults.POSITION_INCREMENT_GAP; - fieldType.setIndexAnalyzer(new NamedAnalyzer(fieldType.indexAnalyzer(), overrideInc)); - fieldType.setSearchAnalyzer(new NamedAnalyzer(fieldType.searchAnalyzer(), overrideInc)); - fieldType.setSearchQuoteAnalyzer(new NamedAnalyzer(fieldType.searchQuoteAnalyzer(),overrideInc)); + ft.setIndexAnalyzer(indexAnalyzer, overrideInc); + ft.setSearchAnalyzer(new NamedAnalyzer(searchAnalyzer, overrideInc)); + ft.setSearchQuoteAnalyzer(new NamedAnalyzer(searchQuoteAnalyzer,overrideInc)); } } - setupFieldType(context); + return ft; + } + + @Override + public AnnotatedTextFieldMapper build(BuilderContext context) { + if (fieldType.indexOptions() == IndexOptions.NONE ) { + throw new IllegalArgumentException("[" + CONTENT_TYPE + "] fields must be indexed"); + } return new AnnotatedTextFieldMapper( - name, fieldType(), defaultFieldType, positionIncrementGap, + name, fieldType, buildFieldType(context), positionIncrementGap, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); } } @@ -150,10 +151,9 @@ public static class TypeParser implements Mapper.TypeParser { public Mapper.Builder parse( String fieldName, Map node, ParserContext parserContext) throws MapperParsingException { AnnotatedTextFieldMapper.Builder builder = new AnnotatedTextFieldMapper.Builder(fieldName); - - builder.fieldType().setIndexAnalyzer(parserContext.getIndexAnalyzers().getDefaultIndexAnalyzer()); - builder.fieldType().setSearchAnalyzer(parserContext.getIndexAnalyzers().getDefaultSearchAnalyzer()); - builder.fieldType().setSearchQuoteAnalyzer(parserContext.getIndexAnalyzers().getDefaultSearchQuoteAnalyzer()); + builder.indexAnalyzer(parserContext.getIndexAnalyzers().getDefaultIndexAnalyzer()); + builder.searchAnalyzer(parserContext.getIndexAnalyzers().getDefaultSearchAnalyzer()); + builder.searchQuoteAnalyzer(parserContext.getIndexAnalyzers().getDefaultSearchQuoteAnalyzer()); parseTextField(builder, fieldName, node, parserContext); for (Iterator> iterator = node.entrySet().iterator(); iterator.hasNext();) { Map.Entry entry = iterator.next(); @@ -522,23 +522,22 @@ private void emitAnnotation(int firstSpannedTextPosInc, int annotationPosLen) th public static final class AnnotatedTextFieldType extends TextFieldMapper.TextFieldType { - public AnnotatedTextFieldType() { - setTokenized(true); + public AnnotatedTextFieldType(String name, boolean hasPositions, Map meta) { + super(name, true, hasPositions, meta); } protected AnnotatedTextFieldType(AnnotatedTextFieldType ref) { super(ref); } - @Override - public void setIndexAnalyzer(NamedAnalyzer delegate) { + public void setIndexAnalyzer(NamedAnalyzer delegate, int positionIncrementGap) { if(delegate.analyzer() instanceof AnnotationAnalyzerWrapper){ // Already wrapped the Analyzer with an AnnotationAnalyzer super.setIndexAnalyzer(delegate); } else { // Wrap the analyzer with an AnnotationAnalyzer that will inject required annotations super.setIndexAnalyzer(new NamedAnalyzer(delegate.name(), AnalyzerScope.INDEX, - new AnnotationAnalyzerWrapper(delegate.analyzer()))); + new AnnotationAnalyzerWrapper(delegate.analyzer()), positionIncrementGap)); } } @@ -554,12 +553,11 @@ public String typeName() { } private int positionIncrementGap; - protected AnnotatedTextFieldMapper(String simpleName, AnnotatedTextFieldType fieldType, MappedFieldType defaultFieldType, + protected AnnotatedTextFieldMapper(String simpleName, FieldType fieldType, AnnotatedTextFieldType mappedFieldType, int positionIncrementGap, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { - super(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, copyTo); + super(simpleName, fieldType, mappedFieldType, indexSettings, multiFields, copyTo); assert fieldType.tokenized(); - assert fieldType.hasDocValues() == false; this.positionIncrementGap = positionIncrementGap; } @@ -590,10 +588,10 @@ protected void parseCreateField(ParseContext context) throws IOException { return; } - if (fieldType().indexOptions() != IndexOptions.NONE || fieldType().stored()) { - Field field = new Field(fieldType().name(), value, fieldType()); + if (fieldType.indexOptions() != IndexOptions.NONE || fieldType.stored()) { + Field field = new Field(mappedFieldType.name(), value, fieldType); context.doc().add(field); - if (fieldType().omitNorms()) { + if (fieldType.omitNorms()) { createFieldNamesField(context); } } @@ -609,11 +607,18 @@ public AnnotatedTextFieldType fieldType() { return (AnnotatedTextFieldType) super.fieldType(); } + @Override + protected boolean docValuesByDefault() { + return false; + } + @Override protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException { super.doXContentBody(builder, includeDefaults, params); doXContentAnalyzers(builder, includeDefaults); - + if (includeDefaults || fieldType.omitNorms()) { + builder.field("norms", fieldType.omitNorms() == false); + } if (includeDefaults || positionIncrementGap != POSITION_INCREMENT_GAP_USE_ANALYZER) { builder.field("position_increment_gap", positionIncrementGap); } diff --git a/plugins/mapper-annotated-text/src/test/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextFieldTypeTests.java b/plugins/mapper-annotated-text/src/test/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextFieldTypeTests.java index 83af5cc6a053b..b227ba9b1432a 100644 --- a/plugins/mapper-annotated-text/src/test/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextFieldTypeTests.java +++ b/plugins/mapper-annotated-text/src/test/java/org/elasticsearch/index/mapper/annotatedtext/AnnotatedTextFieldTypeTests.java @@ -28,15 +28,17 @@ import org.elasticsearch.index.mapper.MappedFieldType; import java.io.IOException; +import java.util.Collections; +import java.util.Map; public class AnnotatedTextFieldTypeTests extends FieldTypeTestCase { @Override - protected MappedFieldType createDefaultFieldType() { - return new AnnotatedTextFieldMapper.AnnotatedTextFieldType(); + protected MappedFieldType createDefaultFieldType(String name, Map meta) { + return new AnnotatedTextFieldMapper.AnnotatedTextFieldType(name, true, meta); } public void testIntervals() throws IOException { - MappedFieldType ft = createDefaultFieldType(); + MappedFieldType ft = createDefaultFieldType("field", Collections.emptyMap()); NamedAnalyzer a = new NamedAnalyzer("name", AnalyzerScope.INDEX, new StandardAnalyzer()); IntervalsSource source = ft.intervals("Donald Trump", 0, true, a, false); assertEquals(Intervals.phrase(Intervals.term("donald"), Intervals.term("trump")), source); diff --git a/plugins/mapper-murmur3/src/main/java/org/elasticsearch/index/mapper/murmur3/Murmur3FieldMapper.java b/plugins/mapper-murmur3/src/main/java/org/elasticsearch/index/mapper/murmur3/Murmur3FieldMapper.java index 2815c108de0ba..fe89d463b874f 100644 --- a/plugins/mapper-murmur3/src/main/java/org/elasticsearch/index/mapper/murmur3/Murmur3FieldMapper.java +++ b/plugins/mapper-murmur3/src/main/java/org/elasticsearch/index/mapper/murmur3/Murmur3FieldMapper.java @@ -19,6 +19,7 @@ package org.elasticsearch.index.mapper.murmur3; +import org.apache.lucene.document.FieldType; import org.apache.lucene.document.SortedNumericDocValuesField; import org.apache.lucene.document.StoredField; import org.apache.lucene.index.IndexOptions; @@ -48,8 +49,9 @@ public class Murmur3FieldMapper extends FieldMapper { public static final String CONTENT_TYPE = "murmur3"; public static class Defaults { - public static final MappedFieldType FIELD_TYPE = new Murmur3FieldType(); + public static final FieldType FIELD_TYPE = new FieldType(); static { + FIELD_TYPE.setIndexOptions(IndexOptions.NONE); FIELD_TYPE.freeze(); } } @@ -57,25 +59,15 @@ public static class Defaults { public static class Builder extends FieldMapper.Builder { public Builder(String name) { - super(name, Defaults.FIELD_TYPE, Defaults.FIELD_TYPE); + super(name, Defaults.FIELD_TYPE); builder = this; } @Override public Murmur3FieldMapper build(BuilderContext context) { - setupFieldType(context); - return new Murmur3FieldMapper(name, fieldType, defaultFieldType, + return new Murmur3FieldMapper(name, fieldType, new Murmur3FieldType(buildFullName(context), meta), context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); } - - @Override - protected void setupFieldType(BuilderContext context) { - super.setupFieldType(context); - fieldType.setIndexOptions(IndexOptions.NONE); - defaultFieldType.setIndexOptions(IndexOptions.NONE); - fieldType.setHasDocValues(true); - defaultFieldType.setHasDocValues(true); - } } public static class TypeParser implements Mapper.TypeParser { @@ -100,7 +92,8 @@ public Mapper.Builder parse(String name, Map node, ParserCont // this only exists so a check can be done to match the field type to using murmur3 hashing... public static class Murmur3FieldType extends MappedFieldType { - public Murmur3FieldType() { + public Murmur3FieldType(String name, Map meta) { + super(name, false, true, meta); } protected Murmur3FieldType(Murmur3FieldType ref) { @@ -134,9 +127,9 @@ public Query termQuery(Object value, QueryShardContext context) { } } - protected Murmur3FieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, + protected Murmur3FieldMapper(String simpleName, FieldType fieldType, MappedFieldType mappedFieldType, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { - super(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, copyTo); + super(simpleName, fieldType, mappedFieldType, indexSettings, multiFields, copyTo); } @Override @@ -157,12 +150,17 @@ protected void parseCreateField(ParseContext context) final BytesRef bytes = new BytesRef(value.toString()); final long hash = MurmurHash3.hash128(bytes.bytes, bytes.offset, bytes.length, 0, new MurmurHash3.Hash128()).h1; context.doc().add(new SortedNumericDocValuesField(fieldType().name(), hash)); - if (fieldType().stored()) { + if (fieldType.stored()) { context.doc().add(new StoredField(name(), hash)); } } } + @Override + protected boolean indexedByDefault() { + return false; + } + @Override protected void mergeOptions(FieldMapper other, List conflicts) { diff --git a/plugins/mapper-murmur3/src/test/java/org/elasticsearch/index/mapper/murmur3/Murmur3FieldMapperTests.java b/plugins/mapper-murmur3/src/test/java/org/elasticsearch/index/mapper/murmur3/Murmur3FieldMapperTests.java index 8360b2cc7484e..be4645574d4c5 100644 --- a/plugins/mapper-murmur3/src/test/java/org/elasticsearch/index/mapper/murmur3/Murmur3FieldMapperTests.java +++ b/plugins/mapper-murmur3/src/test/java/org/elasticsearch/index/mapper/murmur3/Murmur3FieldMapperTests.java @@ -30,30 +30,37 @@ import org.elasticsearch.index.IndexService; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.DocumentMapperParser; +import org.elasticsearch.index.mapper.FieldMapperTestCase; import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.index.mapper.ParsedDocument; import org.elasticsearch.index.mapper.SourceToParse; import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.indices.mapper.MapperRegistry; +import org.elasticsearch.plugin.mapper.MapperMurmur3Plugin; import org.elasticsearch.plugins.MapperPlugin; import org.elasticsearch.plugins.Plugin; -import org.elasticsearch.test.ESSingleNodeTestCase; import org.elasticsearch.test.InternalSettingsPlugin; import org.junit.Before; import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.Set; import java.util.function.Supplier; import static org.hamcrest.Matchers.containsString; -public class Murmur3FieldMapperTests extends ESSingleNodeTestCase { +public class Murmur3FieldMapperTests extends FieldMapperTestCase { MapperRegistry mapperRegistry; IndexService indexService; DocumentMapperParser parser; + @Override + protected Set unsupportedProperties() { + return Set.of("analyzer", "similarity", "doc_values"); + } + @Before public void setup() { indexService = createIndex("test"); @@ -69,7 +76,7 @@ public void setup() { @Override protected Collection> getPlugins() { - return pluginList(InternalSettingsPlugin.class); + return pluginList(InternalSettingsPlugin.class, MapperMurmur3Plugin.class); } public void testDefaults() throws Exception { @@ -156,4 +163,9 @@ public void testEmptyName() throws Exception { ); assertThat(e.getMessage(), containsString("name cannot be empty string")); } + + @Override + protected Murmur3FieldMapper.Builder newBuilder() { + return new Murmur3FieldMapper.Builder("murmur"); + } } diff --git a/plugins/mapper-size/src/main/java/org/elasticsearch/index/mapper/size/SizeFieldMapper.java b/plugins/mapper-size/src/main/java/org/elasticsearch/index/mapper/size/SizeFieldMapper.java index a7a3a1ccd2f95..902a9c9e02249 100644 --- a/plugins/mapper-size/src/main/java/org/elasticsearch/index/mapper/size/SizeFieldMapper.java +++ b/plugins/mapper-size/src/main/java/org/elasticsearch/index/mapper/size/SizeFieldMapper.java @@ -19,9 +19,7 @@ package org.elasticsearch.index.mapper.size; -import org.apache.lucene.index.IndexOptions; -import org.elasticsearch.Version; -import org.elasticsearch.common.lucene.Lucene; +import org.apache.lucene.document.FieldType; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.support.XContentMapValues; @@ -44,32 +42,20 @@ public class SizeFieldMapper extends MetadataFieldMapper { public static class Defaults { public static final EnabledAttributeMapper ENABLED_STATE = EnabledAttributeMapper.UNSET_DISABLED; - public static final MappedFieldType SIZE_FIELD_TYPE = - new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); + public static final FieldType SIZE_FIELD_TYPE = new FieldType(); static { SIZE_FIELD_TYPE.setStored(true); - SIZE_FIELD_TYPE.setName(NAME); - SIZE_FIELD_TYPE.setIndexAnalyzer(Lucene.KEYWORD_ANALYZER); - SIZE_FIELD_TYPE.setSearchAnalyzer(Lucene.KEYWORD_ANALYZER); - SIZE_FIELD_TYPE.setHasDocValues(true); SIZE_FIELD_TYPE.freeze(); } } - private static MappedFieldType defaultFieldType(Version indexCreated) { - MappedFieldType defaultFieldType = Defaults.SIZE_FIELD_TYPE.clone(); - defaultFieldType.setHasDocValues(true); - return defaultFieldType; - } - public static class Builder extends MetadataFieldMapper.Builder { protected EnabledAttributeMapper enabledState = EnabledAttributeMapper.UNSET_DISABLED; - private Builder(MappedFieldType existing, Version indexCreated) { - super(NAME, existing == null ? defaultFieldType(indexCreated) : existing.clone(), - defaultFieldType(indexCreated)); + private Builder() { + super(NAME, Defaults.SIZE_FIELD_TYPE); builder = this; } @@ -80,8 +66,8 @@ public Builder enabled(EnabledAttributeMapper enabled) { @Override public SizeFieldMapper build(BuilderContext context) { - setupFieldType(context); - return new SizeFieldMapper(enabledState, fieldType, context.indexSettings()); + return new SizeFieldMapper(fieldType, enabledState, + new NumberFieldMapper.NumberFieldType(NAME, NumberFieldMapper.NumberType.INTEGER), context.indexSettings()); } } @@ -89,8 +75,7 @@ public static class TypeParser implements MetadataFieldMapper.TypeParser { @Override public MetadataFieldMapper.Builder parse(String name, Map node, ParserContext parserContext) throws MapperParsingException { - Builder builder = new Builder(parserContext.mapperService().fieldType(NAME), - parserContext.indexVersionCreated()); + Builder builder = new Builder(); for (Iterator> iterator = node.entrySet().iterator(); iterator.hasNext();) { Map.Entry entry = iterator.next(); String fieldName = entry.getKey(); @@ -106,21 +91,17 @@ public MetadataFieldMapper.Builder parse(String name, Map nod @Override public MetadataFieldMapper getDefault(ParserContext context) { - final Settings indexSettings = context.mapperService().getIndexSettings().getSettings(); - return new SizeFieldMapper(indexSettings, defaultFieldType(Version.indexCreated(indexSettings))); + return new SizeFieldMapper(Defaults.SIZE_FIELD_TYPE, Defaults.ENABLED_STATE, + new NumberFieldMapper.NumberFieldType(NAME, NumberFieldMapper.NumberType.INTEGER), + context.mapperService().getIndexSettings().getSettings()); } } private EnabledAttributeMapper enabledState; - private SizeFieldMapper(Settings indexSettings, MappedFieldType existing) { - this(Defaults.ENABLED_STATE, - existing == null ? defaultFieldType(Version.indexCreated(indexSettings)) : existing.clone(), - indexSettings); - } - - private SizeFieldMapper(EnabledAttributeMapper enabled, MappedFieldType fieldType, Settings indexSettings) { - super(NAME, fieldType, defaultFieldType(Version.indexCreated(indexSettings)), indexSettings); + private SizeFieldMapper(FieldType fieldType, EnabledAttributeMapper enabled, + MappedFieldType mappedFieldType, Settings indexSettings) { + super(fieldType, mappedFieldType, indexSettings); this.enabledState = enabled; } @@ -134,7 +115,7 @@ public boolean enabled() { } @Override - public void preParse(ParseContext context) throws IOException { + public void preParse(ParseContext context) { } @Override @@ -144,19 +125,19 @@ public void postParse(ParseContext context) throws IOException { } @Override - public void parse(ParseContext context) throws IOException { + public void parse(ParseContext context) { // nothing to do here, we call the parent in postParse } @Override - protected void parseCreateField(ParseContext context) throws IOException { + protected void parseCreateField(ParseContext context) { if (!enabledState.enabled) { return; } final int value = context.sourceToParse().source().length(); - boolean indexed = fieldType().indexOptions() != IndexOptions.NONE; + boolean indexed = fieldType().isSearchable(); boolean docValued = fieldType().hasDocValues(); - boolean stored = fieldType().stored(); + boolean stored = fieldType.stored(); context.doc().addAll(NumberFieldMapper.NumberType.INTEGER.createFields(name(), value, indexed, docValued, stored)); } diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/analyze/TransportAnalyzeAction.java b/server/src/main/java/org/elasticsearch/action/admin/indices/analyze/TransportAnalyzeAction.java index 3d87a56ebcf00..ce772d9c4f789 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/analyze/TransportAnalyzeAction.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/analyze/TransportAnalyzeAction.java @@ -49,8 +49,8 @@ import org.elasticsearch.index.analysis.NamedAnalyzer; import org.elasticsearch.index.analysis.TokenFilterFactory; import org.elasticsearch.index.analysis.TokenizerFactory; -import org.elasticsearch.index.mapper.KeywordFieldMapper; import org.elasticsearch.index.mapper.MappedFieldType; +import org.elasticsearch.index.mapper.StringFieldType; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.indices.IndicesService; import org.elasticsearch.threadpool.ThreadPool; @@ -179,7 +179,7 @@ private static Analyzer getAnalyzer(AnalyzeAction.Request request, AnalysisRegis } MappedFieldType fieldType = indexService.mapperService().fieldType(request.field()); if (fieldType != null) { - if (fieldType.tokenized() || fieldType instanceof KeywordFieldMapper.KeywordFieldType) { + if (fieldType instanceof StringFieldType) { return fieldType.indexAnalyzer(); } else { throw new IllegalArgumentException("Can't process field [" + request.field() + diff --git a/server/src/main/java/org/elasticsearch/index/analysis/NamedAnalyzer.java b/server/src/main/java/org/elasticsearch/index/analysis/NamedAnalyzer.java index d7e8696421db8..8db00dfb7c770 100644 --- a/server/src/main/java/org/elasticsearch/index/analysis/NamedAnalyzer.java +++ b/server/src/main/java/org/elasticsearch/index/analysis/NamedAnalyzer.java @@ -47,7 +47,7 @@ public NamedAnalyzer(String name, AnalyzerScope scope, Analyzer analyzer) { this(name, scope, analyzer, Integer.MIN_VALUE); } - NamedAnalyzer(String name, AnalyzerScope scope, Analyzer analyzer, int positionIncrementGap) { + public NamedAnalyzer(String name, AnalyzerScope scope, Analyzer analyzer, int positionIncrementGap) { super(ERROR_STRATEGY); this.name = name; this.scope = scope; diff --git a/server/src/main/java/org/elasticsearch/index/mapper/AbstractGeometryFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/AbstractGeometryFieldMapper.java index 721a4033965f5..0de7f9ae4c64d 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/AbstractGeometryFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/AbstractGeometryFieldMapper.java @@ -18,7 +18,7 @@ */ package org.elasticsearch.index.mapper; -import org.apache.lucene.index.IndexOptions; +import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexableField; import org.apache.lucene.index.Term; import org.apache.lucene.search.DocValuesFieldExistsQuery; @@ -58,6 +58,11 @@ public static class Names { public static class Defaults { public static final Explicit IGNORE_MALFORMED = new Explicit<>(false, false); public static final Explicit IGNORE_Z_VALUE = new Explicit<>(true, false); + public static final FieldType FIELD_TYPE = new FieldType(); + static { + FIELD_TYPE.setStored(false); + FIELD_TYPE.freeze(); + } } /** @@ -76,18 +81,19 @@ public interface Parser { Parsed parse(XContentParser parser, AbstractGeometryFieldMapper mapper) throws IOException, ParseException; } - public abstract static class Builder + public abstract static class Builder, FT extends AbstractGeometryFieldType> extends FieldMapper.Builder { protected Boolean ignoreMalformed; protected Boolean ignoreZValue; + protected boolean indexed = true; - public Builder(String name, MappedFieldType fieldType, MappedFieldType defaultFieldType) { - super(name, fieldType, defaultFieldType); + public Builder(String name, FieldType fieldType) { + super(name, fieldType); } - public Builder(String name, MappedFieldType fieldType, MappedFieldType defaultFieldType, boolean ignoreMalformed, + public Builder(String name, FieldType fieldType, boolean ignoreMalformed, boolean ignoreZValue) { - super(name, fieldType, defaultFieldType); + super(name, fieldType); this.ignoreMalformed = ignoreMalformed; this.ignoreZValue = ignoreZValue; } @@ -132,30 +138,6 @@ public Builder ignoreZValue(final boolean ignoreZValue) { this.ignoreZValue = ignoreZValue; return this; } - - @Override - protected void setupFieldType(BuilderContext context) { - super.setupFieldType(context); - - // field mapper handles this at build time - // but prefix tree strategies require a name, so throw a similar exception - if (name().isEmpty()) { - throw new IllegalArgumentException("name cannot be empty string"); - } - - setGeometryParser(); - setGeometryIndexer(fieldType()); - setGeometryQueryBuilder(fieldType()); - } - - @Override - public FT fieldType() { - return (FT)fieldType; - } - - protected abstract void setGeometryParser(); - protected abstract void setGeometryIndexer(FT fieldType); - protected abstract void setGeometryQueryBuilder(FT fieldType); } public abstract static class TypeParser implements Mapper.TypeParser { @@ -202,12 +184,8 @@ public abstract static class AbstractGeometryFieldType extend protected Parser geometryParser; protected QueryProcessor geometryQueryBuilder; - protected AbstractGeometryFieldType() { - setIndexOptions(IndexOptions.DOCS); - setTokenized(false); - setStored(false); - setStoreTermVectors(false); - setOmitNorms(true); + protected AbstractGeometryFieldType(String name, boolean indexed, boolean hasDocValues, Map meta) { + super(name, indexed, hasDocValues, meta); } protected AbstractGeometryFieldType(AbstractGeometryFieldType ref) { @@ -226,7 +204,7 @@ protected Indexer geometryIndexer() { return geometryIndexer; } - public void setGeometryParser(Parser geometryParser) { + public void setGeometryParser(Parser geometryParser) { this.geometryParser = geometryParser; } @@ -271,10 +249,10 @@ public Query termQuery(Object value, QueryShardContext context) { protected Explicit ignoreMalformed; protected Explicit ignoreZValue; - protected AbstractGeometryFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, + protected AbstractGeometryFieldMapper(String simpleName, FieldType fieldType, MappedFieldType mappedFieldType, Settings indexSettings, Explicit ignoreMalformed, Explicit ignoreZValue, MultiFields multiFields, CopyTo copyTo) { - super(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, copyTo); + super(simpleName, fieldType, mappedFieldType, indexSettings, multiFields, copyTo); this.ignoreMalformed = ignoreMalformed; this.ignoreZValue = ignoreZValue; } @@ -293,7 +271,7 @@ protected void mergeOptions(FieldMapper other, List conflicts) { @Override public AbstractGeometryFieldType fieldType() { - return (AbstractGeometryFieldType)fieldType; + return (AbstractGeometryFieldType)mappedFieldType; } @Override @@ -313,10 +291,10 @@ public final boolean parsesArrayValue() { /** parsing logic for geometry indexing */ @Override public void parse(ParseContext context) throws IOException { - AbstractGeometryFieldMapper.AbstractGeometryFieldType fieldType = fieldType(); + AbstractGeometryFieldMapper.AbstractGeometryFieldType mappedFieldType = fieldType(); - @SuppressWarnings("unchecked") Indexer geometryIndexer = fieldType.geometryIndexer(); - @SuppressWarnings("unchecked") Parser geometryParser = fieldType.geometryParser(); + @SuppressWarnings("unchecked") Indexer geometryIndexer = mappedFieldType.geometryIndexer(); + @SuppressWarnings("unchecked") Parser geometryParser = mappedFieldType.geometryParser(); try { Processed shape = context.parseExternalValue(geometryIndexer.processedClass()); if (shape == null) { @@ -328,13 +306,13 @@ public void parse(ParseContext context) throws IOException { } List fields = new ArrayList<>(); - if (fieldType.indexOptions() != IndexOptions.NONE || fieldType.hasDocValues()) { + if (mappedFieldType.isSearchable() || mappedFieldType.hasDocValues()) { fields.addAll(geometryIndexer.indexShape(context, shape)); } // indexed: List indexedFields = new ArrayList<>(); - if (fieldType.indexOptions() != IndexOptions.NONE) { + if (mappedFieldType.isSearchable()) { indexedFields.addAll(fields); } // stored: @@ -343,8 +321,8 @@ public void parse(ParseContext context) throws IOException { } // docValues: if (fieldType().hasDocValues()) { - addDocValuesFields(fieldType.name(), shape, fields, context); - } else if (fieldType.stored() || fieldType.indexOptions() != IndexOptions.NONE) { + addDocValuesFields(mappedFieldType.name(), shape, fields, context); + } else if (fieldType.stored() || fieldType().isSearchable()) { createFieldNamesField(context); } @@ -360,7 +338,7 @@ public void parse(ParseContext context) throws IOException { throw new MapperParsingException("failed to parse field [{}] of type [{}]", e, fieldType().name(), fieldType().typeName()); } - context.addIgnoredField(fieldType.name()); + context.addIgnoredField(mappedFieldType.name()); } } diff --git a/server/src/main/java/org/elasticsearch/index/mapper/AbstractPointGeometryFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/AbstractPointGeometryFieldMapper.java index e5b06789406c1..eb684a7607286 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/AbstractPointGeometryFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/AbstractPointGeometryFieldMapper.java @@ -18,6 +18,7 @@ */ package org.elasticsearch.index.mapper; +import org.apache.lucene.document.FieldType; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.Explicit; import org.elasticsearch.common.ParseField; @@ -44,37 +45,45 @@ public static class Names extends AbstractGeometryFieldMapper.Names { public static final ParseField NULL_VALUE = new ParseField("null_value"); } - public abstract static class Builder, FT extends AbstractPointGeometryFieldType> extends AbstractGeometryFieldMapper.Builder { - public Builder(String name, MappedFieldType fieldType, MappedFieldType defaultFieldType) { - super(name, fieldType, defaultFieldType); - } - public abstract AbstractPointGeometryFieldMapper build(BuilderContext context, String simpleName, MappedFieldType fieldType, - MappedFieldType defaultFieldType, Settings indexSettings, - MultiFields multiFields, Explicit ignoreMalformed, Explicit ignoreZValue, - CopyTo copyTo); + protected ParsedPoint nullValue; + public Builder(String name, FieldType fieldType) { + super(name, fieldType); + } - @Override - public AbstractPointGeometryFieldMapper build(BuilderContext context) { - return build(context, name, fieldType, defaultFieldType, context.indexSettings(), - multiFieldsBuilder.build(this, context), ignoreMalformed(context), - ignoreZValue(context), copyTo); + public void setNullValue(ParsedPoint nullValue) { + this.nullValue = nullValue; } + public abstract AbstractPointGeometryFieldMapper build(BuilderContext context, String simpleName, FieldType fieldType, + Settings indexSettings, MultiFields multiFields, + Explicit ignoreMalformed, + Explicit ignoreZValue, + ParsedPoint nullValue, CopyTo copyTo); + @Override - public FT fieldType() { - return (FT)fieldType; + public AbstractPointGeometryFieldMapper build(BuilderContext context) { + return build(context, name, fieldType, context.indexSettings(), + multiFieldsBuilder.build(this, context), ignoreMalformed(context), + ignoreZValue(context), nullValue, copyTo); } } - public abstract static class TypeParser extends AbstractGeometryFieldMapper.TypeParser { - protected abstract Processed parseNullValue(Object nullValue, boolean ignoreZValue, boolean ignoreMalformed); + public abstract static class TypeParser extends AbstractGeometryFieldMapper.TypeParser { + protected abstract ParsedPoint parseNullValue(Object nullValue, boolean ignoreZValue, boolean ignoreMalformed); @Override - @SuppressWarnings("rawtypes") public T parse(String name, Map node, Map params, ParserContext parserContext) { T builder = (T)(super.parse(name, node, params, parserContext)); parseField(builder, name, node, parserContext); @@ -94,7 +103,7 @@ public T parse(String name, Map node, Map params } if (nullValue != null) { - builder.nullValue(parseNullValue(nullValue, (Boolean)builder.ignoreZValue().value(), + builder.setNullValue(parseNullValue(nullValue, (Boolean)builder.ignoreZValue().value(), (Boolean)builder.ignoreMalformed().value())); } @@ -102,12 +111,12 @@ public T parse(String name, Map node, Map params } } + ParsedPoint nullValue; + public abstract static class AbstractPointGeometryFieldType extends AbstractGeometryFieldType { - protected AbstractPointGeometryFieldType() { - super(); - setHasDocValues(true); - setDimensions(2, Integer.BYTES); + protected AbstractPointGeometryFieldType(String name, boolean indexed, boolean hasDocValues, Map meta) { + super(name, indexed, hasDocValues, meta); } protected AbstractPointGeometryFieldType(AbstractPointGeometryFieldType ref) { @@ -115,29 +124,34 @@ protected AbstractPointGeometryFieldType(AbstractPointGeometryFieldType ref) { } } - protected AbstractPointGeometryFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, + protected AbstractPointGeometryFieldMapper(String simpleName, FieldType fieldType, MappedFieldType mappedFieldType, Settings indexSettings, MultiFields multiFields, Explicit ignoreMalformed, - Explicit ignoreZValue, CopyTo copyTo) { - super(simpleName, fieldType, defaultFieldType, indexSettings, ignoreMalformed, ignoreZValue, multiFields, copyTo); + Explicit ignoreZValue, ParsedPoint nullValue, CopyTo copyTo) { + super(simpleName, fieldType, mappedFieldType, indexSettings, ignoreMalformed, ignoreZValue, multiFields, copyTo); + this.nullValue = nullValue; } @Override protected void mergeOptions(FieldMapper other, List conflicts) { AbstractPointGeometryFieldMapper gpfm = (AbstractPointGeometryFieldMapper)other; - if (gpfm.fieldType().nullValue() != null) { - this.fieldType().setNullValue(gpfm.fieldType().nullValue()); + // TODO make this un-updateable + if (gpfm.nullValue != null) { + this.nullValue = gpfm.nullValue; } } @Override - @SuppressWarnings("rawtypes") public void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException { super.doXContentBody(builder, includeDefaults, params); - if (includeDefaults || fieldType().nullValue() != null) { - builder.field(Names.NULL_VALUE.getPreferredName(), fieldType().nullValue()); + if (nullValue != null) { + builder.field(Names.NULL_VALUE.getPreferredName(), nullValue); } } + public ParsedPoint getNullValue() { + return nullValue; + } + protected abstract ParsedPoint newParsedPoint(); /** represents a Point that has been parsed by {@link PointParser} */ @@ -221,8 +235,8 @@ public XContentBuilder toXContent(List

points, XContentBuilder builder, Param public List

fromXContent(XContentParser parser) throws IOException, ParseException { P point = null; ArrayList

points = null; - if (mapper.fieldType().nullValue() != null) { - point = (P)(mapper.fieldType().nullValue()); + if (mapper.nullValue != null) { + point = (P)(mapper.nullValue); if ((Boolean)(mapper.ignoreMalformed().value()) == false) { point.validate(mapper.name()); } else { diff --git a/server/src/main/java/org/elasticsearch/index/mapper/AbstractShapeGeometryFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/AbstractShapeGeometryFieldMapper.java index 637f1a2471970..b98f0449cf341 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/AbstractShapeGeometryFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/AbstractShapeGeometryFieldMapper.java @@ -18,7 +18,7 @@ */ package org.elasticsearch.index.mapper; -import org.elasticsearch.Version; +import org.apache.lucene.document.FieldType; import org.elasticsearch.common.Explicit; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.geo.builders.ShapeBuilder; @@ -50,19 +50,19 @@ public static class Defaults extends AbstractGeometryFieldMapper.Defaults { public static final Explicit COERCE = new Explicit<>(false, false); } - public abstract static class Builder, FT extends AbstractShapeGeometryFieldType> extends AbstractGeometryFieldMapper.Builder { protected Boolean coerce; protected Orientation orientation; /** default builder - used for external mapper*/ - public Builder(String name, MappedFieldType fieldType, MappedFieldType defaultFieldType) { - super(name, fieldType, defaultFieldType); + public Builder(String name, FieldType fieldType) { + super(name, fieldType); } - public Builder(String name, MappedFieldType fieldType, MappedFieldType defaultFieldType, + public Builder(String name, FieldType fieldType, boolean coerce, boolean ignoreMalformed, Orientation orientation, boolean ignoreZ) { - super(name, fieldType, defaultFieldType, ignoreMalformed, ignoreZ); + super(name, fieldType, ignoreMalformed, ignoreZ); this.coerce = coerce; this.orientation = orientation; } @@ -101,19 +101,6 @@ protected Explicit orientation() { return Defaults.ORIENTATION; } - @Override - protected boolean defaultDocValues(Version indexCreated) { - return false; - } - - @Override - protected void setGeometryParser() { - AbstractShapeGeometryFieldType ft = fieldType(); - ft.setOrientation(orientation().value()); - setGeometryParser(fieldType()); - } - - protected abstract void setGeometryParser(FT fieldType); } protected static final String DEPRECATED_PARAMETERS_KEY = "deprecated_parameters"; @@ -171,8 +158,8 @@ public Builder parse(String name, Map node, Map public abstract static class AbstractShapeGeometryFieldType extends AbstractGeometryFieldType { protected Orientation orientation = Defaults.ORIENTATION.value(); - protected AbstractShapeGeometryFieldType() { - super(); + protected AbstractShapeGeometryFieldType(String name, boolean isSearchable, boolean hasDocValues, Map meta) { + super(name, isSearchable, hasDocValues, meta); } protected AbstractShapeGeometryFieldType(AbstractShapeGeometryFieldType ref) { @@ -195,7 +182,6 @@ public int hashCode() { public Orientation orientation() { return this.orientation; } public void setOrientation(Orientation orientation) { - checkIfFrozen(); this.orientation = orientation; } } @@ -203,11 +189,11 @@ public void setOrientation(Orientation orientation) { protected Explicit coerce; protected Explicit orientation; - protected AbstractShapeGeometryFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, + protected AbstractShapeGeometryFieldMapper(String simpleName, FieldType fieldType, MappedFieldType mappedFieldType, Explicit ignoreMalformed, Explicit coerce, Explicit ignoreZValue, Explicit orientation, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { - super(simpleName, fieldType, defaultFieldType, indexSettings, ignoreMalformed, ignoreZValue, multiFields, copyTo); + super(simpleName, fieldType, mappedFieldType, indexSettings, ignoreMalformed, ignoreZValue, multiFields, copyTo); this.coerce = coerce; this.orientation = orientation; } @@ -233,8 +219,8 @@ public void doXContentBody(XContentBuilder builder, boolean includeDefaults, Par if (includeDefaults || coerce.explicit()) { builder.field(AbstractShapeGeometryFieldMapper.Names.COERCE.getPreferredName(), coerce.value()); } - if (includeDefaults || ft.orientation() != Defaults.ORIENTATION.value()) { - builder.field(Names.ORIENTATION.getPreferredName(), ft.orientation()); + if (includeDefaults || orientation.explicit()) { + builder.field(Names.ORIENTATION.getPreferredName(), orientation.value()); } } @@ -243,6 +229,6 @@ public Explicit coerce() { } public Orientation orientation() { - return ((AbstractShapeGeometryFieldType)fieldType).orientation(); + return ((AbstractShapeGeometryFieldType)mappedFieldType).orientation(); } } diff --git a/server/src/main/java/org/elasticsearch/index/mapper/BinaryFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/BinaryFieldMapper.java index 1419aebcd8f28..42f4776fb77d6 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/BinaryFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/BinaryFieldMapper.java @@ -21,6 +21,7 @@ import com.carrotsearch.hppc.ObjectArrayList; import org.apache.lucene.document.Field; +import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.Term; import org.apache.lucene.search.DocValuesFieldExistsQuery; @@ -44,6 +45,7 @@ import java.io.IOException; import java.time.ZoneId; import java.util.Base64; +import java.util.Collections; import java.util.List; import java.util.Map; @@ -54,7 +56,7 @@ public class BinaryFieldMapper extends FieldMapper { public static final String CONTENT_TYPE = "binary"; public static class Defaults { - public static final MappedFieldType FIELD_TYPE = new BinaryFieldType(); + public static final FieldType FIELD_TYPE = new FieldType(); static { FIELD_TYPE.setIndexOptions(IndexOptions.NONE); @@ -65,16 +67,23 @@ public static class Defaults { public static class Builder extends FieldMapper.Builder { public Builder(String name) { - super(name, Defaults.FIELD_TYPE, Defaults.FIELD_TYPE); + super(name, Defaults.FIELD_TYPE); builder = this; } @Override public BinaryFieldMapper build(BuilderContext context) { - setupFieldType(context); - return new BinaryFieldMapper(name, fieldType, defaultFieldType, + return new BinaryFieldMapper(name, fieldType, new BinaryFieldType(buildFullName(context), hasDocValues, meta), context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); } + + @Override + public Builder index(boolean index) { + if (index) { + throw new MapperParsingException("Binary field [" + name() + "] cannot be indexed"); + } + return builder; + } } public static class TypeParser implements Mapper.TypeParser { @@ -87,9 +96,15 @@ public BinaryFieldMapper.Builder parse(String name, Map node, Pa } } - static final class BinaryFieldType extends MappedFieldType { + public static final class BinaryFieldType extends MappedFieldType { + + public BinaryFieldType(String name, boolean hasDocValues, Map meta) { + super(name, false, hasDocValues, meta); + } - BinaryFieldType() {} + public BinaryFieldType(String name) { + this(name, true, Collections.emptyMap()); + } protected BinaryFieldType(BinaryFieldType ref) { super(ref); @@ -151,14 +166,14 @@ public Query termQuery(Object value, QueryShardContext context) { } } - protected BinaryFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, + protected BinaryFieldMapper(String simpleName, FieldType fieldType, MappedFieldType mappedFieldType, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { - super(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, copyTo); + super(simpleName, fieldType, mappedFieldType, indexSettings, multiFields, copyTo); } @Override protected void parseCreateField(ParseContext context) throws IOException { - if (!fieldType().stored() && !fieldType().hasDocValues()) { + if (!fieldType.stored() && !fieldType().hasDocValues()) { return; } byte[] value = context.parseExternalValue(byte[].class); @@ -172,8 +187,8 @@ protected void parseCreateField(ParseContext context) throws IOException { if (value == null) { return; } - if (fieldType().stored()) { - context.doc().add(new Field(fieldType().name(), value, fieldType())); + if (fieldType.stored()) { + context.doc().add(new Field(fieldType().name(), value, fieldType)); } if (fieldType().hasDocValues()) { diff --git a/server/src/main/java/org/elasticsearch/index/mapper/BooleanFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/BooleanFieldMapper.java index 44f9ffbf421e5..7ab98372bc98e 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/BooleanFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/BooleanFieldMapper.java @@ -20,6 +20,7 @@ package org.elasticsearch.index.mapper; import org.apache.lucene.document.Field; +import org.apache.lucene.document.FieldType; import org.apache.lucene.document.SortedNumericDocValuesField; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.Term; @@ -29,7 +30,6 @@ import org.apache.lucene.search.TermRangeQuery; import org.apache.lucene.util.BytesRef; import org.elasticsearch.common.Nullable; -import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; @@ -42,6 +42,7 @@ import java.io.IOException; import java.time.ZoneId; +import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -56,14 +57,12 @@ public class BooleanFieldMapper extends FieldMapper { public static final String CONTENT_TYPE = "boolean"; public static class Defaults { - public static final MappedFieldType FIELD_TYPE = new BooleanFieldType(); + public static final FieldType FIELD_TYPE = new FieldType(); static { FIELD_TYPE.setOmitNorms(true); FIELD_TYPE.setIndexOptions(IndexOptions.DOCS); FIELD_TYPE.setTokenized(false); - FIELD_TYPE.setIndexAnalyzer(Lucene.KEYWORD_ANALYZER); - FIELD_TYPE.setSearchAnalyzer(Lucene.KEYWORD_ANALYZER); FIELD_TYPE.freeze(); } } @@ -75,16 +74,23 @@ public static class Values { public static class Builder extends FieldMapper.Builder { + private Boolean nullValue; + public Builder(String name) { - super(name, Defaults.FIELD_TYPE, Defaults.FIELD_TYPE); + super(name, Defaults.FIELD_TYPE); this.builder = this; } + public Builder nullValue(Boolean nullValue) { + this.nullValue = nullValue; + return builder; + } + @Override public BooleanFieldMapper build(BuilderContext context) { - setupFieldType(context); - return new BooleanFieldMapper(name, fieldType, defaultFieldType, - context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); + return new BooleanFieldMapper(name, fieldType, + new BooleanFieldType(buildFullName(context), indexed, hasDocValues, meta), + context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo, nullValue); } } @@ -112,7 +118,13 @@ public BooleanFieldMapper.Builder parse(String name, Map node, P public static final class BooleanFieldType extends TermBasedFieldType { - public BooleanFieldType() {} + public BooleanFieldType(String name, boolean isSearchable, boolean hasDocValues, Map meta) { + super(name, isSearchable, hasDocValues, meta); + } + + public BooleanFieldType(String name) { + this(name, true, true, Collections.emptyMap()); + } protected BooleanFieldType(BooleanFieldType ref) { super(ref); @@ -137,11 +149,6 @@ public Query existsQuery(QueryShardContext context) { } } - @Override - public Boolean nullValue() { - return (Boolean)super.nullValue(); - } - @Override public BytesRef indexedValueForSearch(Object value) { if (value == null) { @@ -210,9 +217,12 @@ public Query rangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower } } - protected BooleanFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, - Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { - super(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, copyTo); + private final Boolean nullValue; + + protected BooleanFieldMapper(String simpleName, FieldType fieldType, MappedFieldType mappedFieldType, + Settings indexSettings, MultiFields multiFields, CopyTo copyTo, Boolean nullValue) { + super(simpleName, fieldType, mappedFieldType, indexSettings, multiFields, copyTo); + this.nullValue = nullValue; } @Override @@ -222,7 +232,7 @@ public BooleanFieldType fieldType() { @Override protected void parseCreateField(ParseContext context) throws IOException { - if (fieldType().indexOptions() == IndexOptions.NONE && !fieldType().stored() && !fieldType().hasDocValues()) { + if (fieldType().isSearchable() == false && !fieldType.stored() && !fieldType().hasDocValues()) { return; } @@ -230,8 +240,8 @@ protected void parseCreateField(ParseContext context) throws IOException { if (value == null) { XContentParser.Token token = context.parser().currentToken(); if (token == XContentParser.Token.VALUE_NULL) { - if (fieldType().nullValue() != null) { - value = fieldType().nullValue(); + if (nullValue != null) { + value = nullValue; } } else { value = context.parser().booleanValue(); @@ -241,8 +251,8 @@ protected void parseCreateField(ParseContext context) throws IOException { if (value == null) { return; } - if (fieldType().indexOptions() != IndexOptions.NONE || fieldType().stored()) { - context.doc().add(new Field(fieldType().name(), value ? "T" : "F", fieldType())); + if (fieldType().isSearchable() || fieldType.stored()) { + context.doc().add(new Field(fieldType().name(), value ? "T" : "F", fieldType)); } if (fieldType().hasDocValues()) { context.doc().add(new SortedNumericDocValuesField(fieldType().name(), value ? 1 : 0)); @@ -253,7 +263,7 @@ protected void parseCreateField(ParseContext context) throws IOException { @Override protected void mergeOptions(FieldMapper other, List conflicts) { - + // TODO ban updating null values } @Override @@ -264,8 +274,8 @@ protected String contentType() { @Override protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException { super.doXContentBody(builder, includeDefaults, params); - if (includeDefaults || fieldType().nullValue() != null) { - builder.field("null_value", fieldType().nullValue()); + if (includeDefaults || nullValue != null) { + builder.field("null_value", nullValue); } } } diff --git a/server/src/main/java/org/elasticsearch/index/mapper/CompletionFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/CompletionFieldMapper.java index 4109913122d4f..8296adc0b4de1 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/CompletionFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/CompletionFieldMapper.java @@ -20,6 +20,8 @@ import org.apache.logging.log4j.LogManager; import org.apache.lucene.codecs.PostingsFormat; +import org.apache.lucene.document.FieldType; +import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.Term; import org.apache.lucene.search.Query; import org.apache.lucene.search.TermQuery; @@ -92,8 +94,12 @@ public class CompletionFieldMapper extends FieldMapper { static final int COMPLETION_CONTEXTS_LIMIT = 10; public static class Defaults { - public static final MappedFieldType FIELD_TYPE = new CompletionFieldType(); + public static final FieldType FIELD_TYPE = new FieldType(); static { + FIELD_TYPE.setTokenized(true); + FIELD_TYPE.setStored(false); + FIELD_TYPE.setStoreTermVectors(false); + FIELD_TYPE.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS); FIELD_TYPE.setOmitNorms(true); FIELD_TYPE.freeze(); } @@ -189,7 +195,12 @@ public static final class CompletionFieldType extends TermBasedFieldType { private boolean preservePositionIncrements = Defaults.DEFAULT_POSITION_INCREMENTS; private ContextMappings contextMappings = null; - public CompletionFieldType() { + public CompletionFieldType(String name, Map meta) { + super(name, true, false, meta); + } + + public CompletionFieldType(String name) { + this(name, Collections.emptyMap()); } private CompletionFieldType(CompletionFieldType ref) { @@ -200,17 +211,14 @@ private CompletionFieldType(CompletionFieldType ref) { } public void setPreserveSep(boolean preserveSep) { - checkIfFrozen(); this.preserveSep = preserveSep; } public void setPreservePositionIncrements(boolean preservePositionIncrements) { - checkIfFrozen(); this.preservePositionIncrements = preservePositionIncrements; } public void setContextMappings(ContextMappings contextMappings) { - checkIfFrozen(); this.contextMappings = contextMappings; } @@ -308,7 +316,7 @@ public boolean equals(Object o) { if (preserveSep != that.preserveSep) return false; if (preservePositionIncrements != that.preservePositionIncrements) return false; - return !(contextMappings != null ? !contextMappings.equals(that.contextMappings) : that.contextMappings != null); + return Objects.equals(contextMappings, that.contextMappings); } @@ -348,7 +356,7 @@ public static class Builder extends FieldMapper.Builder { * @param name of the completion field to build */ public Builder(String name) { - super(name, Defaults.FIELD_TYPE, Defaults.FIELD_TYPE); + super(name, Defaults.FIELD_TYPE); builder = this; } @@ -388,12 +396,15 @@ public Builder preservePositionIncrements(boolean preservePositionIncrements) { @Override public CompletionFieldMapper build(BuilderContext context) { checkCompletionContextsLimit(context); - setupFieldType(context); - CompletionFieldType completionFieldType = (CompletionFieldType) this.fieldType; - completionFieldType.setContextMappings(contextMappings); - completionFieldType.setPreservePositionIncrements(preservePositionIncrements); - completionFieldType.setPreserveSep(preserveSeparators); - return new CompletionFieldMapper(name, this.fieldType, context.indexSettings(), + CompletionFieldType ft = new CompletionFieldType(buildFullName(context), meta); + ft.setContextMappings(contextMappings); + ft.setPreservePositionIncrements(preservePositionIncrements); + ft.setPreserveSep(preserveSeparators); + ft.setIndexAnalyzer(indexAnalyzer); + ft.setSearchAnalyzer(searchAnalyzer); + ft.setSearchQuoteAnalyzer(searchQuoteAnalyzer); + ft.setSimilarity(similarity); + return new CompletionFieldMapper(name, this.fieldType, ft, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo, maxInputLength); } @@ -411,13 +422,21 @@ private void checkCompletionContextsLimit(BuilderContext context) { } } } + + @Override + public Builder index(boolean index) { + if (index == false) { + throw new MapperParsingException("Completion field type must be indexed"); + } + return builder; + } } private int maxInputLength; - public CompletionFieldMapper(String simpleName, MappedFieldType fieldType, Settings indexSettings, + public CompletionFieldMapper(String simpleName, FieldType fieldType, MappedFieldType mappedFieldType, Settings indexSettings, MultiFields multiFields, CopyTo copyTo, int maxInputLength) { - super(simpleName, fieldType, Defaults.FIELD_TYPE, indexSettings, multiFields, copyTo); + super(simpleName, fieldType, mappedFieldType, indexSettings, multiFields, copyTo); this.maxInputLength = maxInputLength; } @@ -467,7 +486,7 @@ public void parse(ParseContext context) throws IOException { for (Map.Entry completionInput : inputMap.entrySet()) { String input = completionInput.getKey(); if (input.trim().isEmpty()) { - context.addIgnoredField(fieldType.name()); + context.addIgnoredField(mappedFieldType.name()); continue; } // truncate input diff --git a/server/src/main/java/org/elasticsearch/index/mapper/ConstantFieldType.java b/server/src/main/java/org/elasticsearch/index/mapper/ConstantFieldType.java index b2f67bdc6cea9..c39994d7ef078 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/ConstantFieldType.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/ConstantFieldType.java @@ -30,6 +30,7 @@ import org.elasticsearch.index.query.QueryShardContext; import java.util.List; +import java.util.Map; /** * A {@link MappedFieldType} that has the same value for all documents. @@ -40,8 +41,8 @@ */ public abstract class ConstantFieldType extends MappedFieldType { - public ConstantFieldType() { - super(); + public ConstantFieldType(String name, Map meta) { + super(name, true, true, meta); } public ConstantFieldType(ConstantFieldType other) { diff --git a/server/src/main/java/org/elasticsearch/index/mapper/DateFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/DateFieldMapper.java index c1351d9f1f39c..db35a363ffdac 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/DateFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/DateFieldMapper.java @@ -19,6 +19,7 @@ package org.elasticsearch.index.mapper; +import org.apache.lucene.document.FieldType; import org.apache.lucene.document.LongPoint; import org.apache.lucene.document.SortedNumericDocValuesField; import org.apache.lucene.document.StoredField; @@ -36,8 +37,8 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.Explicit; import org.elasticsearch.common.Nullable; -import org.elasticsearch.common.Strings; import org.elasticsearch.common.geo.ShapeRelation; +import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.time.DateFormatter; import org.elasticsearch.common.time.DateFormatters; @@ -59,6 +60,7 @@ import java.time.Instant; import java.time.ZoneId; import java.time.ZoneOffset; +import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Locale; @@ -77,6 +79,14 @@ public final class DateFieldMapper extends FieldMapper { public static class Defaults { public static final Explicit IGNORE_MALFORMED = new Explicit<>(false, false); + public static final FieldType FIELD_TYPE = new FieldType(); + static { + FIELD_TYPE.setTokenized(true); + FIELD_TYPE.setStored(false); + FIELD_TYPE.setStoreTermVectors(false); + FIELD_TYPE.setOmitNorms(false); + FIELD_TYPE.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS); + } } public enum Resolution { @@ -176,18 +186,14 @@ public static class Builder extends FieldMapper.Builder { private Explicit format = new Explicit<>(DEFAULT_DATE_TIME_FORMATTER.pattern(), false); private Locale locale; private Resolution resolution = Resolution.MILLISECONDS; + String nullValue; public Builder(String name) { - super(name, new DateFieldType(), new DateFieldType()); + super(name, Defaults.FIELD_TYPE); builder = this; locale = Locale.ROOT; } - @Override - public DateFieldType fieldType() { - return (DateFieldType)fieldType; - } - public Builder ignoreMalformed(boolean ignoreMalformed) { this.ignoreMalformed = ignoreMalformed; return builder; @@ -208,6 +214,11 @@ public Builder locale(Locale locale) { return this; } + public Builder nullValue(String nullValue) { + this.nullValue = nullValue; + return this; + } + public Locale locale() { return locale; } @@ -230,24 +241,17 @@ public boolean isFormatterSet() { return format.explicit(); } - @Override - protected void setupFieldType(BuilderContext context) { - super.setupFieldType(context); + protected DateFieldType setupFieldType(BuilderContext context) { String pattern = this.format.value(); - DateFormatter dateTimeFormatter = fieldType().dateTimeFormatter; - - boolean hasPatternChanged = Strings.hasLength(pattern) && Objects.equals(pattern, dateTimeFormatter.pattern()) == false; - if (hasPatternChanged || Objects.equals(builder.locale, dateTimeFormatter.locale()) == false) { - fieldType().setDateTimeFormatter(DateFormatter.forPattern(pattern).withLocale(locale)); - } - - fieldType().setResolution(resolution); + DateFormatter dateTimeFormatter = DateFormatter.forPattern(pattern).withLocale(locale); + return new DateFieldType(buildFullName(context), indexed, hasDocValues, dateTimeFormatter, resolution, meta); } @Override public DateFieldMapper build(BuilderContext context) { - setupFieldType(context); - return new DateFieldMapper(name, fieldType, defaultFieldType, ignoreMalformed(context), + DateFieldType ft = setupFieldType(context); + Long nullTimestamp = nullValue == null ? null : ft.dateTimeFormatter.parseMillis(nullValue); + return new DateFieldMapper(name, fieldType, ft, ignoreMalformed(context), nullTimestamp, nullValue, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); } } @@ -293,23 +297,28 @@ public Mapper.Builder parse(String name, Map node, ParserCont } public static final class DateFieldType extends MappedFieldType { - protected DateFormatter dateTimeFormatter; - protected DateMathParser dateMathParser; - protected Resolution resolution; + protected final DateFormatter dateTimeFormatter; + protected final DateMathParser dateMathParser; + protected final Resolution resolution; + + public DateFieldType(String name, boolean isSearchable, boolean hasDocValues, + DateFormatter dateTimeFormatter, Resolution resolution, Map meta) { + super(name, isSearchable, hasDocValues, meta); + this.dateTimeFormatter = dateTimeFormatter; + this.dateMathParser = dateTimeFormatter.toDateMathParser(); + this.resolution = resolution; + setSearchAnalyzer(Lucene.KEYWORD_ANALYZER); // allows match queries on date fields + } - public DateFieldType() { - super(); - setTokenized(false); - setHasDocValues(true); - setOmitNorms(true); - setDateTimeFormatter(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER); - setResolution(Resolution.MILLISECONDS); + public DateFieldType(String name) { + this(name, true, true, DEFAULT_DATE_TIME_FORMATTER, Resolution.MILLISECONDS, Collections.emptyMap()); } DateFieldType(DateFieldType other) { super(other); - setDateTimeFormatter(other.dateTimeFormatter); - setResolution(other.resolution); + this.dateTimeFormatter = other.dateTimeFormatter; + this.dateMathParser = other.dateMathParser; + this.resolution = other.resolution; } @Override @@ -344,17 +353,6 @@ public Resolution resolution() { return resolution; } - void setDateTimeFormatter(DateFormatter formatter) { - checkIfFrozen(); - this.dateTimeFormatter = formatter; - this.dateMathParser = dateTimeFormatter.toDateMathParser(); - } - - void setResolution(Resolution resolution) { - checkIfFrozen(); - this.resolution = resolution; - } - protected DateMathParser dateMathParser() { return dateMathParser; } @@ -550,17 +548,22 @@ public DocValueFormat docValueFormat(@Nullable String format, ZoneId timeZone) { } private Explicit ignoreMalformed; + private final Long nullValue; + private final String nullValueAsString; private DateFieldMapper( String simpleName, - MappedFieldType fieldType, - MappedFieldType defaultFieldType, + FieldType fieldType, + MappedFieldType mappedFieldType, Explicit ignoreMalformed, + Long nullValue, String nullValueAsString, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { - super(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, copyTo); + super(simpleName, fieldType, mappedFieldType, indexSettings, multiFields, copyTo); this.ignoreMalformed = ignoreMalformed; + this.nullValue = nullValue; + this.nullValueAsString = nullValueAsString; } @Override @@ -570,7 +573,7 @@ public DateFieldType fieldType() { @Override protected String contentType() { - return fieldType.typeName(); + return fieldType().resolution.type(); } @Override @@ -592,35 +595,34 @@ protected void parseCreateField(ParseContext context) throws IOException { dateAsString = context.parser().textOrNull(); } - if (dateAsString == null) { - dateAsString = fieldType().nullValueAsString(); - } - - if (dateAsString == null) { - return; - } - long timestamp; - try { - timestamp = fieldType().parse(dateAsString); - } catch (IllegalArgumentException | ElasticsearchParseException | DateTimeException e) { - if (ignoreMalformed.value()) { - context.addIgnoredField(fieldType.name()); + if (dateAsString == null) { + if (nullValue == null) { return; - } else { - throw e; + } + timestamp = nullValue; + } else { + try { + timestamp = fieldType().parse(dateAsString); + } catch (IllegalArgumentException | ElasticsearchParseException | DateTimeException e) { + if (ignoreMalformed.value()) { + context.addIgnoredField(mappedFieldType.name()); + return; + } else { + throw e; + } } } - if (fieldType().indexOptions() != IndexOptions.NONE) { + if (mappedFieldType.isSearchable()) { context.doc().add(new LongPoint(fieldType().name(), timestamp)); } if (fieldType().hasDocValues()) { context.doc().add(new SortedNumericDocValuesField(fieldType().name(), timestamp)); - } else if (fieldType().stored() || fieldType().indexOptions() != IndexOptions.NONE) { + } else if (fieldType.stored() || mappedFieldType.isSearchable()) { createFieldNamesField(context); } - if (fieldType().stored()) { + if (fieldType.stored()) { context.doc().add(new StoredField(fieldType().name(), timestamp)); } } @@ -650,8 +652,8 @@ protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, builder.field("ignore_malformed", ignoreMalformed.value()); } - if (includeDefaults || fieldType().nullValue() != null) { - builder.field("null_value", fieldType().nullValueAsString()); + if (nullValue != null) { + builder.field("null_value", nullValueAsString); } if (includeDefaults diff --git a/server/src/main/java/org/elasticsearch/index/mapper/DynamicKeyFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/DynamicKeyFieldMapper.java index 0bbd383079c76..9135559f998d9 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/DynamicKeyFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/DynamicKeyFieldMapper.java @@ -19,6 +19,7 @@ package org.elasticsearch.index.mapper; +import org.apache.lucene.document.FieldType; import org.elasticsearch.common.settings.Settings; /** @@ -42,7 +43,7 @@ public abstract class DynamicKeyFieldMapper extends FieldMapper { public DynamicKeyFieldMapper(String simpleName, - MappedFieldType fieldType, + FieldType fieldType, MappedFieldType defaultFieldType, Settings indexSettings, CopyTo copyTo) { diff --git a/server/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java index 0cbd4d75c76fa..7624f458cb6de 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java @@ -26,7 +26,6 @@ import org.apache.lucene.index.IndexOptions; import org.elasticsearch.Version; import org.elasticsearch.common.collect.ImmutableOpenMap; -import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Setting.Property; import org.elasticsearch.common.settings.Settings; @@ -56,66 +55,46 @@ public abstract class FieldMapper extends Mapper implements Cloneable { Setting.boolSetting("index.mapping.ignore_malformed", false, Property.IndexScope); public static final Setting COERCE_SETTING = Setting.boolSetting("index.mapping.coerce", false, Property.IndexScope); - public abstract static class Builder extends Mapper.Builder { + public abstract static class Builder> extends Mapper.Builder { - protected final MappedFieldType fieldType; - protected final MappedFieldType defaultFieldType; - private final IndexOptions defaultOptions; + protected final FieldType fieldType; protected boolean omitNormsSet = false; protected boolean indexOptionsSet = false; - protected boolean docValuesSet = false; + protected boolean hasDocValues = true; + protected boolean indexed = true; protected final MultiFields.Builder multiFieldsBuilder; protected CopyTo copyTo = CopyTo.empty(); - - protected Builder(String name, MappedFieldType fieldType, MappedFieldType defaultFieldType) { + protected float boost = 1.0f; + protected Map meta = Collections.emptyMap(); + // TODO move to KeywordFieldMapper.Builder + protected boolean eagerGlobalOrdinals; + // TODO move to text-specific builder base class + protected NamedAnalyzer indexAnalyzer; + protected NamedAnalyzer searchAnalyzer; + protected NamedAnalyzer searchQuoteAnalyzer; + protected SimilarityProvider similarity; + + protected Builder(String name, FieldType fieldType) { super(name); - this.fieldType = fieldType.clone(); - this.defaultFieldType = defaultFieldType.clone(); - this.defaultOptions = fieldType.indexOptions(); // we have to store it the fieldType is mutable - this.docValuesSet = fieldType.hasDocValues(); + this.fieldType = new FieldType(fieldType); multiFieldsBuilder = new MultiFields.Builder(); } - public MappedFieldType fieldType() { - return fieldType; - } - public T index(boolean index) { - if (index) { - if (fieldType.indexOptions() == IndexOptions.NONE) { - /* - * the logic here is to reset to the default options only if we are not indexed ie. options are null - * if the fieldType has a non-null option we are all good it might have been set through a different - * call. - */ - IndexOptions options = getDefaultIndexOption(); - if (options == IndexOptions.NONE) { - // can happen when an existing type on the same index has disabled indexing - // since we inherit the default field type from the first mapper that is - // created on an index - throw new IllegalArgumentException("mapper [" + name + "] has different [index] values from other types" - + " of the same index"); - } - fieldType.setIndexOptions(options); - } - } else { - fieldType.setIndexOptions(IndexOptions.NONE); + this.indexed = index; + if (index == false) { + this.fieldType.setIndexOptions(IndexOptions.NONE); } return builder; } - protected IndexOptions getDefaultIndexOption() { - return defaultOptions; - } - public T store(boolean store) { this.fieldType.setStored(store); return builder; } public T docValues(boolean docValues) { - this.fieldType.setHasDocValues(docValues); - this.docValuesSet = true; + this.hasDocValues = docValues; return builder; } @@ -151,7 +130,7 @@ public T storeTermVectorPayloads(boolean termVectorPayloads) { } public T boost(float boost) { - this.fieldType.setBoost(boost); + this.boost = boost; return builder; } @@ -168,28 +147,28 @@ public T indexOptions(IndexOptions indexOptions) { } public T indexAnalyzer(NamedAnalyzer indexAnalyzer) { - this.fieldType.setIndexAnalyzer(indexAnalyzer); + this.indexAnalyzer = indexAnalyzer; return builder; } public T searchAnalyzer(NamedAnalyzer searchAnalyzer) { - this.fieldType.setSearchAnalyzer(searchAnalyzer); + this.searchAnalyzer = searchAnalyzer; return builder; } public T searchQuoteAnalyzer(NamedAnalyzer searchQuoteAnalyzer) { - this.fieldType.setSearchQuoteAnalyzer(searchQuoteAnalyzer); + this.searchQuoteAnalyzer = searchQuoteAnalyzer; return builder; } public T similarity(SimilarityProvider similarity) { - this.fieldType.setSimilarity(similarity); + this.similarity = similarity; return builder; } - public Builder nullValue(Object nullValue) { - this.fieldType.setNullValue(nullValue); - return this; + public T setEagerGlobalOrdinals(boolean eagerGlobalOrdinals) { + this.eagerGlobalOrdinals = eagerGlobalOrdinals; + return builder; } public T addMultiField(Mapper.Builder mapperBuilder) { @@ -206,48 +185,30 @@ protected String buildFullName(BuilderContext context) { return context.path().pathAsText(name); } - protected boolean defaultDocValues(Version indexCreated) { - return fieldType.tokenized() == false; - } - - protected void setupFieldType(BuilderContext context) { - fieldType.setName(buildFullName(context)); - if (fieldType.indexAnalyzer() == null && fieldType.tokenized() == false && fieldType.indexOptions() != IndexOptions.NONE) { - fieldType.setIndexAnalyzer(Lucene.KEYWORD_ANALYZER); - fieldType.setSearchAnalyzer(Lucene.KEYWORD_ANALYZER); - } - boolean defaultDocValues = defaultDocValues(context.indexCreatedVersion()); - defaultFieldType.setHasDocValues(defaultDocValues); - if (docValuesSet == false) { - fieldType.setHasDocValues(defaultDocValues); - } - } - /** Set metadata on this field. */ public T meta(Map meta) { - fieldType.setMeta(meta); + this.meta = meta; return (T) this; } } protected final Version indexCreatedVersion; - protected MappedFieldType fieldType; - protected final MappedFieldType defaultFieldType; + protected FieldType fieldType; + protected MappedFieldType mappedFieldType; protected MultiFields multiFields; protected CopyTo copyTo; - protected FieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, + protected FieldMapper(String simpleName, FieldType fieldType, MappedFieldType mappedFieldType, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { super(simpleName); assert indexSettings != null; this.indexCreatedVersion = Version.indexCreated(indexSettings); - if (simpleName.isEmpty()) { + if (mappedFieldType.name().isEmpty()) { throw new IllegalArgumentException("name cannot be empty string"); } fieldType.freeze(); this.fieldType = fieldType; - defaultFieldType.freeze(); - this.defaultFieldType = defaultFieldType; + this.mappedFieldType = mappedFieldType; this.multiFields = multiFields; this.copyTo = Objects.requireNonNull(copyTo); } @@ -259,11 +220,11 @@ public String name() { @Override public String typeName() { - return fieldType.typeName(); + return mappedFieldType.typeName(); } public MappedFieldType fieldType() { - return fieldType; + return mappedFieldType; } /** @@ -324,7 +285,7 @@ protected void createFieldNamesField(ParseContext context) { FieldNamesFieldType fieldNamesFieldType = context.docMapper().metadataMapper(FieldNamesFieldMapper.class).fieldType(); if (fieldNamesFieldType != null && fieldNamesFieldType.isEnabled()) { for (String fieldName : FieldNamesFieldMapper.extractFieldNames(fieldType().name())) { - context.doc().add(new Field(FieldNamesFieldMapper.NAME, fieldName, fieldNamesFieldType)); + context.doc().add(new Field(FieldNamesFieldMapper.NAME, fieldName, FieldNamesFieldMapper.Defaults.FIELD_TYPE)); } } } @@ -348,7 +309,7 @@ public final FieldMapper merge(Mapper mergeWith) { FieldMapper merged = clone(); List conflicts = new ArrayList<>(); if (mergeWith instanceof FieldMapper == false) { - throw new IllegalArgumentException("mapper [" + fieldType.name() + "] cannot be changed from type [" + throw new IllegalArgumentException("mapper [" + mappedFieldType.name() + "] cannot be changed from type [" + contentType() + "] to [" + mergeWith.getClass().getSimpleName() + "]"); } FieldMapper toMerge = (FieldMapper) mergeWith; @@ -360,6 +321,7 @@ public final FieldMapper merge(Mapper mergeWith) { } merged.multiFields = multiFields.merge(toMerge.multiFields); // apply changeable values + merged.mappedFieldType = toMerge.mappedFieldType; merged.fieldType = toMerge.fieldType; merged.copyTo = toMerge.copyTo; return merged; @@ -372,18 +334,23 @@ private void mergeSharedOptions(FieldMapper mergeWith, List conflicts) { + "] to [" + mergeWith.contentType() + "]"); } - MappedFieldType other = mergeWith.fieldType; + FieldType other = mergeWith.fieldType; + MappedFieldType otherm = mergeWith.mappedFieldType; + this.mappedFieldType.updateMeta(otherm.meta()); boolean indexed = fieldType.indexOptions() != IndexOptions.NONE; boolean mergeWithIndexed = other.indexOptions() != IndexOptions.NONE; - // TODO: should be validating if index options go "up" (but "down" is ok) if (indexed != mergeWithIndexed) { conflicts.add("mapper [" + name() + "] has different [index] values"); } + // TODO: should be validating if index options go "up" (but "down" is ok) + if (fieldType.indexOptions() != other.indexOptions()) { + conflicts.add("mapper [" + name() + "] has different [index_options] values"); + } if (fieldType.stored() != other.stored()) { conflicts.add("mapper [" + name() + "] has different [store] values"); } - if (fieldType.hasDocValues() != other.hasDocValues()) { + if (this.mappedFieldType.hasDocValues() != otherm.hasDocValues()) { conflicts.add("mapper [" + name() + "] has different [doc_values] values"); } if (fieldType.omitNorms() && !other.omitNorms()) { @@ -403,17 +370,17 @@ private void mergeSharedOptions(FieldMapper mergeWith, List conflicts) { } // null and "default"-named index analyzers both mean the default is used - if (fieldType.indexAnalyzer() == null || "default".equals(fieldType.indexAnalyzer().name())) { - if (other.indexAnalyzer() != null && "default".equals(other.indexAnalyzer().name()) == false) { + if (mappedFieldType.indexAnalyzer() == null || "default".equals(mappedFieldType.indexAnalyzer().name())) { + if (otherm.indexAnalyzer() != null && "default".equals(otherm.indexAnalyzer().name()) == false) { conflicts.add("mapper [" + name() + "] has different [analyzer]"); } - } else if (other.indexAnalyzer() == null || "default".equals(other.indexAnalyzer().name())) { + } else if (otherm.indexAnalyzer() == null || "default".equals(otherm.indexAnalyzer().name())) { conflicts.add("mapper [" + name() + "] has different [analyzer]"); - } else if (fieldType.indexAnalyzer().name().equals(other.indexAnalyzer().name()) == false) { + } else if (mappedFieldType.indexAnalyzer().name().equals(otherm.indexAnalyzer().name()) == false) { conflicts.add("mapper [" + name() + "] has different [analyzer]"); } - if (Objects.equals(fieldType.similarity(), other.similarity()) == false) { + if (Objects.equals(mappedFieldType.similarity(), otherm.similarity()) == false) { conflicts.add("mapper [" + name() + "] has different [similarity]"); } } @@ -431,6 +398,18 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws return builder.endObject(); } + protected boolean indexedByDefault() { + return true; + } + + protected boolean docValuesByDefault() { + return true; + } + + protected boolean storedByDefault() { + return false; + } + protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException { builder.field("type", contentType()); @@ -439,26 +418,14 @@ protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, builder.field("boost", fieldType().boost()); } - boolean indexed = fieldType().indexOptions() != IndexOptions.NONE; - boolean defaultIndexed = defaultFieldType.indexOptions() != IndexOptions.NONE; - if (includeDefaults || indexed != defaultIndexed) { - builder.field("index", indexed); - } - if (includeDefaults || fieldType().stored() != defaultFieldType.stored()) { - builder.field("store", fieldType().stored()); - } - doXContentDocValues(builder, includeDefaults); - if (includeDefaults || fieldType().storeTermVectors() != defaultFieldType.storeTermVectors()) { - builder.field("term_vector", termVectorOptionsToString(fieldType())); - } - if (includeDefaults || fieldType().omitNorms() != defaultFieldType.omitNorms()) { - builder.field("norms", fieldType().omitNorms() == false); + if (includeDefaults || mappedFieldType.isSearchable() != indexedByDefault()) { + builder.field("index", mappedFieldType.isSearchable()); } - if (indexed && (includeDefaults || fieldType().indexOptions() != defaultFieldType.indexOptions())) { - builder.field("index_options", indexOptionToString(fieldType().indexOptions())); + if (includeDefaults || mappedFieldType.hasDocValues() != docValuesByDefault()) { + builder.field("doc_values", mappedFieldType.hasDocValues()); } - if (includeDefaults || fieldType().eagerGlobalOrdinals() != defaultFieldType.eagerGlobalOrdinals()) { - builder.field("eager_global_ordinals", fieldType().eagerGlobalOrdinals()); + if (includeDefaults || fieldType.stored() != storedByDefault()) { + builder.field("store", fieldType.stored()); } if (fieldType().similarity() != null) { @@ -500,12 +467,6 @@ protected final void doXContentAnalyzers(XContentBuilder builder, boolean includ } } - protected void doXContentDocValues(XContentBuilder builder, boolean includeDefaults) throws IOException { - if (includeDefaults || defaultFieldType.hasDocValues() != fieldType().hasDocValues()) { - builder.field("doc_values", fieldType().hasDocValues()); - } - } - protected static String indexOptionToString(IndexOptions indexOption) { switch (indexOption) { case DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS: diff --git a/server/src/main/java/org/elasticsearch/index/mapper/FieldNamesFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/FieldNamesFieldMapper.java index 68523a9db9a9d..6c2843a055d65 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/FieldNamesFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/FieldNamesFieldMapper.java @@ -20,17 +20,18 @@ package org.elasticsearch.index.mapper; import org.apache.logging.log4j.LogManager; +import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.search.Query; import org.elasticsearch.Version; import org.elasticsearch.common.logging.DeprecationLogger; -import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.support.XContentMapValues; import org.elasticsearch.index.query.QueryShardContext; import java.io.IOException; +import java.util.Collections; import java.util.Iterator; import java.util.Map; import java.util.Objects; @@ -53,16 +54,13 @@ public static class Defaults { public static final String NAME = FieldNamesFieldMapper.NAME; public static final boolean ENABLED = true; - public static final MappedFieldType FIELD_TYPE = new FieldNamesFieldType(); + public static final FieldType FIELD_TYPE = new FieldType(); static { FIELD_TYPE.setIndexOptions(IndexOptions.DOCS); FIELD_TYPE.setTokenized(false); FIELD_TYPE.setStored(false); FIELD_TYPE.setOmitNorms(true); - FIELD_TYPE.setIndexAnalyzer(Lucene.KEYWORD_ANALYZER); - FIELD_TYPE.setSearchAnalyzer(Lucene.KEYWORD_ANALYZER); - FIELD_TYPE.setName(NAME); FIELD_TYPE.freeze(); } } @@ -70,8 +68,8 @@ public static class Defaults { static class Builder extends MetadataFieldMapper.Builder { private boolean enabled = Defaults.ENABLED; - Builder(MappedFieldType existing) { - super(Defaults.NAME, existing == null ? Defaults.FIELD_TYPE : existing, Defaults.FIELD_TYPE); + Builder() { + super(Defaults.NAME, Defaults.FIELD_TYPE); } Builder enabled(boolean enabled) { @@ -81,11 +79,9 @@ Builder enabled(boolean enabled) { @Override public FieldNamesFieldMapper build(BuilderContext context) { - setupFieldType(context); - fieldType.setHasDocValues(false); - FieldNamesFieldType fieldNamesFieldType = (FieldNamesFieldType)fieldType; + FieldNamesFieldType fieldNamesFieldType = new FieldNamesFieldType(); fieldNamesFieldType.setEnabled(enabled); - return new FieldNamesFieldMapper(fieldType, context.indexSettings()); + return new FieldNamesFieldMapper(fieldType, fieldNamesFieldType, context.indexSettings()); } } @@ -98,7 +94,7 @@ public static class TypeParser implements MetadataFieldMapper.TypeParser { @Override public MetadataFieldMapper.Builder parse(String name, Map node, ParserContext parserContext) throws MapperParsingException { - Builder builder = new Builder(parserContext.mapperService().fieldType(NAME)); + Builder builder = new Builder(); for (Iterator> iterator = node.entrySet().iterator(); iterator.hasNext();) { Map.Entry entry = iterator.next(); @@ -122,7 +118,7 @@ public MetadataFieldMapper.Builder parse(String name, Map nod @Override public MetadataFieldMapper getDefault(ParserContext context) { final Settings indexSettings = context.mapperService().getIndexSettings().getSettings(); - return new FieldNamesFieldMapper(Defaults.FIELD_TYPE, indexSettings); + return new FieldNamesFieldMapper(Defaults.FIELD_TYPE, new FieldNamesFieldType(), indexSettings); } } @@ -131,6 +127,7 @@ public static final class FieldNamesFieldType extends TermBasedFieldType { private boolean enabled = Defaults.ENABLED; public FieldNamesFieldType() { + super(Defaults.NAME, true, false, Collections.emptyMap()); } protected FieldNamesFieldType(FieldNamesFieldType ref) { @@ -161,7 +158,6 @@ public String typeName() { } public void setEnabled(boolean enabled) { - checkIfFrozen(); this.enabled = enabled; } @@ -185,8 +181,8 @@ public Query termQuery(Object value, QueryShardContext context) { } } - private FieldNamesFieldMapper(MappedFieldType fieldType, Settings indexSettings) { - super(NAME, fieldType, Defaults.FIELD_TYPE, indexSettings); + private FieldNamesFieldMapper(FieldType fieldType, MappedFieldType mappedFieldType, Settings indexSettings) { + super(fieldType, mappedFieldType, indexSettings); } @Override diff --git a/server/src/main/java/org/elasticsearch/index/mapper/GeoPointFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/GeoPointFieldMapper.java index 5b4ac56b6138e..7888ba791a67d 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/GeoPointFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/GeoPointFieldMapper.java @@ -18,6 +18,7 @@ */ package org.elasticsearch.index.mapper; +import org.apache.lucene.document.FieldType; import org.apache.lucene.document.LatLonDocValuesField; import org.apache.lucene.document.LatLonPoint; import org.apache.lucene.document.StoredField; @@ -46,40 +47,33 @@ */ public class GeoPointFieldMapper extends AbstractPointGeometryFieldMapper, List> { public static final String CONTENT_TYPE = "geo_point"; + public static final FieldType FIELD_TYPE = new FieldType(); + static { + FIELD_TYPE.setStored(false); + FIELD_TYPE.freeze(); + } public static class Builder extends AbstractPointGeometryFieldMapper.Builder { + public Builder(String name) { - super(name, new GeoPointFieldType(), new GeoPointFieldType()); + super(name, FIELD_TYPE); + hasDocValues = true; builder = this; } - public GeoPointFieldMapper build(BuilderContext context, String simpleName, MappedFieldType fieldType, - MappedFieldType defaultFieldType, Settings indexSettings, - MultiFields multiFields, Explicit ignoreMalformed, - Explicit ignoreZValue, CopyTo copyTo) { - setupFieldType(context); - return new GeoPointFieldMapper(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, - ignoreMalformed, ignoreZValue, copyTo); - } - - @Override - protected void setGeometryParser() { - PointParser pointParser = new PointParser<>(); - fieldType().setGeometryParser((parser, mapper) -> pointParser.parse(parser, mapper)); - } - - @Override - protected void setGeometryIndexer(GeoPointFieldType fieldType) { - fieldType.setGeometryIndexer(new GeoPointIndexer(fieldType)); - } - - @Override - protected void setGeometryQueryBuilder(GeoPointFieldType fieldType) { - fieldType.setGeometryQueryBuilder(new VectorGeoPointShapeQueryProcessor()); + public GeoPointFieldMapper build(BuilderContext context, String simpleName, FieldType fieldType, + Settings indexSettings, MultiFields multiFields, Explicit ignoreMalformed, + Explicit ignoreZValue, ParsedPoint nullValue, CopyTo copyTo) { + GeoPointFieldType ft = new GeoPointFieldType(buildFullName(context), indexed, hasDocValues, meta); + ft.setGeometryParser(new PointParser<>()); + ft.setGeometryIndexer(new GeoPointIndexer(ft)); + ft.setGeometryQueryBuilder(new VectorGeoPointShapeQueryProcessor()); + return new GeoPointFieldMapper(name, fieldType, ft, indexSettings, multiFields, + ignoreMalformed, ignoreZValue, nullValue, copyTo); } } - public static class TypeParser extends AbstractPointGeometryFieldMapper.TypeParser { + public static class TypeParser extends AbstractPointGeometryFieldMapper.TypeParser { @Override protected Builder newBuilder(String name, Map params) { return new GeoPointFieldMapper.Builder(name); @@ -111,16 +105,16 @@ protected void parsePointIgnoringMalformed(XContentParser parser, ParsedPoint po super.parsePointIgnoringMalformed(parser, point); } - public GeoPointFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, + public GeoPointFieldMapper(String simpleName, FieldType fieldType, MappedFieldType mappedFieldType, Settings indexSettings, MultiFields multiFields, Explicit ignoreMalformed, - Explicit ignoreZValue, CopyTo copyTo) { - super(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, ignoreMalformed, ignoreZValue, copyTo); + Explicit ignoreZValue, ParsedPoint nullValue, CopyTo copyTo) { + super(simpleName, fieldType, mappedFieldType, indexSettings, multiFields, ignoreMalformed, ignoreZValue, nullValue, copyTo); } @Override protected void addStoredFields(ParseContext context, List points) { for (GeoPoint point : points) { - context.doc().add(new StoredField(fieldType.name(), point.toString())); + context.doc().add(new StoredField(fieldType().name(), point.toString())); } } @@ -149,7 +143,7 @@ protected void addMultiFields(ParseContext context, List poi @Override protected void addDocValuesFields(String name, List points, List fields, ParseContext context) { for (GeoPoint point : points) { - context.doc().add(new LatLonDocValuesField(fieldType.name(), point.lat(), point.lon())); + context.doc().add(new LatLonDocValuesField(fieldType().name(), point.lat(), point.lon())); } } @@ -160,7 +154,7 @@ protected String contentType() { @Override public GeoPointFieldType fieldType() { - return (GeoPointFieldType)fieldType; + return (GeoPointFieldType)mappedFieldType; } @Override @@ -169,8 +163,12 @@ protected ParsedPoint newParsedPoint() { } public static class GeoPointFieldType extends AbstractPointGeometryFieldType, List> { - public GeoPointFieldType() { - super(); + public GeoPointFieldType(String name, boolean indexed, boolean hasDocValues, Map meta) { + super(name, indexed, hasDocValues, meta); + } + + public GeoPointFieldType(String name) { + this(name, true, true, Collections.emptyMap()); } GeoPointFieldType(GeoPointFieldType ref) { diff --git a/server/src/main/java/org/elasticsearch/index/mapper/GeoShapeFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/GeoShapeFieldMapper.java index e66068f16fdf5..bec622bc46b81 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/GeoShapeFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/GeoShapeFieldMapper.java @@ -18,6 +18,7 @@ */ package org.elasticsearch.index.mapper; +import org.apache.lucene.document.FieldType; import org.apache.lucene.document.LatLonShape; import org.elasticsearch.common.Explicit; import org.elasticsearch.common.geo.GeometryParser; @@ -51,43 +52,42 @@ */ public class GeoShapeFieldMapper extends AbstractShapeGeometryFieldMapper { public static final String CONTENT_TYPE = "geo_shape"; + public static final FieldType FIELD_TYPE = new FieldType(); + static { + FIELD_TYPE.setDimensions(7, 4, Integer.BYTES); + FIELD_TYPE.setOmitNorms(true); + FIELD_TYPE.freeze(); + } - public static class Builder extends AbstractShapeGeometryFieldMapper.Builder { - public Builder(String name) { - super (name, new GeoShapeFieldType(), new GeoShapeFieldType()); - } + public static class Builder extends AbstractShapeGeometryFieldMapper.Builder { - @Override - public GeoShapeFieldMapper build(BuilderContext context) { - setupFieldType(context); - return new GeoShapeFieldMapper(name, fieldType, defaultFieldType, ignoreMalformed(context), coerce(context), - ignoreZValue(), orientation(), context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); + public Builder(String name) { + super (name, FIELD_TYPE); + this.hasDocValues = false; } - @Override - protected void setGeometryParser(GeoShapeFieldType ft) { - // @todo check coerce + private GeoShapeFieldType buildFieldType(BuilderContext context) { + GeoShapeFieldType ft = new GeoShapeFieldType(buildFullName(context), indexed, hasDocValues, meta); GeometryParser geometryParser = new GeometryParser(ft.orientation.getAsBoolean(), coerce().value(), ignoreZValue().value()); - ft.setGeometryParser( (parser, mapper) -> geometryParser.parse(parser)); + ft.setGeometryParser((parser, mapper) -> geometryParser.parse(parser)); + ft.setGeometryIndexer(new GeoShapeIndexer(orientation().value().getAsBoolean(), buildFullName(context))); + ft.setGeometryQueryBuilder(new VectorGeoShapeQueryProcessor()); + ft.setOrientation(orientation == null ? Defaults.ORIENTATION.value() : orientation); + return ft; } @Override - protected void setGeometryIndexer(GeoShapeFieldType fieldType) { - fieldType.setGeometryIndexer(new GeoShapeIndexer(fieldType.orientation.getAsBoolean(), fieldType.name())); - } - - @Override - protected void setGeometryQueryBuilder(GeoShapeFieldType fieldType) { - fieldType.setGeometryQueryBuilder(new VectorGeoShapeQueryProcessor()); + public GeoShapeFieldMapper build(BuilderContext context) { + return new GeoShapeFieldMapper(name, fieldType, buildFieldType(context), ignoreMalformed(context), coerce(context), + ignoreZValue(), orientation(), context.indexSettings(), + multiFieldsBuilder.build(this, context), copyTo); } } public static class GeoShapeFieldType extends AbstractShapeGeometryFieldType { - public GeoShapeFieldType() { - super(); - setDimensions(7, 4, Integer.BYTES); + public GeoShapeFieldType(String name, boolean indexed, boolean hasDocValues, Map meta) { + super(name, indexed, hasDocValues, meta); } protected GeoShapeFieldType(GeoShapeFieldType ref) { @@ -117,11 +117,11 @@ protected AbstractShapeGeometryFieldMapper.Builder newBuilder(String name, Map ignoreMalformed, Explicit coerce, Explicit ignoreZValue, Explicit orientation, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { - super(simpleName, fieldType, defaultFieldType, ignoreMalformed, coerce, ignoreZValue, orientation, indexSettings, + super(simpleName, fieldType, mappedFieldType, ignoreMalformed, coerce, ignoreZValue, orientation, indexSettings, multiFields, copyTo); } @@ -146,7 +146,7 @@ protected void addMultiFields(ParseContext context, Geometry geometry) { protected void mergeGeoOptions(AbstractShapeGeometryFieldMapper mergeWith, List conflicts) { if (mergeWith instanceof LegacyGeoShapeFieldMapper) { LegacyGeoShapeFieldMapper legacy = (LegacyGeoShapeFieldMapper) mergeWith; - throw new IllegalArgumentException("[" + fieldType.name() + "] with field mapper [" + fieldType.typeName() + "] " + + throw new IllegalArgumentException("[" + fieldType().name() + "] with field mapper [" + fieldType().typeName() + "] " + "using [BKD] strategy cannot be merged with " + "[" + legacy.fieldType().typeName() + "] with [" + legacy.fieldType().strategy() + "] strategy"); } @@ -161,4 +161,9 @@ public GeoShapeFieldType fieldType() { protected String contentType() { return CONTENT_TYPE; } + + @Override + protected boolean docValuesByDefault() { + return false; + } } diff --git a/server/src/main/java/org/elasticsearch/index/mapper/GeoShapeIndexer.java b/server/src/main/java/org/elasticsearch/index/mapper/GeoShapeIndexer.java index 4ec25f797e443..3e8dd4590b983 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/GeoShapeIndexer.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/GeoShapeIndexer.java @@ -190,7 +190,7 @@ public List indexShape(ParseContext context, Geometry shape) { private static class LuceneGeometryIndexer implements GeometryVisitor { private List fields = new ArrayList<>(); - private String name; + private final String name; private LuceneGeometryIndexer(String name) { this.name = name; diff --git a/server/src/main/java/org/elasticsearch/index/mapper/IdFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/IdFieldMapper.java index c354efa848675..4b340167269f0 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/IdFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/IdFieldMapper.java @@ -21,6 +21,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.lucene.document.Field; +import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.search.MatchAllDocsQuery; @@ -54,6 +55,7 @@ import java.io.IOException; import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.Map; @@ -76,20 +78,22 @@ public class IdFieldMapper extends MetadataFieldMapper { public static class Defaults { public static final String NAME = IdFieldMapper.NAME; - public static final MappedFieldType FIELD_TYPE = new IdFieldType(); - public static final MappedFieldType NESTED_FIELD_TYPE; + public static final FieldType FIELD_TYPE = new FieldType(); + public static final FieldType NESTED_FIELD_TYPE; + public static final MappedFieldType MAPPED_FIELD_TYPE = new IdFieldType(); static { FIELD_TYPE.setTokenized(false); FIELD_TYPE.setIndexOptions(IndexOptions.DOCS); FIELD_TYPE.setStored(true); FIELD_TYPE.setOmitNorms(true); - FIELD_TYPE.setIndexAnalyzer(Lucene.KEYWORD_ANALYZER); - FIELD_TYPE.setSearchAnalyzer(Lucene.KEYWORD_ANALYZER); - FIELD_TYPE.setName(NAME); FIELD_TYPE.freeze(); - NESTED_FIELD_TYPE = FIELD_TYPE.clone(); + NESTED_FIELD_TYPE = new FieldType(); + NESTED_FIELD_TYPE.setTokenized(false); + NESTED_FIELD_TYPE.setIndexOptions(IndexOptions.DOCS); + NESTED_FIELD_TYPE.setStored(true); + NESTED_FIELD_TYPE.setOmitNorms(true); NESTED_FIELD_TYPE.setStored(false); NESTED_FIELD_TYPE.freeze(); } @@ -105,13 +109,18 @@ public MetadataFieldMapper.Builder parse(String name, Map nod @Override public MetadataFieldMapper getDefault(ParserContext context) { final IndexSettings indexSettings = context.mapperService().getIndexSettings(); - return new IdFieldMapper(indexSettings, Defaults.FIELD_TYPE); + return new IdFieldMapper(Defaults.FIELD_TYPE, indexSettings); } } static final class IdFieldType extends TermBasedFieldType { - IdFieldType() { + public static final IdFieldType INSTANCE = new IdFieldType(); + + private IdFieldType() { + super(NAME, true, false, Collections.emptyMap()); + setIndexAnalyzer(Lucene.KEYWORD_ANALYZER); + setSearchAnalyzer(Lucene.KEYWORD_ANALYZER); } protected IdFieldType(IdFieldType ref) { @@ -160,9 +169,6 @@ public Query termsQuery(List values, QueryShardContext context) { @Override public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName) { - if (indexOptions() == IndexOptions.NONE) { - throw new IllegalArgumentException("Fielddata access on the _id field is disallowed"); - } final IndexFieldData.Builder fieldDataBuilder = new PagedBytesIndexFieldData.Builder( TextFieldMapper.Defaults.FIELDDATA_MIN_FREQUENCY, TextFieldMapper.Defaults.FIELDDATA_MAX_FREQUENCY, @@ -279,19 +285,8 @@ public boolean advanceExact(int doc) throws IOException { }; } - static MappedFieldType defaultFieldType(IndexSettings indexSettings) { - MappedFieldType defaultFieldType = Defaults.FIELD_TYPE.clone(); - defaultFieldType.setIndexOptions(IndexOptions.DOCS); - defaultFieldType.setStored(true); - return defaultFieldType; - } - - private IdFieldMapper(IndexSettings indexSettings, MappedFieldType existing) { - this(existing == null ? defaultFieldType(indexSettings) : existing, indexSettings); - } - - private IdFieldMapper(MappedFieldType fieldType, IndexSettings indexSettings) { - super(NAME, fieldType, defaultFieldType(indexSettings), indexSettings.getSettings()); + private IdFieldMapper(FieldType fieldType, IndexSettings indexSettings) { + super(fieldType, new IdFieldType(), indexSettings.getSettings()); } @Override diff --git a/server/src/main/java/org/elasticsearch/index/mapper/IgnoredFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/IgnoredFieldMapper.java index eb0abc15f2c18..fda7cb7a0fb38 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/IgnoredFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/IgnoredFieldMapper.java @@ -20,15 +20,16 @@ package org.elasticsearch.index.mapper; import org.apache.lucene.document.Field; +import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.search.Query; import org.apache.lucene.search.TermRangeQuery; -import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.index.query.QueryShardContext; import java.io.IOException; +import java.util.Collections; import java.util.Map; /** @@ -43,24 +44,21 @@ public final class IgnoredFieldMapper extends MetadataFieldMapper { public static class Defaults { public static final String NAME = IgnoredFieldMapper.NAME; - public static final MappedFieldType FIELD_TYPE = new IgnoredFieldType(); + public static final FieldType FIELD_TYPE = new FieldType(); static { FIELD_TYPE.setIndexOptions(IndexOptions.DOCS); FIELD_TYPE.setTokenized(false); FIELD_TYPE.setStored(true); FIELD_TYPE.setOmitNorms(true); - FIELD_TYPE.setIndexAnalyzer(Lucene.KEYWORD_ANALYZER); - FIELD_TYPE.setSearchAnalyzer(Lucene.KEYWORD_ANALYZER); - FIELD_TYPE.setName(NAME); FIELD_TYPE.freeze(); } } public static class Builder extends MetadataFieldMapper.Builder { - public Builder(MappedFieldType existing) { - super(Defaults.NAME, existing == null ? Defaults.FIELD_TYPE : existing, Defaults.FIELD_TYPE); + public Builder() { + super(Defaults.NAME, Defaults.FIELD_TYPE); } @Override @@ -73,7 +71,7 @@ public static class TypeParser implements MetadataFieldMapper.TypeParser { @Override public MetadataFieldMapper.Builder parse(String name, Map node, ParserContext parserContext) throws MapperParsingException { - return new Builder(parserContext.mapperService().fieldType(NAME)); + return new Builder(); } @Override @@ -85,7 +83,10 @@ public MetadataFieldMapper getDefault(ParserContext context) { public static final class IgnoredFieldType extends StringFieldType { - public IgnoredFieldType() { + public static final IgnoredFieldType INSTANCE = new IgnoredFieldType(); + + private IgnoredFieldType() { + super(NAME, true, false, Collections.emptyMap()); } protected IgnoredFieldType(IgnoredFieldType ref) { @@ -114,7 +115,7 @@ public Query existsQuery(QueryShardContext context) { } private IgnoredFieldMapper(Settings indexSettings) { - super(NAME, Defaults.FIELD_TYPE, Defaults.FIELD_TYPE, indexSettings); + super(Defaults.FIELD_TYPE, IgnoredFieldType.INSTANCE, indexSettings); } @Override @@ -134,7 +135,7 @@ public void parse(ParseContext context) throws IOException { @Override protected void parseCreateField(ParseContext context) throws IOException { for (String field : context.getIgnoredFields()) { - context.doc().add(new Field(NAME, field, fieldType())); + context.doc().add(new Field(NAME, field, fieldType)); } } diff --git a/server/src/main/java/org/elasticsearch/index/mapper/IndexFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/IndexFieldMapper.java index dc746b326ac58..9882fd852ed38 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/IndexFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/IndexFieldMapper.java @@ -19,10 +19,10 @@ package org.elasticsearch.index.mapper; +import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.search.MatchAllDocsQuery; import org.apache.lucene.search.Query; -import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.index.fielddata.IndexFieldData; @@ -31,6 +31,7 @@ import org.elasticsearch.search.aggregations.support.CoreValuesSourceType; import java.io.IOException; +import java.util.Collections; import java.util.Map; @@ -43,29 +44,25 @@ public class IndexFieldMapper extends MetadataFieldMapper { public static class Defaults { public static final String NAME = IndexFieldMapper.NAME; - public static final MappedFieldType FIELD_TYPE = new IndexFieldType(); + public static final FieldType FIELD_TYPE = new FieldType(); static { FIELD_TYPE.setIndexOptions(IndexOptions.NONE); FIELD_TYPE.setTokenized(false); FIELD_TYPE.setStored(false); FIELD_TYPE.setOmitNorms(true); - FIELD_TYPE.setIndexAnalyzer(Lucene.KEYWORD_ANALYZER); - FIELD_TYPE.setSearchAnalyzer(Lucene.KEYWORD_ANALYZER); - FIELD_TYPE.setName(NAME); FIELD_TYPE.freeze(); } } public static class Builder extends MetadataFieldMapper.Builder { - public Builder(MappedFieldType existing) { - super(Defaults.NAME, existing == null ? Defaults.FIELD_TYPE : existing, Defaults.FIELD_TYPE); + public Builder() { + super(Defaults.NAME, Defaults.FIELD_TYPE); } @Override public IndexFieldMapper build(BuilderContext context) { - setupFieldType(context); return new IndexFieldMapper(fieldType, context.indexSettings()); } } @@ -80,13 +77,17 @@ public MetadataFieldMapper.Builder parse(String name, Map nod @Override public MetadataFieldMapper getDefault(ParserContext context) { final Settings indexSettings = context.mapperService().getIndexSettings().getSettings(); - return new IndexFieldMapper(indexSettings, Defaults.FIELD_TYPE); + return new IndexFieldMapper(Defaults.FIELD_TYPE, indexSettings); } } static final class IndexFieldType extends ConstantFieldType { - IndexFieldType() {} + static final IndexFieldType INSTANCE = new IndexFieldType(); + + private IndexFieldType() { + super(NAME, Collections.emptyMap()); + } protected IndexFieldType(IndexFieldType ref) { super(ref); @@ -119,12 +120,8 @@ public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName) { } - private IndexFieldMapper(Settings indexSettings, MappedFieldType existing) { - this(existing == null ? Defaults.FIELD_TYPE.clone() : existing, indexSettings); - } - - private IndexFieldMapper(MappedFieldType fieldType, Settings indexSettings) { - super(NAME, fieldType, Defaults.FIELD_TYPE, indexSettings); + private IndexFieldMapper(FieldType fieldType, Settings indexSettings) { + super(fieldType, IndexFieldType.INSTANCE, indexSettings); } @Override diff --git a/server/src/main/java/org/elasticsearch/index/mapper/IpFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/IpFieldMapper.java index d1466103b3aa2..f0a7a4e216a3b 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/IpFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/IpFieldMapper.java @@ -19,10 +19,10 @@ package org.elasticsearch.index.mapper; +import org.apache.lucene.document.FieldType; import org.apache.lucene.document.InetAddressPoint; import org.apache.lucene.document.SortedSetDocValuesField; import org.apache.lucene.document.StoredField; -import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.SortedSetDocValues; import org.apache.lucene.index.Term; import org.apache.lucene.search.DocValuesFieldExistsQuery; @@ -49,6 +49,7 @@ import java.net.InetAddress; import java.time.ZoneId; import java.util.Arrays; +import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -60,14 +61,20 @@ public class IpFieldMapper extends FieldMapper { public static class Defaults { public static final Explicit IGNORE_MALFORMED = new Explicit<>(false, false); + public static final FieldType FIELD_TYPE = new FieldType(); + static { + FIELD_TYPE.setDimensions(1, Integer.BYTES); + FIELD_TYPE.freeze(); + } } public static class Builder extends FieldMapper.Builder { private Boolean ignoreMalformed; + private InetAddress nullValue; public Builder(String name) { - super(name, new IpFieldType(), new IpFieldType()); + super(name, Defaults.FIELD_TYPE); builder = this; } @@ -76,6 +83,11 @@ public Builder ignoreMalformed(boolean ignoreMalformed) { return builder; } + public Builder nullValue(InetAddress nullValue) { + this.nullValue = nullValue; + return builder; + } + protected Explicit ignoreMalformed(BuilderContext context) { if (ignoreMalformed != null) { return new Explicit<>(ignoreMalformed, true); @@ -88,10 +100,11 @@ protected Explicit ignoreMalformed(BuilderContext context) { @Override public IpFieldMapper build(BuilderContext context) { - setupFieldType(context); - return new IpFieldMapper(name, fieldType, defaultFieldType, ignoreMalformed(context), - context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); + return new IpFieldMapper(name, fieldType, new IpFieldType(buildFullName(context), indexed, hasDocValues, meta), + ignoreMalformed(context), nullValue, context.indexSettings(), + multiFieldsBuilder.build(this, context), copyTo); } + } public static class TypeParser implements Mapper.TypeParser { @@ -126,10 +139,12 @@ public Mapper.Builder parse(String name, Map node, ParserCont public static final class IpFieldType extends SimpleMappedFieldType { - public IpFieldType() { - super(); - setTokenized(false); - setHasDocValues(true); + public IpFieldType(String name, boolean indexed, boolean hasDocValues, Map meta) { + super(name, indexed, hasDocValues, meta); + } + + public IpFieldType(String name) { + this(name, true, true, Collections.emptyMap()); } IpFieldType(IpFieldType other) { @@ -316,17 +331,20 @@ public DocValueFormat docValueFormat(@Nullable String format, ZoneId timeZone) { } private Explicit ignoreMalformed; + private final InetAddress nullValue; private IpFieldMapper( String simpleName, - MappedFieldType fieldType, - MappedFieldType defaultFieldType, + FieldType fieldType, + MappedFieldType mappedFieldType, Explicit ignoreMalformed, + InetAddress nullValue, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { - super(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, copyTo); + super(simpleName, fieldType, mappedFieldType, indexSettings, multiFields, copyTo); this.ignoreMalformed = ignoreMalformed; + this.nullValue = nullValue; } @Override @@ -336,7 +354,7 @@ public IpFieldType fieldType() { @Override protected String contentType() { - return fieldType.typeName(); + return fieldType().typeName(); } @Override @@ -354,7 +372,7 @@ protected void parseCreateField(ParseContext context) throws IOException { } if (addressAsObject == null) { - addressAsObject = fieldType().nullValue(); + addressAsObject = nullValue; } if (addressAsObject == null) { @@ -370,7 +388,7 @@ protected void parseCreateField(ParseContext context) throws IOException { address = InetAddresses.forString(addressAsString); } catch (IllegalArgumentException e) { if (ignoreMalformed.value()) { - context.addIgnoredField(fieldType.name()); + context.addIgnoredField(fieldType().name()); return; } else { throw e; @@ -378,15 +396,15 @@ protected void parseCreateField(ParseContext context) throws IOException { } } - if (fieldType().indexOptions() != IndexOptions.NONE) { + if (fieldType().isSearchable()) { context.doc().add(new InetAddressPoint(fieldType().name(), address)); } if (fieldType().hasDocValues()) { context.doc().add(new SortedSetDocValuesField(fieldType().name(), new BytesRef(InetAddressPoint.encode(address)))); - } else if (fieldType().stored() || fieldType().indexOptions() != IndexOptions.NONE) { + } else if (fieldType.stored() || fieldType().isSearchable()) { createFieldNamesField(context); } - if (fieldType().stored()) { + if (fieldType.stored()) { context.doc().add(new StoredField(fieldType().name(), new BytesRef(InetAddressPoint.encode(address)))); } } @@ -394,6 +412,9 @@ protected void parseCreateField(ParseContext context) throws IOException { @Override protected void mergeOptions(FieldMapper other, List conflicts) { IpFieldMapper mergeWith = (IpFieldMapper) other; + if (mergeWith.nullValue != this.nullValue) { + conflicts.add("mapper [" + name() + "] has different [null_value] values"); + } if (mergeWith.ignoreMalformed.explicit()) { this.ignoreMalformed = mergeWith.ignoreMalformed; } @@ -403,12 +424,8 @@ protected void mergeOptions(FieldMapper other, List conflicts) { protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException { super.doXContentBody(builder, includeDefaults, params); - if (includeDefaults || fieldType().nullValue() != null) { - Object nullValue = fieldType().nullValue(); - if (nullValue != null) { - nullValue = InetAddresses.toAddrString((InetAddress) nullValue); - } - builder.field("null_value", nullValue); + if (nullValue != null) { + builder.field("null_value", InetAddresses.toAddrString(nullValue)); } if (includeDefaults || ignoreMalformed.explicit()) { diff --git a/server/src/main/java/org/elasticsearch/index/mapper/KeywordFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/KeywordFieldMapper.java index 8218fc1be4b91..d3d4cec29f558 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/KeywordFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/KeywordFieldMapper.java @@ -23,6 +23,7 @@ import org.apache.lucene.analysis.core.WhitespaceAnalyzer; import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; import org.apache.lucene.document.Field; +import org.apache.lucene.document.FieldType; import org.apache.lucene.document.SortedSetDocValuesField; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.Term; @@ -42,9 +43,11 @@ import org.elasticsearch.index.fielddata.IndexFieldData; import org.elasticsearch.index.fielddata.plain.SortedSetOrdinalsIndexFieldData; import org.elasticsearch.index.query.QueryShardContext; +import org.elasticsearch.index.similarity.SimilarityProvider; import org.elasticsearch.search.aggregations.support.CoreValuesSourceType; import java.io.IOException; +import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -60,7 +63,7 @@ public final class KeywordFieldMapper extends FieldMapper { public static final String CONTENT_TYPE = "keyword"; public static class Defaults { - public static final MappedFieldType FIELD_TYPE = new KeywordFieldType(); + public static final FieldType FIELD_TYPE = new FieldType(); static { FIELD_TYPE.setTokenized(false); @@ -71,6 +74,20 @@ public static class Defaults { public static final String NULL_VALUE = null; public static final int IGNORE_ABOVE = Integer.MAX_VALUE; + public static final boolean EAGER_GLOBAL_ORDINALS = false; + public static final boolean SPLIT_QUERIES_ON_WHITESPACE = false; + } + + public static class KeywordField extends Field { + + public KeywordField(String field, BytesRef term, FieldType ft) { + super(field, term, ft); + } + + public KeywordField(String field, BytesRef term) { + super(field, term, Defaults.FIELD_TYPE); + } + } public static class Builder extends FieldMapper.Builder { @@ -78,16 +95,19 @@ public static class Builder extends FieldMapper.Builder { protected String nullValue = Defaults.NULL_VALUE; protected int ignoreAbove = Defaults.IGNORE_ABOVE; private IndexAnalyzers indexAnalyzers; - private String normalizerName; + private String normalizerName = "default"; + private boolean eagerGlobalOrdinals = Defaults.EAGER_GLOBAL_ORDINALS; + private boolean splitQueriesOnWhitespace = Defaults.SPLIT_QUERIES_ON_WHITESPACE; public Builder(String name) { - super(name, Defaults.FIELD_TYPE, Defaults.FIELD_TYPE); + super(name, Defaults.FIELD_TYPE); builder = this; } @Override - public KeywordFieldType fieldType() { - return (KeywordFieldType) super.fieldType(); + public Builder omitNorms(boolean omitNorms) { + fieldType.setOmitNorms(omitNorms); + return builder; } public Builder ignoreAbove(int ignoreAbove) { @@ -108,12 +128,12 @@ public Builder indexOptions(IndexOptions indexOptions) { } public Builder eagerGlobalOrdinals(boolean eagerGlobalOrdinals) { - fieldType().setEagerGlobalOrdinals(eagerGlobalOrdinals); + this.eagerGlobalOrdinals = eagerGlobalOrdinals; return builder; } public Builder splitQueriesOnWhitespace(boolean splitQueriesOnWhitespace) { - fieldType().setSplitQueriesOnWhitespace(splitQueriesOnWhitespace); + this.splitQueriesOnWhitespace = splitQueriesOnWhitespace; return builder; } @@ -123,27 +143,37 @@ public Builder normalizer(IndexAnalyzers indexAnalyzers, String name) { return builder; } - @Override - public KeywordFieldMapper build(BuilderContext context) { - setupFieldType(context); - if (normalizerName != null) { - NamedAnalyzer normalizer = indexAnalyzers.getNormalizer(normalizerName); + public Builder nullValue(String nullValue) { + this.nullValue = nullValue; + return builder; + } + + private KeywordFieldType buildFieldType(BuilderContext context) { + NamedAnalyzer normalizer = Lucene.KEYWORD_ANALYZER; + NamedAnalyzer searchAnalyzer = Lucene.KEYWORD_ANALYZER; + if (normalizerName == null || "default".equals(normalizerName) == false) { + normalizer = indexAnalyzers.getNormalizer(normalizerName); if (normalizer == null) { throw new MapperParsingException("normalizer [" + normalizerName + "] not found for field [" + name + "]"); } - fieldType().setNormalizer(normalizer); - final NamedAnalyzer searchAnalyzer; - if (fieldType().splitQueriesOnWhitespace) { + if (splitQueriesOnWhitespace) { searchAnalyzer = indexAnalyzers.getWhitespaceNormalizer(normalizerName); } else { searchAnalyzer = normalizer; } - fieldType().setSearchAnalyzer(searchAnalyzer); - } else if (fieldType().splitQueriesOnWhitespace) { - fieldType().setSearchAnalyzer(new NamedAnalyzer("whitespace", AnalyzerScope.INDEX, new WhitespaceAnalyzer())); } - return new KeywordFieldMapper( - name, fieldType, defaultFieldType, ignoreAbove, + else if (splitQueriesOnWhitespace) { + // TODO should this be a Lucene global analyzer as well? + searchAnalyzer = new NamedAnalyzer("whitespace", AnalyzerScope.INDEX, new WhitespaceAnalyzer()); + } + return new KeywordFieldType(buildFullName(context), indexed, hasDocValues, fieldType.omitNorms() == false, + eagerGlobalOrdinals, normalizer, searchAnalyzer, similarity, meta, boost); + } + + @Override + public KeywordFieldMapper build(BuilderContext context) { + return new KeywordFieldMapper(name, + fieldType, buildFieldType(context), ignoreAbove, splitQueriesOnWhitespace, nullValue, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); } } @@ -188,18 +218,34 @@ public Mapper.Builder parse(String name, Map node, ParserCont public static final class KeywordFieldType extends StringFieldType { - private NamedAnalyzer normalizer = null; - private boolean splitQueriesOnWhitespace; + boolean hasNorms; - public KeywordFieldType() { - setIndexAnalyzer(Lucene.KEYWORD_ANALYZER); - setSearchAnalyzer(Lucene.KEYWORD_ANALYZER); + public KeywordFieldType(String name, boolean isSearchable, boolean hasDocValues, boolean hasNorms, + boolean eagerGlobalOrdinals, NamedAnalyzer normalizer, NamedAnalyzer searchAnalyzer, + SimilarityProvider similarity, Map meta, float boost) { + super(name, isSearchable, hasDocValues, meta); + this.hasNorms = hasNorms; + setEagerGlobalOrdinals(eagerGlobalOrdinals); + setIndexAnalyzer(normalizer); + setSearchAnalyzer(searchAnalyzer); + setSimilarity(similarity); + setBoost(boost); + } + + public KeywordFieldType(String name, boolean isSearchable, boolean hasDocValues, Map meta) { + this(name, isSearchable, hasDocValues, true, false, Lucene.KEYWORD_ANALYZER, Lucene.KEYWORD_ANALYZER, null, meta, 1.0f); + } + + public KeywordFieldType(String name) { + this(name, true, true, Collections.emptyMap()); } protected KeywordFieldType(KeywordFieldType ref) { super(ref); - this.normalizer = ref.normalizer; - this.splitQueriesOnWhitespace = ref.splitQueriesOnWhitespace; + this.hasNorms = ref.hasNorms; + setEagerGlobalOrdinals(ref.eagerGlobalOrdinals()); + setIndexAnalyzer(ref.indexAnalyzer()); + setSearchAnalyzer(ref.searchAnalyzer()); } @Override @@ -213,13 +259,12 @@ public boolean equals(Object o) { return false; } KeywordFieldType other = (KeywordFieldType) o; - return Objects.equals(normalizer, other.normalizer) && - splitQueriesOnWhitespace == other.splitQueriesOnWhitespace; + return Objects.equals(searchAnalyzer(), other.searchAnalyzer()); } @Override public int hashCode() { - return 31 * super.hashCode() + Objects.hash(normalizer, splitQueriesOnWhitespace); + return 31 * super.hashCode() + Objects.hash(searchAnalyzer()); } @Override @@ -228,29 +273,14 @@ public String typeName() { } NamedAnalyzer normalizer() { - return normalizer; - } - - public void setNormalizer(NamedAnalyzer normalizer) { - checkIfFrozen(); - this.normalizer = normalizer; - setIndexAnalyzer(normalizer); - } - - public boolean splitQueriesOnWhitespace() { - return splitQueriesOnWhitespace; - } - - public void setSplitQueriesOnWhitespace(boolean splitQueriesOnWhitespace) { - checkIfFrozen(); - this.splitQueriesOnWhitespace = splitQueriesOnWhitespace; + return indexAnalyzer(); } @Override public Query existsQuery(QueryShardContext context) { if (hasDocValues()) { return new DocValuesFieldExistsQuery(name()); - } else if (omitNorms()) { + } else if (hasNorms == false) { return new TermQuery(new Term(FieldNamesFieldMapper.NAME, name())); } else { return new NormsFieldExistsQuery(name()); @@ -295,13 +325,17 @@ protected BytesRef indexedValueForSearch(Object value) { } private int ignoreAbove; + private boolean splitQueriesOnWhitespace; + private String nullValue; - protected KeywordFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, - int ignoreAbove, Settings indexSettings, - MultiFields multiFields, CopyTo copyTo) { - super(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, copyTo); + protected KeywordFieldMapper(String simpleName, FieldType fieldType, MappedFieldType mappedFieldType, + int ignoreAbove, boolean splitQueriesOnWhitespace, String nullValue, + Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { + super(simpleName, fieldType, mappedFieldType, indexSettings, multiFields, copyTo); assert fieldType.indexOptions().compareTo(IndexOptions.DOCS_AND_FREQS) <= 0; this.ignoreAbove = ignoreAbove; + this.splitQueriesOnWhitespace = splitQueriesOnWhitespace; + this.nullValue = nullValue; } /** Values that have more chars than the return value of this method will @@ -328,7 +362,7 @@ protected void parseCreateField(ParseContext context) throws IOException { } else { XContentParser parser = context.parser(); if (parser.currentToken() == XContentParser.Token.VALUE_NULL) { - value = fieldType().nullValueAsString(); + value = nullValue; } else { value = parser.textOrNull(); } @@ -361,11 +395,11 @@ protected void parseCreateField(ParseContext context) throws IOException { // convert to utf8 only once before feeding postings/dv/stored fields final BytesRef binaryValue = new BytesRef(value); - if (fieldType().indexOptions() != IndexOptions.NONE || fieldType().stored()) { - Field field = new Field(fieldType().name(), binaryValue, fieldType()); + if (fieldType.indexOptions() != IndexOptions.NONE || fieldType.stored()) { + Field field = new KeywordField(fieldType().name(), binaryValue, fieldType); context.doc().add(field); - if (fieldType().hasDocValues() == false && fieldType().omitNorms()) { + if (fieldType().hasDocValues() == false && fieldType.omitNorms()) { createFieldNamesField(context); } } @@ -382,32 +416,44 @@ protected String contentType() { @Override protected void mergeOptions(FieldMapper other, List conflicts) { KeywordFieldMapper k = (KeywordFieldMapper) other; - if (Objects.equals(fieldType().normalizer, k.fieldType().normalizer) == false) { + if (Objects.equals(fieldType().indexAnalyzer(), k.fieldType().indexAnalyzer()) == false) { conflicts.add("mapper [" + name() + "] has different [normalizer]"); } this.ignoreAbove = k.ignoreAbove; + this.splitQueriesOnWhitespace = k.splitQueriesOnWhitespace; + this.fieldType().setSearchAnalyzer(k.fieldType().searchAnalyzer()); + this.fieldType().setBoost(k.fieldType().boost()); } @Override protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException { super.doXContentBody(builder, includeDefaults, params); - if (includeDefaults || fieldType().nullValue() != null) { - builder.field("null_value", fieldType().nullValue()); + if (includeDefaults || (mappedFieldType.isSearchable() && fieldType.indexOptions() != IndexOptions.DOCS)) { + builder.field("index_options", indexOptionToString(fieldType.indexOptions())); + } + if (nullValue != null) { + builder.field("null_value", nullValue); } - if (includeDefaults || ignoreAbove != Defaults.IGNORE_ABOVE) { builder.field("ignore_above", ignoreAbove); } + if (includeDefaults || fieldType.omitNorms() != Defaults.FIELD_TYPE.omitNorms()) { + builder.field("norms", fieldType.omitNorms() == false); + } + if (includeDefaults || fieldType().eagerGlobalOrdinals() != false) { + builder.field("eager_global_ordinals", fieldType().eagerGlobalOrdinals()); + } - if (fieldType().normalizer() != null) { + if (fieldType().normalizer() != null && fieldType().normalizer() != Lucene.KEYWORD_ANALYZER) { builder.field("normalizer", fieldType().normalizer().name()); - } else if (includeDefaults) { - builder.nullField("normalizer"); + } + else if (includeDefaults) { + builder.field("normalizer", "default"); } - if (includeDefaults || fieldType().splitQueriesOnWhitespace) { - builder.field("split_queries_on_whitespace", fieldType().splitQueriesOnWhitespace); + if (includeDefaults || splitQueriesOnWhitespace) { + builder.field("split_queries_on_whitespace", splitQueriesOnWhitespace); } } } diff --git a/server/src/main/java/org/elasticsearch/index/mapper/LegacyGeoShapeFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/LegacyGeoShapeFieldMapper.java index eae3cc2a27061..565db3eb79dc3 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/LegacyGeoShapeFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/LegacyGeoShapeFieldMapper.java @@ -20,7 +20,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.apache.lucene.index.IndexOptions; +import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexableField; import org.apache.lucene.spatial.prefix.PrefixTreeStrategy; import org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy; @@ -49,7 +49,9 @@ import org.locationtech.spatial4j.shape.Shape; import java.io.IOException; +import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.Objects; /** @@ -179,7 +181,7 @@ private static void checkPrefixTreeSupport(String fieldName) { private static final Logger logger = LogManager.getLogger(LegacyGeoShapeFieldMapper.class); private static final DeprecationLogger DEPRECATION_LOGGER = new DeprecationLogger(logger); - public static class Builder extends AbstractShapeGeometryFieldMapper.Builder { DeprecatedParameters deprecatedParameters; @@ -189,27 +191,11 @@ public Builder(String name) { } public Builder(String name, DeprecatedParameters deprecatedParameters) { - super(name, new GeoShapeFieldType(), new GeoShapeFieldType()); + super(name, Defaults.FIELD_TYPE); this.deprecatedParameters = deprecatedParameters; } - @Override - protected void setGeometryParser(GeoShapeFieldType fieldType) { - fieldType().setGeometryParser(ShapeParser::parse); - } - - @Override - public void setGeometryIndexer(LegacyGeoShapeFieldMapper.GeoShapeFieldType fieldType) { - fieldType().setGeometryIndexer(new LegacyGeoShapeIndexer(fieldType)); - } - - @Override - protected void setGeometryQueryBuilder(GeoShapeFieldType fieldType) { - fieldType().setGeometryQueryBuilder(new LegacyGeoShapeQueryProcessor(fieldType())); - } - - private void setupFieldTypeDeprecatedParameters(BuilderContext context) { - GeoShapeFieldType ft = fieldType(); + private void setupFieldTypeDeprecatedParameters(GeoShapeFieldType ft) { if (deprecatedParameters.strategy != null) { ft.setStrategy(deprecatedParameters.strategy); } @@ -231,15 +217,12 @@ private void setupFieldTypeDeprecatedParameters(BuilderContext context) { ft.setPointsOnly(deprecatedParameters.pointsOnly); } - GeoShapeFieldType geoShapeFieldType = (GeoShapeFieldType)fieldType; - - if (geoShapeFieldType.treeLevels() == 0 && geoShapeFieldType.precisionInMeters() < 0) { - geoShapeFieldType.setDefaultDistanceErrorPct(DeprecatedParameters.Defaults.DISTANCE_ERROR_PCT); + if (ft.treeLevels() == 0 && ft.precisionInMeters() < 0) { + ft.setDefaultDistanceErrorPct(DeprecatedParameters.Defaults.DISTANCE_ERROR_PCT); } } - private void setupPrefixTrees() { - GeoShapeFieldType ft = fieldType(); + private void setupPrefixTrees(GeoShapeFieldType ft) { SpatialPrefixTree prefixTree; if (ft.tree().equals(DeprecatedParameters.PrefixTrees.GEOHASH)) { prefixTree = new GeohashPrefixTree(ShapeBuilder.SPATIAL_CONTEXT, @@ -271,13 +254,15 @@ private void setupPrefixTrees() { ft.defaultPrefixTreeStrategy.setPointsOnly(ft.pointsOnly()); } - @Override - protected void setupFieldType(BuilderContext context) { - super.setupFieldType(context); - - // setup the deprecated parameters and the prefix tree configuration - setupFieldTypeDeprecatedParameters(context); - setupPrefixTrees(); + private GeoShapeFieldType buildFieldType(BuilderContext context) { + GeoShapeFieldType ft = new GeoShapeFieldType(buildFullName(context), indexed, false, meta); + setupFieldTypeDeprecatedParameters(ft); + setupPrefixTrees(ft); + ft.setGeometryIndexer(new LegacyGeoShapeIndexer(ft)); + ft.setGeometryParser(ShapeParser::parse); + ft.setGeometryQueryBuilder(new LegacyGeoShapeQueryProcessor(ft)); + ft.setOrientation(orientation == null ? Defaults.ORIENTATION.value() : orientation); + return ft; } private static int getLevels(int treeLevels, double precisionInMeters, int defaultLevels, boolean geoHash) { @@ -290,9 +275,7 @@ private static int getLevels(int treeLevels, double precisionInMeters, int defau @Override public LegacyGeoShapeFieldMapper build(BuilderContext context) { - setupFieldType(context); - - return new LegacyGeoShapeFieldMapper(name, fieldType, defaultFieldType, ignoreMalformed(context), + return new LegacyGeoShapeFieldMapper(name, fieldType, buildFieldType(context), ignoreMalformed(context), coerce(context), orientation(), ignoreZValue(), context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); } @@ -313,12 +296,12 @@ public static final class GeoShapeFieldType extends AbstractShapeGeometryFieldTy private RecursivePrefixTreeStrategy recursiveStrategy; private TermQueryPrefixTreeStrategy termStrategy; - public GeoShapeFieldType() { - setIndexOptions(IndexOptions.DOCS); - setTokenized(false); - setStored(false); - setStoreTermVectors(false); - setOmitNorms(true); + public GeoShapeFieldType(String name, boolean indexed, boolean hasDocValues, Map meta) { + super(name, indexed, hasDocValues, meta); + } + + public GeoShapeFieldType(String name) { + this(name, true, true, Collections.emptyMap()); } protected GeoShapeFieldType(GeoShapeFieldType ref) { @@ -330,6 +313,9 @@ protected GeoShapeFieldType(GeoShapeFieldType ref) { this.precisionInMeters = ref.precisionInMeters; this.distanceErrorPct = ref.distanceErrorPct; this.defaultDistanceErrorPct = ref.defaultDistanceErrorPct; + this.defaultPrefixTreeStrategy = ref.defaultPrefixTreeStrategy; + this.recursiveStrategy = ref.recursiveStrategy; + this.termStrategy = ref.termStrategy; } @Override @@ -366,7 +352,6 @@ public String tree() { } public void setTree(String tree) { - checkIfFrozen(); this.tree = tree; } @@ -375,7 +360,6 @@ public SpatialStrategy strategy() { } public void setStrategy(SpatialStrategy strategy) { - checkIfFrozen(); this.strategy = strategy; if (this.strategy.equals(SpatialStrategy.TERM)) { this.pointsOnly = true; @@ -387,7 +371,6 @@ public boolean pointsOnly() { } public void setPointsOnly(boolean pointsOnly) { - checkIfFrozen(); this.pointsOnly = pointsOnly; } public int treeLevels() { @@ -395,7 +378,6 @@ public int treeLevels() { } public void setTreeLevels(int treeLevels) { - checkIfFrozen(); this.treeLevels = treeLevels; } @@ -404,7 +386,6 @@ public double precisionInMeters() { } public void setPrecisionInMeters(double precisionInMeters) { - checkIfFrozen(); this.precisionInMeters = precisionInMeters; } @@ -413,12 +394,10 @@ public double distanceErrorPct() { } public void setDistanceErrorPct(double distanceErrorPct) { - checkIfFrozen(); this.distanceErrorPct = distanceErrorPct; } public void setDefaultDistanceErrorPct(double defaultDistanceErrorPct) { - checkIfFrozen(); this.defaultDistanceErrorPct = defaultDistanceErrorPct; } @@ -441,11 +420,11 @@ public PrefixTreeStrategy resolvePrefixTreeStrategy(String strategyName) { } } - public LegacyGeoShapeFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, - Explicit ignoreMalformed, Explicit coerce, Explicit orientation, - Explicit ignoreZValue, Settings indexSettings, - MultiFields multiFields, CopyTo copyTo) { - super(simpleName, fieldType, defaultFieldType, ignoreMalformed, coerce, ignoreZValue, orientation, indexSettings, + public LegacyGeoShapeFieldMapper(String simpleName, FieldType fieldType, MappedFieldType mappedFieldType, + Explicit ignoreMalformed, Explicit coerce, Explicit orientation, + Explicit ignoreZValue, Settings indexSettings, + MultiFields multiFields, CopyTo copyTo) { + super(simpleName, fieldType, mappedFieldType, ignoreMalformed, coerce, ignoreZValue, orientation, indexSettings, multiFields, copyTo); } @@ -469,6 +448,11 @@ protected void addMultiFields(ParseContext context, Shape geometry) { // noop (completion suggester currently not compatible with geo_shape) } + @Override + protected boolean docValuesByDefault() { + return false; + } + @Override public void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException { super.doXContentBody(builder, includeDefaults, params); @@ -554,6 +538,8 @@ protected void mergeGeoOptions(AbstractShapeGeometryFieldMapper mergeWith, if (fieldType().precisionInMeters() != g.precisionInMeters()) { conflicts.add("mapper [" + name() + "] has different [precision]"); } + + this.orientation = mergeWith.orientation; } @Override diff --git a/server/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java b/server/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java index c0f0b8897143e..fa7186262c095 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java @@ -20,8 +20,6 @@ package org.elasticsearch.index.mapper; import org.apache.lucene.analysis.TokenStream; -import org.apache.lucene.document.FieldType; -import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.PrefixCodedTerms; import org.apache.lucene.index.PrefixCodedTerms.TermIterator; @@ -53,7 +51,6 @@ import java.io.IOException; import java.time.ZoneId; -import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Objects; @@ -61,44 +58,40 @@ /** * This defines the core properties and functions to operate on a field. */ -public abstract class MappedFieldType extends FieldType { +public abstract class MappedFieldType { - private String name; + private final String name; + private final boolean docValues; + private final boolean isIndexed; private float boost; - // TODO: remove this docvalues flag and use docValuesType - private boolean docValues; private NamedAnalyzer indexAnalyzer; private NamedAnalyzer searchAnalyzer; private NamedAnalyzer searchQuoteAnalyzer; + protected boolean hasPositions; private SimilarityProvider similarity; - private Object nullValue; - private String nullValueAsString; // for sending null value to _all field private boolean eagerGlobalOrdinals; private Map meta; protected MappedFieldType(MappedFieldType ref) { - super(ref); this.name = ref.name(); this.boost = ref.boost(); + this.isIndexed = ref.isIndexed; this.docValues = ref.hasDocValues(); this.indexAnalyzer = ref.indexAnalyzer(); this.searchAnalyzer = ref.searchAnalyzer(); this.searchQuoteAnalyzer = ref.searchQuoteAnalyzer; this.similarity = ref.similarity(); - this.nullValue = ref.nullValue(); - this.nullValueAsString = ref.nullValueAsString(); this.eagerGlobalOrdinals = ref.eagerGlobalOrdinals; this.meta = ref.meta; + this.hasPositions = ref.hasPositions; } - public MappedFieldType() { - setTokenized(true); - setStored(false); - setStoreTermVectors(false); - setOmitNorms(false); - setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS); + public MappedFieldType(String name, boolean isIndexed, boolean hasDocValues, Map meta) { setBoost(1.0f); - meta = Collections.emptyMap(); + this.name = Objects.requireNonNull(name); + this.isIndexed = isIndexed; + this.docValues = hasDocValues; + this.meta = meta; } @Override @@ -119,7 +112,9 @@ public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName) { @Override public boolean equals(Object o) { - if (!super.equals(o)) return false; + if (o == null || getClass() != o.getClass()) { + return false; + } MappedFieldType fieldType = (MappedFieldType) o; return boost == fieldType.boost && @@ -129,17 +124,14 @@ public boolean equals(Object o) { Objects.equals(searchAnalyzer, fieldType.searchAnalyzer) && Objects.equals(searchQuoteAnalyzer(), fieldType.searchQuoteAnalyzer()) && Objects.equals(eagerGlobalOrdinals, fieldType.eagerGlobalOrdinals) && - Objects.equals(nullValue, fieldType.nullValue) && - Objects.equals(nullValueAsString, fieldType.nullValueAsString) && Objects.equals(similarity, fieldType.similarity) && Objects.equals(meta, fieldType.meta); } @Override public int hashCode() { - int hash = Objects.hash(super.hashCode(), name, boost, docValues, indexAnalyzer, searchAnalyzer, searchQuoteAnalyzer, - eagerGlobalOrdinals, similarity == null ? null : similarity.name(), nullValue, nullValueAsString, meta); - return hash; + return Objects.hash(name, boost, docValues, indexAnalyzer, searchAnalyzer, searchQuoteAnalyzer, + eagerGlobalOrdinals, similarity == null ? null : similarity.name(), meta); } // TODO: we need to override freeze() and add safety checks that all settings are actually set @@ -151,27 +143,20 @@ public String name() { return name; } - public void setName(String name) { - checkIfFrozen(); - this.name = name; - } - public float boost() { return boost; } public void setBoost(float boost) { - checkIfFrozen(); this.boost = boost; } - public boolean hasDocValues() { - return docValues; + public boolean hasPositions() { + return hasPositions; } - public void setHasDocValues(boolean hasDocValues) { - checkIfFrozen(); - this.docValues = hasDocValues; + public boolean hasDocValues() { + return docValues; } public NamedAnalyzer indexAnalyzer() { @@ -179,7 +164,6 @@ public NamedAnalyzer indexAnalyzer() { } public void setIndexAnalyzer(NamedAnalyzer analyzer) { - checkIfFrozen(); this.indexAnalyzer = analyzer; } @@ -188,7 +172,6 @@ public NamedAnalyzer searchAnalyzer() { } public void setSearchAnalyzer(NamedAnalyzer analyzer) { - checkIfFrozen(); this.searchAnalyzer = analyzer; } @@ -197,7 +180,6 @@ public NamedAnalyzer searchQuoteAnalyzer() { } public void setSearchQuoteAnalyzer(NamedAnalyzer analyzer) { - checkIfFrozen(); this.searchQuoteAnalyzer = analyzer; } @@ -206,27 +188,9 @@ public SimilarityProvider similarity() { } public void setSimilarity(SimilarityProvider similarity) { - checkIfFrozen(); this.similarity = similarity; } - /** Returns the value that should be added when JSON null is found, or null if no value should be added */ - public Object nullValue() { - return nullValue; - } - - /** Returns the null value stringified or null if there is no null value */ - public String nullValueAsString() { - return nullValueAsString; - } - - /** Sets the null value and initializes the string version */ - public void setNullValue(Object nullValue) { - checkIfFrozen(); - this.nullValue = nullValue; - this.nullValueAsString = nullValue == null ? null : nullValue.toString(); - } - /** Given a value that comes from the stored fields API, convert it to the * expected type. For instance a date field would store dates as longs and * format it back to a string in this method. */ @@ -234,11 +198,11 @@ public Object valueForDisplay(Object value) { return value; } - /** Returns true if the field is searchable. - * + /** + * Returns true if the field is searchable. */ public boolean isSearchable() { - return indexOptions() != IndexOptions.NONE; + return isIndexed; } /** Returns true if the field is aggregatable. @@ -378,7 +342,7 @@ protected final void failIfNoDocValues() { } protected final void failIfNotIndexed() { - if (indexOptions() == IndexOptions.NONE && pointDimensionCount() == 0) { + if (isIndexed == false) { // we throw an IAE rather than an ISE so that it translates to a 4xx code rather than 5xx code on the http layer throw new IllegalArgumentException("Cannot search on field [" + name() + "] since it is not indexed."); } @@ -389,7 +353,6 @@ public boolean eagerGlobalOrdinals() { } public void setEagerGlobalOrdinals(boolean eagerGlobalOrdinals) { - checkIfFrozen(); this.eagerGlobalOrdinals = eagerGlobalOrdinals; } @@ -445,8 +408,7 @@ public Map meta() { /** * Associate metadata with this field. */ - public void setMeta(Map meta) { - checkIfFrozen(); + public void updateMeta(Map meta) { this.meta = Map.copyOf(Objects.requireNonNull(meta)); } } diff --git a/server/src/main/java/org/elasticsearch/index/mapper/MapperService.java b/server/src/main/java/org/elasticsearch/index/mapper/MapperService.java index 68ba92e9fa140..aa5aa54a7da35 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/MapperService.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/MapperService.java @@ -22,6 +22,7 @@ import org.apache.logging.log4j.message.ParameterizedMessage; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.DelegatingAnalyzerWrapper; +import org.apache.lucene.document.FieldType; import org.elasticsearch.Assertions; import org.elasticsearch.Version; import org.elasticsearch.cluster.metadata.IndexMetadata; @@ -160,6 +161,17 @@ public DocumentMapperParser documentMapperParser() { return this.documentParser; } + public FieldType getLuceneFieldType(String field) { + Mapper mapper = documentMapper().mappers().getMapper(field); + if (mapper == null) { + return null; + } + if (mapper instanceof FieldMapper == false) { + return null; + } + return ((FieldMapper) mapper).fieldType; + } + /** * Parses the mappings (formatted as JSON) into a map */ diff --git a/server/src/main/java/org/elasticsearch/index/mapper/MetadataFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/MetadataFieldMapper.java index 4aa98fe4a1123..1a5a361534382 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/MetadataFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/MetadataFieldMapper.java @@ -19,6 +19,7 @@ package org.elasticsearch.index.mapper; +import org.apache.lucene.document.FieldType; import org.elasticsearch.common.settings.Settings; import java.io.IOException; @@ -46,16 +47,24 @@ MetadataFieldMapper.Builder parse(String name, Map node, } @SuppressWarnings("rawtypes") - public abstract static class Builder extends FieldMapper.Builder { - public Builder(String name, MappedFieldType fieldType, MappedFieldType defaultFieldType) { - super(name, fieldType, defaultFieldType); + public abstract static class Builder> extends FieldMapper.Builder { + public Builder(String name, FieldType fieldType) { + super(name, fieldType); + } + + @Override + public T index(boolean index) { + if (index == false) { + throw new IllegalArgumentException("Metadata fields must be indexed"); + } + return builder; } public abstract MetadataFieldMapper build(BuilderContext context); } - protected MetadataFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, Settings indexSettings) { - super(simpleName, fieldType, defaultFieldType, indexSettings, MultiFields.empty(), CopyTo.empty()); + protected MetadataFieldMapper(FieldType fieldType, MappedFieldType mappedFieldType, Settings indexSettings) { + super(mappedFieldType.name(), fieldType, mappedFieldType, indexSettings, MultiFields.empty(), CopyTo.empty()); } /** @@ -72,4 +81,5 @@ public void postParse(ParseContext context) throws IOException { @Override protected void mergeOptions(FieldMapper other, List conflicts) { } + } diff --git a/server/src/main/java/org/elasticsearch/index/mapper/NestedPathFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/NestedPathFieldMapper.java index ed42b3319534f..8af5fb68446cd 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/NestedPathFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/NestedPathFieldMapper.java @@ -20,19 +20,20 @@ package org.elasticsearch.index.mapper; import org.apache.lucene.document.Field; +import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.Term; import org.apache.lucene.search.Query; import org.apache.lucene.search.TermQuery; import org.apache.lucene.util.BytesRef; import org.elasticsearch.Version; -import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.query.QueryShardContext; import java.io.IOException; +import java.util.Collections; import java.util.Map; public class NestedPathFieldMapper extends MetadataFieldMapper { @@ -57,15 +58,13 @@ public static Field field(Settings settings, String path) { public static class Defaults { - public static final MappedFieldType FIELD_TYPE = new NestedPathFieldType(); + public static final FieldType FIELD_TYPE = new FieldType(); static { FIELD_TYPE.setIndexOptions(IndexOptions.DOCS); FIELD_TYPE.setTokenized(false); FIELD_TYPE.setStored(false); FIELD_TYPE.setOmitNorms(true); - FIELD_TYPE.setIndexAnalyzer(Lucene.KEYWORD_ANALYZER); - FIELD_TYPE.setSearchAnalyzer(Lucene.KEYWORD_ANALYZER); FIELD_TYPE.freeze(); } } @@ -80,13 +79,14 @@ public MetadataFieldMapper.Builder parse(String name, Map nod @Override public MetadataFieldMapper getDefault(ParserContext context) { final IndexSettings indexSettings = context.mapperService().getIndexSettings(); - return new NestedPathFieldMapper(indexSettings, defaultFieldType(indexSettings)); + return new NestedPathFieldMapper(indexSettings.getSettings()); } } public static final class NestedPathFieldType extends StringFieldType { - NestedPathFieldType() { + NestedPathFieldType(Settings settings) { + super(NestedPathFieldMapper.name(settings), true, false, Collections.emptyMap()); } protected NestedPathFieldType(NestedPathFieldType ref) { @@ -114,21 +114,8 @@ public Query existsQuery(QueryShardContext context) { } } - private NestedPathFieldMapper(IndexSettings indexSettings, MappedFieldType existing) { - this(existing == null ? defaultFieldType(indexSettings) : existing.clone(), - indexSettings); - } - - private NestedPathFieldMapper(MappedFieldType fieldType, IndexSettings indexSettings) { - super(name(indexSettings.getSettings()), fieldType, defaultFieldType(indexSettings), indexSettings.getSettings()); - } - - private static MappedFieldType defaultFieldType(IndexSettings indexSettings) { - MappedFieldType defaultFieldType = Defaults.FIELD_TYPE.clone(); - defaultFieldType.setIndexOptions(IndexOptions.NONE); - defaultFieldType.setHasDocValues(false); - defaultFieldType.setName(name(indexSettings.getSettings())); - return defaultFieldType; + private NestedPathFieldMapper(Settings settings) { + super(Defaults.FIELD_TYPE, new NestedPathFieldType(settings), settings); } @Override diff --git a/server/src/main/java/org/elasticsearch/index/mapper/NumberFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/NumberFieldMapper.java index 6899c8308bbb2..51202fa65ec00 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/NumberFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/NumberFieldMapper.java @@ -20,10 +20,10 @@ package org.elasticsearch.index.mapper; import com.fasterxml.jackson.core.JsonParseException; - import com.fasterxml.jackson.core.exc.InputCoercionException; import org.apache.lucene.document.DoublePoint; import org.apache.lucene.document.Field; +import org.apache.lucene.document.FieldType; import org.apache.lucene.document.FloatPoint; import org.apache.lucene.document.HalfFloatPoint; import org.apache.lucene.document.IntPoint; @@ -43,6 +43,7 @@ import org.apache.lucene.util.NumericUtils; import org.elasticsearch.common.Explicit; import org.elasticsearch.common.Numbers; +import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Setting.Property; @@ -61,6 +62,7 @@ import java.time.ZoneId; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -75,15 +77,23 @@ public class NumberFieldMapper extends FieldMapper { public static class Defaults { public static final Explicit IGNORE_MALFORMED = new Explicit<>(false, false); public static final Explicit COERCE = new Explicit<>(true, false); + public static final FieldType FIELD_TYPE = new FieldType(); + static { + FIELD_TYPE.setStored(false); + FIELD_TYPE.freeze(); + } } public static class Builder extends FieldMapper.Builder { private Boolean ignoreMalformed; private Boolean coerce; + private Number nullValue; + private final NumberType type; public Builder(String name, NumberType type) { - super(name, new NumberFieldType(type), new NumberFieldType(type)); + super(name, Defaults.FIELD_TYPE); + this.type = type; builder = this; } @@ -92,10 +102,15 @@ public Builder ignoreMalformed(boolean ignoreMalformed) { return builder; } + public Builder nullValue(Number nullValue) { + this.nullValue = nullValue; + return builder; + } + @Override public Builder indexOptions(IndexOptions indexOptions) { throw new MapperParsingException( - "index_options not allowed in field [" + name + "] of type [" + builder.fieldType().typeName() + "]"); + "index_options not allowed in field [" + name + "] of type [" + type.typeName() + "]"); } protected Explicit ignoreMalformed(BuilderContext context) { @@ -123,16 +138,11 @@ protected Explicit coerce(BuilderContext context) { return Defaults.COERCE; } - @Override - protected void setupFieldType(BuilderContext context) { - super.setupFieldType(context); - } - @Override public NumberFieldMapper build(BuilderContext context) { - setupFieldType(context); - return new NumberFieldMapper(name, fieldType, defaultFieldType, ignoreMalformed(context), - coerce(context), context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); + return new NumberFieldMapper(name, fieldType, new NumberFieldType(buildFullName(context), type, indexed, hasDocValues, meta), + ignoreMalformed(context), coerce(context), nullValue, context.indexSettings(), + multiFieldsBuilder.build(this, context), copyTo); } } @@ -893,12 +903,15 @@ public static final class NumberFieldType extends SimpleMappedFieldType { private final NumberType type; - public NumberFieldType(NumberType type) { - super(); + public NumberFieldType(String name, NumberType type, boolean isSearchable, boolean hasDocValues, Map meta) { + super(name, isSearchable, hasDocValues, meta); this.type = Objects.requireNonNull(type); - setTokenized(false); - setHasDocValues(true); - setOmitNorms(true); + this.setIndexAnalyzer(Lucene.KEYWORD_ANALYZER); // allows number fields in significant text aggs - do we need this? + this.setSearchAnalyzer(Lucene.KEYWORD_ANALYZER); // allows match queries on number fields + } + + public NumberFieldType(String name, NumberType type) { + this(name, type, true, true, Collections.emptyMap()); } private NumberFieldType(NumberFieldType other) { @@ -1006,21 +1019,23 @@ public int hashCode() { } private Explicit ignoreMalformed; - private Explicit coerce; + private final Number nullValue; private NumberFieldMapper( String simpleName, - MappedFieldType fieldType, - MappedFieldType defaultFieldType, + FieldType fieldType, + MappedFieldType mappedFieldType, Explicit ignoreMalformed, Explicit coerce, + Number nullValue, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { - super(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, copyTo); + super(simpleName, fieldType, mappedFieldType, indexSettings, multiFields, copyTo); this.ignoreMalformed = ignoreMalformed; this.coerce = coerce; + this.nullValue = nullValue; } @Override @@ -1030,7 +1045,7 @@ public NumberFieldType fieldType() { @Override protected String contentType() { - return fieldType.typeName(); + return fieldType().type.typeName(); } @Override @@ -1056,7 +1071,7 @@ protected void parseCreateField(ParseContext context) throws IOException { numericValue = fieldType().type.parse(parser, coerce.value()); } catch (InputCoercionException | IllegalArgumentException | JsonParseException e) { if (ignoreMalformed.value() && parser.currentToken().isValue()) { - context.addIgnoredField(fieldType.name()); + context.addIgnoredField(mappedFieldType.name()); return; } else { throw e; @@ -1066,7 +1081,7 @@ protected void parseCreateField(ParseContext context) throws IOException { } if (value == null) { - value = fieldType().nullValue(); + value = nullValue; } if (value == null) { @@ -1077,12 +1092,12 @@ protected void parseCreateField(ParseContext context) throws IOException { numericValue = fieldType().type.parse(value, coerce.value()); } - boolean indexed = fieldType().indexOptions() != IndexOptions.NONE; boolean docValued = fieldType().hasDocValues(); - boolean stored = fieldType().stored(); - context.doc().addAll(fieldType().type.createFields(fieldType().name(), numericValue, indexed, docValued, stored)); + boolean stored = fieldType.stored(); + context.doc().addAll(fieldType().type.createFields(fieldType().name(), numericValue, + fieldType().isSearchable(), docValued, stored)); - if (docValued == false && (stored || indexed)) { + if (docValued == false && (stored || fieldType().isSearchable())) { createFieldNamesField(context); } } @@ -1114,8 +1129,8 @@ protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, builder.field("coerce", coerce.value()); } - if (includeDefaults || fieldType().nullValue() != null) { - builder.field("null_value", fieldType().nullValue()); + if (nullValue != null) { + builder.field("null_value", nullValue); } } } diff --git a/server/src/main/java/org/elasticsearch/index/mapper/RangeFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/RangeFieldMapper.java index 8404603f72d96..e371d675143a7 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/RangeFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/RangeFieldMapper.java @@ -19,7 +19,7 @@ package org.elasticsearch.index.mapper; -import org.apache.lucene.index.IndexOptions; +import org.apache.lucene.document.FieldType; import org.apache.lucene.index.Term; import org.apache.lucene.search.BoostQuery; import org.apache.lucene.search.DocValuesFieldExistsQuery; @@ -28,9 +28,9 @@ import org.apache.lucene.util.BytesRef; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.Explicit; -import org.elasticsearch.common.Strings; import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.geo.ShapeRelation; +import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.network.InetAddresses; import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Settings; @@ -51,6 +51,7 @@ import java.net.UnknownHostException; import java.time.ZoneId; import java.time.ZoneOffset; +import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.List; @@ -71,6 +72,12 @@ public class RangeFieldMapper extends FieldMapper { public static class Defaults { public static final Explicit COERCE = new Explicit<>(true, false); + public static final FieldType FIELD_TYPE = new FieldType(); + static { + FIELD_TYPE.setStored(false); + FIELD_TYPE.freeze(); + } + public static final DateFormatter DATE_FORMATTER = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER; } // this is private since it has a different default @@ -81,17 +88,14 @@ public static class Builder extends FieldMapper.Builder { private Boolean coerce; private Locale locale = Locale.ROOT; private String pattern; + private final RangeType type; public Builder(String name, RangeType type) { - super(name, new RangeFieldType(type), new RangeFieldType(type)); + super(name, Defaults.FIELD_TYPE); + this.type = type; builder = this; } - @Override - public RangeFieldType fieldType() { - return (RangeFieldType)fieldType; - } - public Builder coerce(boolean coerce) { this.coerce = coerce; return builder; @@ -112,36 +116,29 @@ public Builder format(String format) { return this; } - @Override - public Builder nullValue(Object nullValue) { - throw new IllegalArgumentException("Field [" + name() + "] does not support null value."); - } - public void locale(Locale locale) { this.locale = locale; } - @Override - protected void setupFieldType(BuilderContext context) { - super.setupFieldType(context); - DateFormatter formatter = fieldType().dateTimeFormatter; - if (fieldType().rangeType == RangeType.DATE) { - boolean hasPatternChanged = Strings.hasLength(builder.pattern) && - Objects.equals(builder.pattern, formatter.pattern()) == false; - - if (hasPatternChanged || Objects.equals(builder.locale, formatter.locale()) == false) { - fieldType().setDateTimeFormatter(DateFormatter.forPattern(pattern).withLocale(locale)); + protected RangeFieldType setupFieldType(BuilderContext context) { + if (pattern != null) { + if (type != RangeType.DATE) { + throw new IllegalArgumentException("field [" + name() + "] of type [range]" + + " should not define a dateTimeFormatter unless it is a " + RangeType.DATE + " type"); } - } else if (pattern != null) { - throw new IllegalArgumentException("field [" + name() + "] of type [" + fieldType().rangeType - + "] should not define a dateTimeFormatter unless it is a " + RangeType.DATE + " type"); + return new RangeFieldType(buildFullName(context), indexed, hasDocValues, + DateFormatter.forPattern(pattern).withLocale(locale), meta); } + if (type == RangeType.DATE) { + return new RangeFieldType(buildFullName(context), indexed, hasDocValues, Defaults.DATE_FORMATTER, meta); + } + return new RangeFieldType(buildFullName(context), type, indexed, hasDocValues, meta); } @Override public RangeFieldMapper build(BuilderContext context) { setupFieldType(context); - return new RangeFieldMapper(name, fieldType, defaultFieldType, coerce(context), + return new RangeFieldMapper(name, fieldType, setupFieldType(context), coerce(context), context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); } } @@ -183,27 +180,42 @@ public Mapper.Builder parse(String name, Map node, } public static final class RangeFieldType extends MappedFieldType { - protected RangeType rangeType; - protected DateFormatter dateTimeFormatter; - protected DateMathParser dateMathParser; + protected final RangeType rangeType; + protected final DateFormatter dateTimeFormatter; + protected final DateMathParser dateMathParser; - RangeFieldType(RangeType type) { - super(); + public RangeFieldType(String name, RangeType type, boolean indexed, boolean hasDocValues, Map meta) { + super(name, indexed, hasDocValues, meta); + assert type != RangeType.DATE; this.rangeType = Objects.requireNonNull(type); - setTokenized(false); - setHasDocValues(true); - setOmitNorms(true); - if (rangeType == RangeType.DATE) { - setDateTimeFormatter(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER); - } + dateTimeFormatter = null; + dateMathParser = null; + setIndexAnalyzer(Lucene.KEYWORD_ANALYZER); + setSearchAnalyzer(Lucene.KEYWORD_ANALYZER); + } + + public RangeFieldType(String name, RangeType type) { + this(name, type, true, true, Collections.emptyMap()); + } + + public RangeFieldType(String name, boolean indexed, boolean hasDocValues, DateFormatter formatter, Map meta) { + super(name, indexed, hasDocValues, meta); + this.rangeType = RangeType.DATE; + this.dateTimeFormatter = Objects.requireNonNull(formatter); + this.dateMathParser = dateTimeFormatter.toDateMathParser(); + setIndexAnalyzer(Lucene.KEYWORD_ANALYZER); + setSearchAnalyzer(Lucene.KEYWORD_ANALYZER); + } + + public RangeFieldType(String name, DateFormatter formatter) { + this(name, true, true, formatter, Collections.emptyMap()); } RangeFieldType(RangeFieldType other) { super(other); this.rangeType = other.rangeType; - if (other.rangeType == RangeType.DATE && other.dateTimeFormatter() != null) { - setDateTimeFormatter(other.dateTimeFormatter()); - } + this.dateTimeFormatter = other.dateTimeFormatter; + this.dateMathParser = other.dateMathParser; } public RangeType rangeType() { return rangeType; } @@ -243,12 +255,6 @@ public DateFormatter dateTimeFormatter() { return dateTimeFormatter; } - public void setDateTimeFormatter(DateFormatter dateTimeFormatter) { - checkIfFrozen(); - this.dateTimeFormatter = dateTimeFormatter; - this.dateMathParser = dateTimeFormatter.toDateMathParser(); - } - protected DateMathParser dateMathParser() { return dateMathParser; } @@ -304,13 +310,13 @@ public Query rangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower private RangeFieldMapper( String simpleName, - MappedFieldType fieldType, - MappedFieldType defaultFieldType, + FieldType fieldType, + MappedFieldType mappedFieldType, Explicit coerce, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { - super(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, copyTo); + super(simpleName, fieldType, mappedFieldType, indexSettings, multiFields, copyTo); this.coerce = coerce; } @@ -321,7 +327,7 @@ public RangeFieldType fieldType() { @Override protected String contentType() { - return fieldType.typeName(); + return fieldType().typeName(); } @Override @@ -386,8 +392,8 @@ protected void parseCreateField(ParseContext context) throws IOException { + name() + "], expected an object but got " + parser.currentName()); } } - boolean indexed = fieldType.indexOptions() != IndexOptions.NONE; - boolean docValued = fieldType.hasDocValues(); + boolean docValued = fieldType().hasDocValues(); + boolean indexed = fieldType().isSearchable(); boolean stored = fieldType.stored(); context.doc().addAll(fieldType().rangeType.createFields(context, name(), range, indexed, docValued, stored)); diff --git a/server/src/main/java/org/elasticsearch/index/mapper/RoutingFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/RoutingFieldMapper.java index 1ed9262265b27..75e8bd3725ee0 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/RoutingFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/RoutingFieldMapper.java @@ -20,6 +20,7 @@ package org.elasticsearch.index.mapper; import org.apache.lucene.document.Field; +import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.Term; import org.apache.lucene.search.Query; @@ -31,6 +32,7 @@ import org.elasticsearch.index.query.QueryShardContext; import java.io.IOException; +import java.util.Collections; import java.util.Iterator; import java.util.Map; @@ -42,16 +44,13 @@ public class RoutingFieldMapper extends MetadataFieldMapper { public static class Defaults { public static final String NAME = "_routing"; - public static final MappedFieldType FIELD_TYPE = new RoutingFieldType(); + public static final FieldType FIELD_TYPE = new FieldType(); static { FIELD_TYPE.setIndexOptions(IndexOptions.DOCS); FIELD_TYPE.setTokenized(false); FIELD_TYPE.setStored(true); FIELD_TYPE.setOmitNorms(true); - FIELD_TYPE.setIndexAnalyzer(Lucene.KEYWORD_ANALYZER); - FIELD_TYPE.setSearchAnalyzer(Lucene.KEYWORD_ANALYZER); - FIELD_TYPE.setName(NAME); FIELD_TYPE.freeze(); } @@ -62,8 +61,8 @@ public static class Builder extends MetadataFieldMapper.Builder { private boolean required = Defaults.REQUIRED; - public Builder(MappedFieldType existing) { - super(Defaults.NAME, existing == null ? Defaults.FIELD_TYPE : existing, Defaults.FIELD_TYPE); + public Builder() { + super(Defaults.NAME, Defaults.FIELD_TYPE); } public Builder required(boolean required) { @@ -81,7 +80,7 @@ public static class TypeParser implements MetadataFieldMapper.TypeParser { @Override public MetadataFieldMapper.Builder parse(String name, Map node, ParserContext parserContext) throws MapperParsingException { - Builder builder = new Builder(parserContext.mapperService().fieldType(NAME)); + Builder builder = new Builder(); for (Iterator> iterator = node.entrySet().iterator(); iterator.hasNext();) { Map.Entry entry = iterator.next(); String fieldName = entry.getKey(); @@ -97,13 +96,18 @@ public MetadataFieldMapper.Builder parse(String name, Map nod @Override public MetadataFieldMapper getDefault(ParserContext context) { final Settings indexSettings = context.mapperService().getIndexSettings().getSettings(); - return new RoutingFieldMapper(indexSettings, Defaults.FIELD_TYPE); + return new RoutingFieldMapper(Defaults.FIELD_TYPE, Defaults.REQUIRED, indexSettings); } } static final class RoutingFieldType extends StringFieldType { - RoutingFieldType() { + static RoutingFieldType INSTANCE = new RoutingFieldType(); + + private RoutingFieldType() { + super(NAME, true, false, Collections.emptyMap()); + setIndexAnalyzer(Lucene.KEYWORD_ANALYZER); + setSearchAnalyzer(Lucene.KEYWORD_ANALYZER); } protected RoutingFieldType(RoutingFieldType ref) { @@ -126,21 +130,13 @@ public Query existsQuery(QueryShardContext context) { } } - private boolean required; - - private RoutingFieldMapper(Settings indexSettings, MappedFieldType existing) { - this(existing.clone(), Defaults.REQUIRED, indexSettings); - } + private final boolean required; - private RoutingFieldMapper(MappedFieldType fieldType, boolean required, Settings indexSettings) { - super(NAME, fieldType, Defaults.FIELD_TYPE, indexSettings); + private RoutingFieldMapper(FieldType fieldType, boolean required, Settings indexSettings) { + super(fieldType, RoutingFieldType.INSTANCE, indexSettings); this.required = required; } - public void markAsRequired() { - this.required = true; - } - public boolean required() { return this.required; } @@ -161,8 +157,8 @@ public void parse(ParseContext context) throws IOException { protected void parseCreateField(ParseContext context) throws IOException { String routing = context.sourceToParse().routing(); if (routing != null) { - if (fieldType().indexOptions() != IndexOptions.NONE || fieldType().stored()) { - context.doc().add(new Field(fieldType().name(), routing, fieldType())); + if (fieldType.indexOptions() != IndexOptions.NONE || fieldType.stored()) { + context.doc().add(new Field(fieldType().name(), routing, fieldType)); createFieldNamesField(context); } } diff --git a/server/src/main/java/org/elasticsearch/index/mapper/SeqNoFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/SeqNoFieldMapper.java index 0d7cb411d56b5..24bfd20401924 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/SeqNoFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/SeqNoFieldMapper.java @@ -20,6 +20,7 @@ package org.elasticsearch.index.mapper; import org.apache.lucene.document.Field; +import org.apache.lucene.document.FieldType; import org.apache.lucene.document.LongPoint; import org.apache.lucene.document.NumericDocValuesField; import org.apache.lucene.index.DocValuesType; @@ -38,6 +39,7 @@ import org.elasticsearch.index.seqno.SequenceNumbers; import java.io.IOException; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Objects; @@ -91,30 +93,17 @@ public static SequenceIDFields emptySeqID() { public static final String PRIMARY_TERM_NAME = "_primary_term"; public static final String TOMBSTONE_NAME = "_tombstone"; - public static class SeqNoDefaults { + public static class Defaults { public static final String NAME = SeqNoFieldMapper.NAME; - public static final MappedFieldType FIELD_TYPE = new SeqNoFieldType(); + public static final MappedFieldType MAPPED_FIELD_TYPE = new SeqNoFieldType(); + public static final FieldType FIELD_TYPE = new FieldType(); static { - FIELD_TYPE.setName(NAME); FIELD_TYPE.setDocValuesType(DocValuesType.SORTED); - FIELD_TYPE.setHasDocValues(true); FIELD_TYPE.freeze(); } } - public static class Builder extends MetadataFieldMapper.Builder { - - public Builder() { - super(SeqNoDefaults.NAME, SeqNoDefaults.FIELD_TYPE, SeqNoDefaults.FIELD_TYPE); - } - - @Override - public SeqNoFieldMapper build(BuilderContext context) { - return new SeqNoFieldMapper(context.indexSettings()); - } - } - public static class TypeParser implements MetadataFieldMapper.TypeParser { @Override public MetadataFieldMapper.Builder parse(String name, Map node, ParserContext parserContext) @@ -132,6 +121,7 @@ public MetadataFieldMapper getDefault(ParserContext context) { static final class SeqNoFieldType extends SimpleMappedFieldType { SeqNoFieldType() { + super(NAME, true, true, Collections.emptyMap()); } protected SeqNoFieldType(SeqNoFieldType ref) { @@ -220,7 +210,7 @@ public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName) { } public SeqNoFieldMapper(Settings indexSettings) { - super(NAME, SeqNoDefaults.FIELD_TYPE, SeqNoDefaults.FIELD_TYPE, indexSettings); + super(Defaults.FIELD_TYPE, Defaults.MAPPED_FIELD_TYPE, indexSettings); } @Override diff --git a/server/src/main/java/org/elasticsearch/index/mapper/SimpleMappedFieldType.java b/server/src/main/java/org/elasticsearch/index/mapper/SimpleMappedFieldType.java index 366eb3b36f0fe..cc011f4473c10 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/SimpleMappedFieldType.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/SimpleMappedFieldType.java @@ -25,14 +25,15 @@ import org.elasticsearch.index.query.QueryShardContext; import java.time.ZoneId; +import java.util.Map; /** * {@link MappedFieldType} base impl for field types that are neither dates nor ranges. */ public abstract class SimpleMappedFieldType extends MappedFieldType { - protected SimpleMappedFieldType() { - super(); + protected SimpleMappedFieldType(String name, boolean isSearchable, boolean hasDocValues, Map meta) { + super(name, isSearchable, hasDocValues, meta); } protected SimpleMappedFieldType(MappedFieldType ref) { diff --git a/server/src/main/java/org/elasticsearch/index/mapper/SourceFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/SourceFieldMapper.java index 019357109be04..b9e39a2f08f74 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/SourceFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/SourceFieldMapper.java @@ -19,6 +19,7 @@ package org.elasticsearch.index.mapper; +import org.apache.lucene.document.FieldType; import org.apache.lucene.document.NumericDocValuesField; import org.apache.lucene.document.StoredField; import org.apache.lucene.index.IndexOptions; @@ -29,7 +30,6 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.io.stream.BytesStreamOutput; -import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.CollectionUtils; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -42,6 +42,7 @@ import java.io.IOException; import java.util.Arrays; +import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -59,15 +60,12 @@ public static class Defaults { public static final String NAME = SourceFieldMapper.NAME; public static final boolean ENABLED = true; - public static final MappedFieldType FIELD_TYPE = new SourceFieldType(); + public static final FieldType FIELD_TYPE = new FieldType(); static { FIELD_TYPE.setIndexOptions(IndexOptions.NONE); // not indexed FIELD_TYPE.setStored(true); FIELD_TYPE.setOmitNorms(true); - FIELD_TYPE.setIndexAnalyzer(Lucene.KEYWORD_ANALYZER); - FIELD_TYPE.setSearchAnalyzer(Lucene.KEYWORD_ANALYZER); - FIELD_TYPE.setName(NAME); FIELD_TYPE.freeze(); } @@ -81,7 +79,7 @@ public static class Builder extends MetadataFieldMapper.Builder { private String[] excludes = null; public Builder() { - super(Defaults.NAME, Defaults.FIELD_TYPE, Defaults.FIELD_TYPE); + super(Defaults.NAME, new FieldType(Defaults.FIELD_TYPE)); } public Builder enabled(boolean enabled) { @@ -148,7 +146,11 @@ public MetadataFieldMapper getDefault(ParserContext context) { static final class SourceFieldType extends MappedFieldType { - SourceFieldType() {} + public static final SourceFieldType INSTANCE = new SourceFieldType(); + + private SourceFieldType() { + super(NAME, false, false, Collections.emptyMap()); + } protected SourceFieldType(SourceFieldType ref) { super(ref); @@ -188,12 +190,12 @@ private SourceFieldMapper(Settings indexSettings) { } private SourceFieldMapper(boolean enabled, String[] includes, String[] excludes, Settings indexSettings) { - super(NAME, Defaults.FIELD_TYPE.clone(), Defaults.FIELD_TYPE, indexSettings); // Only stored. + super(Defaults.FIELD_TYPE, SourceFieldType.INSTANCE, indexSettings); // Only stored. this.enabled = enabled; this.includes = includes; this.excludes = excludes; final boolean filtered = CollectionUtils.isEmpty(includes) == false || CollectionUtils.isEmpty(excludes) == false; - this.filter = enabled && filtered && fieldType().stored() ? XContentMapValues.filter(includes, excludes) : null; + this.filter = enabled && filtered && fieldType.stored() ? XContentMapValues.filter(includes, excludes) : null; this.complete = enabled && includes == null && excludes == null; } @@ -245,7 +247,7 @@ protected void parseCreateField(ParseContext context) throws IOException { @Nullable public BytesReference applyFilters(@Nullable BytesReference originalSource, @Nullable XContentType contentType) throws IOException { - if (enabled && fieldType().stored() && originalSource != null) { + if (enabled && fieldType.stored() && originalSource != null) { // Percolate and tv APIs may not set the source and that is ok, because these APIs will not index any data if (filter != null) { // we don't update the context source if we filter, we want to keep it as is... diff --git a/server/src/main/java/org/elasticsearch/index/mapper/StringFieldType.java b/server/src/main/java/org/elasticsearch/index/mapper/StringFieldType.java index 37c20b236ed56..3b79f54c7b2b2 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/StringFieldType.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/StringFieldType.java @@ -38,6 +38,7 @@ import org.elasticsearch.index.query.support.QueryParsers; import java.util.List; +import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -51,7 +52,9 @@ public abstract class StringFieldType extends TermBasedFieldType { private static final Pattern WILDCARD_PATTERN = Pattern.compile("(\\\\.)|([?*]+)"); - public StringFieldType() {} + public StringFieldType(String name, boolean isSearchable, boolean hasDocValues, Map meta) { + super(name, isSearchable, hasDocValues, meta); + } protected StringFieldType(MappedFieldType ref) { super(ref); @@ -122,8 +125,8 @@ public static final String normalizeWildcardPattern(String fieldname, String val sb.append(normalized); } return sb.toBytesRef().utf8ToString(); - } - + } + @Override public Query wildcardQuery(String value, MultiTermQuery.RewriteMethod method, QueryShardContext context) { failIfNotIndexed(); diff --git a/server/src/main/java/org/elasticsearch/index/mapper/TermBasedFieldType.java b/server/src/main/java/org/elasticsearch/index/mapper/TermBasedFieldType.java index 9638fecb982e4..078a17e365710 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/TermBasedFieldType.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/TermBasedFieldType.java @@ -19,8 +19,6 @@ package org.elasticsearch.index.mapper; -import java.util.List; - import org.apache.lucene.index.Term; import org.apache.lucene.search.BoostQuery; import org.apache.lucene.search.Query; @@ -30,11 +28,16 @@ import org.elasticsearch.common.lucene.BytesRefs; import org.elasticsearch.index.query.QueryShardContext; +import java.util.List; +import java.util.Map; + /** Base {@link MappedFieldType} implementation for a field that is indexed * with the inverted index. */ abstract class TermBasedFieldType extends SimpleMappedFieldType { - TermBasedFieldType() {} + TermBasedFieldType(String name, boolean isSearchable, boolean hasDocValues, Map meta) { + super(name, isSearchable, hasDocValues, meta); + } protected TermBasedFieldType(MappedFieldType ref) { super(ref); diff --git a/server/src/main/java/org/elasticsearch/index/mapper/TextFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/TextFieldMapper.java index 5753a8c0e8d2c..893530651d1af 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/TextFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/TextFieldMapper.java @@ -30,6 +30,7 @@ import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute; import org.apache.lucene.analysis.tokenattributes.TermToBytesRefAttribute; import org.apache.lucene.document.Field; +import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.Term; import org.apache.lucene.queries.intervals.Intervals; @@ -73,6 +74,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -96,9 +98,14 @@ public static class Defaults { public static final int INDEX_PREFIX_MIN_CHARS = 2; public static final int INDEX_PREFIX_MAX_CHARS = 5; - public static final MappedFieldType FIELD_TYPE = new TextFieldType(); + public static final FieldType FIELD_TYPE = new FieldType(); static { + FIELD_TYPE.setTokenized(true); + FIELD_TYPE.setStored(false); + FIELD_TYPE.setStoreTermVectors(false); + FIELD_TYPE.setOmitNorms(false); + FIELD_TYPE.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS); FIELD_TYPE.freeze(); } @@ -114,17 +121,18 @@ public static class Builder extends FieldMapper.Builder { private int positionIncrementGap = POSITION_INCREMENT_GAP_USE_ANALYZER; private int minPrefixChars = -1; private int maxPrefixChars = -1; + private boolean fielddata = false; + private boolean indexPhrases = false; + private boolean eagerGlobalOrdinals = false; + private double fielddataMinFreq = Defaults.FIELDDATA_MIN_FREQUENCY; + private double fielddataMaxFreq = Defaults.FIELDDATA_MAX_FREQUENCY; + private int fielddataMinSegSize = Defaults.FIELDDATA_MIN_SEGMENT_SIZE; public Builder(String name) { - super(name, Defaults.FIELD_TYPE, Defaults.FIELD_TYPE); + super(name, Defaults.FIELD_TYPE); builder = this; } - @Override - public TextFieldType fieldType() { - return (TextFieldType) super.fieldType(); - } - public Builder positionIncrementGap(int positionIncrementGap) { if (positionIncrementGap < 0) { throw new MapperParsingException("[positions_increment_gap] must be positive, got " + positionIncrementGap); @@ -134,12 +142,12 @@ public Builder positionIncrementGap(int positionIncrementGap) { } public Builder fielddata(boolean fielddata) { - fieldType().setFielddata(fielddata); + this.fielddata = fielddata; return builder; } public Builder indexPhrases(boolean indexPhrases) { - fieldType().setIndexPhrases(indexPhrases); + this.indexPhrases = indexPhrases; return builder; } @@ -152,14 +160,14 @@ public Builder docValues(boolean docValues) { } public Builder eagerGlobalOrdinals(boolean eagerGlobalOrdinals) { - fieldType().setEagerGlobalOrdinals(eagerGlobalOrdinals); + this.eagerGlobalOrdinals = eagerGlobalOrdinals; return builder; } public Builder fielddataFrequencyFilter(double minFreq, double maxFreq, int minSegmentSize) { - fieldType().setFielddataMinFrequency(minFreq); - fieldType().setFielddataMaxFrequency(maxFreq); - fieldType().setFielddataMinSegmentSize(minSegmentSize); + this.fielddataMinFreq = minFreq; + this.fielddataMaxFreq = maxFreq; + this.fielddataMinSegSize = minSegmentSize; return builder; } @@ -179,6 +187,71 @@ public Builder indexPrefixes(int minChars, int maxChars) { return this; } + private TextFieldType buildFieldType(BuilderContext context) { + TextFieldType ft = new TextFieldType(buildFullName(context), indexed, + fieldType.indexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0, meta); + ft.setIndexAnalyzer(indexAnalyzer); + ft.setSearchAnalyzer(searchAnalyzer); + ft.setSearchQuoteAnalyzer(searchQuoteAnalyzer); + ft.setEagerGlobalOrdinals(eagerGlobalOrdinals); + ft.setSimilarity(similarity); + if (fielddata) { + ft.setFielddata(true); + ft.setFielddataMinFrequency(fielddataMinFreq); + ft.setFielddataMaxFrequency(fielddataMaxFreq); + ft.setFielddataMinSegmentSize(fielddataMinSegSize); + } + return ft; + } + + private PrefixFieldMapper buildPrefixMapper(BuilderContext context) { + if (minPrefixChars == -1) { + return null; + } + if (indexed == false) { + throw new IllegalArgumentException("Cannot set index_prefixes on unindexed field [" + name() + "]"); + } + /* + * Mappings before v7.2.1 use {@link Builder#name} instead of {@link Builder#fullName} + * to build prefix field names so we preserve the name that was used at creation time + * even if it is different from the expected one (in case the field is nested under an object + * or a multi-field). This way search will continue to work on old indices and new indices + * will use the expected full name. + */ + String fullName = context.indexCreatedVersion().before(Version.V_7_2_1) ? name() : buildFullName(context); + // Copy the index options of the main field to allow phrase queries on + // the prefix field. + FieldType pft = new FieldType(fieldType); + pft.setOmitNorms(true); + if (fieldType.indexOptions() == IndexOptions.DOCS_AND_FREQS) { + // frequencies are not needed because prefix queries always use a constant score + pft.setIndexOptions(IndexOptions.DOCS); + } else { + pft.setIndexOptions(fieldType.indexOptions()); + } + if (fieldType.storeTermVectorOffsets()) { + pft.setStoreTermVectorOffsets(true); + } + PrefixFieldType prefixFieldType = new PrefixFieldType(fullName, fullName + "._index_prefix", + minPrefixChars, maxPrefixChars, pft.indexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0); + prefixFieldType.setAnalyzer(indexAnalyzer); + return new PrefixFieldMapper(pft, prefixFieldType, context.indexSettings()); + } + + private PhraseFieldMapper buildPhraseMapper(BuilderContext context, TextFieldType parent) { + if (indexPhrases == false) { + return null; + } + if (indexed == false) { + throw new IllegalArgumentException("Cannot set index_phrases on unindexed field [" + name() + "]"); + } + if (fieldType.indexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) < 0) { + throw new IllegalArgumentException("Cannot set index_phrases on field [" + name() + "] if positions are not enabled"); + } + FieldType phraseFieldType = new FieldType(fieldType); + return new PhraseFieldMapper(phraseFieldType, new PhraseFieldType(parent), context.indexSettings()); + } + @Override public TextFieldMapper build(BuilderContext context) { if (positionIncrementGap != POSITION_INCREMENT_GAP_USE_ANALYZER) { @@ -186,51 +259,13 @@ public TextFieldMapper build(BuilderContext context) { throw new IllegalArgumentException("Cannot set position_increment_gap on field [" + name + "] without positions enabled"); } - fieldType.setIndexAnalyzer(new NamedAnalyzer(fieldType.indexAnalyzer(), positionIncrementGap)); - fieldType.setSearchAnalyzer(new NamedAnalyzer(fieldType.searchAnalyzer(), positionIncrementGap)); - fieldType.setSearchQuoteAnalyzer(new NamedAnalyzer(fieldType.searchQuoteAnalyzer(), positionIncrementGap)); + indexAnalyzer = new NamedAnalyzer(indexAnalyzer, positionIncrementGap); + searchAnalyzer = new NamedAnalyzer(searchAnalyzer, positionIncrementGap); + searchQuoteAnalyzer = new NamedAnalyzer(searchQuoteAnalyzer, positionIncrementGap); } - setupFieldType(context); - PrefixFieldMapper prefixMapper = null; - if (minPrefixChars != -1) { - /** - * Mappings before v7.2.1 use {@link Builder#name} instead of {@link Builder#fullName} - * to build prefix field names so we preserve the name that was used at creation time - * even if it is different from the expected one (in case the field is nested under an object - * or a multi-field). This way search will continue to work on old indices and new indices - * will use the expected full name. - **/ - String fullName = context.indexCreatedVersion().before(Version.V_7_2_1) ? name() : buildFullName(context); - PrefixFieldType prefixFieldType = - new PrefixFieldType(fullName, fullName + "._index_prefix", minPrefixChars, maxPrefixChars); - fieldType().setPrefixFieldType(prefixFieldType); - if (fieldType().isSearchable() == false) { - throw new IllegalArgumentException("Cannot set index_prefixes on unindexed field [" + name() + "]"); - } - // Copy the index options of the main field to allow phrase queries on - // the prefix field. - if (fieldType.indexOptions() == IndexOptions.DOCS_AND_FREQS) { - // frequencies are not needed because prefix queries always use a constant score - prefixFieldType.setIndexOptions(IndexOptions.DOCS); - } else { - prefixFieldType.setIndexOptions(fieldType.indexOptions()); - } - if (fieldType.storeTermVectorOffsets()) { - prefixFieldType.setStoreTermVectorOffsets(true); - } - prefixFieldType.setAnalyzer(fieldType.indexAnalyzer()); - prefixMapper = new PrefixFieldMapper(prefixFieldType, context.indexSettings()); - } - if (fieldType().indexPhrases) { - if (fieldType().isSearchable() == false) { - throw new IllegalArgumentException("Cannot set index_phrases on unindexed field [" + name() + "]"); - } - if (fieldType.indexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) < 0) { - throw new IllegalArgumentException("Cannot set index_phrases on field [" + name() + "] if positions are not enabled"); - } - } - return new TextFieldMapper( - name, fieldType(), defaultFieldType, positionIncrementGap, prefixMapper, + TextFieldType tft = buildFieldType(context); + return new TextFieldMapper(name, + fieldType, tft, positionIncrementGap, buildPrefixMapper(context), buildPhraseMapper(context, tft), context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); } } @@ -239,9 +274,9 @@ public static class TypeParser implements Mapper.TypeParser { @Override public Mapper.Builder parse(String fieldName, Map node, ParserContext parserContext) throws MapperParsingException { TextFieldMapper.Builder builder = new TextFieldMapper.Builder(fieldName); - builder.fieldType().setIndexAnalyzer(parserContext.getIndexAnalyzers().getDefaultIndexAnalyzer()); - builder.fieldType().setSearchAnalyzer(parserContext.getIndexAnalyzers().getDefaultSearchAnalyzer()); - builder.fieldType().setSearchQuoteAnalyzer(parserContext.getIndexAnalyzers().getDefaultSearchQuoteAnalyzer()); + builder.indexAnalyzer(parserContext.getIndexAnalyzers().getDefaultIndexAnalyzer()); + builder.searchAnalyzer(parserContext.getIndexAnalyzers().getDefaultSearchAnalyzer()); + builder.searchQuoteAnalyzer(parserContext.getIndexAnalyzers().getDefaultSearchQuoteAnalyzer()); parseTextField(builder, fieldName, node, parserContext); for (Iterator> iterator = node.entrySet().iterator(); iterator.hasNext();) { Map.Entry entry = iterator.next(); @@ -333,18 +368,8 @@ private static final class PhraseFieldType extends StringFieldType { final TextFieldType parent; PhraseFieldType(TextFieldType parent) { - setTokenized(true); - setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS); - if (parent.indexOptions() == IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) { - setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS); - } - if (parent.storeTermVectorOffsets()) { - setStoreTermVectors(true); - setStoreTermVectorPositions(true); - setStoreTermVectorOffsets(true); - } + super(parent.name() + FAST_PHRASE_SUFFIX, true, false, Collections.emptyMap()); setAnalyzer(parent.indexAnalyzer().name(), parent.indexAnalyzer().analyzer()); - setName(parent.name() + FAST_PHRASE_SUFFIX); this.parent = parent; } @@ -374,20 +399,17 @@ static final class PrefixFieldType extends StringFieldType { final int maxChars; final String parentField; - PrefixFieldType(String parentField, String name, int minChars, int maxChars) { - setTokenized(true); - setOmitNorms(true); - setIndexOptions(IndexOptions.DOCS); - setName(name); + PrefixFieldType(String parentField, String name, int minChars, int maxChars, boolean hasPositions) { + super(name, true, false, Collections.emptyMap()); this.minChars = minChars; this.maxChars = maxChars; this.parentField = parentField; + this.hasPositions = hasPositions; } - PrefixFieldType setAnalyzer(NamedAnalyzer delegate) { + void setAnalyzer(NamedAnalyzer delegate) { setIndexAnalyzer(new NamedAnalyzer(delegate.name(), AnalyzerScope.INDEX, new PrefixWrappedAnalyzer(delegate.analyzer(), minChars, maxChars))); - return this; } boolean accept(int length) { @@ -421,6 +443,9 @@ public Query prefixQuery(String value, MultiTermQuery.RewriteMethod method, Quer } public IntervalsSource intervals(BytesRef term) { + if (hasPositions == false) { + throw new IllegalArgumentException("Cannot create intervals over a field [" + name() + "] without indexed positions"); + } if (term.length > maxChars) { return Intervals.prefix(term); } @@ -433,7 +458,7 @@ public IntervalsSource intervals(BytesRef term) { @Override public PrefixFieldType clone() { - return new PrefixFieldType(parentField, name(), minChars, maxChars); + return new PrefixFieldType(parentField, name(), minChars, maxChars, hasPositions); } @Override @@ -469,8 +494,8 @@ public int hashCode() { private static final class PhraseFieldMapper extends FieldMapper { - PhraseFieldMapper(PhraseFieldType fieldType, Settings indexSettings) { - super(fieldType.name(), fieldType, fieldType, indexSettings, MultiFields.empty(), CopyTo.empty()); + PhraseFieldMapper(FieldType fieldType, PhraseFieldType mappedFieldType, Settings indexSettings) { + super(mappedFieldType.name(), fieldType, mappedFieldType, indexSettings, MultiFields.empty(), CopyTo.empty()); } @Override @@ -491,12 +516,12 @@ protected String contentType() { private static final class PrefixFieldMapper extends FieldMapper { - protected PrefixFieldMapper(PrefixFieldType fieldType, Settings indexSettings) { - super(fieldType.name(), fieldType, fieldType, indexSettings, MultiFields.empty(), CopyTo.empty()); + protected PrefixFieldMapper(FieldType fieldType, PrefixFieldType mappedFieldType, Settings indexSettings) { + super(mappedFieldType.name(), fieldType, mappedFieldType, indexSettings, MultiFields.empty(), CopyTo.empty()); } void addField(ParseContext context, String value) { - context.doc().add(new Field(fieldType().name(), value, fieldType())); + context.doc().add(new Field(fieldType().name(), value, fieldType)); } @Override @@ -529,14 +554,19 @@ public static class TextFieldType extends StringFieldType { private PrefixFieldType prefixFieldType; private boolean indexPhrases = false; - public TextFieldType() { - setTokenized(true); + public TextFieldType(String name, boolean indexed, boolean hasPositions, Map meta) { + super(name, indexed, false, meta); + this.hasPositions = hasPositions; fielddata = false; fielddataMinFrequency = Defaults.FIELDDATA_MIN_FREQUENCY; fielddataMaxFrequency = Defaults.FIELDDATA_MAX_FREQUENCY; fielddataMinSegmentSize = Defaults.FIELDDATA_MIN_SEGMENT_SIZE; } + public TextFieldType(String name) { + this(name, true, true, Collections.emptyMap()); + } + protected TextFieldType(TextFieldType ref) { super(ref); this.fielddata = ref.fielddata; @@ -547,6 +577,7 @@ protected TextFieldType(TextFieldType ref) { if (ref.prefixFieldType != null) { this.prefixFieldType = ref.prefixFieldType.clone(); } + this.hasPositions = ref.hasPositions; } @Override @@ -579,7 +610,6 @@ public boolean fielddata() { } public void setFielddata(boolean fielddata) { - checkIfFrozen(); this.fielddata = fielddata; } @@ -588,7 +618,6 @@ public double fielddataMinFrequency() { } public void setFielddataMinFrequency(double fielddataMinFrequency) { - checkIfFrozen(); this.fielddataMinFrequency = fielddataMinFrequency; } @@ -597,7 +626,6 @@ public double fielddataMaxFrequency() { } public void setFielddataMaxFrequency(double fielddataMaxFrequency) { - checkIfFrozen(); this.fielddataMaxFrequency = fielddataMaxFrequency; } @@ -606,17 +634,14 @@ public int fielddataMinSegmentSize() { } public void setFielddataMinSegmentSize(int fielddataMinSegmentSize) { - checkIfFrozen(); this.fielddataMinSegmentSize = fielddataMinSegmentSize; } void setPrefixFieldType(PrefixFieldType prefixFieldType) { - checkIfFrozen(); this.prefixFieldType = prefixFieldType; } void setIndexPhrases(boolean indexPhrases) { - checkIfFrozen(); this.indexPhrases = indexPhrases; } @@ -624,6 +649,10 @@ public PrefixFieldType getPrefixFieldType() { return this.prefixFieldType; } + public boolean hasPositions() { + return this.hasPositions; + } + @Override public String typeName() { return CONTENT_TYPE; @@ -648,7 +677,7 @@ public SpanQuery spanPrefixQuery(String value, SpanMultiTermQueryWrapper.SpanRew if (prefixFieldType != null && value.length() >= prefixFieldType.minChars && value.length() <= prefixFieldType.maxChars - && prefixFieldType.indexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0) { + && prefixFieldType.hasPositions()) { return new FieldMaskingSpanQuery(new SpanTermQuery(new Term(prefixFieldType.name(), indexedValueForSearch(value))), name()); } else { @@ -661,7 +690,7 @@ public SpanQuery spanPrefixQuery(String value, SpanMultiTermQueryWrapper.SpanRew @Override public Query existsQuery(QueryShardContext context) { - if (omitNorms()) { + if (context.getMapperService().getLuceneFieldType(name()).omitNorms()) { return new TermQuery(new Term(FieldNamesFieldMapper.NAME, name())); } else { return new NormsFieldExistsQuery(name()); @@ -671,7 +700,7 @@ public Query existsQuery(QueryShardContext context) { @Override public IntervalsSource intervals(String text, int maxGaps, boolean ordered, NamedAnalyzer analyzer, boolean prefix) throws IOException { - if (indexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) < 0) { + if (hasPositions() == false) { throw new IllegalArgumentException("Cannot create intervals over field [" + name() + "] with no positions indexed"); } if (analyzer == null) { @@ -773,22 +802,27 @@ public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName) { } - private int positionIncrementGap; + private final int positionIncrementGap; private PrefixFieldMapper prefixFieldMapper; private PhraseFieldMapper phraseFieldMapper; - protected TextFieldMapper(String simpleName, TextFieldType fieldType, MappedFieldType defaultFieldType, + protected TextFieldMapper(String simpleName, FieldType fieldType, TextFieldType mappedFieldType, int positionIncrementGap, PrefixFieldMapper prefixFieldMapper, + PhraseFieldMapper phraseFieldMapper, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { - super(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, copyTo); + super(simpleName, fieldType, mappedFieldType, indexSettings, multiFields, copyTo); assert fieldType.tokenized(); - assert fieldType.hasDocValues() == false; - if (fieldType().indexOptions() == IndexOptions.NONE && fieldType().fielddata()) { + assert mappedFieldType.hasDocValues() == false; + if (fieldType.indexOptions() == IndexOptions.NONE && fieldType().fielddata()) { throw new IllegalArgumentException("Cannot enable fielddata on a [text] field that is not indexed: [" + name() + "]"); } this.positionIncrementGap = positionIncrementGap; this.prefixFieldMapper = prefixFieldMapper; - this.phraseFieldMapper = fieldType.indexPhrases ? new PhraseFieldMapper(new PhraseFieldType(fieldType), indexSettings) : null; + this.phraseFieldMapper = phraseFieldMapper; + if (prefixFieldMapper != null) { + mappedFieldType.setPrefixFieldType((PrefixFieldType)prefixFieldMapper.mappedFieldType); + } + mappedFieldType.setIndexPhrases(phraseFieldMapper != null); } @Override @@ -813,17 +847,17 @@ protected void parseCreateField(ParseContext context) throws IOException { return; } - if (fieldType().indexOptions() != IndexOptions.NONE || fieldType().stored()) { - Field field = new Field(fieldType().name(), value, fieldType()); + if (fieldType.indexOptions() != IndexOptions.NONE || fieldType.stored()) { + Field field = new Field(fieldType().name(), value, fieldType); context.doc().add(field); - if (fieldType().omitNorms()) { + if (fieldType.omitNorms()) { createFieldNamesField(context); } if (prefixFieldMapper != null) { prefixFieldMapper.addField(context, value); } if (phraseFieldMapper != null) { - context.doc().add(new Field(phraseFieldMapper.fieldType.name(), value, phraseFieldMapper.fieldType)); + context.doc().add(new Field(phraseFieldMapper.fieldType().name(), value, phraseFieldMapper.fieldType)); } } } @@ -851,6 +885,9 @@ protected String contentType() { @Override protected void mergeOptions(FieldMapper other, List conflicts) { TextFieldMapper mw = (TextFieldMapper) other; + if (Objects.equals(mw.fieldType().similarity(), this.fieldType().similarity()) == false) { + conflicts.add("mapper [" + name() + "] has different [similarity] settings"); + } if (mw.fieldType().indexPhrases != this.fieldType().indexPhrases) { conflicts.add("mapper [" + name() + "] has different [index_phrases] settings"); } @@ -860,10 +897,11 @@ protected void mergeOptions(FieldMapper other, List conflicts) { if (this.prefixFieldMapper != null && mw.prefixFieldMapper != null) { this.prefixFieldMapper = (PrefixFieldMapper) this.prefixFieldMapper.merge(mw.prefixFieldMapper); } - if (this.phraseFieldMapper != null && mw.phraseFieldMapper != null) { this.phraseFieldMapper = (PhraseFieldMapper) this.phraseFieldMapper.merge(mw.phraseFieldMapper); } + this.fieldType().setSearchAnalyzer(mw.fieldType().searchAnalyzer()); + this.fieldType().setSearchQuoteAnalyzer(mw.fieldType().searchQuoteAnalyzer()); } @Override @@ -871,16 +909,33 @@ public TextFieldType fieldType() { return (TextFieldType) super.fieldType(); } + @Override + protected boolean docValuesByDefault() { + return false; + } + @Override protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException { super.doXContentBody(builder, includeDefaults, params); + if (includeDefaults || (mappedFieldType.isSearchable() && fieldType.indexOptions() != Defaults.FIELD_TYPE.indexOptions())) { + builder.field("index_options", indexOptionToString(fieldType.indexOptions())); + } + if (includeDefaults || fieldType.storeTermVectors() != Defaults.FIELD_TYPE.storeTermVectors()) { + builder.field("term_vector", termVectorOptionsToString(fieldType)); + } + if (includeDefaults || fieldType.omitNorms()) { + builder.field("norms", fieldType.omitNorms() == false); + } doXContentAnalyzers(builder, includeDefaults); - if (includeDefaults || positionIncrementGap != POSITION_INCREMENT_GAP_USE_ANALYZER) { + if (includeDefaults || fieldType().eagerGlobalOrdinals()) { + builder.field("eager_global_ordinals", fieldType().eagerGlobalOrdinals()); + } + if (positionIncrementGap != POSITION_INCREMENT_GAP_USE_ANALYZER) { builder.field("position_increment_gap", positionIncrementGap); } - if (includeDefaults || fieldType().fielddata() != ((TextFieldType) defaultFieldType).fielddata()) { + if (includeDefaults || fieldType().fielddata() != false) { builder.field("fielddata", fieldType().fielddata()); } if (fieldType().fielddata()) { diff --git a/server/src/main/java/org/elasticsearch/index/mapper/TypeFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/TypeFieldMapper.java index a95c8df24f6c1..37dc2a77c24ad 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/TypeFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/TypeFieldMapper.java @@ -19,8 +19,7 @@ package org.elasticsearch.index.mapper; -import org.apache.lucene.document.Field; -import org.apache.lucene.document.SortedSetDocValuesField; +import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Term; @@ -33,7 +32,6 @@ import org.apache.lucene.search.TermInSetQuery; import org.apache.lucene.search.TermQuery; import org.apache.lucene.util.BytesRef; -import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.fielddata.IndexFieldData; @@ -43,6 +41,7 @@ import java.io.IOException; import java.util.Arrays; +import java.util.Collections; import java.util.HashSet; import java.util.Map; import java.util.Set; @@ -57,16 +56,13 @@ public class TypeFieldMapper extends MetadataFieldMapper { public static class Defaults { public static final String NAME = TypeFieldMapper.NAME; - public static final MappedFieldType FIELD_TYPE = new TypeFieldType(); + public static final FieldType FIELD_TYPE = new FieldType(); static { FIELD_TYPE.setIndexOptions(IndexOptions.DOCS); FIELD_TYPE.setTokenized(false); FIELD_TYPE.setStored(false); FIELD_TYPE.setOmitNorms(true); - FIELD_TYPE.setIndexAnalyzer(Lucene.KEYWORD_ANALYZER); - FIELD_TYPE.setSearchAnalyzer(Lucene.KEYWORD_ANALYZER); - FIELD_TYPE.setName(NAME); FIELD_TYPE.freeze(); } } @@ -81,13 +77,16 @@ public MetadataFieldMapper.Builder parse(String name, Map nod @Override public MetadataFieldMapper getDefault(ParserContext context) { final IndexSettings indexSettings = context.mapperService().getIndexSettings(); - return new TypeFieldMapper(indexSettings, defaultFieldType(indexSettings)); + return new TypeFieldMapper(Defaults.FIELD_TYPE, indexSettings); } } public static final class TypeFieldType extends ConstantFieldType { - TypeFieldType() { + public static final TypeFieldType INSTANCE = new TypeFieldType(); + + private TypeFieldType() { + super(NAME, Collections.emptyMap()); } protected TypeFieldType(TypeFieldType ref) { @@ -203,20 +202,8 @@ public String toString(String field) { } } - private TypeFieldMapper(IndexSettings indexSettings, MappedFieldType existing) { - this(existing == null ? defaultFieldType(indexSettings) : existing.clone(), - indexSettings); - } - - private TypeFieldMapper(MappedFieldType fieldType, IndexSettings indexSettings) { - super(NAME, fieldType, defaultFieldType(indexSettings), indexSettings.getSettings()); - } - - private static MappedFieldType defaultFieldType(IndexSettings indexSettings) { - MappedFieldType defaultFieldType = Defaults.FIELD_TYPE.clone(); - defaultFieldType.setIndexOptions(IndexOptions.NONE); - defaultFieldType.setHasDocValues(false); - return defaultFieldType; + private TypeFieldMapper(FieldType fieldType, IndexSettings indexSettings) { + super(fieldType, new TypeFieldType(), indexSettings.getSettings()); } @Override @@ -231,13 +218,7 @@ public void parse(ParseContext context) throws IOException { @Override protected void parseCreateField(ParseContext context) throws IOException { - if (fieldType().indexOptions() == IndexOptions.NONE && !fieldType().stored()) { - return; - } - context.doc().add(new Field(fieldType().name(), MapperService.SINGLE_MAPPING_NAME, fieldType())); - if (fieldType().hasDocValues()) { - context.doc().add(new SortedSetDocValuesField(fieldType().name(), new BytesRef(MapperService.SINGLE_MAPPING_NAME))); - } + } @Override diff --git a/server/src/main/java/org/elasticsearch/index/mapper/VersionFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/VersionFieldMapper.java index cc3d843358a73..48ab2c341dcfe 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/VersionFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/VersionFieldMapper.java @@ -20,6 +20,7 @@ package org.elasticsearch.index.mapper; import org.apache.lucene.document.Field; +import org.apache.lucene.document.FieldType; import org.apache.lucene.document.NumericDocValuesField; import org.apache.lucene.index.DocValuesType; import org.apache.lucene.index.IndexOptions; @@ -32,6 +33,7 @@ import org.elasticsearch.index.query.QueryShardException; import java.io.IOException; +import java.util.Collections; import java.util.Map; /** Mapper for the _version field. */ @@ -43,13 +45,12 @@ public class VersionFieldMapper extends MetadataFieldMapper { public static class Defaults { public static final String NAME = VersionFieldMapper.NAME; - public static final MappedFieldType FIELD_TYPE = new VersionFieldType(); + public static final FieldType FIELD_TYPE = new FieldType(); + public static final MappedFieldType MAPPED_FIELD_TYPE = new VersionFieldType(); static { - FIELD_TYPE.setName(NAME); FIELD_TYPE.setDocValuesType(DocValuesType.NUMERIC); FIELD_TYPE.setIndexOptions(IndexOptions.NONE); - FIELD_TYPE.setHasDocValues(true); FIELD_TYPE.freeze(); } } @@ -70,7 +71,10 @@ public MetadataFieldMapper getDefault(ParserContext context) { static final class VersionFieldType extends MappedFieldType { - VersionFieldType() { + public static final VersionFieldType INSTANCE = new VersionFieldType(); + + private VersionFieldType() { + super(NAME, false, true, Collections.emptyMap()); } protected VersionFieldType(VersionFieldType ref) { @@ -99,7 +103,7 @@ public Query termQuery(Object value, QueryShardContext context) { } private VersionFieldMapper(Settings indexSettings) { - super(NAME, Defaults.FIELD_TYPE, Defaults.FIELD_TYPE, indexSettings); + super(Defaults.FIELD_TYPE, Defaults.MAPPED_FIELD_TYPE, indexSettings); } @Override diff --git a/server/src/main/java/org/elasticsearch/index/query/IntervalsSourceProvider.java b/server/src/main/java/org/elasticsearch/index/query/IntervalsSourceProvider.java index 1210c62319f9c..ee4c6713947f0 100644 --- a/server/src/main/java/org/elasticsearch/index/query/IntervalsSourceProvider.java +++ b/server/src/main/java/org/elasticsearch/index/query/IntervalsSourceProvider.java @@ -19,7 +19,6 @@ package org.elasticsearch.index.query; -import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.Term; import org.apache.lucene.queries.intervals.FilteredIntervalsSource; import org.apache.lucene.queries.intervals.IntervalIterator; @@ -664,7 +663,7 @@ public IntervalsSource getSource(QueryShardContext context, MappedFieldType fiel } private void checkPositions(MappedFieldType type) { - if (type.indexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) < 0) { + if (type.hasPositions() == false) { throw new IllegalArgumentException("Cannot create intervals over field [" + type.name() + "] with no positions indexed"); } } @@ -802,7 +801,7 @@ public IntervalsSource getSource(QueryShardContext context, MappedFieldType fiel } private void checkPositions(MappedFieldType type) { - if (type.indexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) < 0) { + if (type.hasPositions() == false) { throw new IllegalArgumentException("Cannot create intervals over field [" + type.name() + "] with no positions indexed"); } } diff --git a/server/src/main/java/org/elasticsearch/index/search/MatchQuery.java b/server/src/main/java/org/elasticsearch/index/search/MatchQuery.java index c7c8c428bc7e9..cba8b1c6ab367 100644 --- a/server/src/main/java/org/elasticsearch/index/search/MatchQuery.java +++ b/server/src/main/java/org/elasticsearch/index/search/MatchQuery.java @@ -27,7 +27,6 @@ import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute; import org.apache.lucene.analysis.tokenattributes.PositionLengthAttribute; import org.apache.lucene.analysis.tokenattributes.TermToBytesRefAttribute; -import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.Term; import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BooleanQuery; @@ -317,10 +316,6 @@ protected Query zeroTermsQuery() { } } - private boolean hasPositions(MappedFieldType fieldType) { - return fieldType.indexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) >= 0; - } - class MatchQueryBuilder extends QueryBuilder { private final MappedFieldType fieldType; @@ -332,7 +327,7 @@ class MatchQueryBuilder extends QueryBuilder { super(analyzer); this.fieldType = fieldType; setEnablePositionIncrements(enablePositionIncrements); - if (hasPositions(fieldType)) { + if (fieldType.hasPositions()) { setAutoGenerateMultiTermSynonymsPhraseQuery(autoGenerateSynonymsPhraseQuery); } else { setAutoGenerateMultiTermSynonymsPhraseQuery(false); @@ -804,7 +799,7 @@ private Query analyzeGraphPhrase(TokenStream source, String field, Type type, in } private void checkForPositions(String field) { - if (hasPositions(fieldType) == false) { + if (fieldType.hasPositions() == false) { throw new IllegalStateException("field:[" + field + "] was indexed without position data; cannot run PhraseQuery"); } } diff --git a/server/src/main/java/org/elasticsearch/index/search/QueryStringQueryParser.java b/server/src/main/java/org/elasticsearch/index/search/QueryStringQueryParser.java index 30d6fd4826036..cccf1777bfc4f 100644 --- a/server/src/main/java/org/elasticsearch/index/search/QueryStringQueryParser.java +++ b/server/src/main/java/org/elasticsearch/index/search/QueryStringQueryParser.java @@ -23,6 +23,7 @@ import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute; +import org.apache.lucene.document.FieldType; import org.apache.lucene.index.Term; import org.apache.lucene.queryparser.classic.ParseException; import org.apache.lucene.queryparser.classic.Token; @@ -521,8 +522,9 @@ private Query getPrefixQuerySingle(String field, String termStr) throws ParseExc return newUnmappedFieldQuery(field); } setAnalyzer(forceAnalyzer == null ? queryBuilder.context.getSearchAnalyzer(currentFieldType) : forceAnalyzer); + FieldType ft = context.getMapperService().getLuceneFieldType(field); Query query = null; - if (currentFieldType.tokenized() == false) { + if (ft.tokenized() == false) { query = currentFieldType.prefixQuery(termStr, getMultiTermRewriteMethod(), context); } else { query = getPossiblyAnalyzedPrefixQuery(currentFieldType.name(), termStr, currentFieldType); diff --git a/server/src/main/java/org/elasticsearch/index/termvectors/TermVectorsService.java b/server/src/main/java/org/elasticsearch/index/termvectors/TermVectorsService.java index 65df5e2b35268..390804fa4d0a0 100644 --- a/server/src/main/java/org/elasticsearch/index/termvectors/TermVectorsService.java +++ b/server/src/main/java/org/elasticsearch/index/termvectors/TermVectorsService.java @@ -44,7 +44,6 @@ import org.elasticsearch.index.get.GetResult; import org.elasticsearch.index.mapper.DocumentMapperForType; import org.elasticsearch.index.mapper.IdFieldMapper; -import org.elasticsearch.index.mapper.KeywordFieldMapper; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.mapper.ParseContext; @@ -184,7 +183,7 @@ private static boolean isValidField(MappedFieldType fieldType) { return false; } // and must be indexed - if (fieldType.indexOptions() == IndexOptions.NONE) { + if (fieldType.isSearchable() == false) { return false; } return true; @@ -200,7 +199,7 @@ private static Fields addGeneratedTermVectors(IndexShard indexShard, Engine.GetR continue; } // already retrieved, only if the analyzer hasn't been overridden at the field - if (fieldType.storeTermVectors() && + if (indexShard.mapperService().getLuceneFieldType(field).storeTermVectors() && (request.perFieldAnalyzer() == null || !request.perFieldAnalyzer().containsKey(field))) { continue; } @@ -333,10 +332,12 @@ private static Fields generateTermVectorsFromDoc(IndexShard indexShard, TermVect public static String[] getValues(IndexableField[] fields) { List result = new ArrayList<>(); for (IndexableField field : fields) { - if (field.fieldType() instanceof KeywordFieldMapper.KeywordFieldType) { - result.add(field.binaryValue().utf8ToString()); - } else if (field.fieldType() instanceof StringFieldType) { - result.add(field.stringValue()); + if (field.fieldType().indexOptions() != IndexOptions.NONE) { + if (field.binaryValue() != null) { + result.add(field.binaryValue().utf8ToString()); + } else { + result.add(field.stringValue()); + } } } return result.toArray(new String[0]); diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/composite/SingleDimensionValuesSource.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/composite/SingleDimensionValuesSource.java index f49e20e5bd0dc..a212987f2980b 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/composite/SingleDimensionValuesSource.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/composite/SingleDimensionValuesSource.java @@ -19,7 +19,6 @@ package org.elasticsearch.search.aggregations.bucket.composite; -import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.search.Query; @@ -153,7 +152,7 @@ abstract LeafBucketCollector getLeafCollector(Comparable value, protected boolean checkIfSortedDocsIsApplicable(IndexReader reader, MappedFieldType fieldType) { if (fieldType == null || (missingBucket && afterValue == null) || - fieldType.indexOptions() == IndexOptions.NONE || + fieldType.isSearchable() == false || // inverse of the natural order reverseMul == -1) { return false; diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MinAggregator.java b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MinAggregator.java index 8c5dd731c113c..34b905ec5fc57 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MinAggregator.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MinAggregator.java @@ -18,7 +18,6 @@ */ package org.elasticsearch.search.aggregations.metrics; -import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.LeafReader; import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.index.PointValues; @@ -179,7 +178,7 @@ static Function getPointReaderOrNull(SearchContext context, Aggr } if (config.fieldContext() != null && config.script() == null && config.missing() == null) { MappedFieldType fieldType = config.fieldContext().fieldType(); - if (fieldType == null || fieldType.indexOptions() == IndexOptions.NONE) { + if (fieldType == null || fieldType.isSearchable() == false) { return null; } Function converter = null; diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/support/CoreValuesSourceType.java b/server/src/main/java/org/elasticsearch/search/aggregations/support/CoreValuesSourceType.java index 1b79bd7925f95..a6944a6e4119f 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/support/CoreValuesSourceType.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/support/CoreValuesSourceType.java @@ -19,7 +19,6 @@ package org.elasticsearch.search.aggregations.support; -import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.PointValues; import org.apache.lucene.util.BytesRef; @@ -255,7 +254,7 @@ private ValuesSource.Numeric fieldData(FieldContext fieldContext) { throw new IllegalArgumentException("Expected numeric type on field [" + fieldContext.field() + "], but got [" + fieldContext.fieldType().typeName() + "]"); } - if (fieldContext.fieldType().indexOptions() == IndexOptions.NONE + if (fieldContext.fieldType().isSearchable() == false || fieldContext.fieldType() instanceof DateFieldType == false) { /* * We can't implement roundingPreparer in these cases because diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/support/ValuesSourceRegistry.java b/server/src/main/java/org/elasticsearch/search/aggregations/support/ValuesSourceRegistry.java index f08738bf13a81..ea2a3d6bf0d92 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/support/ValuesSourceRegistry.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/support/ValuesSourceRegistry.java @@ -133,7 +133,7 @@ public AggregatorSupplier getAggregator(ValuesSourceConfig valuesSourceConfig, S if (supplier == null) { // TODO: push building the description into ValuesSourceConfig MappedFieldType fieldType = valuesSourceConfig.fieldContext().fieldType(); - String fieldDescription = fieldType.typeName() + "(" + fieldType.toString() + ")"; + String fieldDescription = fieldType.typeName(); throw new IllegalArgumentException("Field [" + fieldType.name() + "] of type [" + fieldDescription + "] is not supported for aggregation [" + aggregationName + "]"); } return supplier; diff --git a/server/src/main/java/org/elasticsearch/search/collapse/CollapseBuilder.java b/server/src/main/java/org/elasticsearch/search/collapse/CollapseBuilder.java index 3246680986dcb..a471230cd999a 100644 --- a/server/src/main/java/org/elasticsearch/search/collapse/CollapseBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/collapse/CollapseBuilder.java @@ -18,7 +18,6 @@ */ package org.elasticsearch.search.collapse; -import org.apache.lucene.index.IndexOptions; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.Strings; @@ -213,7 +212,7 @@ public CollapseContext build(QueryShardContext queryShardContext) { if (fieldType.hasDocValues() == false) { throw new IllegalArgumentException("cannot collapse on field `" + field + "` without `doc_values`"); } - if (fieldType.indexOptions() == IndexOptions.NONE && (innerHits != null && !innerHits.isEmpty())) { + if (fieldType.isSearchable() == false && (innerHits != null && !innerHits.isEmpty())) { throw new IllegalArgumentException("cannot expand `inner_hits` for collapse field `" + field + "`, " + "only indexed field can retrieve `inner_hits`"); } diff --git a/server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/FastVectorHighlighter.java b/server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/FastVectorHighlighter.java index ed2ddccc7edfa..b9b151b000c97 100644 --- a/server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/FastVectorHighlighter.java +++ b/server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/FastVectorHighlighter.java @@ -18,6 +18,7 @@ */ package org.elasticsearch.search.fetch.subphase.highlight; +import org.apache.lucene.document.FieldType; import org.apache.lucene.search.highlight.Encoder; import org.apache.lucene.search.vectorhighlight.BaseFragmentsBuilder; import org.apache.lucene.search.vectorhighlight.BoundaryScanner; @@ -74,7 +75,7 @@ public HighlightField highlight(HighlighterContext highlighterContext) { FetchSubPhase.HitContext hitContext = highlighterContext.hitContext; MappedFieldType fieldType = highlighterContext.fieldType; - if (canHighlight(fieldType) == false) { + if (canHighlight(highlighterContext.luceneFieldType) == false) { throw new IllegalArgumentException("the field [" + highlighterContext.fieldName + "] should be indexed with term vector with position offsets to be used with fast vector highlighter"); } @@ -98,7 +99,7 @@ public HighlightField highlight(HighlighterContext highlighterContext) { if (field.fieldOptions().numberOfFragments() == 0) { fragListBuilder = new SingleFragListBuilder(); - if (!forceSource && fieldType.stored()) { + if (!forceSource && highlighterContext.luceneFieldType.stored()) { fragmentsBuilder = new SimpleFragmentsBuilder(fieldType, field.fieldOptions().preTags(), field.fieldOptions().postTags(), boundaryScanner); } else { @@ -109,7 +110,7 @@ public HighlightField highlight(HighlighterContext highlighterContext) { fragListBuilder = field.fieldOptions().fragmentOffset() == -1 ? new SimpleFragListBuilder() : new SimpleFragListBuilder(field.fieldOptions().fragmentOffset()); if (field.fieldOptions().scoreOrdered()) { - if (!forceSource && fieldType.stored()) { + if (!forceSource && highlighterContext.luceneFieldType.stored()) { fragmentsBuilder = new ScoreOrderFragmentsBuilder(field.fieldOptions().preTags(), field.fieldOptions().postTags(), boundaryScanner); } else { @@ -117,7 +118,7 @@ public HighlightField highlight(HighlighterContext highlighterContext) { field.fieldOptions().preTags(), field.fieldOptions().postTags(), boundaryScanner); } } else { - if (!forceSource && fieldType.stored()) { + if (!forceSource && highlighterContext.luceneFieldType.stored()) { fragmentsBuilder = new SimpleFragmentsBuilder(fieldType, field.fieldOptions().preTags(), field.fieldOptions().postTags(), boundaryScanner); } else { @@ -210,7 +211,7 @@ public HighlightField highlight(HighlighterContext highlighterContext) { } @Override - public boolean canHighlight(MappedFieldType fieldType) { + public boolean canHighlight(FieldType fieldType) { return fieldType.storeTermVectors() && fieldType.storeTermVectorOffsets() && fieldType.storeTermVectorPositions(); diff --git a/server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightPhase.java b/server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightPhase.java index 82e8d1b017ee9..5c3ad5e9cd6af 100644 --- a/server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightPhase.java +++ b/server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightPhase.java @@ -108,9 +108,10 @@ public void hitExecute(SearchShardTarget shardTarget, highlightQuery = query; } HighlighterContext highlighterContext = new HighlighterContext(fieldType.name(), - field, fieldType, shardTarget, context, highlight, hitContext, highlightQuery); + field, fieldType, shardTarget, context, highlight, hitContext, highlightQuery, + context.getMapperService().getLuceneFieldType(fieldType.name())); - if ((highlighter.canHighlight(fieldType) == false) && fieldNameContainsWildcards) { + if ((highlighter.canHighlight(highlighterContext.luceneFieldType) == false) && fieldNameContainsWildcards) { // if several fieldnames matched the wildcard then we want to skip those that we cannot highlight continue; } diff --git a/server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightUtils.java b/server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightUtils.java index 3651c21e4396b..f03d6b7650ab4 100644 --- a/server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightUtils.java +++ b/server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightUtils.java @@ -18,6 +18,7 @@ */ package org.elasticsearch.search.fetch.subphase.highlight; +import org.apache.lucene.document.FieldType; import org.apache.lucene.search.highlight.DefaultEncoder; import org.apache.lucene.search.highlight.Encoder; import org.apache.lucene.search.highlight.SimpleHTMLEncoder; @@ -52,7 +53,8 @@ public static List loadFieldValues(MappedFieldType fieldType, boolean forceSource) throws IOException { //percolator needs to always load from source, thus it sets the global force source to true List textsToHighlight; - if (forceSource == false && fieldType.stored()) { + FieldType luceneFieldType = context.getMapperService().getLuceneFieldType(fieldType.name()); + if (forceSource == false && luceneFieldType.stored()) { CustomFieldsVisitor fieldVisitor = new CustomFieldsVisitor(singleton(fieldType.name()), false); hitContext.reader().document(hitContext.docId(), fieldVisitor); textsToHighlight = fieldVisitor.fields().get(fieldType.name()); @@ -73,5 +75,5 @@ public static class Encoders { public static final Encoder DEFAULT = new DefaultEncoder(); public static final Encoder HTML = new SimpleHTMLEncoder(); } - + } diff --git a/server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/Highlighter.java b/server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/Highlighter.java index 7de8a9f9a9b29..a1028cc9d9a0b 100644 --- a/server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/Highlighter.java +++ b/server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/Highlighter.java @@ -18,7 +18,7 @@ */ package org.elasticsearch.search.fetch.subphase.highlight; -import org.elasticsearch.index.mapper.MappedFieldType; +import org.apache.lucene.document.FieldType; /** * Highlights a search result. @@ -27,5 +27,5 @@ public interface Highlighter { HighlightField highlight(HighlighterContext highlighterContext); - boolean canHighlight(MappedFieldType fieldType); + boolean canHighlight(FieldType fieldType); } diff --git a/server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/HighlighterContext.java b/server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/HighlighterContext.java index aedb6b3999cf7..86d90bda8c5e5 100644 --- a/server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/HighlighterContext.java +++ b/server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/HighlighterContext.java @@ -18,6 +18,7 @@ */ package org.elasticsearch.search.fetch.subphase.highlight; +import org.apache.lucene.document.FieldType; import org.apache.lucene.search.Query; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.query.QueryShardContext; @@ -34,6 +35,7 @@ public class HighlighterContext { public final SearchContextHighlight highlight; public final FetchSubPhase.HitContext hitContext; public final Query query; + public final FieldType luceneFieldType; public HighlighterContext(String fieldName, SearchContextHighlight.Field field, @@ -42,7 +44,7 @@ public HighlighterContext(String fieldName, QueryShardContext context, SearchContextHighlight highlight, FetchSubPhase.HitContext hitContext, - Query query) { + Query query, FieldType luceneFieldType) { this.fieldName = fieldName; this.field = field; this.fieldType = fieldType; @@ -51,5 +53,6 @@ public HighlighterContext(String fieldName, this.highlight = highlight; this.hitContext = hitContext; this.query = query; + this.luceneFieldType = luceneFieldType; } } diff --git a/server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/PlainHighlighter.java b/server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/PlainHighlighter.java index d1743e6182e2f..27af8cd8b367c 100644 --- a/server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/PlainHighlighter.java +++ b/server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/PlainHighlighter.java @@ -22,6 +22,7 @@ import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; import org.apache.lucene.analysis.tokenattributes.OffsetAttribute; +import org.apache.lucene.document.FieldType; import org.apache.lucene.search.highlight.Encoder; import org.apache.lucene.search.highlight.Formatter; import org.apache.lucene.search.highlight.Fragmenter; @@ -202,7 +203,7 @@ public int compare(TextFragment o1, TextFragment o2) { } @Override - public boolean canHighlight(MappedFieldType fieldType) { + public boolean canHighlight(FieldType fieldType) { return true; } diff --git a/server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/UnifiedHighlighter.java b/server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/UnifiedHighlighter.java index 28ff7220d9bd7..094f518be53e5 100644 --- a/server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/UnifiedHighlighter.java +++ b/server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/UnifiedHighlighter.java @@ -19,6 +19,8 @@ package org.elasticsearch.search.fetch.subphase.highlight; import org.apache.lucene.analysis.Analyzer; +import org.apache.lucene.document.FieldType; +import org.apache.lucene.index.FieldInfo; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.highlight.Encoder; @@ -54,7 +56,7 @@ public class UnifiedHighlighter implements Highlighter { @Override - public boolean canHighlight(MappedFieldType fieldType) { + public boolean canHighlight(FieldType fieldType) { return true; } @@ -86,7 +88,8 @@ public HighlightField highlight(HighlighterContext highlighterContext) { final IndexSearcher searcher = new IndexSearcher(hitContext.reader()); final CustomUnifiedHighlighter highlighter; final String fieldValue = mergeFieldValues(fieldValues, MULTIVAL_SEP_CHAR); - final OffsetSource offsetSource = getOffsetSource(fieldType); + FieldInfo fi = hitContext.reader().getFieldInfos().fieldInfo(field.field()); + final OffsetSource offsetSource = getOffsetSource(highlighterContext.luceneFieldType); int fieldValueLength = fieldValue.length(); if (keywordIgnoreAbove != null && fieldValueLength > keywordIgnoreAbove) { return null; // skip highlighting keyword terms that were ignored during indexing @@ -101,7 +104,7 @@ public HighlightField highlight(HighlighterContext highlighterContext) { } if (numberOfFragments == 0 // non-tokenized fields should not use any break iterator (ignore boundaryScannerType) - || fieldType.tokenized() == false) { + || highlighterContext.luceneFieldType.tokenized() == false) { // we use a control char to separate values, which is the only char that the custom break iterator // breaks the text on, so we don't lose the distinction between the different values of a field and we // get back a snippet per value @@ -216,7 +219,7 @@ protected static String mergeFieldValues(List fieldValues, char valuesSe return rawValue.substring(0, Math.min(rawValue.length(), Integer.MAX_VALUE - 1)); } - protected OffsetSource getOffsetSource(MappedFieldType fieldType) { + protected OffsetSource getOffsetSource(FieldType fieldType) { if (fieldType.indexOptions() == IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) { return fieldType.storeTermVectors() ? OffsetSource.POSTINGS_WITH_TERM_VECTORS : OffsetSource.POSTINGS; } diff --git a/server/src/main/java/org/elasticsearch/search/query/QueryPhase.java b/server/src/main/java/org/elasticsearch/search/query/QueryPhase.java index cf81f7cc39e13..a371adf5f04b0 100644 --- a/server/src/main/java/org/elasticsearch/search/query/QueryPhase.java +++ b/server/src/main/java/org/elasticsearch/search/query/QueryPhase.java @@ -22,7 +22,6 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.lucene.document.LongPoint; -import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.index.PointValues; @@ -440,7 +439,7 @@ private static Query tryRewriteLongSort(SearchContext searchContext, IndexReader final MappedFieldType fieldType = searchContext.mapperService().fieldType(fieldName); if (fieldType == null) return null; // for unmapped fields, default behaviour depending on "unmapped_type" flag if ((fieldType.typeName().equals("long") == false) && (fieldType instanceof DateFieldType == false)) return null; - if (fieldType.indexOptions() == IndexOptions.NONE) return null; //TODO: change to pointDataDimensionCount() when implemented + if (fieldType.isSearchable() == false) return null; if (fieldType.hasDocValues() == false) return null; diff --git a/server/src/main/java/org/elasticsearch/search/sort/FieldSortBuilder.java b/server/src/main/java/org/elasticsearch/search/sort/FieldSortBuilder.java index c0e4b15d52716..807c37bbb9756 100644 --- a/server/src/main/java/org/elasticsearch/search/sort/FieldSortBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/sort/FieldSortBuilder.java @@ -20,7 +20,6 @@ package org.elasticsearch.search.sort; import org.apache.lucene.document.LongPoint; -import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.MultiTerms; import org.apache.lucene.index.PointValues; @@ -374,7 +373,7 @@ public boolean isBottomSortShardDisjoint(QueryShardContext context, SearchSortVa // unmapped return false; } - if (fieldType.indexOptions() == IndexOptions.NONE) { + if (fieldType.isSearchable() == false) { return false; } DocValueFormat docValueFormat = bottomSortValues.getSortValueFormats()[0]; @@ -497,7 +496,7 @@ public static MinAndMax getMinMaxOrNull(QueryShardContext context, FieldSortB } IndexReader reader = context.getIndexReader(); MappedFieldType fieldType = context.fieldMapper(sortField.getField()); - if (reader == null || (fieldType == null || fieldType.indexOptions() == IndexOptions.NONE)) { + if (reader == null || (fieldType == null || fieldType.isSearchable() == false)) { return null; } switch (IndexSortConfig.getSortFieldType(sortField)) { diff --git a/server/src/main/java/org/elasticsearch/search/suggest/completion/context/CategoryContextMapping.java b/server/src/main/java/org/elasticsearch/search/suggest/completion/context/CategoryContextMapping.java index affdb5528123c..feadc41807154 100644 --- a/server/src/main/java/org/elasticsearch/search/suggest/completion/context/CategoryContextMapping.java +++ b/server/src/main/java/org/elasticsearch/search/suggest/completion/context/CategoryContextMapping.java @@ -31,7 +31,6 @@ import org.elasticsearch.index.mapper.KeywordFieldMapper; import org.elasticsearch.index.mapper.ParseContext; import org.elasticsearch.index.mapper.ParseContext.Document; -import org.elasticsearch.index.mapper.StringFieldType; import java.io.IOException; import java.util.ArrayList; @@ -139,14 +138,15 @@ public Set parseContext(Document document) { if (fieldName != null) { IndexableField[] fields = document.getFields(fieldName); values = new HashSet<>(fields.length); + // TODO we should be checking mapped field types, not lucene field types for (IndexableField field : fields) { if (field instanceof SortedDocValuesField || field instanceof SortedSetDocValuesField || field instanceof StoredField) { // Ignore doc values and stored fields - } else if (field.fieldType() instanceof KeywordFieldMapper.KeywordFieldType) { + } else if (field instanceof KeywordFieldMapper.KeywordField) { values.add(field.binaryValue().utf8ToString()); - } else if (field.fieldType() instanceof StringFieldType) { + } else if (field.stringValue() != null) { values.add(field.stringValue()); } else { throw new IllegalArgumentException("Failed to parse context field [" + fieldName + diff --git a/server/src/test/java/org/apache/lucene/grouping/CollapsingTopDocsCollectorTests.java b/server/src/test/java/org/apache/lucene/grouping/CollapsingTopDocsCollectorTests.java index bec14c973e92c..74c5fff8df40a 100644 --- a/server/src/test/java/org/apache/lucene/grouping/CollapsingTopDocsCollectorTests.java +++ b/server/src/test/java/org/apache/lucene/grouping/CollapsingTopDocsCollectorTests.java @@ -120,8 +120,7 @@ private > void assertSearchCollapse(CollapsingDocValuesP final SortField sort2 = new SortField("sort2", SortField.Type.LONG); Sort sort = new Sort(sort1, sort2, collapseField); - MappedFieldType fieldType = new MockFieldMapper.FakeFieldType(); - fieldType.setName(collapseField.getField()); + MappedFieldType fieldType = new MockFieldMapper.FakeFieldType(collapseField.getField()); int expectedNumGroups = values.size(); @@ -381,8 +380,7 @@ public void testEmptyNumericSegment() throws Exception { final IndexReader reader = w.getReader(); final IndexSearcher searcher = newSearcher(reader); - MappedFieldType fieldType = new MockFieldMapper.FakeFieldType(); - fieldType.setName("group"); + MappedFieldType fieldType = new MockFieldMapper.FakeFieldType("group"); SortField sortField = new SortField("group", SortField.Type.LONG); sortField.setMissingValue(Long.MAX_VALUE); @@ -424,8 +422,7 @@ public void testEmptySortedSegment() throws Exception { final IndexReader reader = w.getReader(); final IndexSearcher searcher = newSearcher(reader); - MappedFieldType fieldType = new MockFieldMapper.FakeFieldType(); - fieldType.setName("group"); + MappedFieldType fieldType = new MockFieldMapper.FakeFieldType("group"); Sort sort = new Sort(new SortField("group", SortField.Type.STRING_VAL)); diff --git a/server/src/test/java/org/elasticsearch/index/fielddata/IndexFieldDataServiceTests.java b/server/src/test/java/org/elasticsearch/index/fielddata/IndexFieldDataServiceTests.java index a03a74a19bfe3..7eae7404402da 100644 --- a/server/src/test/java/org/elasticsearch/index/fielddata/IndexFieldDataServiceTests.java +++ b/server/src/test/java/org/elasticsearch/index/fielddata/IndexFieldDataServiceTests.java @@ -54,6 +54,7 @@ import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.concurrent.atomic.AtomicInteger; import static org.hamcrest.Matchers.containsString; @@ -254,14 +255,11 @@ private void doTestRequireDocValues(MappedFieldType ft) { IndicesFieldDataCache cache = new IndicesFieldDataCache(Settings.EMPTY, null); IndexFieldDataService ifds = new IndexFieldDataService(IndexSettingsModule.newIndexSettings("test", Settings.EMPTY), cache, null, null); - ft.setName("some_long"); - ft.setHasDocValues(true); - ifds.getForField(ft); // no exception - ft.setHasDocValues(false); - try { - ifds.getForField(ft); - fail(); - } catch (IllegalArgumentException e) { + if (ft.hasDocValues()) { + ifds.getForField(ft); // no exception + } + else { + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> ifds.getForField(ft)); assertThat(e.getMessage(), containsString("doc values")); } } finally { @@ -270,15 +268,20 @@ private void doTestRequireDocValues(MappedFieldType ft) { } public void testRequireDocValuesOnLongs() { - doTestRequireDocValues(new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG)); + doTestRequireDocValues(new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.LONG)); + doTestRequireDocValues(new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.LONG, + true, false, Collections.emptyMap())); } public void testRequireDocValuesOnDoubles() { - doTestRequireDocValues(new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE)); + doTestRequireDocValues(new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.DOUBLE)); + doTestRequireDocValues(new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.DOUBLE, + true, false, Collections.emptyMap())); } public void testRequireDocValuesOnBools() { - doTestRequireDocValues(new BooleanFieldMapper.BooleanFieldType()); + doTestRequireDocValues(new BooleanFieldMapper.BooleanFieldType("field")); + doTestRequireDocValues(new BooleanFieldMapper.BooleanFieldType("field", true, false, Collections.emptyMap())); } } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/BinaryFieldMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/BinaryFieldMapperTests.java index db1c46f1ac06b..108b2cc719ff0 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/BinaryFieldMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/BinaryFieldMapperTests.java @@ -32,18 +32,28 @@ import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.plugins.Plugin; -import org.elasticsearch.test.ESSingleNodeTestCase; import org.elasticsearch.test.InternalSettingsPlugin; import java.io.IOException; import java.util.Arrays; import java.util.Collection; +import java.util.Set; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.instanceOf; -public class BinaryFieldMapperTests extends ESSingleNodeTestCase { +public class BinaryFieldMapperTests extends FieldMapperTestCase { + + @Override + protected Set unsupportedProperties() { + return Set.of("analyzer", "eager_global_ordinals", "norms", "similarity"); + } + + @Override + protected BinaryFieldMapper.Builder newBuilder() { + return new BinaryFieldMapper.Builder("binary"); + } @Override protected Collection> getPlugins() { @@ -60,10 +70,10 @@ public void testDefaultMapping() throws Exception { .endObject().endObject(); MapperService mapperService = createIndex("test", Settings.EMPTY, mapping).mapperService(); - MappedFieldType fieldType = mapperService.fieldType("field"); + FieldMapper mapper = (FieldMapper) mapperService.documentMapper().mappers().getMapper("field"); - assertThat(fieldType, instanceOf(BinaryFieldMapper.BinaryFieldType.class)); - assertThat(fieldType.stored(), equalTo(false)); + assertThat(mapper, instanceOf(BinaryFieldMapper.class)); + assertThat(mapper.fieldType.stored(), equalTo(false)); } public void testStoredValue() throws IOException { diff --git a/server/src/test/java/org/elasticsearch/index/mapper/BinaryFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/BinaryFieldTypeTests.java index ce36a9af517bc..7f84a3d36f8ae 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/BinaryFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/BinaryFieldTypeTests.java @@ -18,10 +18,12 @@ */ package org.elasticsearch.index.mapper; +import java.util.Map; + public class BinaryFieldTypeTests extends FieldTypeTestCase { @Override - protected MappedFieldType createDefaultFieldType() { - return new BinaryFieldMapper.BinaryFieldType(); + protected MappedFieldType createDefaultFieldType(String name, Map meta) { + return new BinaryFieldMapper.BinaryFieldType(name, true, meta); } } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/BooleanFieldMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/BooleanFieldMapperTests.java index d779cb518d1bf..564be1efdca85 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/BooleanFieldMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/BooleanFieldMapperTests.java @@ -47,6 +47,7 @@ import java.io.IOException; import java.util.Collection; import java.util.Collections; +import java.util.Set; import static org.hamcrest.Matchers.containsString; @@ -54,11 +55,15 @@ public class BooleanFieldMapperTests extends FieldMapperTestCase unsupportedProperties() { + return Set.of("analyzer", "similarity"); + } + @Before public void setup() { indexService = createIndex("test"); parser = indexService.mapperService().documentMapperParser(); - setDummyNullValue(true); } @Override diff --git a/server/src/test/java/org/elasticsearch/index/mapper/BooleanFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/BooleanFieldTypeTests.java index 220dd88c562f2..0eb3b5776e630 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/BooleanFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/BooleanFieldTypeTests.java @@ -18,24 +18,26 @@ */ package org.elasticsearch.index.mapper; -import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.Term; import org.apache.lucene.search.TermQuery; +import java.util.Collections; +import java.util.Map; + public class BooleanFieldTypeTests extends FieldTypeTestCase { @Override - protected MappedFieldType createDefaultFieldType() { - return new BooleanFieldMapper.BooleanFieldType(); + protected MappedFieldType createDefaultFieldType(String name, Map meta) { + return new BooleanFieldMapper.BooleanFieldType(name, true, true, meta); } public void testValueFormat() { - MappedFieldType ft = createDefaultFieldType(); + MappedFieldType ft = new BooleanFieldMapper.BooleanFieldType("field"); assertEquals(false, ft.docValueFormat(null, null).format(0)); assertEquals(true, ft.docValueFormat(null, null).format(1)); } public void testValueForSearch() { - MappedFieldType ft = createDefaultFieldType(); + MappedFieldType ft = new BooleanFieldMapper.BooleanFieldType("field"); assertEquals(true, ft.valueForDisplay("T")); assertEquals(false, ft.valueForDisplay("F")); expectThrows(IllegalArgumentException.class, () -> ft.valueForDisplay(0)); @@ -44,15 +46,13 @@ public void testValueForSearch() { } public void testTermQuery() { - MappedFieldType ft = createDefaultFieldType(); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ft = new BooleanFieldMapper.BooleanFieldType("field"); assertEquals(new TermQuery(new Term("field", "T")), ft.termQuery("true", null)); assertEquals(new TermQuery(new Term("field", "F")), ft.termQuery("false", null)); - ft.setIndexOptions(IndexOptions.NONE); + MappedFieldType unsearchable = new BooleanFieldMapper.BooleanFieldType("field", false, true, Collections.emptyMap()); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> ft.termQuery("true", null)); + () -> unsearchable.termQuery("true", null)); assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage()); } } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/CompletionFieldMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/CompletionFieldMapperTests.java index 425b2fba9bbe6..f2046a80d2067 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/CompletionFieldMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/CompletionFieldMapperTests.java @@ -18,6 +18,7 @@ */ package org.elasticsearch.index.mapper; +import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.SortedSetDocValuesField; import org.apache.lucene.index.IndexableField; import org.apache.lucene.search.Query; @@ -42,6 +43,7 @@ import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.index.IndexService; +import org.elasticsearch.index.analysis.AnalyzerScope; import org.elasticsearch.index.analysis.NamedAnalyzer; import org.elasticsearch.search.suggest.completion.context.ContextBuilder; import org.elasticsearch.search.suggest.completion.context.ContextMappings; @@ -54,6 +56,7 @@ import java.io.IOException; import java.util.Arrays; import java.util.Map; +import java.util.Set; import java.util.function.Function; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; @@ -77,9 +80,17 @@ public void addModifiers() { }); } + @Override + protected Set unsupportedProperties() { + return Set.of("doc_values"); + } + @Override protected CompletionFieldMapper.Builder newBuilder() { - return new CompletionFieldMapper.Builder("completion"); + CompletionFieldMapper.Builder builder = new CompletionFieldMapper.Builder("completion"); + builder.indexAnalyzer(new NamedAnalyzer("standard", AnalyzerScope.INDEX, new StandardAnalyzer())); + builder.searchAnalyzer(new NamedAnalyzer("standard", AnalyzerScope.INDEX, new StandardAnalyzer())); + return builder; } public void testDefaultConfiguration() throws IOException { @@ -965,9 +976,8 @@ private CombinableMatcher sortedSetDocValuesField(String value) .and(Matchers.instanceOf(SortedSetDocValuesField.class)); } - private CombinableMatcher keywordField(String value) { - return Matchers.both(hasProperty(IndexableField::binaryValue, equalTo(new BytesRef(value)))) - .and(hasProperty(IndexableField::fieldType, Matchers.instanceOf(KeywordFieldMapper.KeywordFieldType.class))); + private Matcher keywordField(String value) { + return hasProperty(IndexableField::binaryValue, equalTo(new BytesRef(value))); } private Matcher hasProperty(Function property, Matcher valueMatcher) { diff --git a/server/src/test/java/org/elasticsearch/index/mapper/DateFieldMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/DateFieldMapperTests.java index 2ca1b20a6f317..d10ba525fcd3e 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/DateFieldMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/DateFieldMapperTests.java @@ -42,6 +42,7 @@ import java.util.Collection; import java.util.Collections; import java.util.Locale; +import java.util.Set; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.notNullValue; @@ -64,6 +65,11 @@ public void setup() { }); } + @Override + protected Set unsupportedProperties() { + return Set.of("analyzer", "similarity"); + } + @Override protected Collection> getPlugins() { return pluginList(InternalSettingsPlugin.class); @@ -384,7 +390,7 @@ public void testMergeDate() throws IOException { MapperService.MergeReason.MAPPING_UPDATE); assertThat(indexService.mapperService().fieldType("release_date"), notNullValue()); - assertFalse(indexService.mapperService().fieldType("release_date").stored()); + assertFalse(indexService.mapperService().getLuceneFieldType("release_date").stored()); String updateFormatMapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("movie") .startObject("properties") diff --git a/server/src/test/java/org/elasticsearch/index/mapper/DateFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/DateFieldTypeTests.java index ece0f1f38400c..f983b61257fc9 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/DateFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/DateFieldTypeTests.java @@ -22,7 +22,6 @@ import org.apache.lucene.document.NumericDocValuesField; import org.apache.lucene.document.SortedNumericDocValuesField; import org.apache.lucene.index.DirectoryReader; -import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; @@ -36,7 +35,6 @@ import org.elasticsearch.Version; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.time.DateFormatter; import org.elasticsearch.common.time.DateFormatters; import org.elasticsearch.common.time.DateMathParser; import org.elasticsearch.common.util.BigArrays; @@ -53,67 +51,46 @@ import org.elasticsearch.index.query.QueryRewriteContext; import org.elasticsearch.index.query.QueryShardContext; import org.joda.time.DateTimeZone; -import org.junit.Before; import java.io.IOException; import java.time.Instant; import java.time.ZoneOffset; +import java.util.Collections; +import java.util.Map; public class DateFieldTypeTests extends FieldTypeTestCase { - @Override - protected DateFieldType createDefaultFieldType() { - return new DateFieldType(); - } - @Before - public void addModifiers() { - addModifier(t -> { - DateFieldType copy = (DateFieldType) t.clone(); - if (copy.dateTimeFormatter == DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER) { - copy.setDateTimeFormatter(DateFormatter.forPattern("epoch_millis")); - } else { - copy.setDateTimeFormatter(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER); - } - return copy; - }); - addModifier(t -> { - DateFieldType copy = (DateFieldType) t.clone(); - if (copy.resolution() == Resolution.MILLISECONDS) { - copy.setResolution(Resolution.NANOSECONDS); - } else { - copy.setResolution(Resolution.MILLISECONDS); - } - return copy; - }); - } - private static long nowInMillis; + @Override + protected DateFieldType createDefaultFieldType(String name, Map meta) { + return new DateFieldType(name, true, true, DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER, + Resolution.MILLISECONDS, meta); + } + private static final long nowInMillis = 0; public void testIsFieldWithinRangeEmptyReader() throws IOException { QueryRewriteContext context = new QueryRewriteContext(xContentRegistry(), writableRegistry(), null, () -> nowInMillis); IndexReader reader = new MultiReader(); - DateFieldType ft = new DateFieldType(); - ft.setName("my_date"); + DateFieldType ft = new DateFieldType("my_date"); assertEquals(Relation.DISJOINT, ft.isFieldWithinQuery(reader, "2015-10-12", "2016-04-03", randomBoolean(), randomBoolean(), null, null, context)); assertEquals(Relation.DISJOINT, ft.isFieldWithinRange(reader, instant("2015-10-12"), instant("2016-04-03"))); } public void testIsFieldWithinQueryDateMillis() throws IOException { - DateFieldType ft = new DateFieldType(); - ft.setResolution(Resolution.MILLISECONDS); + DateFieldType ft = new DateFieldType("my_date", true, true, + DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER, Resolution.MILLISECONDS, Collections.emptyMap()); isFieldWithinRangeTestCase(ft); } public void testIsFieldWithinQueryDateNanos() throws IOException { - DateFieldType ft = new DateFieldType(); - ft.setResolution(Resolution.NANOSECONDS); + DateFieldType ft = new DateFieldType("my_date", true, true, + DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER, Resolution.NANOSECONDS, Collections.emptyMap()); isFieldWithinRangeTestCase(ft); } public void isFieldWithinRangeTestCase(DateFieldType ft) throws IOException { - ft.setName("my_date"); Directory dir = newDirectory(); IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(null)); @@ -159,8 +136,7 @@ public void isFieldWithinRangeTestCase(DateFieldType ft) throws IOException { assertEquals(Relation.WITHIN, ft.isFieldWithinRange(reader, instant("2015-10-12"), Instant.ofEpochMilli(Long.MAX_VALUE))); // Fields with no value indexed. - DateFieldType ft2 = new DateFieldType(); - ft2.setName("my_date2"); + DateFieldType ft2 = new DateFieldType("my_date2"); assertEquals(Relation.DISJOINT, ft2.isFieldWithinQuery(reader, "2015-10-09", "2016-01-02", false, false, null, null, context)); assertEquals(Relation.DISJOINT, ft2.isFieldWithinRange(reader, instant("2015-10-09"), instant("2016-01-02"))); @@ -206,7 +182,7 @@ private void doTestIsFieldWithinQuery(DateFieldType ft, DirectoryReader reader, } public void testValueFormat() { - MappedFieldType ft = createDefaultFieldType(); + MappedFieldType ft = new DateFieldType("field"); long instant = DateFormatters.from(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parse("2015-10-12T14:10:55")) .toInstant().toEpochMilli(); @@ -215,7 +191,7 @@ public void testValueFormat() { assertEquals("2015-10-12T15:10:55.000+01:00", ft.docValueFormat(null, ZoneOffset.ofHours(1)).format(instant)); assertEquals("2015", - createDefaultFieldType().docValueFormat("YYYY", ZoneOffset.UTC).format(instant)); + new DateFieldType("field").docValueFormat("YYYY", ZoneOffset.UTC).format(instant)); assertEquals(instant, ft.docValueFormat(null, ZoneOffset.UTC).parseLong("2015-10-12T14:10:55", false, null)); assertEquals(instant + 999, @@ -225,7 +201,7 @@ public void testValueFormat() { } public void testValueForSearch() { - MappedFieldType ft = createDefaultFieldType(); + MappedFieldType ft = new DateFieldType("field"); String date = "2015-10-12T12:09:55.000Z"; long instant = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parseMillis(date); assertEquals(date, ft.valueForDisplay(instant)); @@ -238,19 +214,18 @@ public void testTermQuery() { new IndexSettings(IndexMetadata.builder("foo").settings(indexSettings).build(), indexSettings), BigArrays.NON_RECYCLING_INSTANCE, null, null, null, null, null, xContentRegistry(), writableRegistry(), null, null, () -> nowInMillis, null, null, () -> true, null); - MappedFieldType ft = createDefaultFieldType(); - ft.setName("field"); + MappedFieldType ft = new DateFieldType("field"); String date = "2015-10-12T14:10:55"; long instant = DateFormatters.from(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parse(date)).toInstant().toEpochMilli(); - ft.setIndexOptions(IndexOptions.DOCS); Query expected = new IndexOrDocValuesQuery( LongPoint.newRangeQuery("field", instant, instant + 999), SortedNumericDocValuesField.newSlowRangeQuery("field", instant, instant + 999)); assertEquals(expected, ft.termQuery(date, context)); - ft.setIndexOptions(IndexOptions.NONE); + MappedFieldType unsearchable = new DateFieldType("field", false, true, DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER, + Resolution.MILLISECONDS, Collections.emptyMap()); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> ft.termQuery(date, context)); + () -> unsearchable.termQuery(date, context)); assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage()); } @@ -261,14 +236,12 @@ public void testRangeQuery() throws IOException { new IndexSettings(IndexMetadata.builder("foo").settings(indexSettings).build(), indexSettings), BigArrays.NON_RECYCLING_INSTANCE, null, null, null, null, null, xContentRegistry(), writableRegistry(), null, null, () -> nowInMillis, null, null, () -> true, null); - MappedFieldType ft = createDefaultFieldType(); - ft.setName("field"); + MappedFieldType ft = new DateFieldType("field"); String date1 = "2015-10-12T14:10:55"; String date2 = "2016-04-28T11:33:52"; long instant1 = DateFormatters.from(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parse(date1)).toInstant().toEpochMilli(); long instant2 = DateFormatters.from(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parse(date2)).toInstant().toEpochMilli() + 999; - ft.setIndexOptions(IndexOptions.DOCS); Query expected = new IndexOrDocValuesQuery( LongPoint.newRangeQuery("field", instant1, instant2), SortedNumericDocValuesField.newSlowRangeQuery("field", instant1, instant2)); @@ -284,9 +257,10 @@ BigArrays.NON_RECYCLING_INSTANCE, null, null, null, null, null, xContentRegistry assertEquals(expected, ft.rangeQuery("now", instant2, true, true, null, null, null, context)); - ft.setIndexOptions(IndexOptions.NONE); + MappedFieldType unsearchable = new DateFieldType("field", false, true, DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER, + Resolution.MILLISECONDS, Collections.emptyMap()); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> ft.rangeQuery(date1, date2, true, true, null, null, null, context)); + () -> unsearchable.rangeQuery(date1, date2, true, true, null, null, null, context)); assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage()); } @@ -307,13 +281,11 @@ public void testRangeQueryWithIndexSort() { BigArrays.NON_RECYCLING_INSTANCE, null, null, null, null, null, xContentRegistry(), writableRegistry(), null, null, () -> 0L, null, null, () -> true, null); - MappedFieldType ft = createDefaultFieldType(); - ft.setName("field"); + MappedFieldType ft = new DateFieldType("field"); String date1 = "2015-10-12T14:10:55"; String date2 = "2016-04-28T11:33:52"; long instant1 = DateFormatters.from(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parse(date1)).toInstant().toEpochMilli(); long instant2 = DateFormatters.from(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parse(date2)).toInstant().toEpochMilli() + 999; - ft.setIndexOptions(IndexOptions.DOCS); Query pointQuery = LongPoint.newRangeQuery("field", instant1, instant2); Query dvQuery = SortedNumericDocValuesField.newSlowRangeQuery("field", instant1, instant2); diff --git a/server/src/test/java/org/elasticsearch/index/mapper/DocumentFieldMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/DocumentFieldMapperTests.java index d75967a069e8c..0712e97325738 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/DocumentFieldMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/DocumentFieldMapperTests.java @@ -23,6 +23,7 @@ import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.Tokenizer; import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; +import org.apache.lucene.document.FieldType; import org.apache.lucene.index.Term; import org.apache.lucene.search.DocValuesFieldExistsQuery; import org.apache.lucene.search.Query; @@ -74,8 +75,8 @@ public boolean incrementToken() throws IOException { static class FakeFieldType extends TermBasedFieldType { - FakeFieldType() { - super(); + FakeFieldType(String name) { + super(name, true, true, Collections.emptyMap()); } FakeFieldType(FakeFieldType other) { @@ -107,8 +108,8 @@ static class FakeFieldMapper extends FieldMapper { 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, MultiFields.empty(), CopyTo.empty()); + FakeFieldMapper(FakeFieldType fieldType) { + super(fieldType.name(), new FieldType(), fieldType, SETTINGS, MultiFields.empty(), CopyTo.empty()); } @Override @@ -128,16 +129,14 @@ protected String contentType() { } public void testAnalyzers() throws IOException { - FakeFieldType fieldType1 = new FakeFieldType(); - fieldType1.setName("field1"); + FakeFieldType fieldType1 = new FakeFieldType("field1"); fieldType1.setIndexAnalyzer(new NamedAnalyzer("foo", AnalyzerScope.INDEX, new FakeAnalyzer("index"))); fieldType1.setSearchAnalyzer(new NamedAnalyzer("bar", AnalyzerScope.INDEX, new FakeAnalyzer("search"))); fieldType1.setSearchQuoteAnalyzer(new NamedAnalyzer("baz", AnalyzerScope.INDEX, new FakeAnalyzer("search_quote"))); - FieldMapper fieldMapper1 = new FakeFieldMapper("field1", fieldType1); + FieldMapper fieldMapper1 = new FakeFieldMapper(fieldType1); - FakeFieldType fieldType2 = new FakeFieldType(); - fieldType2.setName("field2"); - FieldMapper fieldMapper2 = new FakeFieldMapper("field2", fieldType2); + FakeFieldType fieldType2 = new FakeFieldType("field2"); + FieldMapper fieldMapper2 = new FakeFieldMapper(fieldType2); Analyzer defaultIndex = new FakeAnalyzer("default_index"); Analyzer defaultSearch = new FakeAnalyzer("default_search"); diff --git a/server/src/test/java/org/elasticsearch/index/mapper/DocumentParserTests.java b/server/src/test/java/org/elasticsearch/index/mapper/DocumentParserTests.java index a64656b03d376..b467032c88d63 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/DocumentParserTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/DocumentParserTests.java @@ -738,7 +738,6 @@ public void testDynamicBigInteger() throws Exception { IndexableField[] fields = doc.rootDoc().getFields("big-integer"); assertEquals(2, fields.length); - assertTrue(fields[0].fieldType() instanceof KeywordFieldMapper.KeywordFieldType); assertEquals(new BytesRef(value.toString()), fields[0].binaryValue()); } @@ -767,7 +766,6 @@ public void testDynamicBigDecimal() throws Exception { IndexableField[] fields = doc.rootDoc().getFields("big-decimal"); assertEquals(2, fields.length); - assertTrue(fields[0].fieldType() instanceof KeywordFieldMapper.KeywordFieldType); assertEquals(new BytesRef(value.toString()), fields[0].binaryValue()); } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/DynamicTemplatesTests.java b/server/src/test/java/org/elasticsearch/index/mapper/DynamicTemplatesTests.java index 76921941a5b96..4b63f5483bb74 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/DynamicTemplatesTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/DynamicTemplatesTests.java @@ -56,10 +56,10 @@ public void testMatchTypeOnly() throws Exception { .setSource(parsedDoc.dynamicMappingsUpdate().toString(), XContentType.JSON).get(); assertThat(mapperService.fieldType("s"), notNullValue()); - assertEquals(IndexOptions.NONE, mapperService.fieldType("s").indexOptions()); + assertFalse(mapperService.fieldType("s").isSearchable()); assertThat(mapperService.fieldType("l"), notNullValue()); - assertNotSame(IndexOptions.NONE, mapperService.fieldType("l").indexOptions()); + assertTrue(mapperService.fieldType("l").isSearchable()); } public void testSimple() throws Exception { diff --git a/server/src/test/java/org/elasticsearch/index/mapper/ExternalMapper.java b/server/src/test/java/org/elasticsearch/index/mapper/ExternalMapper.java index fb730f675fbcf..3787ec4839e35 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/ExternalMapper.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/ExternalMapper.java @@ -19,6 +19,7 @@ package org.elasticsearch.index.mapper; +import org.apache.lucene.document.FieldType; import org.apache.lucene.index.Term; import org.apache.lucene.search.DocValuesFieldExistsQuery; import org.apache.lucene.search.Query; @@ -35,6 +36,7 @@ import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -68,7 +70,7 @@ public static class Builder extends FieldMapper.Builder { private String mapperName; public Builder(String name, String generatedValue, String mapperName) { - super(name, new ExternalFieldType(), new ExternalFieldType()); + super(name, new FieldType()); this.builder = this; this.stringBuilder = new TextFieldMapper.Builder(name).store(false); this.generatedValue = generatedValue; @@ -90,10 +92,13 @@ public ExternalMapper build(BuilderContext context) { FieldMapper stringMapper = (FieldMapper)stringBuilder.build(context); context.path().remove(); - setupFieldType(context); + return new ExternalMapper(name, buildFullName(context), fieldType, generatedValue, mapperName, binMapper, boolMapper, + pointMapper, shapeMapper, stringMapper, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo, true); + } - return new ExternalMapper(name, fieldType, generatedValue, mapperName, binMapper, boolMapper, pointMapper, - shapeMapper, stringMapper, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); + @Override + public Builder index(boolean index) { + throw new UnsupportedOperationException(); } } @@ -118,7 +123,9 @@ public Mapper.Builder parse(String name, Map node, ParserContext static class ExternalFieldType extends TermBasedFieldType { - ExternalFieldType() {} + ExternalFieldType(String name, boolean indexed, boolean hasDocValues) { + super(name, indexed, hasDocValues, Collections.emptyMap()); + } protected ExternalFieldType(ExternalFieldType ref) { super(ref); @@ -153,12 +160,12 @@ public Query existsQuery(QueryShardContext context) { private AbstractShapeGeometryFieldMapper shapeMapper; private FieldMapper stringMapper; - public ExternalMapper(String simpleName, MappedFieldType fieldType, + public ExternalMapper(String simpleName, String contextName, FieldType fieldType, String generatedValue, String mapperName, BinaryFieldMapper binMapper, BooleanFieldMapper boolMapper, GeoPointFieldMapper pointMapper, AbstractShapeGeometryFieldMapper shapeMapper, FieldMapper stringMapper, Settings indexSettings, - MultiFields multiFields, CopyTo copyTo) { - super(simpleName, fieldType, new ExternalFieldType(), indexSettings, multiFields, copyTo); + MultiFields multiFields, CopyTo copyTo, boolean indexed) { + super(simpleName, fieldType, new ExternalFieldType(contextName, indexed, false), indexSettings, multiFields, copyTo); this.generatedValue = generatedValue; this.mapperName = mapperName; this.binMapper = binMapper; diff --git a/server/src/test/java/org/elasticsearch/index/mapper/ExternalMetadataMapper.java b/server/src/test/java/org/elasticsearch/index/mapper/ExternalMetadataMapper.java index 844d824083924..4bdeef6573c6f 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/ExternalMetadataMapper.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/ExternalMetadataMapper.java @@ -20,6 +20,7 @@ package org.elasticsearch.index.mapper; import org.apache.lucene.document.Field.Store; +import org.apache.lucene.document.FieldType; import org.apache.lucene.document.StringField; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -35,14 +36,8 @@ public class ExternalMetadataMapper extends MetadataFieldMapper { static final String FIELD_NAME = "_is_external"; static final String FIELD_VALUE = "true"; - private static MappedFieldType FIELD_TYPE = new BooleanFieldMapper.BooleanFieldType(); - static { - FIELD_TYPE.setName(FIELD_NAME); - FIELD_TYPE.freeze(); - } - protected ExternalMetadataMapper(Settings indexSettings) { - super(FIELD_NAME, FIELD_TYPE, FIELD_TYPE, indexSettings); + super(new FieldType(), new BooleanFieldMapper.BooleanFieldType(FIELD_NAME), indexSettings); } @Override @@ -77,7 +72,7 @@ public void postParse(ParseContext context) throws IOException { public static class Builder extends MetadataFieldMapper.Builder { protected Builder() { - super(FIELD_NAME, FIELD_TYPE, FIELD_TYPE); + super(FIELD_NAME, new FieldType()); } @Override diff --git a/server/src/test/java/org/elasticsearch/index/mapper/FakeStringFieldMapper.java b/server/src/test/java/org/elasticsearch/index/mapper/FakeStringFieldMapper.java index 84a5f54b89082..d1d24e64b5ed7 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/FakeStringFieldMapper.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/FakeStringFieldMapper.java @@ -20,6 +20,7 @@ package org.elasticsearch.index.mapper; import org.apache.lucene.document.Field; +import org.apache.lucene.document.FieldType; import org.apache.lucene.document.SortedSetDocValuesField; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.Term; @@ -27,11 +28,13 @@ import org.apache.lucene.search.Query; import org.apache.lucene.search.TermQuery; import org.apache.lucene.util.BytesRef; +import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.index.query.QueryShardContext; import java.io.IOException; +import java.util.Collections; import java.util.List; import java.util.Map; @@ -42,32 +45,27 @@ public class FakeStringFieldMapper extends FieldMapper { public static final String CONTENT_TYPE = "fake_string"; - public static class Defaults { - - public static final MappedFieldType FIELD_TYPE = new FakeStringFieldType(); - - static { - FIELD_TYPE.freeze(); - } + public static final FieldType FIELD_TYPE = new FieldType(); + static { + FIELD_TYPE.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS); } public static class Builder extends FieldMapper.Builder { public Builder(String name) { - super(name, Defaults.FIELD_TYPE, Defaults.FIELD_TYPE); + super(name, FIELD_TYPE); builder = this; } @Override - public FakeStringFieldType fieldType() { - return (FakeStringFieldType) super.fieldType(); + public Builder index(boolean index) { + throw new UnsupportedOperationException(); } @Override public FakeStringFieldMapper build(BuilderContext context) { - setupFieldType(context); return new FakeStringFieldMapper( - name, fieldType(), defaultFieldType, + fieldType, new FakeStringFieldType(name), context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); } } @@ -88,7 +86,10 @@ public Mapper.Builder parse(String fieldName, Map node, ParserCo public static final class FakeStringFieldType extends StringFieldType { - public FakeStringFieldType() { + public FakeStringFieldType(String name) { + super(name, true, true, Collections.emptyMap()); + setIndexAnalyzer(Lucene.STANDARD_ANALYZER); + setSearchAnalyzer(Lucene.STANDARD_ANALYZER); } protected FakeStringFieldType(FakeStringFieldType ref) { @@ -114,9 +115,9 @@ public Query existsQuery(QueryShardContext context) { } } - protected FakeStringFieldMapper(String simpleName, FakeStringFieldType fieldType, MappedFieldType defaultFieldType, + protected FakeStringFieldMapper(FieldType fieldType, MappedFieldType mappedFieldType, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { - super(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, copyTo); + super(mappedFieldType.name(), fieldType, mappedFieldType, indexSettings, multiFields, copyTo); } @Override @@ -132,8 +133,8 @@ protected void parseCreateField(ParseContext context) throws IOException { return; } - if (fieldType().indexOptions() != IndexOptions.NONE || fieldType().stored()) { - Field field = new Field(fieldType().name(), value, fieldType()); + if (fieldType.indexOptions() != IndexOptions.NONE || fieldType.stored()) { + Field field = new Field(fieldType().name(), value, fieldType); context.doc().add(field); } if (fieldType().hasDocValues()) { @@ -159,7 +160,6 @@ public FakeStringFieldType fieldType() { @Override protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException { super.doXContentBody(builder, includeDefaults, params); - doXContentAnalyzers(builder, includeDefaults); } } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/FieldNamesFieldMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/FieldNamesFieldMapperTests.java index fb83c9bda5dae..5b95e4f635779 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/FieldNamesFieldMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/FieldNamesFieldMapperTests.java @@ -29,8 +29,8 @@ import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.termvectors.TermVectorsService; +import org.elasticsearch.test.ESSingleNodeTestCase; import org.elasticsearch.test.VersionUtils; -import org.junit.Before; import java.util.Arrays; import java.util.Collections; @@ -38,22 +38,7 @@ import java.util.SortedSet; import java.util.TreeSet; -public class FieldNamesFieldMapperTests extends FieldMapperTestCase { - - @Override - protected FieldNamesFieldMapper.Builder newBuilder() { - return new FieldNamesFieldMapper.Builder(null); - } - - @Before - public void addModifiers() { - addBooleanModifier("enabled", true, FieldNamesFieldMapper.Builder::enabled); - } - - @Override - protected boolean supportsDocValues() { - return false; - } +public class FieldNamesFieldMapperTests extends ESSingleNodeTestCase { private static SortedSet extract(String path) { SortedSet set = new TreeSet<>(); @@ -91,10 +76,11 @@ public void testFieldType() throws Exception { .parse("type", new CompressedXContent(mapping)); FieldNamesFieldMapper fieldNamesMapper = docMapper.metadataMapper(FieldNamesFieldMapper.class); assertFalse(fieldNamesMapper.fieldType().hasDocValues()); - assertEquals(IndexOptions.DOCS, fieldNamesMapper.fieldType().indexOptions()); - assertFalse(fieldNamesMapper.fieldType().tokenized()); - assertFalse(fieldNamesMapper.fieldType().stored()); - assertTrue(fieldNamesMapper.fieldType().omitNorms()); + + assertEquals(IndexOptions.DOCS, fieldNamesMapper.fieldType.indexOptions()); + assertFalse(fieldNamesMapper.fieldType.tokenized()); + assertFalse(fieldNamesMapper.fieldType.stored()); + assertTrue(fieldNamesMapper.fieldType.omitNorms()); } public void testInjectIntoDocDuringParsing() throws Exception { @@ -172,13 +158,13 @@ public void testMergingMappingsBefore8() throws Exception { VersionUtils.randomPreviousCompatibleVersion(random(), Version.V_8_0_0)) .build()).mapperService(); - DocumentMapper mapperEnabled = mapperService.merge("type", new CompressedXContent(enabledMapping), - MapperService.MergeReason.MAPPING_UPDATE); + mapperService.merge("type", new CompressedXContent(enabledMapping), MapperService.MergeReason.MAPPING_UPDATE); DocumentMapper mapperDisabled = mapperService.merge("type", new CompressedXContent(disabledMapping), MapperService.MergeReason.MAPPING_UPDATE); assertFalse(mapperDisabled.metadataMapper(FieldNamesFieldMapper.class).fieldType().isEnabled()); - mapperEnabled = mapperService.merge("type", new CompressedXContent(enabledMapping), MapperService.MergeReason.MAPPING_UPDATE); + DocumentMapper mapperEnabled + = mapperService.merge("type", new CompressedXContent(enabledMapping), MapperService.MergeReason.MAPPING_UPDATE); assertTrue(mapperEnabled.metadataMapper(FieldNamesFieldMapper.class).fieldType().isEnabled()); assertWarnings(FieldNamesFieldMapper.TypeParser.ENABLED_DEPRECATION_MESSAGE.replace("{}", "test")); } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/FieldNamesFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/FieldNamesFieldTypeTests.java index 1b5239fdc4bda..d0b71cc2d5c46 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/FieldNamesFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/FieldNamesFieldTypeTests.java @@ -27,24 +27,20 @@ import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.query.QueryShardContext; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.EqualsHashCodeTestUtils; import java.util.Collections; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -public class FieldNamesFieldTypeTests extends FieldTypeTestCase { - @Override - protected MappedFieldType createDefaultFieldType() { - return new FieldNamesFieldMapper.FieldNamesFieldType(); - } +public class FieldNamesFieldTypeTests extends ESTestCase { public void testTermQuery() { FieldNamesFieldMapper.FieldNamesFieldType fieldNamesFieldType = new FieldNamesFieldMapper.FieldNamesFieldType(); - fieldNamesFieldType.setName(FieldNamesFieldMapper.CONTENT_TYPE); - KeywordFieldMapper.KeywordFieldType fieldType = new KeywordFieldMapper.KeywordFieldType(); - fieldType.setName("field_name"); + KeywordFieldMapper.KeywordFieldType fieldType = new KeywordFieldMapper.KeywordFieldType("field_name"); Settings settings = settings(Version.CURRENT).build(); IndexSettings indexSettings = new IndexSettings( @@ -65,4 +61,14 @@ public void testTermQuery() { IllegalStateException e = expectThrows(IllegalStateException.class, () -> fieldNamesFieldType.termQuery("field_name", null)); assertEquals("Cannot run [exists] queries if the [_field_names] field is disabled", e.getMessage()); } + + public void testHashcodeAndEquals() { + EqualsHashCodeTestUtils.checkEqualsAndHashCode(new FieldNamesFieldMapper.FieldNamesFieldType(), + FieldNamesFieldMapper.FieldNamesFieldType::clone, + t -> { + FieldNamesFieldMapper.FieldNamesFieldType m = t.clone(); + m.setEnabled(false); + return m; + }); + } } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/FieldTypeLookupTests.java b/server/src/test/java/org/elasticsearch/index/mapper/FieldTypeLookupTests.java index 6fcf2f70df79e..d789388974332 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/FieldTypeLookupTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/FieldTypeLookupTests.java @@ -19,11 +19,6 @@ package org.elasticsearch.index.mapper; -import org.apache.lucene.index.Term; -import org.apache.lucene.search.DocValuesFieldExistsQuery; -import org.apache.lucene.search.Query; -import org.apache.lucene.search.TermQuery; -import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.test.ESTestCase; import java.util.Arrays; @@ -82,8 +77,8 @@ public void testAddFieldAlias() { public void testUpdateFieldAlias() { // Add an alias 'alias' to the concrete field 'foo'. - MockFieldMapper.FakeFieldType fieldType1 = new MockFieldMapper.FakeFieldType(); - MockFieldMapper field1 = new MockFieldMapper("foo", fieldType1); + MockFieldMapper.FakeFieldType fieldType1 = new MockFieldMapper.FakeFieldType("foo"); + MockFieldMapper field1 = new MockFieldMapper(fieldType1); FieldAliasMapper alias1 = new FieldAliasMapper("alias", "alias", "foo"); FieldTypeLookup lookup = new FieldTypeLookup(); @@ -94,9 +89,8 @@ public void testUpdateFieldAlias() { assertEquals(fieldType1, aliasType1); // Update the alias to refer to a new concrete field 'bar'. - MockFieldMapper.FakeFieldType fieldType2 = new MockFieldMapper.FakeFieldType(); - fieldType2.setStored(!fieldType1.stored()); - MockFieldMapper field2 = new MockFieldMapper("bar", fieldType2); + MockFieldMapper.FakeFieldType fieldType2 = new MockFieldMapper.FakeFieldType("bar"); + MockFieldMapper field2 = new MockFieldMapper(fieldType2); FieldAliasMapper alias2 = new FieldAliasMapper("alias", "alias", "bar"); lookup = lookup.copyAndAddAll(newList(field2), newList(alias2)); @@ -109,9 +103,9 @@ public void testUpdateFieldAlias() { public void testUpdateConcreteFieldWithAlias() { // Add an alias 'alias' to the concrete field 'foo'. FieldAliasMapper alias1 = new FieldAliasMapper("alias", "alias", "foo"); - MockFieldMapper.FakeFieldType fieldType1 = new MockFieldMapper.FakeFieldType(); + MockFieldMapper.FakeFieldType fieldType1 = new MockFieldMapper.FakeFieldType("foo"); fieldType1.setBoost(1.0f); - MockFieldMapper field1 = new MockFieldMapper("foo", fieldType1); + MockFieldMapper field1 = new MockFieldMapper(fieldType1); FieldTypeLookup lookup = new FieldTypeLookup(); lookup = lookup.copyAndAddAll(newList(field1), newList(alias1)); @@ -121,9 +115,9 @@ public void testUpdateConcreteFieldWithAlias() { assertEquals(fieldType1, aliasType1); // Update the boost for field 'foo'. - MockFieldMapper.FakeFieldType fieldType2 = new MockFieldMapper.FakeFieldType(); + MockFieldMapper.FakeFieldType fieldType2 = new MockFieldMapper.FakeFieldType("foo"); fieldType2.setBoost(2.0f); - MockFieldMapper field2 = new MockFieldMapper("foo", fieldType2); + MockFieldMapper field2 = new MockFieldMapper(fieldType2); lookup = lookup.copyAndAddAll(newList(field2), emptyList()); // Check that the alias maps to the new field type. @@ -185,32 +179,4 @@ private int size(Iterator iterator) { } return count; } - - static class OtherFakeFieldType extends TermBasedFieldType { - OtherFakeFieldType() { - } - - protected OtherFakeFieldType(OtherFakeFieldType ref) { - super(ref); - } - - @Override - public MappedFieldType clone() { - return new OtherFakeFieldType(this); - } - - @Override - public String typeName() { - return "otherfaketype"; - } - - @Override - public Query existsQuery(QueryShardContext context) { - if (hasDocValues()) { - return new DocValuesFieldExistsQuery(name()); - } else { - return new TermQuery(new Term(FieldNamesFieldMapper.NAME, name())); - } - } - } } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/GenericStoreDynamicTemplateTests.java b/server/src/test/java/org/elasticsearch/index/mapper/GenericStoreDynamicTemplateTests.java index 0f6332620cd23..51bdf7d0a2a73 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/GenericStoreDynamicTemplateTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/GenericStoreDynamicTemplateTests.java @@ -19,6 +19,7 @@ package org.elasticsearch.index.mapper; +import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexableField; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.xcontent.XContentType; @@ -50,7 +51,7 @@ public void testSimple() throws Exception { assertThat(f.stringValue(), equalTo("some name")); assertThat(f.fieldType().stored(), equalTo(true)); - MappedFieldType fieldType = mapperService.fieldType("name"); + FieldType fieldType = mapperService.getLuceneFieldType("name"); assertThat(fieldType.stored(), equalTo(true)); boolean stored = false; @@ -59,7 +60,7 @@ public void testSimple() throws Exception { } assertTrue(stored); - fieldType = mapperService.fieldType("age"); + fieldType = mapperService.getLuceneFieldType("age"); assertThat(fieldType.stored(), equalTo(true)); } } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/GeoPointFieldMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/GeoPointFieldMapperTests.java index b90999283b6c7..abe4805bce088 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/GeoPointFieldMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/GeoPointFieldMapperTests.java @@ -18,6 +18,7 @@ */ package org.elasticsearch.index.mapper; +import org.apache.lucene.index.IndexableField; import org.apache.lucene.util.BytesRef; import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder; import org.elasticsearch.action.search.SearchResponse; @@ -30,17 +31,17 @@ import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.plugins.Plugin; -import org.elasticsearch.test.ESSingleNodeTestCase; import org.elasticsearch.test.InternalSettingsPlugin; import org.elasticsearch.test.geo.RandomGeoGenerator; import org.hamcrest.CoreMatchers; import java.io.IOException; import java.util.Collection; +import java.util.Set; import static org.elasticsearch.action.support.WriteRequest.RefreshPolicy.IMMEDIATE; -import static org.elasticsearch.geometry.utils.Geohash.stringEncode; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; +import static org.elasticsearch.geometry.utils.Geohash.stringEncode; import static org.elasticsearch.index.mapper.AbstractGeometryFieldMapper.Names.IGNORE_MALFORMED; import static org.elasticsearch.index.mapper.AbstractGeometryFieldMapper.Names.IGNORE_Z_VALUE; import static org.elasticsearch.index.mapper.AbstractPointGeometryFieldMapper.Names.NULL_VALUE; @@ -52,7 +53,12 @@ import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; -public class GeoPointFieldMapperTests extends ESSingleNodeTestCase { +public class GeoPointFieldMapperTests extends FieldMapperTestCase { + + @Override + protected Set unsupportedProperties() { + return Set.of("analyzer", "similarity", "doc_values"); + } @Override protected Collection> getPlugins() { @@ -220,6 +226,7 @@ public void testLatLonInOneValueArray() throws Exception { XContentType.JSON)); // doc values are enabled by default, but in this test we disable them; we should only have 2 points + IndexableField[] fields = doc.rootDoc().getFields("point"); assertThat(doc.rootDoc().getFields("point"), notNullValue()); assertThat(doc.rootDoc().getFields("point").length, equalTo(4)); } @@ -396,7 +403,7 @@ public void testNullValue() throws Exception { Mapper fieldMapper = defaultMapper.mappers().getMapper("location"); assertThat(fieldMapper, instanceOf(GeoPointFieldMapper.class)); - Object nullValue = ((GeoPointFieldMapper) fieldMapper).fieldType().nullValue(); + AbstractPointGeometryFieldMapper.ParsedPoint nullValue = ((GeoPointFieldMapper) fieldMapper).nullValue; assertThat(nullValue, equalTo(new GeoPoint(1, 2))); ParsedDocument doc = defaultMapper.parse(new SourceToParse("test", "1", @@ -449,7 +456,7 @@ public void testNullValueWithIgnoreMalformed() throws Exception { Mapper fieldMapper = defaultMapper.mappers().getMapper("location"); assertThat(fieldMapper, instanceOf(GeoPointFieldMapper.class)); - Object nullValue = ((GeoPointFieldMapper) fieldMapper).fieldType().nullValue(); + AbstractPointGeometryFieldMapper.ParsedPoint nullValue = ((GeoPointFieldMapper) fieldMapper).nullValue; // geo_point [91, 181] should have been normalized to [89, 1] assertThat(nullValue, equalTo(new GeoPoint(89, 1))); } @@ -578,4 +585,9 @@ public void testInvalidGeopointValuesIgnored() throws Exception { .startObject().startObject("location").nullField("lat").nullField("lon").endObject().endObject() ), XContentType.JSON)).rootDoc().getField("location"), nullValue()); } + + @Override + protected GeoPointFieldMapper.Builder newBuilder() { + return new GeoPointFieldMapper.Builder("geo"); + } } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/GeoPointFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/GeoPointFieldTypeTests.java index ec6fc21d191ba..30f361674c7e5 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/GeoPointFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/GeoPointFieldTypeTests.java @@ -20,9 +20,11 @@ import org.elasticsearch.index.mapper.GeoPointFieldMapper.GeoPointFieldType; +import java.util.Map; + public class GeoPointFieldTypeTests extends FieldTypeTestCase { @Override - protected MappedFieldType createDefaultFieldType() { - return new GeoPointFieldType(); + protected MappedFieldType createDefaultFieldType(String name, Map meta) { + return new GeoPointFieldType(name, true, true, meta); } } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/GeoShapeFieldMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/GeoShapeFieldMapperTests.java index c9bf48ab5400d..fafaa1f7b393a 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/GeoShapeFieldMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/GeoShapeFieldMapperTests.java @@ -33,6 +33,7 @@ import java.io.IOException; import java.util.Collection; import java.util.Collections; +import java.util.Set; import static org.elasticsearch.index.mapper.AbstractGeometryFieldMapper.Names.IGNORE_Z_VALUE; import static org.hamcrest.Matchers.containsString; @@ -41,6 +42,11 @@ public class GeoShapeFieldMapperTests extends FieldMapperTestCase { + @Override + protected Set unsupportedProperties() { + return Set.of("analyzer", "similarity", "doc_values", "store"); + } + @Override protected GeoShapeFieldMapper.Builder newBuilder() { return new GeoShapeFieldMapper.Builder("geoshape"); @@ -74,7 +80,7 @@ public void testDefaultConfiguration() throws IOException { GeoShapeFieldMapper geoShapeFieldMapper = (GeoShapeFieldMapper) fieldMapper; assertThat(geoShapeFieldMapper.fieldType().orientation(), equalTo(GeoShapeFieldMapper.Defaults.ORIENTATION.value())); - assertThat(geoShapeFieldMapper.fieldType.hasDocValues(), equalTo(false)); + assertThat(geoShapeFieldMapper.fieldType().hasDocValues(), equalTo(false)); } /** @@ -245,10 +251,6 @@ public void testGeoShapeMapperMerge() throws Exception { MapperService mapperService = createIndex("test").mapperService(); DocumentMapper docMapper = mapperService.merge("type", new CompressedXContent(stage1Mapping), MapperService.MergeReason.MAPPING_UPDATE); - String stage2Mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type") - .startObject("properties").startObject("shape").field("type", "geo_shape") - .field("orientation", "cw").endObject().endObject().endObject().endObject()); - mapperService.merge("type", new CompressedXContent(stage2Mapping), MapperService.MergeReason.MAPPING_UPDATE); // verify nothing changed Mapper fieldMapper = docMapper.mappers().getMapper("shape"); @@ -258,7 +260,7 @@ public void testGeoShapeMapperMerge() throws Exception { assertThat(geoShapeFieldMapper.fieldType().orientation(), equalTo(ShapeBuilder.Orientation.CCW)); // change mapping; orientation - stage2Mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type") + String stage2Mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type") .startObject("properties").startObject("shape").field("type", "geo_shape") .field("orientation", "cw").endObject().endObject().endObject().endObject()); docMapper = mapperService.merge("type", new CompressedXContent(stage2Mapping), MapperService.MergeReason.MAPPING_UPDATE); diff --git a/server/src/test/java/org/elasticsearch/index/mapper/IdFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/IdFieldTypeTests.java index 42dfd164f2eac..738e7acfdf6c6 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/IdFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/IdFieldTypeTests.java @@ -26,17 +26,13 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.query.QueryShardContext; +import org.elasticsearch.test.ESTestCase; import org.mockito.Mockito; -public class IdFieldTypeTests extends FieldTypeTestCase { - @Override - protected MappedFieldType createDefaultFieldType() { - return new IdFieldMapper.IdFieldType(); - } +public class IdFieldTypeTests extends ESTestCase { public void testRangeQuery() { - MappedFieldType ft = createDefaultFieldType(); - ft.setName("_id"); + MappedFieldType ft = IdFieldMapper.IdFieldType.INSTANCE; IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> ft.rangeQuery(null, null, randomBoolean(), randomBoolean(), null, null, null, null)); assertEquals("Field [_id] of type [_id] does not support range queries", e.getMessage()); @@ -57,12 +53,8 @@ public void testTermsQuery() throws Exception { MapperService mapperService = Mockito.mock(MapperService.class); Mockito.when(context.getMapperService()).thenReturn(mapperService); - MappedFieldType ft = IdFieldMapper.defaultFieldType(mockSettings); - ft.setName(IdFieldMapper.NAME); + MappedFieldType ft = IdFieldMapper.IdFieldType.INSTANCE; Query query = ft.termQuery("id", context); assertEquals(new TermInSetQuery("_id", Uid.encodeId("id")), query); - - query = ft.termQuery("id", context); - assertEquals(new TermInSetQuery("_id", Uid.encodeId("id")), query); } } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/IgnoredFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/IgnoredFieldTypeTests.java index 4f103d628383c..f321c25de070a 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/IgnoredFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/IgnoredFieldTypeTests.java @@ -19,7 +19,6 @@ package org.elasticsearch.index.mapper; -import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.Term; import org.apache.lucene.search.PrefixQuery; import org.apache.lucene.search.Query; @@ -27,20 +26,27 @@ import org.apache.lucene.search.WildcardQuery; import org.apache.lucene.util.BytesRef; import org.elasticsearch.ElasticsearchException; +import org.elasticsearch.test.EqualsHashCodeTestUtils; + +import java.util.Map; public class IgnoredFieldTypeTests extends FieldTypeTestCase { @Override - protected MappedFieldType createDefaultFieldType() { - return new IgnoredFieldMapper.IgnoredFieldType(); + public void testEquals() { + EqualsHashCodeTestUtils.checkEqualsAndHashCode(IgnoredFieldMapper.IgnoredFieldType.INSTANCE, + IgnoredFieldMapper.IgnoredFieldType::clone); + } + + @Override + protected MappedFieldType createDefaultFieldType(String name, Map meta) { + return IgnoredFieldMapper.IgnoredFieldType.INSTANCE; } public void testPrefixQuery() { - MappedFieldType ft = createDefaultFieldType(); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ft = IgnoredFieldMapper.IgnoredFieldType.INSTANCE; - Query expected = new PrefixQuery(new Term("field", new BytesRef("foo*"))); + Query expected = new PrefixQuery(new Term("_ignored", new BytesRef("foo*"))); assertEquals(expected, ft.prefixQuery("foo*", null, MOCK_QSC)); ElasticsearchException ee = expectThrows(ElasticsearchException.class, @@ -50,11 +56,9 @@ public void testPrefixQuery() { } public void testRegexpQuery() { - MappedFieldType ft = createDefaultFieldType(); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ft = IgnoredFieldMapper.IgnoredFieldType.INSTANCE; - Query expected = new RegexpQuery(new Term("field", new BytesRef("foo?"))); + Query expected = new RegexpQuery(new Term("_ignored", new BytesRef("foo?"))); assertEquals(expected, ft.regexpQuery("foo?", 0, 10, null, MOCK_QSC)); ElasticsearchException ee = expectThrows(ElasticsearchException.class, @@ -64,11 +68,9 @@ public void testRegexpQuery() { } public void testWildcardQuery() { - MappedFieldType ft = createDefaultFieldType(); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ft = IgnoredFieldMapper.IgnoredFieldType.INSTANCE; - Query expected = new WildcardQuery(new Term("field", new BytesRef("foo*"))); + Query expected = new WildcardQuery(new Term("_ignored", new BytesRef("foo*"))); assertEquals(expected, ft.wildcardQuery("foo*", null, MOCK_QSC)); ElasticsearchException ee = expectThrows(ElasticsearchException.class, diff --git a/server/src/test/java/org/elasticsearch/index/mapper/IndexFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/IndexFieldTypeTests.java index 44f280c8c3bd7..fb592b1d4d7da 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/IndexFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/IndexFieldTypeTests.java @@ -18,7 +18,6 @@ */ package org.elasticsearch.index.mapper; -import org.apache.lucene.index.IndexOptions; import org.apache.lucene.search.MatchAllDocsQuery; import org.apache.lucene.search.MatchNoDocsQuery; import org.elasticsearch.Version; @@ -28,40 +27,36 @@ import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.index.query.QueryShardException; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.test.EqualsHashCodeTestUtils; import java.util.function.Predicate; import static org.hamcrest.Matchers.containsString; -public class IndexFieldTypeTests extends FieldTypeTestCase { +public class IndexFieldTypeTests extends ESTestCase { - @Override - protected MappedFieldType createDefaultFieldType() { - return new IndexFieldMapper.IndexFieldType(); + public void testEqualsHashCode() { + EqualsHashCodeTestUtils.checkEqualsAndHashCode(IndexFieldMapper.IndexFieldType.INSTANCE, + IndexFieldMapper.IndexFieldType::new); } public void testPrefixQuery() { - MappedFieldType ft = createDefaultFieldType(); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ft = IndexFieldMapper.IndexFieldType.INSTANCE; assertEquals(new MatchAllDocsQuery(), ft.prefixQuery("ind", null, createContext())); assertEquals(new MatchNoDocsQuery(), ft.prefixQuery("other_ind", null, createContext())); } public void testWildcardQuery() { - MappedFieldType ft = createDefaultFieldType(); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ft = IndexFieldMapper.IndexFieldType.INSTANCE; assertEquals(new MatchAllDocsQuery(), ft.wildcardQuery("ind*x", null, createContext())); assertEquals(new MatchNoDocsQuery(), ft.wildcardQuery("other_ind*x", null, createContext())); } public void testRegexpQuery() { - MappedFieldType ft = createDefaultFieldType(); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ft = IndexFieldMapper.IndexFieldType.INSTANCE; QueryShardException e = expectThrows(QueryShardException.class, () -> assertEquals(new MatchAllDocsQuery(), ft.regexpQuery("ind.x", 0, 10, null, createContext()))); diff --git a/server/src/test/java/org/elasticsearch/index/mapper/IpFieldMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/IpFieldMapperTests.java index 37ea96e3df976..07ed508a624a1 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/IpFieldMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/IpFieldMapperTests.java @@ -22,6 +22,9 @@ import org.apache.lucene.document.InetAddressPoint; import org.apache.lucene.index.DocValuesType; import org.apache.lucene.index.IndexableField; +import org.apache.lucene.index.Term; +import org.apache.lucene.search.Query; +import org.apache.lucene.search.TermQuery; import org.apache.lucene.util.BytesRef; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; @@ -34,17 +37,17 @@ import org.elasticsearch.index.IndexService; import org.elasticsearch.index.termvectors.TermVectorsService; import org.elasticsearch.plugins.Plugin; -import org.elasticsearch.test.ESSingleNodeTestCase; import org.elasticsearch.test.InternalSettingsPlugin; import org.junit.Before; import java.io.IOException; import java.net.InetAddress; import java.util.Collection; +import java.util.Set; import static org.hamcrest.Matchers.containsString; -public class IpFieldMapperTests extends ESSingleNodeTestCase { +public class IpFieldMapperTests extends FieldMapperTestCase { IndexService indexService; DocumentMapperParser parser; @@ -53,6 +56,14 @@ public class IpFieldMapperTests extends ESSingleNodeTestCase { public void setup() { indexService = createIndex("test"); parser = indexService.mapperService().documentMapperParser(); + addModifier("null_value", false, (a, b) -> { + a.nullValue(InetAddresses.forString("::1")); + }); + } + + @Override + protected Set unsupportedProperties() { + return Set.of("analyzer", "similarity"); } @Override @@ -132,6 +143,14 @@ public void testNoDocValues() throws Exception { IndexableField pointField = fields[0]; assertEquals(1, pointField.fieldType().pointIndexDimensionCount()); assertEquals(new BytesRef(InetAddressPoint.encode(InetAddresses.forString("::1"))), pointField.binaryValue()); + + fields = doc.rootDoc().getFields(FieldNamesFieldMapper.NAME); + assertEquals(1, fields.length); + assertEquals("field", fields[0].stringValue()); + + FieldMapper m = (FieldMapper) mapper.mappers().getMapper("field"); + Query existsQuery = m.fieldType().existsQuery(null); + assertEquals(new TermQuery(new Term(FieldNamesFieldMapper.NAME, "field")), existsQuery); } public void testStore() throws Exception { @@ -264,7 +283,6 @@ public void testSerializeDefaults() throws Exception { // it would be nice to check the entire serialized default mapper, but there are // a whole lot of bogus settings right now it picks up from calling super.doXContentBody... - assertTrue(got, got.contains("\"null_value\":null")); assertTrue(got, got.contains("\"ignore_malformed\":false")); } @@ -278,4 +296,9 @@ public void testEmptyName() throws IOException { ); assertThat(e.getMessage(), containsString("name cannot be empty string")); } + + @Override + protected IpFieldMapper.Builder newBuilder() { + return new IpFieldMapper.Builder("ip"); + } } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/IpFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/IpFieldTypeTests.java index 9c5af237b13b0..fd7bc36e73fde 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/IpFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/IpFieldTypeTests.java @@ -19,7 +19,6 @@ package org.elasticsearch.index.mapper; import org.apache.lucene.document.InetAddressPoint; -import org.apache.lucene.index.IndexOptions; import org.apache.lucene.search.BooleanClause.Occur; import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.ConstantScoreQuery; @@ -29,15 +28,17 @@ import java.net.InetAddress; import java.util.Arrays; +import java.util.Collections; +import java.util.Map; public class IpFieldTypeTests extends FieldTypeTestCase { @Override - protected MappedFieldType createDefaultFieldType() { - return new IpFieldMapper.IpFieldType(); + protected MappedFieldType createDefaultFieldType(String name, Map meta) { + return new IpFieldMapper.IpFieldType(name, true, true, meta); } public void testValueFormat() throws Exception { - MappedFieldType ft = createDefaultFieldType(); + MappedFieldType ft = new IpFieldMapper.IpFieldType("field"); String ip = "2001:db8::2:1"; BytesRef asBytes = new BytesRef(InetAddressPoint.encode(InetAddress.getByName(ip))); assertEquals(ip, ft.docValueFormat(null, null).format(asBytes)); @@ -47,8 +48,8 @@ public void testValueFormat() throws Exception { assertEquals(ip, ft.docValueFormat(null, null).format(asBytes)); } - public void testValueForSearch() throws Exception { - MappedFieldType ft = createDefaultFieldType(); + public void testValueForSearch() { + MappedFieldType ft = new IpFieldMapper.IpFieldType("field"); String ip = "2001:db8::2:1"; BytesRef asBytes = new BytesRef(InetAddressPoint.encode(InetAddresses.forString(ip))); assertEquals(ip, ft.valueForDisplay(asBytes)); @@ -59,8 +60,7 @@ public void testValueForSearch() throws Exception { } public void testTermQuery() { - MappedFieldType ft = createDefaultFieldType(); - ft.setName("field"); + MappedFieldType ft = new IpFieldMapper.IpFieldType("field"); String ip = "2001:db8::2:1"; assertEquals(InetAddressPoint.newExactQuery("field", InetAddresses.forString(ip)), ft.termQuery(ip, null)); @@ -76,15 +76,14 @@ public void testTermQuery() { prefix = ip + "/16"; assertEquals(InetAddressPoint.newPrefixQuery("field", InetAddresses.forString(ip), 16), ft.termQuery(prefix, null)); - ft.setIndexOptions(IndexOptions.NONE); + MappedFieldType unsearchable = new IpFieldMapper.IpFieldType("field", false, true, Collections.emptyMap()); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> ft.termQuery("::1", null)); + () -> unsearchable.termQuery("::1", null)); assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage()); } public void testTermsQuery() { - MappedFieldType ft = createDefaultFieldType(); - ft.setName("field"); + MappedFieldType ft = new IpFieldMapper.IpFieldType("field"); assertEquals(InetAddressPoint.newSetQuery("field", InetAddresses.forString("::2"), InetAddresses.forString("::5")), ft.termsQuery(Arrays.asList(InetAddresses.forString("::2"), InetAddresses.forString("::5")), null)); @@ -99,8 +98,7 @@ public void testTermsQuery() { } public void testRangeQuery() { - MappedFieldType ft = createDefaultFieldType(); - ft.setName("field"); + MappedFieldType ft = new IpFieldMapper.IpFieldType("field"); assertEquals( InetAddressPoint.newRangeQuery("field", @@ -181,9 +179,9 @@ public void testRangeQuery() { InetAddresses.forString("2001:db8::")), ft.rangeQuery("::ffff:c0a8:107", "2001:db8::", true, true, null, null, null, null)); - ft.setIndexOptions(IndexOptions.NONE); + MappedFieldType unsearchable = new IpFieldMapper.IpFieldType("field", false, true, Collections.emptyMap()); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> ft.rangeQuery("::1", "2001::", true, true, null, null, null, null)); + () -> unsearchable.rangeQuery("::1", "2001::", true, true, null, null, null, null)); assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage()); } } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/JavaMultiFieldMergeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/JavaMultiFieldMergeTests.java index 631b91700df6c..49d4504ad2c82 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/JavaMultiFieldMergeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/JavaMultiFieldMergeTests.java @@ -40,7 +40,7 @@ public void testMergeMultiField() throws Exception { mapperService.merge("person", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE); - assertNotSame(IndexOptions.NONE, mapperService.fieldType("name").indexOptions()); + assertNotSame(IndexOptions.NONE, mapperService.getLuceneFieldType("name").indexOptions()); assertThat(mapperService.fieldType("name.indexed"), nullValue()); BytesReference json = BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field("name", "some name").endObject()); @@ -54,7 +54,7 @@ public void testMergeMultiField() throws Exception { mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/multifield/merge/test-mapping2.json"); mapperService.merge("person", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE); - assertNotSame(IndexOptions.NONE, mapperService.fieldType("name").indexOptions()); + assertNotSame(IndexOptions.NONE, mapperService.getLuceneFieldType("name").indexOptions()); assertThat(mapperService.fieldType("name.indexed"), notNullValue()); assertThat(mapperService.fieldType("name.not_indexed"), notNullValue()); @@ -70,7 +70,7 @@ public void testMergeMultiField() throws Exception { mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/multifield/merge/test-mapping3.json"); mapperService.merge("person", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE); - assertNotSame(IndexOptions.NONE, mapperService.fieldType("name").indexOptions()); + assertNotSame(IndexOptions.NONE, mapperService.getLuceneFieldType("name").indexOptions()); assertThat(mapperService.fieldType("name.indexed"), notNullValue()); assertThat(mapperService.fieldType("name.not_indexed"), notNullValue()); @@ -80,7 +80,7 @@ public void testMergeMultiField() throws Exception { mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/multifield/merge/test-mapping4.json"); mapperService.merge("person", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE); - assertNotSame(IndexOptions.NONE, mapperService.fieldType("name").indexOptions()); + assertNotSame(IndexOptions.NONE, mapperService.getLuceneFieldType("name").indexOptions()); assertThat(mapperService.fieldType("name.indexed"), notNullValue()); assertThat(mapperService.fieldType("name.not_indexed"), notNullValue()); @@ -94,7 +94,7 @@ public void testUpgradeFromMultiFieldTypeToMultiFields() throws Exception { mapperService.merge("person", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE); - assertNotSame(IndexOptions.NONE, mapperService.fieldType("name").indexOptions()); + assertNotSame(IndexOptions.NONE, mapperService.getLuceneFieldType("name").indexOptions()); assertThat(mapperService.fieldType("name.indexed"), nullValue()); BytesReference json = BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field("name", "some name").endObject()); @@ -109,7 +109,7 @@ public void testUpgradeFromMultiFieldTypeToMultiFields() throws Exception { mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/multifield/merge/upgrade1.json"); mapperService.merge("person", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE); - assertNotSame(IndexOptions.NONE, mapperService.fieldType("name").indexOptions()); + assertNotSame(IndexOptions.NONE, mapperService.getLuceneFieldType("name").indexOptions()); assertThat(mapperService.fieldType("name.indexed"), notNullValue()); assertThat(mapperService.fieldType("name.not_indexed"), notNullValue()); @@ -126,7 +126,7 @@ public void testUpgradeFromMultiFieldTypeToMultiFields() throws Exception { mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/multifield/merge/upgrade2.json"); mapperService.merge("person", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE); - assertNotSame(IndexOptions.NONE, mapperService.fieldType("name").indexOptions()); + assertNotSame(IndexOptions.NONE, mapperService.getLuceneFieldType("name").indexOptions()); assertThat(mapperService.fieldType("name.indexed"), notNullValue()); assertThat(mapperService.fieldType("name.not_indexed"), notNullValue()); @@ -144,7 +144,7 @@ public void testUpgradeFromMultiFieldTypeToMultiFields() throws Exception { } // There are conflicts, so the `name.not_indexed3` has not been added - assertNotSame(IndexOptions.NONE, mapperService.fieldType("name").indexOptions()); + assertNotSame(IndexOptions.NONE, mapperService.getLuceneFieldType("name").indexOptions()); assertThat(mapperService.fieldType("name.indexed"), notNullValue()); assertThat(mapperService.fieldType("name.not_indexed"), notNullValue()); assertThat(mapperService.fieldType("name.not_indexed2"), notNullValue()); diff --git a/server/src/test/java/org/elasticsearch/index/mapper/KeywordFieldMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/KeywordFieldMapperTests.java index 8215a1f101fe2..b0dc6d575215d 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/KeywordFieldMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/KeywordFieldMapperTests.java @@ -49,6 +49,7 @@ import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.Set; import static java.util.Collections.singletonList; import static java.util.Collections.singletonMap; @@ -64,6 +65,11 @@ protected KeywordFieldMapper.Builder newBuilder() { return new KeywordFieldMapper.Builder("keyword"); } + @Override + protected Set unsupportedProperties() { + return Set.of("analyzer"); + } + /** * Creates a copy of the lowercase token filter which we use for testing merge errors. */ @@ -86,21 +92,32 @@ protected Collection> getPlugins() { return pluginList(InternalSettingsPlugin.class, MockAnalysisPlugin.class); } + @Override + protected Settings getIndexMapperSettings() { + return mapperSettings; + } + + private static final Settings mapperSettings = Settings.builder() + .put("index.analysis.normalizer.my_lowercase.type", "custom") + .putList("index.analysis.normalizer.my_lowercase.filter", "lowercase") + .put("index.analysis.normalizer.my_other_lowercase.type", "custom") + .putList("index.analysis.normalizer.my_other_lowercase.filter", "lowercase").build(); + IndexService indexService; DocumentMapperParser parser; @Before public void setup() { - indexService = createIndex("test", Settings.builder() - .put("index.analysis.normalizer.my_lowercase.type", "custom") - .putList("index.analysis.normalizer.my_lowercase.filter", "lowercase") - .put("index.analysis.normalizer.my_other_lowercase.type", "custom") - .putList("index.analysis.normalizer.my_other_lowercase.filter", "mock_other_lowercase").build()); + indexService = createIndex("test", mapperSettings); parser = indexService.mapperService().documentMapperParser(); addModifier("normalizer", false, (a, b) -> { a.normalizer(indexService.getIndexAnalyzers(), "my_lowercase"); }); addBooleanModifier("split_queries_on_whitespace", true, KeywordFieldMapper.Builder::splitQueriesOnWhitespace); + addModifier("index_options", false, (a, b) -> { + a.indexOptions(IndexOptions.DOCS); + b.indexOptions(IndexOptions.DOCS_AND_FREQS); + }); } public void testDefaults() throws Exception { diff --git a/server/src/test/java/org/elasticsearch/index/mapper/KeywordFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/KeywordFieldTypeTests.java index 47c9c9d0de52c..a75a7a100129f 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/KeywordFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/KeywordFieldTypeTests.java @@ -25,8 +25,8 @@ import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.Tokenizer; import org.apache.lucene.analysis.core.KeywordAnalyzer; +import org.apache.lucene.analysis.core.WhitespaceAnalyzer; import org.apache.lucene.analysis.core.WhitespaceTokenizer; -import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.Term; import org.apache.lucene.search.DocValuesFieldExistsQuery; import org.apache.lucene.search.FuzzyQuery; @@ -49,7 +49,9 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; +import java.util.Map; public class KeywordFieldTypeTests extends FieldTypeTestCase { @@ -58,27 +60,31 @@ public void addModifiers() { addModifier(t -> { KeywordFieldType copy = t.clone(); if (copy.normalizer() == null) { - copy.setNormalizer(new NamedAnalyzer("keyword", AnalyzerScope.INDEX, new KeywordAnalyzer())); + copy.setSearchAnalyzer(new NamedAnalyzer("keyword", AnalyzerScope.INDEX, new KeywordAnalyzer())); } else { - copy.setNormalizer(null); + copy.setSearchAnalyzer(null); } return copy; }); addModifier(t -> { KeywordFieldType copy = t.clone(); - copy.setSplitQueriesOnWhitespace(t.splitQueriesOnWhitespace() == false); + if (copy.searchAnalyzer() == Lucene.KEYWORD_ANALYZER) { + copy.setSearchAnalyzer(new NamedAnalyzer("whitespace", AnalyzerScope.INDEX, new WhitespaceAnalyzer())); + } else { + copy.setSearchAnalyzer(Lucene.KEYWORD_ANALYZER); + } return copy; }); } @Override - protected KeywordFieldType createDefaultFieldType() { - return new KeywordFieldMapper.KeywordFieldType(); + protected KeywordFieldType createDefaultFieldType(String name, Map meta) { + return new KeywordFieldMapper.KeywordFieldType(name, true, true, meta); } public void testIsFieldWithinQuery() throws IOException { - KeywordFieldType ft = new KeywordFieldType(); - // current impl ignores args and shourd always return INTERSECTS + KeywordFieldType ft = new KeywordFieldType("field"); + // current impl ignores args and should always return INTERSECTS assertEquals(Relation.INTERSECTS, ft.isFieldWithinQuery(null, RandomStrings.randomAsciiOfLengthBetween(random(), 0, 5), RandomStrings.randomAsciiOfLengthBetween(random(), 0, 5), @@ -86,21 +92,17 @@ public void testIsFieldWithinQuery() throws IOException { } public void testTermQuery() { - MappedFieldType ft = createDefaultFieldType(); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ft = new KeywordFieldType("field"); assertEquals(new TermQuery(new Term("field", "foo")), ft.termQuery("foo", null)); - ft.setIndexOptions(IndexOptions.NONE); + MappedFieldType unsearchable = new KeywordFieldType("field", false, true, Collections.emptyMap()); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> ft.termQuery("bar", null)); + () -> unsearchable.termQuery("bar", null)); assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage()); } public void testTermQueryWithNormalizer() { - MappedFieldType ft = createDefaultFieldType(); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ft = new KeywordFieldType("field"); Analyzer normalizer = new Analyzer() { @Override protected TokenStreamComponents createComponents(String fieldName) { @@ -115,50 +117,38 @@ protected TokenStream normalize(String fieldName, TokenStream in) { }; ft.setSearchAnalyzer(new NamedAnalyzer("my_normalizer", AnalyzerScope.INDEX, normalizer)); assertEquals(new TermQuery(new Term("field", "foo bar")), ft.termQuery("fOo BaR", null)); - - ft.setIndexOptions(IndexOptions.NONE); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> ft.termQuery("bar", null)); - assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage()); } public void testTermsQuery() { - MappedFieldType ft = createDefaultFieldType(); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ft = new KeywordFieldType("field"); List terms = new ArrayList<>(); terms.add(new BytesRef("foo")); terms.add(new BytesRef("bar")); assertEquals(new TermInSetQuery("field", terms), ft.termsQuery(Arrays.asList("foo", "bar"), null)); - ft.setIndexOptions(IndexOptions.NONE); + MappedFieldType unsearchable = new KeywordFieldType("field", false, true, Collections.emptyMap()); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> ft.termsQuery(Arrays.asList("foo", "bar"), null)); + () -> unsearchable.termsQuery(Arrays.asList("foo", "bar"), null)); assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage()); } public void testExistsQuery() { - MappedFieldType ft = createDefaultFieldType(); - ft.setName("field"); - - ft.setHasDocValues(true); - ft.setOmitNorms(true); + KeywordFieldType ft = new KeywordFieldType("field"); + ft.hasNorms = false; assertEquals(new DocValuesFieldExistsQuery("field"), ft.existsQuery(null)); - ft.setHasDocValues(false); - ft.setOmitNorms(false); + ft = new KeywordFieldType("field", true, false, Collections.emptyMap()); + ft.hasNorms = true; assertEquals(new NormsFieldExistsQuery("field"), ft.existsQuery(null)); - ft.setHasDocValues(false); - ft.setOmitNorms(true); + ft = new KeywordFieldType("field", true, false, Collections.emptyMap()); + ft.hasNorms = false; assertEquals(new TermQuery(new Term(FieldNamesFieldMapper.NAME, "field")), ft.existsQuery(null)); } public void testRangeQuery() { - MappedFieldType ft = createDefaultFieldType(); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ft = new KeywordFieldType("field"); assertEquals(new TermRangeQuery("field", BytesRefs.toBytesRef("foo"), BytesRefs.toBytesRef("bar"), true, false), ft.rangeQuery("foo", "bar", true, false, null, null, null, MOCK_QSC)); @@ -169,15 +159,13 @@ public void testRangeQuery() { } public void testRegexpQuery() { - MappedFieldType ft = createDefaultFieldType(); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ft = new KeywordFieldType("field"); assertEquals(new RegexpQuery(new Term("field","foo.*")), ft.regexpQuery("foo.*", 0, 10, null, MOCK_QSC)); - ft.setIndexOptions(IndexOptions.NONE); + MappedFieldType unsearchable = new KeywordFieldType("field", false, true, Collections.emptyMap()); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> ft.regexpQuery("foo.*", 0, 10, null, MOCK_QSC)); + () -> unsearchable.regexpQuery("foo.*", 0, 10, null, MOCK_QSC)); assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage()); ElasticsearchException ee = expectThrows(ElasticsearchException.class, @@ -187,15 +175,13 @@ public void testRegexpQuery() { } public void testFuzzyQuery() { - MappedFieldType ft = createDefaultFieldType(); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ft = new KeywordFieldType("field"); assertEquals(new FuzzyQuery(new Term("field","foo"), 2, 1, 50, true), ft.fuzzyQuery("foo", Fuzziness.fromEdits(2), 1, 50, true, MOCK_QSC)); - ft.setIndexOptions(IndexOptions.NONE); + MappedFieldType unsearchable = new KeywordFieldType("field", false, true, Collections.emptyMap()); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> ft.fuzzyQuery("foo", Fuzziness.fromEdits(2), 1, 50, true, MOCK_QSC)); + () -> unsearchable.fuzzyQuery("foo", Fuzziness.fromEdits(2), 1, 50, true, MOCK_QSC)); assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage()); ElasticsearchException ee = expectThrows(ElasticsearchException.class, @@ -206,8 +192,7 @@ public void testFuzzyQuery() { } public void testNormalizeQueries() { - MappedFieldType ft = createDefaultFieldType(); - ft.setName("field"); + MappedFieldType ft = new KeywordFieldType("field"); ft.setSearchAnalyzer(Lucene.KEYWORD_ANALYZER); assertEquals(new TermQuery(new Term("field", new BytesRef("FOO"))), ft.termQuery("FOO", null)); ft.setSearchAnalyzer(Lucene.STANDARD_ANALYZER); diff --git a/server/src/test/java/org/elasticsearch/index/mapper/LegacyGeoShapeFieldMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/LegacyGeoShapeFieldMapperTests.java index 7e75070f5a0e4..e75d19fa15c56 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/LegacyGeoShapeFieldMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/LegacyGeoShapeFieldMapperTests.java @@ -44,6 +44,7 @@ import java.io.IOException; import java.util.Collection; import java.util.Collections; +import java.util.Set; import static org.elasticsearch.index.mapper.AbstractGeometryFieldMapper.Names.IGNORE_Z_VALUE; import static org.hamcrest.Matchers.containsString; @@ -60,31 +61,36 @@ protected LegacyGeoShapeFieldMapper.Builder newBuilder() { return new LegacyGeoShapeFieldMapper.Builder("geoshape"); } + @Override + protected Set unsupportedProperties() { + return Set.of("analyzer", "similarity", "doc_values", "store"); + } + @Before public void addModifiers() { addModifier("tree", false, (a, b) -> { - a.fieldType().setTree("geohash"); - b.fieldType().setTree("quadtree"); + a.deprecatedParameters.tree = "geohash"; + b.deprecatedParameters.tree = "quadtree"; }); addModifier("strategy", false, (a, b) -> { - a.fieldType().setStrategy(SpatialStrategy.TERM); - b.fieldType().setStrategy(SpatialStrategy.RECURSIVE); + a.deprecatedParameters.strategy = SpatialStrategy.TERM; + b.deprecatedParameters.strategy = SpatialStrategy.RECURSIVE; }); addModifier("tree_levels", false, (a, b) -> { - a.fieldType().setTreeLevels(2); - b.fieldType().setTreeLevels(3); + a.deprecatedParameters.treeLevels = 2; + b.deprecatedParameters.treeLevels = 3; }); addModifier("precision", false, (a, b) -> { - a.fieldType().setPrecisionInMeters(10); - b.fieldType().setPrecisionInMeters(20); + a.deprecatedParameters.precision = "10"; + b.deprecatedParameters.precision = "20"; }); addModifier("distance_error_pct", true, (a, b) -> { - a.fieldType().setDistanceErrorPct(0.5); - b.fieldType().setDistanceErrorPct(0.6); + a.deprecatedParameters.distanceErrorPct = 0.5; + b.deprecatedParameters.distanceErrorPct = 0.6; }); addModifier("orientation", true, (a, b) -> { - a.fieldType().setOrientation(ShapeBuilder.Orientation.RIGHT); - b.fieldType().setOrientation(ShapeBuilder.Orientation.LEFT); + a.orientation = ShapeBuilder.Orientation.RIGHT; + b.orientation = ShapeBuilder.Orientation.LEFT; }); } @@ -548,6 +554,11 @@ public void testGeoShapeMapperMerge() throws Exception { MapperService mapperService = createIndex("test").mapperService(); DocumentMapper docMapper = mapperService.merge("type", new CompressedXContent(stage1Mapping), MapperService.MergeReason.MAPPING_UPDATE); + + Mapper fieldMapper = docMapper.mappers().getMapper("shape"); + LegacyGeoShapeFieldMapper geoShapeFieldMapper = (LegacyGeoShapeFieldMapper) fieldMapper; + assertThat(geoShapeFieldMapper.fieldType().orientation(), equalTo(ShapeBuilder.Orientation.CCW)); + String stage2Mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type") .startObject("properties").startObject("shape").field("type", "geo_shape") .field("tree", "quadtree") @@ -565,10 +576,10 @@ public void testGeoShapeMapperMerge() throws Exception { } // verify nothing changed - Mapper fieldMapper = docMapper.mappers().getMapper("shape"); + fieldMapper = docMapper.mappers().getMapper("shape"); assertThat(fieldMapper, instanceOf(LegacyGeoShapeFieldMapper.class)); - LegacyGeoShapeFieldMapper geoShapeFieldMapper = (LegacyGeoShapeFieldMapper) fieldMapper; + geoShapeFieldMapper = (LegacyGeoShapeFieldMapper) fieldMapper; PrefixTreeStrategy strategy = geoShapeFieldMapper.fieldType().defaultPrefixTreeStrategy(); assertThat(strategy, instanceOf(RecursivePrefixTreeStrategy.class)); @@ -615,7 +626,7 @@ public void testEmptyName() throws Exception { IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> parser.parse("type1", new CompressedXContent(mapping)) ); - assertThat(e.getMessage(), containsString("name cannot be empty string")); + assertThat(e.getMessage(), containsString("fieldName is required")); assertFieldWarnings("tree"); } @@ -761,6 +772,17 @@ public void testDisallowExpensiveQueries() throws IOException { assertFieldWarnings("tree"); } + @Override + public void testSerialization() throws IOException { + super.testSerialization(); + assertWarnings("Field parameter [strategy] is deprecated and will be removed in a future version.", + "Field parameter [tree] is deprecated and will be removed in a future version.", + "Field parameter [tree_levels] is deprecated and will be removed in a future version.", + "Field parameter [precision] is deprecated and will be removed in a future version.", + "Field parameter [distance_error_pct] is deprecated and will be removed in a future version." + ); + } + public String toXContentString(LegacyGeoShapeFieldMapper mapper, boolean includeDefaults) throws IOException { XContentBuilder builder = XContentFactory.jsonBuilder().startObject(); ToXContent.Params params; diff --git a/server/src/test/java/org/elasticsearch/index/mapper/LegacyGeoShapeFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/LegacyGeoShapeFieldTypeTests.java index 3ed985883f926..4fbfe92f9900e 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/LegacyGeoShapeFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/LegacyGeoShapeFieldTypeTests.java @@ -21,20 +21,20 @@ import org.elasticsearch.common.geo.SpatialStrategy; import org.elasticsearch.index.mapper.LegacyGeoShapeFieldMapper.GeoShapeFieldType; -import java.io.IOException; +import java.util.Map; public class LegacyGeoShapeFieldTypeTests extends FieldTypeTestCase { @Override - protected MappedFieldType createDefaultFieldType() { - return new GeoShapeFieldType(); + protected MappedFieldType createDefaultFieldType(String name, Map meta) { + return new GeoShapeFieldType(name, true, true, meta); } /** * Test for {@link LegacyGeoShapeFieldMapper.GeoShapeFieldType#setStrategy(SpatialStrategy)} that checks * that {@link LegacyGeoShapeFieldMapper.GeoShapeFieldType#pointsOnly()} gets set as a side effect when using SpatialStrategy.TERM */ - public void testSetStrategyName() throws IOException { - GeoShapeFieldType fieldType = new GeoShapeFieldType(); + public void testSetStrategyName() { + GeoShapeFieldType fieldType = new GeoShapeFieldType("field"); assertFalse(fieldType.pointsOnly()); fieldType.setStrategy(SpatialStrategy.RECURSIVE); assertFalse(fieldType.pointsOnly()); diff --git a/server/src/test/java/org/elasticsearch/index/mapper/MultiFieldTests.java b/server/src/test/java/org/elasticsearch/index/mapper/MultiFieldTests.java index bf68c52a95505..e1b821b570be4 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/MultiFieldTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/MultiFieldTests.java @@ -93,35 +93,35 @@ private void testMultiField(String mapping) throws Exception { assertThat(mapperService.fieldType("name"), notNullValue()); assertThat(mapperService.fieldType("name"), instanceOf(TextFieldType.class)); - assertNotSame(IndexOptions.NONE, mapperService.fieldType("name").indexOptions()); - assertThat(mapperService.fieldType("name").stored(), equalTo(true)); - assertThat(mapperService.fieldType("name").tokenized(), equalTo(true)); + assertNotSame(IndexOptions.NONE, mapperService.getLuceneFieldType("name").indexOptions()); + assertThat(mapperService.getLuceneFieldType("name").stored(), equalTo(true)); + assertThat(mapperService.getLuceneFieldType("name").tokenized(), equalTo(true)); assertThat(mapperService.fieldType("name.indexed"), notNullValue()); assertThat(mapperService.fieldType("name"), instanceOf(TextFieldType.class)); - assertNotSame(IndexOptions.NONE, mapperService.fieldType("name.indexed").indexOptions()); - assertThat(mapperService.fieldType("name.indexed").stored(), equalTo(false)); - assertThat(mapperService.fieldType("name.indexed").tokenized(), equalTo(true)); + assertNotSame(IndexOptions.NONE, mapperService.getLuceneFieldType("name.indexed").indexOptions()); + assertThat(mapperService.getLuceneFieldType("name.indexed").stored(), equalTo(false)); + assertThat(mapperService.getLuceneFieldType("name.indexed").tokenized(), equalTo(true)); assertThat(mapperService.fieldType("name.not_indexed"), notNullValue()); assertThat(mapperService.fieldType("name"), instanceOf(TextFieldType.class)); - assertEquals(IndexOptions.NONE, mapperService.fieldType("name.not_indexed").indexOptions()); - assertThat(mapperService.fieldType("name.not_indexed").stored(), equalTo(true)); - assertThat(mapperService.fieldType("name.not_indexed").tokenized(), equalTo(true)); + assertEquals(IndexOptions.NONE, mapperService.getLuceneFieldType("name.not_indexed").indexOptions()); + assertThat(mapperService.getLuceneFieldType("name.not_indexed").stored(), equalTo(true)); + assertThat(mapperService.getLuceneFieldType("name.not_indexed").tokenized(), equalTo(true)); assertThat(mapperService.fieldType("name.test1"), notNullValue()); assertThat(mapperService.fieldType("name"), instanceOf(TextFieldType.class)); - assertNotSame(IndexOptions.NONE, mapperService.fieldType("name.test1").indexOptions()); - assertThat(mapperService.fieldType("name.test1").stored(), equalTo(true)); - assertThat(mapperService.fieldType("name.test1").tokenized(), equalTo(true)); + assertNotSame(IndexOptions.NONE, mapperService.getLuceneFieldType("name.test1").indexOptions()); + assertThat(mapperService.getLuceneFieldType("name.test1").stored(), equalTo(true)); + assertThat(mapperService.getLuceneFieldType("name.test1").tokenized(), equalTo(true)); assertThat(mapperService.fieldType("name.test1").eagerGlobalOrdinals(), equalTo(true)); assertThat(mapperService.fieldType("object1.multi1"), notNullValue()); assertThat(mapperService.fieldType("object1.multi1"), instanceOf(DateFieldMapper.DateFieldType.class)); assertThat(mapperService.fieldType("object1.multi1.string"), notNullValue()); assertThat(mapperService.fieldType("object1.multi1.string"), instanceOf(KeywordFieldMapper.KeywordFieldType.class)); - assertNotSame(IndexOptions.NONE, mapperService.fieldType("object1.multi1.string").indexOptions()); - assertThat(mapperService.fieldType("object1.multi1.string").tokenized(), equalTo(false)); + assertNotSame(IndexOptions.NONE, mapperService.getLuceneFieldType("object1.multi1.string").indexOptions()); + assertThat(mapperService.getLuceneFieldType("object1.multi1.string").tokenized(), equalTo(false)); } public void testBuildThenParse() throws Exception { diff --git a/server/src/test/java/org/elasticsearch/index/mapper/NumberFieldMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/NumberFieldMapperTests.java index bf8269a27cdde..b33383f290477 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/NumberFieldMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/NumberFieldMapperTests.java @@ -42,6 +42,7 @@ import java.util.Arrays; import java.util.HashSet; import java.util.List; +import java.util.Set; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.hamcrest.Matchers.containsString; @@ -54,6 +55,11 @@ protected void setTypeList() { WHOLE_TYPES = new HashSet<>(Arrays.asList("byte", "short", "integer", "long")); } + @Override + protected Set unsupportedProperties() { + return Set.of("analyzer", "similarity"); + } + @Override public void doTestDefaults(String type) throws Exception { String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type") diff --git a/server/src/test/java/org/elasticsearch/index/mapper/NumberFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/NumberFieldTypeTests.java index 5cd91b98960cf..407fdca4d3b51 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/NumberFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/NumberFieldTypeTests.java @@ -28,7 +28,6 @@ import org.apache.lucene.document.LongPoint; import org.apache.lucene.document.SortedNumericDocValuesField; import org.apache.lucene.index.DirectoryReader; -import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.search.IndexOrDocValuesQuery; @@ -60,7 +59,9 @@ import java.math.BigInteger; import java.nio.charset.StandardCharsets; import java.util.Arrays; +import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.function.Supplier; import static org.hamcrest.Matchers.containsString; @@ -77,90 +78,75 @@ public void pickType() { } @Override - protected MappedFieldType createDefaultFieldType() { - return new NumberFieldMapper.NumberFieldType(type); + protected MappedFieldType createDefaultFieldType(String name, Map meta) { + return new NumberFieldMapper.NumberFieldType(name, type, true, true, meta); } public void testEqualsWithDifferentNumberTypes() { NumberType type = randomFrom(NumberType.values()); - NumberFieldType fieldType = new NumberFieldType(type); + NumberFieldType fieldType = new NumberFieldType("foo", type); NumberType otherType = randomValueOtherThan(type, () -> randomFrom(NumberType.values())); - NumberFieldType otherFieldType = new NumberFieldType(otherType); + NumberFieldType otherFieldType = new NumberFieldType("foo", otherType); assertNotEquals(fieldType, otherFieldType); } public void testIsFieldWithinQuery() throws IOException { - MappedFieldType ft = createDefaultFieldType(); + MappedFieldType ft = createDefaultFieldType("field", Collections.emptyMap()); // current impl ignores args and should always return INTERSECTS assertEquals(Relation.INTERSECTS, ft.isFieldWithinQuery(null, randomDouble(), randomDouble(), randomBoolean(), randomBoolean(), null, null, null)); } public void testIntegerTermsQueryWithDecimalPart() { - MappedFieldType ft = new NumberFieldMapper.NumberFieldType(NumberType.INTEGER); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ft = new NumberFieldMapper.NumberFieldType("field", NumberType.INTEGER); assertEquals(IntPoint.newSetQuery("field", 1), ft.termsQuery(Arrays.asList(1, 2.1), null)); assertEquals(IntPoint.newSetQuery("field", 1), ft.termsQuery(Arrays.asList(1.0, 2.1), null)); assertTrue(ft.termsQuery(Arrays.asList(1.1, 2.1), null) instanceof MatchNoDocsQuery); } public void testLongTermsQueryWithDecimalPart() { - MappedFieldType ft = new NumberFieldMapper.NumberFieldType(NumberType.LONG); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ft = new NumberFieldMapper.NumberFieldType("field", NumberType.LONG); assertEquals(LongPoint.newSetQuery("field", 1), ft.termsQuery(Arrays.asList(1, 2.1), null)); assertEquals(LongPoint.newSetQuery("field", 1), ft.termsQuery(Arrays.asList(1.0, 2.1), null)); assertTrue(ft.termsQuery(Arrays.asList(1.1, 2.1), null) instanceof MatchNoDocsQuery); } public void testByteTermQueryWithDecimalPart() { - MappedFieldType ft = new NumberFieldMapper.NumberFieldType(NumberType.BYTE); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ft = new NumberFieldMapper.NumberFieldType("field", NumberType.BYTE); assertTrue(ft.termQuery(42.1, null) instanceof MatchNoDocsQuery); } public void testShortTermQueryWithDecimalPart() { - MappedFieldType ft = new NumberFieldMapper.NumberFieldType(NumberType.SHORT); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ft = new NumberFieldMapper.NumberFieldType("field", NumberType.SHORT); assertTrue(ft.termQuery(42.1, null) instanceof MatchNoDocsQuery); } public void testIntegerTermQueryWithDecimalPart() { - MappedFieldType ft = new NumberFieldMapper.NumberFieldType(NumberType.INTEGER); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ft = new NumberFieldMapper.NumberFieldType("field", NumberType.INTEGER); assertTrue(ft.termQuery(42.1, null) instanceof MatchNoDocsQuery); } public void testLongTermQueryWithDecimalPart() { - MappedFieldType ft = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ft = new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.LONG); assertTrue(ft.termQuery(42.1, null) instanceof MatchNoDocsQuery); } public void testTermQuery() { - MappedFieldType ft = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ft = new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.LONG); assertEquals(LongPoint.newExactQuery("field", 42), ft.termQuery("42", null)); - ft.setIndexOptions(IndexOptions.NONE); + MappedFieldType unsearchable + = new NumberFieldType("field", NumberType.LONG, false, true, Collections.emptyMap()); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> ft.termQuery("42", null)); + () -> unsearchable.termQuery("42", null)); assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage()); } public void testRangeQueryWithNegativeBounds() { - MappedFieldType ftInt = new NumberFieldMapper.NumberFieldType(NumberType.INTEGER); - ftInt.setName("field"); - ftInt.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ftInt = new NumberFieldMapper.NumberFieldType("field", NumberType.INTEGER); assertEquals(ftInt.rangeQuery(-3, -3, true, true, null, null, null, MOCK_QSC), ftInt.rangeQuery(-3.5, -2.5, true, true, null, null, null, MOCK_QSC)); assertEquals(ftInt.rangeQuery(-3, -3, true, true, null, null, null, MOCK_QSC), @@ -187,9 +173,7 @@ public void testRangeQueryWithNegativeBounds() { assertEquals(ftInt.rangeQuery(-2, -1, true, true, null, null, null, MOCK_QSC), ftInt.rangeQuery(-2.5, -0.5, false, false, null, null, null, MOCK_QSC)); - MappedFieldType ftLong = new NumberFieldMapper.NumberFieldType(NumberType.LONG); - ftLong.setName("field"); - ftLong.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ftLong = new NumberFieldMapper.NumberFieldType("field", NumberType.LONG); assertEquals(ftLong.rangeQuery(-3, -3, true, true, null, null, null, MOCK_QSC), ftLong.rangeQuery(-3.5, -2.5, true, true, null, null, null, MOCK_QSC)); assertEquals(ftLong.rangeQuery(-3, -3, true, true, null, null, null, MOCK_QSC), @@ -218,9 +202,7 @@ public void testRangeQueryWithNegativeBounds() { } public void testByteRangeQueryWithDecimalParts() { - MappedFieldType ft = new NumberFieldMapper.NumberFieldType(NumberType.BYTE); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ft = new NumberFieldMapper.NumberFieldType("field", NumberType.BYTE); assertEquals(ft.rangeQuery(2, 10, true, true, null, null, null, MOCK_QSC), ft.rangeQuery(1.1, 10, true, true, null, null, null, MOCK_QSC)); assertEquals(ft.rangeQuery(2, 10, true, true, null, null, null, MOCK_QSC), @@ -232,9 +214,7 @@ public void testByteRangeQueryWithDecimalParts() { } public void testShortRangeQueryWithDecimalParts() { - MappedFieldType ft = new NumberFieldMapper.NumberFieldType(NumberType.SHORT); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ft = new NumberFieldMapper.NumberFieldType("field", NumberType.SHORT); assertEquals(ft.rangeQuery(2, 10, true, true, null, null, null, MOCK_QSC), ft.rangeQuery(1.1, 10, true, true, null, null, null, MOCK_QSC)); assertEquals(ft.rangeQuery(2, 10, true, true, null, null, null, MOCK_QSC), @@ -246,9 +226,7 @@ public void testShortRangeQueryWithDecimalParts() { } public void testIntegerRangeQueryWithDecimalParts() { - MappedFieldType ft = new NumberFieldMapper.NumberFieldType(NumberType.INTEGER); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ft = new NumberFieldMapper.NumberFieldType("field", NumberType.INTEGER); assertEquals(ft.rangeQuery(2, 10, true, true, null, null, null, MOCK_QSC), ft.rangeQuery(1.1, 10, true, true, null, null, null, MOCK_QSC)); assertEquals(ft.rangeQuery(2, 10, true, true, null, null, null, MOCK_QSC), @@ -260,9 +238,7 @@ public void testIntegerRangeQueryWithDecimalParts() { } public void testLongRangeQueryWithDecimalParts() { - MappedFieldType ft = new NumberFieldMapper.NumberFieldType(NumberType.LONG); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ft = new NumberFieldMapper.NumberFieldType("field", NumberType.LONG); assertEquals(ft.rangeQuery(2, 10, true, true, null, null, null, MOCK_QSC), ft.rangeQuery(1.1, 10, true, true, null, null, null, MOCK_QSC)); assertEquals(ft.rangeQuery(2, 10, true, true, null, null, null, MOCK_QSC), @@ -274,17 +250,15 @@ public void testLongRangeQueryWithDecimalParts() { } public void testRangeQuery() { - MappedFieldType ft = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ft = new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.LONG); Query expected = new IndexOrDocValuesQuery( LongPoint.newRangeQuery("field", 1, 3), SortedNumericDocValuesField.newSlowRangeQuery("field", 1, 3)); assertEquals(expected, ft.rangeQuery("1", "3", true, true, null, null, null, MOCK_QSC)); - ft.setIndexOptions(IndexOptions.NONE); + MappedFieldType unsearchable = new NumberFieldType("field", NumberType.LONG, false, true, Collections.emptyMap()); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> ft.rangeQuery("1", "3", true, true, null, null, null, MOCK_QSC)); + () -> unsearchable.rangeQuery("1", "3", true, true, null, null, null, MOCK_QSC)); assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage()); } @@ -478,8 +452,7 @@ public void doTestIndexSortRangeQueries(NumberType type, Supplier valueS IndexSettings indexSettings = new IndexSettings(indexMetadata, settings); // Create an index writer configured with the same index sort. - NumberFieldType fieldType = new NumberFieldType(type); - fieldType.setName("field"); + NumberFieldType fieldType = new NumberFieldType("field", type); IndexNumericFieldData fielddata = (IndexNumericFieldData) fieldType.fielddataBuilder("index") .build(indexSettings, fieldType, null, null, null); SortField sortField = fielddata.sortField(null, MultiValueMode.MIN, null, randomBoolean()); @@ -597,23 +570,23 @@ static OutOfRangeSpec of(NumberType t, V v, String m) { public void testDisplayValue() { for (NumberFieldMapper.NumberType type : NumberFieldMapper.NumberType.values()) { - NumberFieldMapper.NumberFieldType fieldType = new NumberFieldMapper.NumberFieldType(type); + NumberFieldMapper.NumberFieldType fieldType = new NumberFieldMapper.NumberFieldType("field", type); assertNull(fieldType.valueForDisplay(null)); } assertEquals(Byte.valueOf((byte) 3), - new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.BYTE).valueForDisplay(3)); + new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.BYTE).valueForDisplay(3)); assertEquals(Short.valueOf((short) 3), - new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.SHORT).valueForDisplay(3)); + new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.SHORT).valueForDisplay(3)); assertEquals(Integer.valueOf(3), - new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER).valueForDisplay(3)); + new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.INTEGER).valueForDisplay(3)); assertEquals(Long.valueOf(3), - new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG).valueForDisplay(3L)); + new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.LONG).valueForDisplay(3L)); assertEquals(Double.valueOf(1.2), - new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.HALF_FLOAT).valueForDisplay(1.2)); + new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.HALF_FLOAT).valueForDisplay(1.2)); assertEquals(Double.valueOf(1.2), - new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.FLOAT).valueForDisplay(1.2)); + new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.FLOAT).valueForDisplay(1.2)); assertEquals(Double.valueOf(1.2), - new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE).valueForDisplay(1.2)); + new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.DOUBLE).valueForDisplay(1.2)); } public void testParsePoint() { diff --git a/server/src/test/java/org/elasticsearch/index/mapper/ObjectMapperMergeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/ObjectMapperMergeTests.java index a2f62cf02447c..b03e12d029a6a 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/ObjectMapperMergeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/ObjectMapperMergeTests.java @@ -126,9 +126,10 @@ private static ObjectMapper createObjectMapper(String name, boolean enabled, Map } private static TextFieldMapper createTextFieldMapper(String name) { - final TextFieldType fieldType = new TextFieldType(); + final TextFieldType fieldType = new TextFieldType(name); final Settings indexSettings = Settings.builder().put(SETTING_VERSION_CREATED, Version.CURRENT).build(); - return new TextFieldMapper(name, fieldType, fieldType, -1, null, indexSettings, MultiFields.empty(), CopyTo.empty()); + return new TextFieldMapper(name, TextFieldMapper.Defaults.FIELD_TYPE, fieldType, -1, + null, null, indexSettings, MultiFields.empty(), CopyTo.empty()); } } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/PathMatchDynamicTemplateTests.java b/server/src/test/java/org/elasticsearch/index/mapper/PathMatchDynamicTemplateTests.java index 717c0b3a3ffaa..1f1f15fdd9204 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/PathMatchDynamicTemplateTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/PathMatchDynamicTemplateTests.java @@ -19,6 +19,7 @@ package org.elasticsearch.index.mapper; +import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexableField; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.xcontent.XContentType; @@ -50,26 +51,26 @@ public void testSimple() throws Exception { assertThat(f.stringValue(), equalTo("top_level")); assertThat(f.fieldType().stored(), equalTo(false)); - MappedFieldType fieldType = mapperService.fieldType("name"); + FieldType fieldType = mapperService.getLuceneFieldType("name"); assertThat(fieldType.stored(), equalTo(false)); f = doc.getField("obj1.name"); assertThat(f.name(), equalTo("obj1.name")); assertThat(f.fieldType().stored(), equalTo(true)); - fieldType = mapperService.fieldType("obj1.name"); + fieldType = mapperService.getLuceneFieldType("obj1.name"); assertThat(fieldType.stored(), equalTo(true)); f = doc.getField("obj1.obj2.name"); assertThat(f.name(), equalTo("obj1.obj2.name")); assertThat(f.fieldType().stored(), equalTo(false)); - fieldType = mapperService.fieldType("obj1.obj2.name"); + fieldType = mapperService.getLuceneFieldType("obj1.obj2.name"); assertThat(fieldType.stored(), equalTo(false)); // verify more complex path_match expressions - fieldType = mapperService.fieldType("obj3.obj4.prop1"); + fieldType = mapperService.getLuceneFieldType("obj3.obj4.prop1"); assertNotNull(fieldType); } } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/RangeFieldMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/RangeFieldMapperTests.java index 67da23e6abe73..88d2db04f2b73 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/RangeFieldMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/RangeFieldMapperTests.java @@ -40,6 +40,7 @@ import java.util.Collection; import java.util.HashSet; import java.util.Locale; +import java.util.Set; import static org.elasticsearch.index.query.RangeQueryBuilder.GTE_FIELD; import static org.elasticsearch.index.query.RangeQueryBuilder.GT_FIELD; @@ -56,6 +57,11 @@ protected RangeFieldMapper.Builder newBuilder() { .format("iso8601"); } + @Override + protected Set unsupportedProperties() { + return Set.of("analyzer", "similarity"); + } + @Before public void addModifiers() { addModifier("format", true, (a, b) -> { diff --git a/server/src/test/java/org/elasticsearch/index/mapper/RangeFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/RangeFieldTypeTests.java index d0657370f1c70..9dcdb4e32fc28 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/RangeFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/RangeFieldTypeTests.java @@ -25,7 +25,6 @@ import org.apache.lucene.document.InetAddressRange; import org.apache.lucene.document.IntRange; import org.apache.lucene.document.LongRange; -import org.apache.lucene.index.IndexOptions; import org.apache.lucene.queries.BinaryDocValuesRangeQuery; import org.apache.lucene.search.IndexOrDocValuesQuery; import org.apache.lucene.search.MatchNoDocsQuery; @@ -48,6 +47,8 @@ import org.junit.Before; import java.net.InetAddress; +import java.util.Collections; +import java.util.Map; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.instanceOf; @@ -62,29 +63,19 @@ public class RangeFieldTypeTests extends FieldTypeTestCase { public void setupProperties() { type = randomFrom(RangeType.values()); nowInMillis = randomNonNegativeLong(); - if (type == RangeType.DATE) { - addModifier(t -> { - RangeFieldType other = t.clone(); - if (other.dateTimeFormatter == DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER) { - other.setDateTimeFormatter(DateFormatter.forPattern("epoch_millis")); - } else { - other.setDateTimeFormatter(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER); - } - return other; - }); - } } @Override - protected RangeFieldType createDefaultFieldType() { - return new RangeFieldType(type); + protected RangeFieldType createDefaultFieldType(String name, Map meta) { + if (type == RangeType.DATE) { + return new RangeFieldType(name, true, true, RangeFieldMapper.Defaults.DATE_FORMATTER, meta); + } + return new RangeFieldType(name, type, true, true, meta); } public void testRangeQuery() throws Exception { QueryShardContext context = createContext(); - RangeFieldType ft = new RangeFieldType(type); - ft.setName(FIELDNAME); - ft.setIndexOptions(IndexOptions.DOCS); + RangeFieldType ft = createDefaultFieldType(FIELDNAME, Collections.emptyMap()); ShapeRelation relation = randomFrom(ShapeRelation.values()); boolean includeLower = randomBoolean(); @@ -106,9 +97,7 @@ public void testRangeQuery() throws Exception { public void testRangeQueryIntersectsAdjacentValues() throws Exception { QueryShardContext context = createContext(); ShapeRelation relation = randomFrom(ShapeRelation.values()); - RangeFieldType ft = new RangeFieldType(type); - ft.setName(FIELDNAME); - ft.setIndexOptions(IndexOptions.DOCS); + RangeFieldType ft = createDefaultFieldType(FIELDNAME, Collections.emptyMap()); Object from = null; Object to = null; @@ -165,9 +154,7 @@ public void testRangeQueryIntersectsAdjacentValues() throws Exception { */ public void testFromLargerToErrors() throws Exception { QueryShardContext context = createContext(); - RangeFieldType ft = new RangeFieldType(type); - ft.setName(FIELDNAME); - ft.setIndexOptions(IndexOptions.DOCS); + RangeFieldType ft = createDefaultFieldType(FIELDNAME, Collections.emptyMap()); final Object from; final Object to; @@ -231,10 +218,8 @@ private QueryShardContext createContext() { public void testDateRangeQueryUsingMappingFormat() { QueryShardContext context = createContext(); - RangeFieldType fieldType = new RangeFieldType(RangeType.DATE); - fieldType.setName(FIELDNAME); - fieldType.setIndexOptions(IndexOptions.DOCS); - fieldType.setHasDocValues(false); + RangeFieldType strict + = new RangeFieldType(FIELDNAME, true, false, RangeFieldMapper.Defaults.DATE_FORMATTER, Collections.emptyMap()); // don't use DISJOINT here because it doesn't work on date fields which we want to compare bounds with ShapeRelation relation = randomValueOtherThan(ShapeRelation.DISJOINT,() -> randomFrom(ShapeRelation.values())); @@ -243,7 +228,7 @@ public void testDateRangeQueryUsingMappingFormat() { final String to = "2016-16-06T15:29:50+08:00"; ElasticsearchParseException ex = expectThrows(ElasticsearchParseException.class, - () -> fieldType.rangeQuery(from, to, true, true, relation, null, null, context)); + () -> strict.rangeQuery(from, to, true, true, relation, null, null, context)); assertThat(ex.getMessage(), containsString("failed to parse date field [2016-15-06T15:29:50+08:00] with format [strict_date_optional_time||epoch_millis]") ); @@ -253,14 +238,13 @@ public void testDateRangeQueryUsingMappingFormat() { assertEquals(1465975790000L, formatter.parseMillis(from)); assertEquals(1466062190000L, formatter.parseMillis(to)); - fieldType.setDateTimeFormatter(formatter); + RangeFieldType fieldType = new RangeFieldType(FIELDNAME, true, true, formatter, Collections.emptyMap()); final Query query = fieldType.rangeQuery(from, to, true, true, relation, null, null, context); assertEquals("field:", query.toString()); // compare lower and upper bounds with what we would get on a `date` field - DateFieldType dateFieldType = new DateFieldType(); - dateFieldType.setName(FIELDNAME); - dateFieldType.setDateTimeFormatter(formatter); + DateFieldType dateFieldType + = new DateFieldType(FIELDNAME, true, true, formatter, DateFieldMapper.Resolution.MILLISECONDS, Collections.emptyMap()); final Query queryOnDateField = dateFieldType.rangeQuery(from, to, true, true, relation, null, null, context); assertEquals("field:[1465975790000 TO 1466062190999]", queryOnDateField.toString()); } @@ -271,17 +255,13 @@ public void testDateRangeQueryUsingMappingFormat() { */ public void testDateVsDateRangeBounds() { QueryShardContext context = createContext(); - RangeFieldType fieldType = new RangeFieldType(RangeType.DATE); - fieldType.setName(FIELDNAME); - fieldType.setIndexOptions(IndexOptions.DOCS); - fieldType.setHasDocValues(false); // date formatter that truncates seconds, so we get some rounding behavior final DateFormatter formatter = DateFormatter.forPattern("yyyy-dd-MM'T'HH:mm"); long lower = randomLongBetween(formatter.parseMillis("2000-01-01T00:00"), formatter.parseMillis("2010-01-01T00:00")); long upper = randomLongBetween(formatter.parseMillis("2011-01-01T00:00"), formatter.parseMillis("2020-01-01T00:00")); - fieldType.setDateTimeFormatter(formatter); + RangeFieldType fieldType = new RangeFieldType(FIELDNAME, true, false, formatter, Collections.emptyMap()); String lowerAsString = formatter.formatMillis(lower); String upperAsString = formatter.formatMillis(upper); // also add date math rounding to days occasionally @@ -297,7 +277,7 @@ public void testDateVsDateRangeBounds() { null, context); // get exact lower and upper bounds similar to what we would parse for `date` fields for same input strings - DateFieldType dateFieldType = new DateFieldType(); + DateFieldType dateFieldType = new DateFieldType("field"); long lowerBoundLong = dateFieldType.parseToLong(lowerAsString, !includeLower, null, formatter.toDateMathParser(), () -> 0); if (includeLower == false) { ++lowerBoundLong; @@ -494,9 +474,7 @@ public void testParseIp() { public void testTermQuery() throws Exception { // See https://github.com/elastic/elasticsearch/issues/25950 QueryShardContext context = createContext(); - RangeFieldType ft = new RangeFieldType(type); - ft.setName(FIELDNAME); - ft.setIndexOptions(IndexOptions.DOCS); + RangeFieldType ft = createDefaultFieldType(FIELDNAME, Collections.emptyMap()); Object value = nextFrom(); ShapeRelation relation = ShapeRelation.INTERSECTS; diff --git a/server/src/test/java/org/elasticsearch/index/mapper/RoutingFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/RoutingFieldTypeTests.java index 14b25f249e569..8576c57a8bc01 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/RoutingFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/RoutingFieldTypeTests.java @@ -18,7 +18,6 @@ */ package org.elasticsearch.index.mapper; -import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.Term; import org.apache.lucene.search.PrefixQuery; import org.apache.lucene.search.Query; @@ -26,20 +25,27 @@ import org.apache.lucene.search.WildcardQuery; import org.apache.lucene.util.BytesRef; import org.elasticsearch.ElasticsearchException; +import org.elasticsearch.test.EqualsHashCodeTestUtils; + +import java.util.Map; public class RoutingFieldTypeTests extends FieldTypeTestCase { @Override - protected MappedFieldType createDefaultFieldType() { - return new RoutingFieldMapper.RoutingFieldType(); + protected MappedFieldType createDefaultFieldType(String name, Map meta) { + return RoutingFieldMapper.RoutingFieldType.INSTANCE; + } + + @Override + public void testEquals() { + EqualsHashCodeTestUtils.checkEqualsAndHashCode(RoutingFieldMapper.RoutingFieldType.INSTANCE, + RoutingFieldMapper.RoutingFieldType::new); } public void testPrefixQuery() { - MappedFieldType ft = createDefaultFieldType(); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ft = RoutingFieldMapper.RoutingFieldType.INSTANCE; - Query expected = new PrefixQuery(new Term("field", new BytesRef("foo*"))); + Query expected = new PrefixQuery(new Term("_routing", new BytesRef("foo*"))); assertEquals(expected, ft.prefixQuery("foo*", null, MOCK_QSC)); ElasticsearchException ee = expectThrows(ElasticsearchException.class, @@ -49,11 +55,9 @@ public void testPrefixQuery() { } public void testRegexpQuery() { - MappedFieldType ft = createDefaultFieldType(); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ft = RoutingFieldMapper.RoutingFieldType.INSTANCE; - Query expected = new RegexpQuery(new Term("field", new BytesRef("foo?"))); + Query expected = new RegexpQuery(new Term("_routing", new BytesRef("foo?"))); assertEquals(expected, ft.regexpQuery("foo?", 0, 10, null, MOCK_QSC)); ElasticsearchException ee = expectThrows(ElasticsearchException.class, @@ -63,11 +67,9 @@ public void testRegexpQuery() { } public void testWildcardQuery() { - MappedFieldType ft = createDefaultFieldType(); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ft = RoutingFieldMapper.RoutingFieldType.INSTANCE; - Query expected = new WildcardQuery(new Term("field", new BytesRef("foo*"))); + Query expected = new WildcardQuery(new Term("_routing", new BytesRef("foo*"))); assertEquals(expected, ft.wildcardQuery("foo*", null, MOCK_QSC)); ElasticsearchException ee = expectThrows(ElasticsearchException.class, diff --git a/server/src/test/java/org/elasticsearch/index/mapper/SourceFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/SourceFieldTypeTests.java deleted file mode 100644 index 46d07304100c0..0000000000000 --- a/server/src/test/java/org/elasticsearch/index/mapper/SourceFieldTypeTests.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.elasticsearch.index.mapper; - -public class SourceFieldTypeTests extends FieldTypeTestCase { - @Override - protected MappedFieldType createDefaultFieldType() { - return new SourceFieldMapper.SourceFieldType(); - } -} diff --git a/server/src/test/java/org/elasticsearch/index/mapper/TextFieldMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/TextFieldMapperTests.java index 38137d5bd7bbc..d312c8dcf4ee9 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/TextFieldMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/TextFieldMapperTests.java @@ -26,6 +26,7 @@ import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; +import org.apache.lucene.document.FieldType; import org.apache.lucene.index.DocValuesType; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.IndexableField; @@ -80,6 +81,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; +import java.util.Set; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; @@ -91,7 +93,8 @@ public class TextFieldMapperTests extends FieldMapperTestCase { + a.indexPhrases(true); + b.indexPhrases(false); + }); addModifier("index_prefixes", false, (a, b) -> { a.indexPrefixes(2, 4); }); + addModifier("index_options", false, (a, b) -> { + a.indexOptions(IndexOptions.DOCS_AND_FREQS); + b.indexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS); + }); } @Override - protected boolean supportsDocValues() { - return false; + protected Set unsupportedProperties() { + return Set.of("doc_values"); } IndexService indexService; @@ -250,6 +260,10 @@ public void testIndexOptions() throws IOException { String mapping = Strings.toString(mappingBuilder.endObject().endObject().endObject()); DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping)); + String serialized = Strings.toString(mapper); + assertThat(serialized, containsString("\"offsets\":{\"type\":\"text\",\"index_options\":\"offsets\"}")); + assertThat(serialized, containsString("\"freqs\":{\"type\":\"text\",\"index_options\":\"freqs\"}")); + assertThat(serialized, containsString("\"docs\":{\"type\":\"text\",\"index_options\":\"docs\"}")); XContentBuilder jsonDoc = XContentFactory.jsonBuilder().startObject(); for (String option : supportedOptions.keySet()) { @@ -673,9 +687,8 @@ public void testIndexPrefixIndexTypes() throws IOException { DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping)); FieldMapper prefix = (FieldMapper) mapper.mappers().getMapper("field._index_prefix"); - MappedFieldType ft = prefix.fieldType; - assertEquals(ft.name(), "field._index_prefix"); - assertEquals(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS, ft.indexOptions()); + assertEquals(prefix.name(), "field._index_prefix"); + assertEquals(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS, prefix.fieldType.indexOptions()); } { @@ -690,8 +703,8 @@ public void testIndexPrefixIndexTypes() throws IOException { DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping)); FieldMapper prefix = (FieldMapper) mapper.mappers().getMapper("field._index_prefix"); - MappedFieldType ft = prefix.fieldType; - assertEquals(ft.name(), "field._index_prefix"); + FieldType ft = prefix.fieldType; + assertEquals(prefix.name(), "field._index_prefix"); assertEquals(IndexOptions.DOCS, ft.indexOptions()); assertFalse(ft.storeTermVectors()); } @@ -708,8 +721,8 @@ public void testIndexPrefixIndexTypes() throws IOException { DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping)); FieldMapper prefix = (FieldMapper) mapper.mappers().getMapper("field._index_prefix"); - MappedFieldType ft = prefix.fieldType; - assertEquals(ft.name(), "field._index_prefix"); + FieldType ft = prefix.fieldType; + assertEquals(prefix.name(), "field._index_prefix"); assertEquals(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS, ft.indexOptions()); assertFalse(ft.storeTermVectors()); } @@ -726,8 +739,8 @@ public void testIndexPrefixIndexTypes() throws IOException { DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping)); FieldMapper prefix = (FieldMapper) mapper.mappers().getMapper("field._index_prefix"); - MappedFieldType ft = prefix.fieldType; - assertEquals(ft.name(), "field._index_prefix"); + FieldType ft = prefix.fieldType; + assertEquals(prefix.name(), "field._index_prefix"); assertEquals(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS, ft.indexOptions()); assertTrue(ft.storeTermVectorOffsets()); } @@ -744,8 +757,8 @@ public void testIndexPrefixIndexTypes() throws IOException { DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping)); FieldMapper prefix = (FieldMapper) mapper.mappers().getMapper("field._index_prefix"); - MappedFieldType ft = prefix.fieldType; - assertEquals(ft.name(), "field._index_prefix"); + FieldType ft = prefix.fieldType; + assertEquals(prefix.name(), "field._index_prefix"); assertEquals(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS, ft.indexOptions()); assertFalse(ft.storeTermVectorOffsets()); } @@ -774,8 +787,10 @@ public void testNestedIndexPrefixes() throws IOException { assertThat(textField, instanceOf(TextFieldType.class)); MappedFieldType prefix = ((TextFieldType) textField).getPrefixFieldType(); assertEquals(prefix.name(), "object.field._index_prefix"); - assertEquals(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS, prefix.indexOptions()); - assertFalse(prefix.storeTermVectorOffsets()); + FieldMapper mapper + = (FieldMapper) indexService.mapperService().documentMapper().mappers().getMapper("object.field._index_prefix"); + assertEquals(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS, mapper.fieldType.indexOptions()); + assertFalse(mapper.fieldType.storeTermVectorOffsets()); } { @@ -800,8 +815,10 @@ public void testNestedIndexPrefixes() throws IOException { assertThat(textField, instanceOf(TextFieldType.class)); MappedFieldType prefix = ((TextFieldType) textField).getPrefixFieldType(); assertEquals(prefix.name(), "body.with_prefix._index_prefix"); - assertEquals(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS, prefix.indexOptions()); - assertFalse(prefix.storeTermVectorOffsets()); + FieldMapper mapper + = (FieldMapper) indexService.mapperService().documentMapper().mappers().getMapper("body.with_prefix._index_prefix"); + assertEquals(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS, mapper.fieldType.indexOptions()); + assertFalse(mapper.fieldType.storeTermVectorOffsets()); } } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/TextFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/TextFieldTypeTests.java index 867e3314e91fd..e53d85adfb6e7 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/TextFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/TextFieldTypeTests.java @@ -18,7 +18,6 @@ */ package org.elasticsearch.index.mapper; -import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.Term; import org.apache.lucene.search.AutomatonQuery; import org.apache.lucene.search.BooleanClause; @@ -43,7 +42,9 @@ import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; +import java.util.Map; import static org.apache.lucene.search.MultiTermQuery.CONSTANT_SCORE_REWRITE; import static org.hamcrest.Matchers.equalTo; @@ -75,42 +76,36 @@ public void addModifiers() { } @Override - protected TextFieldType createDefaultFieldType() { - return new TextFieldType(); + protected TextFieldType createDefaultFieldType(String name, Map meta) { + return new TextFieldType(name, true, true, meta); } public void testTermQuery() { - MappedFieldType ft = createDefaultFieldType(); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ft = new TextFieldType("field"); assertEquals(new TermQuery(new Term("field", "foo")), ft.termQuery("foo", null)); - ft.setIndexOptions(IndexOptions.NONE); + MappedFieldType unsearchable = new TextFieldType("field", false, true, Collections.emptyMap()); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> ft.termQuery("bar", null)); + () -> unsearchable.termQuery("bar", null)); assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage()); } public void testTermsQuery() { - MappedFieldType ft = createDefaultFieldType(); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ft = new TextFieldType("field"); List terms = new ArrayList<>(); terms.add(new BytesRef("foo")); terms.add(new BytesRef("bar")); assertEquals(new TermInSetQuery("field", terms), ft.termsQuery(Arrays.asList("foo", "bar"), null)); - ft.setIndexOptions(IndexOptions.NONE); + MappedFieldType unsearchable = new TextFieldType("field", false, true, Collections.emptyMap()); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> ft.termsQuery(Arrays.asList("foo", "bar"), null)); + () -> unsearchable.termsQuery(Arrays.asList("foo", "bar"), null)); assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage()); } public void testRangeQuery() { - MappedFieldType ft = createDefaultFieldType(); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ft = new TextFieldType("field"); assertEquals(new TermRangeQuery("field", BytesRefs.toBytesRef("foo"), BytesRefs.toBytesRef("bar"), true, false), ft.rangeQuery("foo", "bar", true, false, null, null, null, MOCK_QSC)); @@ -121,15 +116,13 @@ public void testRangeQuery() { } public void testRegexpQuery() { - MappedFieldType ft = createDefaultFieldType(); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ft = new TextFieldType("field"); assertEquals(new RegexpQuery(new Term("field","foo.*")), ft.regexpQuery("foo.*", 0, 10, null, MOCK_QSC)); - ft.setIndexOptions(IndexOptions.NONE); + MappedFieldType unsearchable = new TextFieldType("field", false, true, Collections.emptyMap()); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> ft.regexpQuery("foo.*", 0, 10, null, MOCK_QSC)); + () -> unsearchable.regexpQuery("foo.*", 0, 10, null, MOCK_QSC)); assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage()); ElasticsearchException ee = expectThrows(ElasticsearchException.class, @@ -139,15 +132,13 @@ public void testRegexpQuery() { } public void testFuzzyQuery() { - MappedFieldType ft = createDefaultFieldType(); - ft.setName("field"); - ft.setIndexOptions(IndexOptions.DOCS); + MappedFieldType ft = new TextFieldType("field"); assertEquals(new FuzzyQuery(new Term("field","foo"), 2, 1, 50, true), ft.fuzzyQuery("foo", Fuzziness.fromEdits(2), 1, 50, true, MOCK_QSC)); - ft.setIndexOptions(IndexOptions.NONE); + MappedFieldType unsearchable = new TextFieldType("field", false, true, Collections.emptyMap()); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> ft.fuzzyQuery("foo", Fuzziness.fromEdits(2), 1, 50, true, MOCK_QSC)); + () -> unsearchable.fuzzyQuery("foo", Fuzziness.fromEdits(2), 1, 50, true, MOCK_QSC)); assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage()); ElasticsearchException ee = expectThrows(ElasticsearchException.class, @@ -158,9 +149,8 @@ public void testFuzzyQuery() { } public void testIndexPrefixes() { - TextFieldType ft = new TextFieldType(); - ft.setName("field"); - ft.setPrefixFieldType(new TextFieldMapper.PrefixFieldType("field", "field._index_prefix", 2, 10)); + TextFieldType ft = new TextFieldType("field"); + ft.setPrefixFieldType(new TextFieldMapper.PrefixFieldType("field", "field._index_prefix", 2, 10, true)); Query q = ft.prefixQuery("goin", CONSTANT_SCORE_REWRITE, randomMockShardContext()); assertEquals(new ConstantScoreQuery(new TermQuery(new Term("field._index_prefix", "goin"))), q); diff --git a/server/src/test/java/org/elasticsearch/index/mapper/TypeFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/TypeFieldTypeTests.java index 2d69ec076c29a..98b11a0362da6 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/TypeFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/TypeFieldTypeTests.java @@ -22,19 +22,15 @@ import org.apache.lucene.search.MatchNoDocsQuery; import org.apache.lucene.search.Query; import org.elasticsearch.index.query.QueryShardContext; +import org.elasticsearch.test.ESTestCase; import org.mockito.Mockito; -public class TypeFieldTypeTests extends FieldTypeTestCase { - @Override - protected MappedFieldType createDefaultFieldType() { - return new TypeFieldMapper.TypeFieldType(); - } +public class TypeFieldTypeTests extends ESTestCase { - public void testTermsQuery() throws Exception { + public void testTermsQuery() { QueryShardContext context = Mockito.mock(QueryShardContext.class); - TypeFieldMapper.TypeFieldType ft = new TypeFieldMapper.TypeFieldType(); - ft.setName(TypeFieldMapper.NAME); + TypeFieldMapper.TypeFieldType ft = TypeFieldMapper.TypeFieldType.INSTANCE; Query query = ft.termQuery("_doc", context); assertEquals(new MatchAllDocsQuery(), query); diff --git a/server/src/test/java/org/elasticsearch/index/mapper/VersionFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/VersionFieldTypeTests.java deleted file mode 100644 index fb0faf6520712..0000000000000 --- a/server/src/test/java/org/elasticsearch/index/mapper/VersionFieldTypeTests.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.elasticsearch.index.mapper; - -public class VersionFieldTypeTests extends FieldTypeTestCase { - @Override - protected MappedFieldType createDefaultFieldType() { - return new VersionFieldMapper.VersionFieldType(); - } -} diff --git a/server/src/test/java/org/elasticsearch/index/query/ExistsQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/ExistsQueryBuilderTests.java index 9fdef59a4f3dc..876c323218258 100644 --- a/server/src/test/java/org/elasticsearch/index/query/ExistsQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/ExistsQueryBuilderTests.java @@ -83,7 +83,7 @@ protected void doAssertLuceneQuery(ExistsQueryBuilder queryBuilder, Query query, assertThat(constantScoreQuery.getQuery(), instanceOf(DocValuesFieldExistsQuery.class)); DocValuesFieldExistsQuery dvExistsQuery = (DocValuesFieldExistsQuery) constantScoreQuery.getQuery(); assertEquals(field, dvExistsQuery.getField()); - } else if (context.getMapperService().fieldType(field).omitNorms() == false) { + } else if (context.getMapperService().getLuceneFieldType(field).omitNorms() == false) { assertThat(constantScoreQuery.getQuery(), instanceOf(NormsFieldExistsQuery.class)); NormsFieldExistsQuery normsExistsQuery = (NormsFieldExistsQuery) constantScoreQuery.getQuery(); assertEquals(field, normsExistsQuery.getField()); diff --git a/server/src/test/java/org/elasticsearch/index/query/MatchPhrasePrefixQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/MatchPhrasePrefixQueryBuilderTests.java index a7659e54b905d..9631bc3a2b8cb 100644 --- a/server/src/test/java/org/elasticsearch/index/query/MatchPhrasePrefixQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/MatchPhrasePrefixQueryBuilderTests.java @@ -112,7 +112,7 @@ public void testBadAnalyzer() throws IOException { public void testPhraseOnFieldWithNoTerms() { MatchPhrasePrefixQueryBuilder matchQuery = new MatchPhrasePrefixQueryBuilder(DATE_FIELD_NAME, "three term phrase"); matchQuery.analyzer("whitespace"); - expectThrows(IllegalArgumentException.class, () -> matchQuery.doToQuery(createShardContext())); + expectThrows(IllegalStateException.class, () -> matchQuery.doToQuery(createShardContext())); } public void testPhrasePrefixMatchQuery() throws IOException { diff --git a/server/src/test/java/org/elasticsearch/index/query/QueryShardContextTests.java b/server/src/test/java/org/elasticsearch/index/query/QueryShardContextTests.java index dc8e6d48a5adc..e119f9fa6e0a3 100644 --- a/server/src/test/java/org/elasticsearch/index/query/QueryShardContextTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/QueryShardContextTests.java @@ -54,7 +54,7 @@ public class QueryShardContextTests extends ESTestCase { public void testFailIfFieldMappingNotFound() { QueryShardContext context = createQueryShardContext(IndexMetadata.INDEX_UUID_NA_VALUE, null); context.setAllowUnmappedFields(false); - MappedFieldType fieldType = new TextFieldMapper.TextFieldType(); + MappedFieldType fieldType = new TextFieldMapper.TextFieldType("text"); MappedFieldType result = context.failIfFieldMappingNotFound("name", fieldType); assertThat(result, sameInstance(fieldType)); QueryShardException e = expectThrows(QueryShardException.class, () -> context.failIfFieldMappingNotFound("name", null)); @@ -118,7 +118,7 @@ public void testClusterAlias() throws IOException { QueryShardContext context = createQueryShardContext(IndexMetadata.INDEX_UUID_NA_VALUE, clusterAlias); Mapper.BuilderContext ctx = new Mapper.BuilderContext(context.getIndexSettings().getSettings(), new ContentPath()); - IndexFieldMapper mapper = new IndexFieldMapper.Builder(null).build(ctx); + IndexFieldMapper mapper = new IndexFieldMapper.Builder().build(ctx); IndexFieldData forField = context.getForField(mapper.fieldType()); String expected = clusterAlias == null ? context.getIndexSettings().getIndexMetadata().getIndex().getName() diff --git a/server/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java index cd507b3c5a25b..cebff5ee27524 100644 --- a/server/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java @@ -1042,7 +1042,7 @@ public void testExistsFieldQuery() throws Exception { QueryShardContext context = createShardContext(); QueryStringQueryBuilder queryBuilder = new QueryStringQueryBuilder(TEXT_FIELD_NAME + ":*"); Query query = queryBuilder.toQuery(context); - if (context.fieldMapper(TEXT_FIELD_NAME).omitNorms() == false) { + if (context.getMapperService().getLuceneFieldType(TEXT_FIELD_NAME).omitNorms() == false) { assertThat(query, equalTo(new ConstantScoreQuery(new NormsFieldExistsQuery(TEXT_FIELD_NAME)))); } else { assertThat(query, equalTo(new ConstantScoreQuery(new TermQuery(new Term("_field_names", TEXT_FIELD_NAME))))); @@ -1052,7 +1052,7 @@ public void testExistsFieldQuery() throws Exception { String value = (quoted ? "\"" : "") + TEXT_FIELD_NAME + (quoted ? "\"" : ""); queryBuilder = new QueryStringQueryBuilder("_exists_:" + value); query = queryBuilder.toQuery(context); - if (context.fieldMapper(TEXT_FIELD_NAME).omitNorms() == false) { + if (context.getMapperService().getLuceneFieldType(TEXT_FIELD_NAME).omitNorms() == false) { assertThat(query, equalTo(new ConstantScoreQuery(new NormsFieldExistsQuery(TEXT_FIELD_NAME)))); } else { assertThat(query, equalTo(new ConstantScoreQuery(new TermQuery(new Term("_field_names", TEXT_FIELD_NAME))))); diff --git a/server/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java index 588af5dddd36f..088585c6ef2dc 100644 --- a/server/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java @@ -136,9 +136,10 @@ protected void doAssertLuceneQuery(RangeQueryBuilder queryBuilder, Query query, String expectedFieldName = expectedFieldName(queryBuilder.fieldName()); if (queryBuilder.from() == null && queryBuilder.to() == null) { final Query expectedQuery; - if (context.getMapperService().fieldType(queryBuilder.fieldName()).hasDocValues()) { + final MappedFieldType resolvedFieldType = context.getMapperService().fieldType(queryBuilder.fieldName()); + if (resolvedFieldType.hasDocValues()) { expectedQuery = new ConstantScoreQuery(new DocValuesFieldExistsQuery(expectedFieldName)); - } else if (context.getMapperService().fieldType(queryBuilder.fieldName()).omitNorms() == false) { + } else if (context.getMapperService().getLuceneFieldType(resolvedFieldType.name()).omitNorms() == false) { expectedQuery = new ConstantScoreQuery(new NormsFieldExistsQuery(expectedFieldName)); } else { expectedQuery = new ConstantScoreQuery(new TermQuery(new Term(FieldNamesFieldMapper.NAME, expectedFieldName))); diff --git a/server/src/test/java/org/elasticsearch/index/query/functionscore/ScoreFunctionBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/functionscore/ScoreFunctionBuilderTests.java index bfb384e5a5ad1..c66cad93e4bd8 100644 --- a/server/src/test/java/org/elasticsearch/index/query/functionscore/ScoreFunctionBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/functionscore/ScoreFunctionBuilderTests.java @@ -61,8 +61,7 @@ public void testRandomScoreFunctionWithSeed() throws Exception { Mockito.when(context.getShardId()).thenReturn(0); Mockito.when(context.getIndexSettings()).thenReturn(settings); MapperService mapperService = Mockito.mock(MapperService.class); - MappedFieldType ft = new NumberFieldMapper.NumberFieldType(NumberType.LONG); - ft.setName("foo"); + MappedFieldType ft = new NumberFieldMapper.NumberFieldType("foo", NumberType.LONG); Mockito.when(mapperService.fieldType(Mockito.anyString())).thenReturn(ft); Mockito.when(context.getMapperService()).thenReturn(mapperService); builder.toFunction(context); diff --git a/server/src/test/java/org/elasticsearch/index/search/MultiMatchQueryTests.java b/server/src/test/java/org/elasticsearch/index/search/MultiMatchQueryTests.java index 33312d74384b4..d8f9c76c971dc 100644 --- a/server/src/test/java/org/elasticsearch/index/search/MultiMatchQueryTests.java +++ b/server/src/test/java/org/elasticsearch/index/search/MultiMatchQueryTests.java @@ -120,10 +120,8 @@ public void testCrossFieldMultiMatchQuery() throws IOException { } public void testBlendTerms() { - FakeFieldType ft1 = new FakeFieldType(); - ft1.setName("foo"); - FakeFieldType ft2 = new FakeFieldType(); - ft2.setName("bar"); + FakeFieldType ft1 = new FakeFieldType("foo"); + FakeFieldType ft2 = new FakeFieldType("bar"); Term[] terms = new Term[] { new Term("foo", "baz"), new Term("bar", "baz") }; float[] boosts = new float[] {2, 3}; Query expected = BlendedTermQuery.dismaxBlendedQuery(terms, boosts, 1.0f); @@ -134,11 +132,9 @@ public void testBlendTerms() { } public void testBlendTermsWithFieldBoosts() { - FakeFieldType ft1 = new FakeFieldType(); - ft1.setName("foo"); + FakeFieldType ft1 = new FakeFieldType("foo"); ft1.setBoost(100); - FakeFieldType ft2 = new FakeFieldType(); - ft2.setName("bar"); + FakeFieldType ft2 = new FakeFieldType("bar"); ft2.setBoost(10); Term[] terms = new Term[] { new Term("foo", "baz"), new Term("bar", "baz") }; float[] boosts = new float[] {200, 30}; @@ -150,15 +146,13 @@ public void testBlendTermsWithFieldBoosts() { } public void testBlendTermsUnsupportedValueWithLenient() { - FakeFieldType ft1 = new FakeFieldType(); - ft1.setName("foo"); - FakeFieldType ft2 = new FakeFieldType() { + FakeFieldType ft1 = new FakeFieldType("foo"); + FakeFieldType ft2 = new FakeFieldType("bar") { @Override public Query termQuery(Object value, QueryShardContext context) { throw new IllegalArgumentException(); } }; - ft2.setName("bar"); Term[] terms = new Term[] { new Term("foo", "baz") }; float[] boosts = new float[] {2}; Query expected = new DisjunctionMaxQuery(Arrays.asList( @@ -172,28 +166,25 @@ public Query termQuery(Object value, QueryShardContext context) { } public void testBlendTermsUnsupportedValueWithoutLenient() { - FakeFieldType ft = new FakeFieldType() { + FakeFieldType ft = new FakeFieldType("bar") { @Override public Query termQuery(Object value, QueryShardContext context) { throw new IllegalArgumentException(); } }; - ft.setName("bar"); expectThrows(IllegalArgumentException.class, () -> MultiMatchQuery.blendTerm( indexService.newQueryShardContext(randomInt(20), null, () -> { throw new UnsupportedOperationException(); }, null), new BytesRef("baz"), 1f, false, Arrays.asList(new FieldAndBoost(ft, 1)))); } public void testBlendNoTermQuery() { - FakeFieldType ft1 = new FakeFieldType(); - ft1.setName("foo"); - FakeFieldType ft2 = new FakeFieldType() { + FakeFieldType ft1 = new FakeFieldType("foo"); + FakeFieldType ft2 = new FakeFieldType("bar") { @Override public Query termQuery(Object value, QueryShardContext context) { return new MatchAllDocsQuery(); } }; - ft2.setName("bar"); Term[] terms = new Term[] { new Term("foo", "baz") }; float[] boosts = new float[] {2}; Query expectedDisjunct1 = BlendedTermQuery.dismaxBlendedQuery(terms, boosts, 1.0f); diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/GlobalAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/GlobalAggregatorTests.java index 433f08618efc6..d1a728383859c 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/GlobalAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/GlobalAggregatorTests.java @@ -75,8 +75,7 @@ private void testCase(CheckedConsumer buildIndex GlobalAggregationBuilder aggregationBuilder = new GlobalAggregationBuilder("_name"); aggregationBuilder.subAggregation(new MinAggregationBuilder("in_global").field("number")); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - fieldType.setName("number"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.LONG); GlobalAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType); aggregator.preCollection(); diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/composite/CompositeAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/composite/CompositeAggregatorTests.java index 3f43a2e017ea7..0cd8b40372651 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/composite/CompositeAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/composite/CompositeAggregatorTests.java @@ -110,17 +110,9 @@ public class CompositeAggregatorTests extends AggregatorTestCase { public void setUp() throws Exception { super.setUp(); FIELD_TYPES = new MappedFieldType[8]; - FIELD_TYPES[0] = new KeywordFieldMapper.KeywordFieldType(); - FIELD_TYPES[0].setName("keyword"); - FIELD_TYPES[0].setHasDocValues(true); - - FIELD_TYPES[1] = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - FIELD_TYPES[1].setName("long"); - FIELD_TYPES[1].setHasDocValues(true); - - FIELD_TYPES[2] = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE); - FIELD_TYPES[2].setName("double"); - FIELD_TYPES[2].setHasDocValues(true); + FIELD_TYPES[0] = new KeywordFieldMapper.KeywordFieldType("keyword"); + FIELD_TYPES[1] = new NumberFieldMapper.NumberFieldType("long", NumberFieldMapper.NumberType.LONG); + FIELD_TYPES[2] = new NumberFieldMapper.NumberFieldType("double", NumberFieldMapper.NumberType.DOUBLE); DateFieldMapper.Builder builder = new DateFieldMapper.Builder("date"); builder.docValues(true); @@ -129,21 +121,10 @@ public void setUp() throws Exception { builder.build(new Mapper.BuilderContext(createIndexSettings().getSettings(), new ContentPath(0))); FIELD_TYPES[3] = fieldMapper.fieldType(); - FIELD_TYPES[4] = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - FIELD_TYPES[4].setName("price"); - FIELD_TYPES[4].setHasDocValues(true); - - FIELD_TYPES[5] = new KeywordFieldMapper.KeywordFieldType(); - FIELD_TYPES[5].setName("terms"); - FIELD_TYPES[5].setHasDocValues(true); - - FIELD_TYPES[6] = new IpFieldMapper.IpFieldType(); - FIELD_TYPES[6].setName("ip"); - FIELD_TYPES[6].setHasDocValues(true); - - FIELD_TYPES[7] = new GeoPointFieldMapper.GeoPointFieldType(); - FIELD_TYPES[7].setName("geo_point"); - FIELD_TYPES[7].setHasDocValues(true); + FIELD_TYPES[4] = new NumberFieldMapper.NumberFieldType("price", NumberFieldMapper.NumberType.INTEGER); + FIELD_TYPES[5] = new KeywordFieldMapper.KeywordFieldType("terms"); + FIELD_TYPES[6] = new IpFieldMapper.IpFieldType("ip"); + FIELD_TYPES[7] = new GeoPointFieldMapper.GeoPointFieldType("geo_point"); } @Override diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/composite/CompositeValuesCollectorQueueTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/composite/CompositeValuesCollectorQueueTests.java index ff893314e5988..8cb35b6b861b4 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/composite/CompositeValuesCollectorQueueTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/composite/CompositeValuesCollectorQueueTests.java @@ -27,7 +27,6 @@ import org.apache.lucene.document.TextField; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.DocValues; -import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.index.LeafReaderContext; @@ -344,21 +343,11 @@ public void collect(int doc, long bucket) throws IOException { } private static MappedFieldType createNumber(String name, NumberFieldMapper.NumberType type) { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(type); - fieldType.setIndexOptions(IndexOptions.DOCS); - fieldType.setName(name); - fieldType.setHasDocValues(true); - fieldType.freeze(); - return fieldType; + return new NumberFieldMapper.NumberFieldType(name, type); } private static MappedFieldType createKeyword(String name) { - MappedFieldType fieldType = new KeywordFieldMapper.KeywordFieldType(); - fieldType.setIndexOptions(IndexOptions.DOCS); - fieldType.setName(name); - fieldType.setHasDocValues(true); - fieldType.freeze(); - return fieldType; + return new KeywordFieldMapper.KeywordFieldType(name); } private static int compareKey(CompositeKey key1, CompositeKey key2) { diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/composite/SingleDimensionValuesSourceTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/composite/SingleDimensionValuesSourceTests.java index a34ec7ada20a6..fdb84a7b53df2 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/composite/SingleDimensionValuesSourceTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/composite/SingleDimensionValuesSourceTests.java @@ -41,8 +41,7 @@ public class SingleDimensionValuesSourceTests extends ESTestCase { public void testBinarySorted() { - MappedFieldType keyword = new KeywordFieldMapper.KeywordFieldType(); - keyword.setName("keyword"); + MappedFieldType keyword = new KeywordFieldMapper.KeywordFieldType("keyword"); BinaryValuesSource source = new BinaryValuesSource( BigArrays.NON_RECYCLING_INSTANCE, (b) -> {}, @@ -86,8 +85,7 @@ public void testBinarySorted() { ); assertNull(source.createSortedDocsProducerOrNull(reader, null)); - MappedFieldType ip = new IpFieldMapper.IpFieldType(); - ip.setName("ip"); + MappedFieldType ip = new IpFieldMapper.IpFieldType("ip"); source = new BinaryValuesSource( BigArrays.NON_RECYCLING_INSTANCE, (b) -> {}, @@ -101,8 +99,7 @@ public void testBinarySorted() { } public void testGlobalOrdinalsSorted() { - final MappedFieldType keyword = new KeywordFieldMapper.KeywordFieldType(); - keyword.setName("keyword"); + final MappedFieldType keyword = new KeywordFieldMapper.KeywordFieldType("keyword"); GlobalOrdinalValuesSource source = new GlobalOrdinalValuesSource( BigArrays.NON_RECYCLING_INSTANCE, keyword, context -> null, @@ -144,8 +141,7 @@ public void testGlobalOrdinalsSorted() { assertNull(source.createSortedDocsProducerOrNull(reader, null)); assertNull(source.createSortedDocsProducerOrNull(reader, new TermQuery(new Term("foo", "bar")))); - final MappedFieldType ip = new IpFieldMapper.IpFieldType(); - ip.setName("ip"); + final MappedFieldType ip = new IpFieldMapper.IpFieldType("ip"); source = new GlobalOrdinalValuesSource( BigArrays.NON_RECYCLING_INSTANCE, ip, @@ -161,8 +157,7 @@ public void testGlobalOrdinalsSorted() { public void testNumericSorted() { for (NumberFieldMapper.NumberType numberType : NumberFieldMapper.NumberType.values()) { - MappedFieldType number = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - number.setName("number"); + MappedFieldType number = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.LONG); final SingleDimensionValuesSource source; if (numberType == NumberFieldMapper.NumberType.BYTE || numberType == NumberFieldMapper.NumberType.SHORT || diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/filter/FilterAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/filter/FilterAggregatorTests.java index 9e57545a47bc5..5afad0400149a 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/filter/FilterAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/filter/FilterAggregatorTests.java @@ -21,7 +21,6 @@ import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.DirectoryReader; -import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.search.IndexSearcher; @@ -52,10 +51,7 @@ public class FilterAggregatorTests extends AggregatorTestCase { @Before public void setUpTest() throws Exception { super.setUp(); - fieldType = new KeywordFieldMapper.KeywordFieldType(); - fieldType.setHasDocValues(true); - fieldType.setIndexOptions(IndexOptions.DOCS); - fieldType.setName("field"); + fieldType = new KeywordFieldMapper.KeywordFieldType("field"); } public void testEmpty() throws Exception { @@ -88,7 +84,7 @@ public void testRandom() throws Exception { } int value = randomInt(maxTerm-1); expectedBucketCount[value] += 1; - document.add(new Field("field", Integer.toString(value), fieldType)); + document.add(new Field("field", Integer.toString(value), KeywordFieldMapper.Defaults.FIELD_TYPE)); indexWriter.addDocument(document); document.clear(); } @@ -126,7 +122,7 @@ public void testRandom() throws Exception { public void testBucketComparator() throws IOException { try (Directory directory = newDirectory()) { try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) { - indexWriter.addDocument(singleton(new Field("field", "1", fieldType))); + indexWriter.addDocument(singleton(new Field("field", "1", KeywordFieldMapper.Defaults.FIELD_TYPE))); } try (IndexReader indexReader = DirectoryReader.open(directory)) { IndexSearcher indexSearcher = newSearcher(indexReader, true, true); diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/filter/FiltersAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/filter/FiltersAggregatorTests.java index 90a1a11dde26f..265115e720600 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/filter/FiltersAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/filter/FiltersAggregatorTests.java @@ -21,7 +21,6 @@ import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.DirectoryReader; -import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.search.IndexSearcher; @@ -45,10 +44,7 @@ public class FiltersAggregatorTests extends AggregatorTestCase { @Before public void setUpTest() throws Exception { super.setUp(); - fieldType = new KeywordFieldMapper.KeywordFieldType(); - fieldType.setHasDocValues(true); - fieldType.setIndexOptions(IndexOptions.DOCS); - fieldType.setName("field"); + fieldType = new KeywordFieldMapper.KeywordFieldType("field"); } public void testEmpty() throws Exception { @@ -78,28 +74,28 @@ public void testKeyedFilter() throws Exception { Directory directory = newDirectory(); RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory); Document document = new Document(); - document.add(new Field("field", "foo", fieldType)); + document.add(new Field("field", "foo", KeywordFieldMapper.Defaults.FIELD_TYPE)); indexWriter.addDocument(document); document.clear(); - document.add(new Field("field", "else", fieldType)); + document.add(new Field("field", "else", KeywordFieldMapper.Defaults.FIELD_TYPE)); indexWriter.addDocument(document); // make sure we have more than one segment to test the merge indexWriter.commit(); - document.add(new Field("field", "foo", fieldType)); + document.add(new Field("field", "foo", KeywordFieldMapper.Defaults.FIELD_TYPE)); indexWriter.addDocument(document); document.clear(); - document.add(new Field("field", "bar", fieldType)); + document.add(new Field("field", "bar", KeywordFieldMapper.Defaults.FIELD_TYPE)); indexWriter.addDocument(document); document.clear(); - document.add(new Field("field", "foobar", fieldType)); + document.add(new Field("field", "foobar", KeywordFieldMapper.Defaults.FIELD_TYPE)); indexWriter.addDocument(document); indexWriter.commit(); document.clear(); - document.add(new Field("field", "something", fieldType)); + document.add(new Field("field", "something", KeywordFieldMapper.Defaults.FIELD_TYPE)); indexWriter.addDocument(document); indexWriter.commit(); document.clear(); - document.add(new Field("field", "foobar", fieldType)); + document.add(new Field("field", "foobar", KeywordFieldMapper.Defaults.FIELD_TYPE)); indexWriter.addDocument(document); indexWriter.close(); @@ -152,7 +148,7 @@ public void testRandom() throws Exception { } int value = randomInt(maxTerm-1); expectedBucketCount[value] += 1; - document.add(new Field("field", Integer.toString(value), fieldType)); + document.add(new Field("field", Integer.toString(value), KeywordFieldMapper.Defaults.FIELD_TYPE)); indexWriter.addDocument(document); document.clear(); } diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoGridAggregatorTestCase.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoGridAggregatorTestCase.java index f5833e9f0036a..c8dc51e6d5a79 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoGridAggregatorTestCase.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoGridAggregatorTestCase.java @@ -293,9 +293,7 @@ private void testCase(Query query, int precision, GeoBoundingBox geoBoundingBox, assertThat(aggregationBuilder.geoBoundingBox(), equalTo(geoBoundingBox)); } - MappedFieldType fieldType = new GeoPointFieldMapper.GeoPointFieldType(); - fieldType.setHasDocValues(true); - fieldType.setName(FIELD_NAME); + MappedFieldType fieldType = new GeoPointFieldMapper.GeoPointFieldType(FIELD_NAME); Aggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType); aggregator.preCollection(); diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/AutoDateHistogramAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/AutoDateHistogramAggregatorTests.java index 8e2239d18ed3d..406bca2cadcfa 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/AutoDateHistogramAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/AutoDateHistogramAggregatorTests.java @@ -216,10 +216,7 @@ public void testAggregateWrongField() throws IOException { AutoDateHistogramAggregationBuilder aggregation = new AutoDateHistogramAggregationBuilder("_name"). setNumBuckets(10).field("bogus_bogus"); - final DateFieldMapper.Builder builder = new DateFieldMapper.Builder("_name"); - final DateFieldMapper.DateFieldType fieldType = builder.fieldType(); - fieldType.setHasDocValues(true); - fieldType.setName("date_field"); + final DateFieldMapper.DateFieldType fieldType = new DateFieldMapper.DateFieldType("date_field"); testCase(aggregation, DEFAULT_QUERY, iw -> {}, @@ -233,10 +230,7 @@ public void testUnmappedMissing() throws IOException { AutoDateHistogramAggregationBuilder aggregation = new AutoDateHistogramAggregationBuilder("_name"). setNumBuckets(10).field("bogus_bogus").missing("2017-12-12"); - final DateFieldMapper.Builder builder = new DateFieldMapper.Builder("_name"); - final DateFieldMapper.DateFieldType fieldType = builder.fieldType(); - fieldType.setHasDocValues(true); - fieldType.setName("date_field"); + final DateFieldMapper.DateFieldType fieldType = new DateFieldMapper.DateFieldType("date_field"); testCase(aggregation, DEFAULT_QUERY, iw -> {}, @@ -829,18 +823,12 @@ private void executeTestCase(final boolean reduced, final Query query, final Lis configure.accept(aggregationBuilder); } - final DateFieldMapper.Builder builder = new DateFieldMapper.Builder("_name"); - final DateFieldMapper.DateFieldType fieldType = builder.fieldType(); - fieldType.setHasDocValues(true); - fieldType.setName(aggregationBuilder.field()); + final DateFieldMapper.DateFieldType fieldType = new DateFieldMapper.DateFieldType(aggregationBuilder.field()); - MappedFieldType instantFieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - instantFieldType.setName(INSTANT_FIELD); - instantFieldType.setHasDocValues(true); - - MappedFieldType numericFieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - numericFieldType.setName(NUMERIC_FIELD); - numericFieldType.setHasDocValues(true); + MappedFieldType instantFieldType + = new NumberFieldMapper.NumberFieldType(INSTANT_FIELD, NumberFieldMapper.NumberType.LONG); + MappedFieldType numericFieldType + = new NumberFieldMapper.NumberFieldType(NUMERIC_FIELD, NumberFieldMapper.NumberType.LONG); final InternalAutoDateHistogram histogram; if (reduced) { diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramAggregatorTests.java index 84b1ef2352e86..19ced2aa074ca 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramAggregatorTests.java @@ -24,7 +24,6 @@ import org.apache.lucene.document.SortedNumericDocValuesField; import org.apache.lucene.document.SortedSetDocValuesField; import org.apache.lucene.index.DirectoryReader; -import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.search.IndexSearcher; @@ -170,12 +169,8 @@ public void testMatchAllDocs() throws IOException { } public void testAsSubAgg() throws IOException { - KeywordFieldMapper.KeywordFieldType k1ft = new KeywordFieldMapper.KeywordFieldType(); - k1ft.setName("k1"); - k1ft.setHasDocValues(true); - KeywordFieldMapper.KeywordFieldType k2ft = new KeywordFieldMapper.KeywordFieldType(); - k2ft.setName("k2"); - k2ft.setHasDocValues(true); + KeywordFieldMapper.KeywordFieldType k1ft = new KeywordFieldMapper.KeywordFieldType("k1"); + KeywordFieldMapper.KeywordFieldType k2ft = new KeywordFieldMapper.KeywordFieldType("k2"); DateFieldMapper.DateFieldType dft = aggregableDateFieldType(false, randomBoolean()); CheckedConsumer buildIndex = iw -> { iw.addDocument(List.of( @@ -1280,14 +1275,10 @@ private void executeTestCase(boolean reduced, } private DateFieldMapper.DateFieldType aggregableDateFieldType(boolean useNanosecondResolution, boolean isSearchable) { - DateFieldMapper.Builder builder = new DateFieldMapper.Builder(AGGREGABLE_DATE); - if (useNanosecondResolution) { - builder.withResolution(DateFieldMapper.Resolution.NANOSECONDS); - } - DateFieldMapper.DateFieldType fieldType = builder.fieldType(); - fieldType.setIndexOptions(isSearchable ? IndexOptions.DOCS : IndexOptions.NONE); - fieldType.setName(AGGREGABLE_DATE); - return fieldType; + return new DateFieldMapper.DateFieldType(AGGREGABLE_DATE, isSearchable, true, + DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER, + useNanosecondResolution ? DateFieldMapper.Resolution.NANOSECONDS : DateFieldMapper.Resolution.MILLISECONDS, + Collections.emptyMap()); } private static long asLong(String dateTime) { diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/DateRangeHistogramAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/DateRangeHistogramAggregatorTests.java index af6f51da50a8c..23f3baf284a2c 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/DateRangeHistogramAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/DateRangeHistogramAggregatorTests.java @@ -97,8 +97,7 @@ public void testUnsupportedRangeType() throws Exception { .field(fieldName) .calendarInterval(DateHistogramInterval.MONTH); - MappedFieldType fieldType = new RangeFieldMapper.Builder(fieldName, rangeType).fieldType(); - fieldType.setName(fieldName); + MappedFieldType fieldType = new RangeFieldMapper.RangeFieldType(fieldName, rangeType); try (IndexReader reader = w.getReader()) { IndexSearcher searcher = new IndexSearcher(reader); @@ -663,8 +662,7 @@ private void testCase(Query query, Consumer configure, CheckedConsumer buildIndex, Consumer verify) throws IOException { - MappedFieldType fieldType = new RangeFieldMapper.Builder(FIELD_NAME, RangeType.DATE).fieldType(); - fieldType.setName(FIELD_NAME); + MappedFieldType fieldType = new RangeFieldMapper.RangeFieldType(FIELD_NAME, RangeFieldMapper.Defaults.DATE_FORMATTER); final DateHistogramAggregationBuilder aggregationBuilder = new DateHistogramAggregationBuilder("_name").field(FIELD_NAME); if (configure != null) { configure.accept(aggregationBuilder); diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/NumericHistogramAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/NumericHistogramAggregatorTests.java index bafbca416390f..b41aa2896d3be 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/NumericHistogramAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/NumericHistogramAggregatorTests.java @@ -388,8 +388,7 @@ public void testExtendedBounds() throws Exception { .field("field") .interval(5) .extendedBounds(-12, 13); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE); - fieldType.setName("field"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.DOUBLE); try (IndexReader reader = w.getReader()) { IndexSearcher searcher = new IndexSearcher(reader); InternalHistogram histogram = searchAndReduce(searcher, new MatchAllDocsQuery(), aggBuilder, doubleField("field")); diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/RangeHistogramAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/RangeHistogramAggregatorTests.java index 9cc63f3352fa1..990c8840c3f9e 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/RangeHistogramAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/RangeHistogramAggregatorTests.java @@ -68,6 +68,7 @@ public void testDoubles() throws Exception { HistogramAggregationBuilder aggBuilder = new HistogramAggregationBuilder("my_agg") .field("field") .interval(5); + try (IndexReader reader = w.getReader()) { IndexSearcher searcher = new IndexSearcher(reader); InternalHistogram histogram = search(searcher, new MatchAllDocsQuery(), aggBuilder, rangeField("field", rangeType)); @@ -113,6 +114,7 @@ public void testLongs() throws Exception { HistogramAggregationBuilder aggBuilder = new HistogramAggregationBuilder("my_agg") .field("field") .interval(5); + try (IndexReader reader = w.getReader()) { IndexSearcher searcher = new IndexSearcher(reader); InternalHistogram histogram = search(searcher, new MatchAllDocsQuery(), aggBuilder, rangeField("field", rangeType)); @@ -156,6 +158,7 @@ public void testMultipleRanges() throws Exception { HistogramAggregationBuilder aggBuilder = new HistogramAggregationBuilder("my_agg") .field("field") .interval(5); + try (IndexReader reader = w.getReader()) { IndexSearcher searcher = new IndexSearcher(reader); InternalHistogram histogram = search(searcher, new MatchAllDocsQuery(), aggBuilder, rangeField("field", rangeType)); @@ -200,6 +203,7 @@ public void testMultipleRangesLotsOfOverlap() throws Exception { HistogramAggregationBuilder aggBuilder = new HistogramAggregationBuilder("my_agg") .field("field") .interval(5); + try (IndexReader reader = w.getReader()) { IndexSearcher searcher = new IndexSearcher(reader); InternalHistogram histogram = search(searcher, new MatchAllDocsQuery(), aggBuilder, rangeField("field", rangeType)); @@ -236,6 +240,7 @@ public void testLongsIrrationalInterval() throws Exception { HistogramAggregationBuilder aggBuilder = new HistogramAggregationBuilder("my_agg") .field("field") .interval(Math.PI); + try (IndexReader reader = w.getReader()) { IndexSearcher searcher = new IndexSearcher(reader); InternalHistogram histogram = search(searcher, new MatchAllDocsQuery(), aggBuilder, rangeField("field", rangeType)); @@ -282,6 +287,7 @@ public void testMinDocCount() throws Exception { .field("field") .interval(5) .minDocCount(2); + try (IndexReader reader = w.getReader()) { IndexSearcher searcher = new IndexSearcher(reader); InternalHistogram histogram = searchAndReduce( @@ -321,6 +327,7 @@ public void testOffset() throws Exception { .field("field") .interval(5) .offset(4); + try (IndexReader reader = w.getReader()) { IndexSearcher searcher = new IndexSearcher(reader); InternalHistogram histogram = search(searcher, new MatchAllDocsQuery(), aggBuilder, rangeField("field", rangeType)); @@ -376,6 +383,7 @@ public void testOffsetGtInterval() throws Exception { .field("field") .interval(interval) .offset(offset); + try (IndexReader reader = w.getReader()) { IndexSearcher searcher = new IndexSearcher(reader); InternalHistogram histogram = search(searcher, new MatchAllDocsQuery(), aggBuilder, rangeField("field", rangeType)); diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/missing/MissingAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/missing/MissingAggregatorTests.java index fe0695a98b0e4..517f1bd07c8ae 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/missing/MissingAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/missing/MissingAggregatorTests.java @@ -77,8 +77,8 @@ public class MissingAggregatorTests extends AggregatorTestCase { public void testMatchNoDocs() throws IOException { final int numDocs = randomIntBetween(10, 200); - final MappedFieldType fieldType = new NumberFieldMapper.Builder("_name", NumberType.LONG).fieldType(); - fieldType.setName("field"); + final MappedFieldType fieldType + = new NumberFieldMapper.NumberFieldType("field", NumberType.LONG); final MissingAggregationBuilder builder = new MissingAggregationBuilder("_name") .field(fieldType.name()); @@ -102,10 +102,8 @@ public void testMatchNoDocs() throws IOException { public void testMatchAllDocs() throws IOException { int numDocs = randomIntBetween(10, 200); - final MappedFieldType aggFieldType = new NumberFieldMapper.Builder("_name", NumberType.LONG).fieldType(); - aggFieldType.setName("agg_field"); - final MappedFieldType anotherFieldType = new NumberFieldMapper.Builder("_name", NumberType.LONG).fieldType(); - anotherFieldType.setName("another_field"); + final MappedFieldType aggFieldType = new NumberFieldMapper.NumberFieldType("agg_field", NumberType.LONG); + final MappedFieldType anotherFieldType = new NumberFieldMapper.NumberFieldType("another_field", NumberType.LONG); final MissingAggregationBuilder builder = new MissingAggregationBuilder("_name") .field(aggFieldType.name()); @@ -127,10 +125,8 @@ public void testMatchAllDocs() throws IOException { } public void testMatchSparse() throws IOException { - final MappedFieldType aggFieldType = new NumberFieldMapper.Builder("_name", NumberType.LONG).fieldType(); - aggFieldType.setName("agg_field"); - final MappedFieldType anotherFieldType = new NumberFieldMapper.Builder("_name", NumberType.LONG).fieldType(); - anotherFieldType.setName("another_field"); + final MappedFieldType aggFieldType = new NumberFieldMapper.NumberFieldType("agg_field", NumberType.LONG); + final MappedFieldType anotherFieldType = new NumberFieldMapper.NumberFieldType("another_field", NumberType.LONG); final MissingAggregationBuilder builder = new MissingAggregationBuilder("_name") .field(aggFieldType.name()); @@ -162,10 +158,8 @@ public void testMatchSparse() throws IOException { public void testMatchSparseRangeField() throws IOException { final RangeType rangeType = RangeType.DOUBLE; - MappedFieldType aggFieldType = new RangeFieldMapper.Builder("_name", rangeType).fieldType(); - aggFieldType.setName("agg_field"); - final MappedFieldType anotherFieldType = new RangeFieldMapper.Builder("_name", rangeType).fieldType(); - anotherFieldType.setName("another_field"); + final MappedFieldType aggFieldType = new RangeFieldMapper.RangeFieldType("agg_field", rangeType); + final MappedFieldType anotherFieldType = new RangeFieldMapper.RangeFieldType("another_field", rangeType); final RangeFieldMapper.Range range = new RangeFieldMapper.Range(rangeType, 1.0D, 5.0D, true, true); final BytesRef encodedRange = rangeType.encodeRanges(singleton(range)); @@ -201,8 +195,7 @@ public void testMatchSparseRangeField() throws IOException { public void testUnmappedWithoutMissingParam() throws IOException { final int numDocs = randomIntBetween(10, 20); - final MappedFieldType aggFieldType = new NumberFieldMapper.Builder("_name", NumberType.LONG).fieldType(); - aggFieldType.setName("agg_field"); + final MappedFieldType aggFieldType = new NumberFieldMapper.NumberFieldType("agg_field", NumberType.LONG); final MissingAggregationBuilder builder = new MissingAggregationBuilder("_name") .field("unknown_field"); @@ -225,8 +218,7 @@ public void testUnmappedWithoutMissingParam() throws IOException { public void testUnmappedWithMissingParam() throws IOException { final int numDocs = randomIntBetween(10, 20); - final MappedFieldType aggFieldType = new NumberFieldMapper.Builder("_name", NumberType.LONG).fieldType(); - aggFieldType.setName("agg_field"); + final MappedFieldType aggFieldType = new NumberFieldMapper.NumberFieldType("agg_field", NumberType.LONG); final MissingAggregationBuilder builder = new MissingAggregationBuilder("_name") .field("unknown_field") @@ -251,10 +243,8 @@ public void testUnmappedWithMissingParam() throws IOException { public void testMissingParam() throws IOException { final int numDocs = randomIntBetween(10, 20); - final MappedFieldType aggFieldType = new NumberFieldMapper.Builder("_name", NumberType.LONG).fieldType(); - aggFieldType.setName("agg_field"); - final MappedFieldType anotherFieldType = new NumberFieldMapper.Builder("_name", NumberType.LONG).fieldType(); - anotherFieldType.setName("another_field"); + final MappedFieldType aggFieldType = new NumberFieldMapper.NumberFieldType("agg_field", NumberType.LONG); + final MappedFieldType anotherFieldType = new NumberFieldMapper.NumberFieldType("another_field", NumberType.LONG); final MissingAggregationBuilder builder = new MissingAggregationBuilder("_name") .field(aggFieldType.name()) @@ -277,10 +267,8 @@ public void testMissingParam() throws IOException { } public void testMultiValuedField() throws IOException { - final MappedFieldType aggFieldType = new NumberFieldMapper.Builder("_name", NumberType.LONG).fieldType(); - aggFieldType.setName("agg_field"); - final MappedFieldType anotherFieldType = new NumberFieldMapper.Builder("_name", NumberType.LONG).fieldType(); - anotherFieldType.setName("another_field"); + final MappedFieldType aggFieldType = new NumberFieldMapper.NumberFieldType("agg_field", NumberType.LONG); + final MappedFieldType anotherFieldType = new NumberFieldMapper.NumberFieldType("another_field", NumberType.LONG); final MissingAggregationBuilder builder = new MissingAggregationBuilder("_name") .field(aggFieldType.name()); @@ -323,10 +311,8 @@ public void testSingleValuedFieldWithValueScriptWithParams() throws IOException } private void valueScriptTestCase(Script script) throws IOException { - final MappedFieldType aggFieldType = new NumberFieldMapper.Builder("_name", NumberType.LONG).fieldType(); - aggFieldType.setName("agg_field"); - final MappedFieldType anotherFieldType = new NumberFieldMapper.Builder("_name", NumberType.LONG).fieldType(); - anotherFieldType.setName("another_field"); + final MappedFieldType aggFieldType = new NumberFieldMapper.NumberFieldType("agg_field", NumberType.LONG); + final MappedFieldType anotherFieldType = new NumberFieldMapper.NumberFieldType("another_field", NumberType.LONG); final MissingAggregationBuilder builder = new MissingAggregationBuilder("_name") .field(aggFieldType.name()) @@ -369,8 +355,7 @@ public void testMultiValuedFieldWithFieldScript() throws IOException { } private void fieldScriptTestCase(Script script, long threshold) throws IOException { - final MappedFieldType aggFieldType = new NumberFieldMapper.Builder("_name", NumberType.LONG).fieldType(); - aggFieldType.setName("agg_field"); + final MappedFieldType aggFieldType = new NumberFieldMapper.NumberFieldType("agg_field", NumberType.LONG); final MissingAggregationBuilder builder = new MissingAggregationBuilder("_name") .script(script); diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/nested/NestedAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/nested/NestedAggregatorTests.java index e2fbcc067c455..ff9184ae7230b 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/nested/NestedAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/nested/NestedAggregatorTests.java @@ -151,9 +151,7 @@ public void testNoDocs() throws IOException { MaxAggregationBuilder maxAgg = new MaxAggregationBuilder(MAX_AGG_NAME) .field(VALUE_FIELD_NAME); nestedBuilder.subAggregation(maxAgg); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType( - NumberFieldMapper.NumberType.LONG); - fieldType.setName(VALUE_FIELD_NAME); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(VALUE_FIELD_NAME, NumberFieldMapper.NumberType.LONG); InternalNested nested = search(newSearcher(indexReader, false, true), new MatchAllDocsQuery(), nestedBuilder, fieldType); @@ -198,9 +196,7 @@ public void testSingleNestingMax() throws IOException { MaxAggregationBuilder maxAgg = new MaxAggregationBuilder(MAX_AGG_NAME) .field(VALUE_FIELD_NAME); nestedBuilder.subAggregation(maxAgg); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType( - NumberFieldMapper.NumberType.LONG); - fieldType.setName(VALUE_FIELD_NAME); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(VALUE_FIELD_NAME, NumberFieldMapper.NumberType.LONG); InternalNested nested = search(newSearcher(indexReader, false, true), new MatchAllDocsQuery(), nestedBuilder, fieldType); @@ -252,9 +248,7 @@ public void testDoubleNestingMax() throws IOException { .field(VALUE_FIELD_NAME); nestedBuilder.subAggregation(maxAgg); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType( - NumberFieldMapper.NumberType.LONG); - fieldType.setName(VALUE_FIELD_NAME); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(VALUE_FIELD_NAME, NumberFieldMapper.NumberType.LONG); InternalNested nested = search(newSearcher(indexReader, false, true), new MatchAllDocsQuery(), nestedBuilder, fieldType); @@ -309,9 +303,7 @@ public void testOrphanedDocs() throws IOException { SumAggregationBuilder sumAgg = new SumAggregationBuilder(SUM_AGG_NAME) .field(VALUE_FIELD_NAME); nestedBuilder.subAggregation(sumAgg); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType( - NumberFieldMapper.NumberType.LONG); - fieldType.setName(VALUE_FIELD_NAME); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(VALUE_FIELD_NAME, NumberFieldMapper.NumberType.LONG); InternalNested nested = search(newSearcher(indexReader, false, true), new MatchAllDocsQuery(), nestedBuilder, fieldType); @@ -390,9 +382,7 @@ public void testResetRootDocId() throws Exception { NestedAggregationBuilder nestedBuilder = new NestedAggregationBuilder(NESTED_AGG, "nested_field"); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType( - NumberFieldMapper.NumberType.LONG); - fieldType.setName(VALUE_FIELD_NAME); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(VALUE_FIELD_NAME, NumberFieldMapper.NumberType.LONG); BooleanQuery.Builder bq = new BooleanQuery.Builder(); bq.add(Queries.newNonNestedFilter(), BooleanClause.Occur.MUST); @@ -425,11 +415,9 @@ public void testNestedOrdering() throws IOException { iw.addDocuments(generateBook("9", new String[]{"g", "c", "e"}, new int[]{18, 8})); } try (IndexReader indexReader = wrapInMockESDirectoryReader(DirectoryReader.open(directory))) { - MappedFieldType fieldType1 = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - fieldType1.setName("num_pages"); - MappedFieldType fieldType2 = new KeywordFieldMapper.KeywordFieldType(); - fieldType2.setHasDocValues(true); - fieldType2.setName("author"); + MappedFieldType fieldType1 + = new NumberFieldMapper.NumberFieldType("num_pages", NumberFieldMapper.NumberType.LONG); + MappedFieldType fieldType2 = new KeywordFieldMapper.KeywordFieldType("author"); TermsAggregationBuilder termsBuilder = new TermsAggregationBuilder("authors").userValueTypeHint(ValueType.STRING) .field("author").order(BucketOrder.aggregation("chapters>num_pages.value", true)); @@ -563,11 +551,9 @@ public void testNestedOrdering_random() throws IOException { } }); try (IndexReader indexReader = wrapInMockESDirectoryReader(DirectoryReader.open(directory))) { - MappedFieldType fieldType1 = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - fieldType1.setName("num_pages"); - MappedFieldType fieldType2 = new KeywordFieldMapper.KeywordFieldType(); - fieldType2.setHasDocValues(true); - fieldType2.setName("author"); + MappedFieldType fieldType1 + = new NumberFieldMapper.NumberFieldType("num_pages", NumberFieldMapper.NumberType.LONG); + MappedFieldType fieldType2 = new KeywordFieldMapper.KeywordFieldType("author"); TermsAggregationBuilder termsBuilder = new TermsAggregationBuilder("authors").userValueTypeHint(ValueType.STRING) .size(books.size()).field("author") @@ -671,12 +657,8 @@ public void testPreGetChildLeafCollectors() throws IOException { FilterAggregationBuilder filterAggregationBuilder = new FilterAggregationBuilder("filterAgg", new MatchAllQueryBuilder()); filterAggregationBuilder.subAggregation(nestedBuilder); - MappedFieldType fieldType1 = new KeywordFieldMapper.KeywordFieldType(); - fieldType1.setName("key"); - fieldType1.setHasDocValues(true); - MappedFieldType fieldType2 = new KeywordFieldMapper.KeywordFieldType(); - fieldType2.setName("value"); - fieldType2.setHasDocValues(true); + MappedFieldType fieldType1 = new KeywordFieldMapper.KeywordFieldType("key"); + MappedFieldType fieldType2 = new KeywordFieldMapper.KeywordFieldType("value"); Filter filter = search(newSearcher(indexReader, false, true), Queries.newNonNestedFilter(), filterAggregationBuilder, fieldType1, fieldType2); @@ -712,9 +694,7 @@ public void testFieldAlias() throws IOException { int numRootDocs = randomIntBetween(1, 20); int expectedNestedDocs = 0; - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType( - NumberFieldMapper.NumberType.LONG); - fieldType.setName(VALUE_FIELD_NAME); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(VALUE_FIELD_NAME, NumberFieldMapper.NumberType.LONG); try (Directory directory = newDirectory()) { try (RandomIndexWriter iw = new RandomIndexWriter(random(), directory)) { @@ -786,8 +766,8 @@ public void testNestedWithPipeline() throws IOException { Collections.singletonMap("_value", MAX_AGG_NAME), new Script(ScriptType.INLINE, MockScriptEngine.NAME, INVERSE_SCRIPT, Collections.emptyMap())))); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - fieldType.setName(VALUE_FIELD_NAME); + MappedFieldType fieldType + = new NumberFieldMapper.NumberFieldType(VALUE_FIELD_NAME, NumberFieldMapper.NumberType.LONG); InternalNested nested = searchAndReduce(newSearcher(indexReader, false, true), new MatchAllDocsQuery(), nestedBuilder, fieldType); @@ -860,10 +840,10 @@ public static CheckedConsumer buildResellerData( } public static MappedFieldType[] resellersMappedFields() { - MappedFieldType productIdField = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - productIdField.setName("product_id"); - MappedFieldType resellerIdField = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - resellerIdField.setName("reseller_id"); + MappedFieldType productIdField + = new NumberFieldMapper.NumberFieldType("product_id", NumberFieldMapper.NumberType.LONG); + MappedFieldType resellerIdField + = new NumberFieldMapper.NumberFieldType("reseller_id", NumberFieldMapper.NumberType.LONG); return new MappedFieldType[] {productIdField, resellerIdField}; } diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/nested/ReverseNestedAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/nested/ReverseNestedAggregatorTests.java index 483d9a21aa004..7736fcf129fc7 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/nested/ReverseNestedAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/nested/ReverseNestedAggregatorTests.java @@ -98,9 +98,7 @@ public void testNoDocs() throws IOException { MaxAggregationBuilder maxAgg = new MaxAggregationBuilder(MAX_AGG_NAME) .field(VALUE_FIELD_NAME); reverseNestedBuilder.subAggregation(maxAgg); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType( - NumberFieldMapper.NumberType.LONG); - fieldType.setName(VALUE_FIELD_NAME); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(VALUE_FIELD_NAME, NumberFieldMapper.NumberType.LONG); Nested nested = search(newSearcher(indexReader, false, true), new MatchAllDocsQuery(), nestedBuilder, fieldType); @@ -162,9 +160,7 @@ public void testMaxFromParentDocs() throws IOException { MaxAggregationBuilder maxAgg = new MaxAggregationBuilder(MAX_AGG_NAME) .field(VALUE_FIELD_NAME); reverseNestedBuilder.subAggregation(maxAgg); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType( - NumberFieldMapper.NumberType.LONG); - fieldType.setName(VALUE_FIELD_NAME); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(VALUE_FIELD_NAME, NumberFieldMapper.NumberType.LONG); Nested nested = search(newSearcher(indexReader, false, true), new MatchAllDocsQuery(), nestedBuilder, fieldType); @@ -187,9 +183,7 @@ public void testFieldAlias() throws IOException { int numParentDocs = randomIntBetween(1, 20); int expectedParentDocs = 0; - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType( - NumberFieldMapper.NumberType.LONG); - fieldType.setName(VALUE_FIELD_NAME); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(VALUE_FIELD_NAME, NumberFieldMapper.NumberType.LONG); try (Directory directory = newDirectory()) { try (RandomIndexWriter iw = new RandomIndexWriter(random(), directory)) { diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/range/DateRangeAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/range/DateRangeAggregatorTests.java index 14f9d9871b411..b42dcc8b52d60 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/range/DateRangeAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/range/DateRangeAggregatorTests.java @@ -44,6 +44,7 @@ import java.io.IOException; import java.time.ZoneOffset; import java.time.ZonedDateTime; +import java.util.Collections; import java.util.List; import java.util.function.Consumer; @@ -72,6 +73,7 @@ public void testNoMatchingField() throws IOException { }); } + @AwaitsFix(bugUrl="https://github.com/elastic/elasticsearch/issues/57651") public void testMatchesSortedNumericDocValues() throws IOException { testBothResolutions(new MatchAllDocsQuery(), iw -> { iw.addDocument(singleton(new SortedNumericDocValuesField(DATE_FIELD_NAME, milli1))); @@ -85,6 +87,7 @@ public void testMatchesSortedNumericDocValues() throws IOException { }); } + @AwaitsFix(bugUrl="https://github.com/elastic/elasticsearch/issues/57651") public void testMatchesNumericDocValues() throws IOException { testBothResolutions(new MatchAllDocsQuery(), iw -> { iw.addDocument(singleton(new NumericDocValuesField(DATE_FIELD_NAME, milli1))); @@ -99,11 +102,7 @@ public void testMatchesNumericDocValues() throws IOException { } public void testMissingDateStringWithDateField() throws IOException { - DateFieldMapper.Builder builder = new DateFieldMapper.Builder(DATE_FIELD_NAME) - .withResolution(DateFieldMapper.Resolution.MILLISECONDS); - DateFieldMapper.DateFieldType fieldType = builder.fieldType(); - fieldType.setHasDocValues(true); - fieldType.setName(DATE_FIELD_NAME); + DateFieldMapper.DateFieldType fieldType = new DateFieldMapper.DateFieldType(DATE_FIELD_NAME); DateRangeAggregationBuilder aggregationBuilder = new DateRangeAggregationBuilder("date_range") .field(DATE_FIELD_NAME) @@ -128,8 +127,8 @@ public void testNumberFieldDateRanges() throws IOException { .field(NUMBER_FIELD_NAME) .addRange("2015-11-13", "2015-11-14"); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName(NUMBER_FIELD_NAME); + MappedFieldType fieldType + = new NumberFieldMapper.NumberFieldType(NUMBER_FIELD_NAME, NumberFieldMapper.NumberType.INTEGER); expectThrows(NumberFormatException.class, () -> testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { @@ -143,8 +142,8 @@ public void testNumberFieldNumberRanges() throws IOException { .field(NUMBER_FIELD_NAME) .addRange(0, 5); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName(NUMBER_FIELD_NAME); + MappedFieldType fieldType + = new NumberFieldMapper.NumberFieldType(NUMBER_FIELD_NAME, NumberFieldMapper.NumberType.INTEGER); testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { iw.addDocument(singleton(new NumericDocValuesField(NUMBER_FIELD_NAME, 7))); @@ -163,8 +162,8 @@ public void testMissingDateStringWithNumberField() throws IOException { .addRange("2015-11-13", "2015-11-14") .missing("1979-01-01T00:00:00"); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName(NUMBER_FIELD_NAME); + MappedFieldType fieldType + = new NumberFieldMapper.NumberFieldType(NUMBER_FIELD_NAME, NumberFieldMapper.NumberType.INTEGER); expectThrows(NumberFormatException.class, () -> testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { @@ -179,8 +178,8 @@ public void testUnmappedWithMissingNumber() throws IOException { .addRange("2015-11-13", "2015-11-14") .missing(1447438575000L); // 2015-11-13 6:16:15 - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName(NUMBER_FIELD_NAME); + MappedFieldType fieldType + = new NumberFieldMapper.NumberFieldType(NUMBER_FIELD_NAME, NumberFieldMapper.NumberType.INTEGER); testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { iw.addDocument(singleton(new NumericDocValuesField(NUMBER_FIELD_NAME, 7))); @@ -199,8 +198,8 @@ public void testUnmappedWithMissingDate() throws IOException { .addRange("2015-11-13", "2015-11-14") .missing("2015-11-13T10:11:12"); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName(NUMBER_FIELD_NAME); + MappedFieldType fieldType + = new NumberFieldMapper.NumberFieldType(NUMBER_FIELD_NAME, NumberFieldMapper.NumberType.INTEGER); testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { iw.addDocument(singleton(new NumericDocValuesField(NUMBER_FIELD_NAME, 7))); @@ -218,15 +217,13 @@ public void testKeywordField() { .field("not_a_number") .addRange("2015-11-13", "2015-11-14"); - MappedFieldType fieldType = new KeywordFieldMapper.KeywordFieldType(); - fieldType.setName("not_a_number"); - fieldType.setHasDocValues(true); + MappedFieldType fieldType = new KeywordFieldMapper.KeywordFieldType("not_a_number"); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { iw.addDocument(singleton(new SortedSetDocValuesField("string", new BytesRef("foo")))); }, range -> fail("Should have thrown exception"), fieldType)); - assertEquals("Field [not_a_number] of type [keyword(indexed,tokenized)] is not supported for aggregation [date_range]", + assertEquals("Field [not_a_number] of type [keyword] is not supported for aggregation [date_range]", e.getMessage()); } @@ -236,8 +233,8 @@ public void testBadMissingField() { .addRange("2020-01-01T00:00:00", "2020-01-02T00:00:00") .missing("bogus"); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName(NUMBER_FIELD_NAME); + MappedFieldType fieldType + = new NumberFieldMapper.NumberFieldType(NUMBER_FIELD_NAME, NumberFieldMapper.NumberType.INTEGER); expectThrows(NumberFormatException.class, () -> testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { @@ -252,8 +249,8 @@ public void testUnmappedWithBadMissingField() { .addRange("2020-01-01T00:00:00", "2020-01-02T00:00:00") .missing("bogus"); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName(NUMBER_FIELD_NAME); + MappedFieldType fieldType + = new NumberFieldMapper.NumberFieldType(NUMBER_FIELD_NAME, NumberFieldMapper.NumberType.INTEGER); expectThrows(ElasticsearchParseException.class, () -> testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { @@ -274,11 +271,8 @@ private void testCase(Query query, CheckedConsumer buildIndex, Consumer> verify, DateFieldMapper.Resolution resolution) throws IOException { - DateFieldMapper.Builder builder = new DateFieldMapper.Builder(DATE_FIELD_NAME) - .withResolution(resolution); - DateFieldMapper.DateFieldType fieldType = builder.fieldType(); - fieldType.setHasDocValues(true); - fieldType.setName(DATE_FIELD_NAME); + DateFieldMapper.DateFieldType fieldType = new DateFieldMapper.DateFieldType(DATE_FIELD_NAME, true, true, + DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER, resolution, Collections.emptyMap()); DateRangeAggregationBuilder aggregationBuilder = new DateRangeAggregationBuilder("test_range_agg"); aggregationBuilder.field(DATE_FIELD_NAME); aggregationBuilder.addRange("2015-01-01", "2015-12-31"); diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/range/IpRangeAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/range/IpRangeAggregatorTests.java index f96e7b01f25ca..f4dd09c6eebd7 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/range/IpRangeAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/range/IpRangeAggregatorTests.java @@ -110,8 +110,7 @@ public void testRanges() throws Exception { } w.addDocument(doc); } - MappedFieldType fieldType = new IpFieldMapper.IpFieldType(); - fieldType.setName("field"); + MappedFieldType fieldType = new IpFieldMapper.IpFieldType("field"); try (IndexReader reader = w.getReader()) { IndexSearcher searcher = new IndexSearcher(reader); InternalBinaryRange range = search(searcher, new MatchAllDocsQuery(), builder, fieldType); diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/range/RangeAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/range/RangeAggregatorTests.java index 092a0319c9c21..0ee15b56ede4e 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/range/RangeAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/range/RangeAggregatorTests.java @@ -41,6 +41,7 @@ import java.io.IOException; import java.time.ZoneOffset; import java.time.ZonedDateTime; +import java.util.Collections; import java.util.List; import java.util.function.Consumer; @@ -94,11 +95,7 @@ public void testMatchesNumericDocValues() throws IOException { } public void testDateFieldMillisecondResolution() throws IOException { - DateFieldMapper.Builder builder = new DateFieldMapper.Builder(DATE_FIELD_NAME) - .withResolution(DateFieldMapper.Resolution.MILLISECONDS); - DateFieldMapper.DateFieldType fieldType = builder.fieldType(); - fieldType.setHasDocValues(true); - fieldType.setName(DATE_FIELD_NAME); + DateFieldMapper.DateFieldType fieldType = new DateFieldMapper.DateFieldType(DATE_FIELD_NAME); long milli1 = ZonedDateTime.of(2015, 11, 13, 16, 14, 34, 0, ZoneOffset.UTC).toInstant().toEpochMilli(); long milli2 = ZonedDateTime.of(2016, 11, 13, 16, 14, 34, 0, ZoneOffset.UTC).toInstant().toEpochMilli(); @@ -118,12 +115,10 @@ public void testDateFieldMillisecondResolution() throws IOException { }, fieldType); } + @AwaitsFix(bugUrl="https://github.com/elastic/elasticsearch/issues/57651") public void testDateFieldNanosecondResolution() throws IOException { - DateFieldMapper.Builder builder = new DateFieldMapper.Builder(DATE_FIELD_NAME) - .withResolution(DateFieldMapper.Resolution.NANOSECONDS); - DateFieldMapper.DateFieldType fieldType = builder.fieldType(); - fieldType.setHasDocValues(true); - fieldType.setName(DATE_FIELD_NAME); + DateFieldMapper.DateFieldType fieldType = new DateFieldMapper.DateFieldType(DATE_FIELD_NAME, true, true, + DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER, DateFieldMapper.Resolution.NANOSECONDS, Collections.emptyMap()); // These values should work because aggs scale nanosecond up to millisecond always. long milli1 = ZonedDateTime.of(2015, 11, 13, 16, 14, 34, 0, ZoneOffset.UTC).toInstant().toEpochMilli(); @@ -144,12 +139,10 @@ public void testDateFieldNanosecondResolution() throws IOException { }, fieldType); } + @AwaitsFix(bugUrl="https://github.com/elastic/elasticsearch/issues/57651") public void testMissingDateWithDateField() throws IOException { - DateFieldMapper.Builder builder = new DateFieldMapper.Builder(DATE_FIELD_NAME) - .withResolution(DateFieldMapper.Resolution.NANOSECONDS); - DateFieldMapper.DateFieldType fieldType = builder.fieldType(); - fieldType.setHasDocValues(true); - fieldType.setName(DATE_FIELD_NAME); + DateFieldMapper.DateFieldType fieldType = new DateFieldMapper.DateFieldType(DATE_FIELD_NAME, true, true, + DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER, DateFieldMapper.Resolution.NANOSECONDS, Collections.emptyMap()); // These values should work because aggs scale nanosecond up to millisecond always. long milli1 = ZonedDateTime.of(2015, 11, 13, 16, 14, 34, 0, ZoneOffset.UTC).toInstant().toEpochMilli(); @@ -179,8 +172,8 @@ public void testMissingDateWithNumberField() throws IOException { .addRange(-2d, 5d) .missing("1979-01-01T00:00:00"); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName(NUMBER_FIELD_NAME); + MappedFieldType fieldType + = new NumberFieldMapper.NumberFieldType(NUMBER_FIELD_NAME, NumberFieldMapper.NumberType.INTEGER); expectThrows(NumberFormatException.class, () -> testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { @@ -195,8 +188,8 @@ public void testUnmappedWithMissingNumber() throws IOException { .addRange(-2d, 5d) .missing(0L); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName(NUMBER_FIELD_NAME); + MappedFieldType fieldType + = new NumberFieldMapper.NumberFieldType(NUMBER_FIELD_NAME, NumberFieldMapper.NumberType.INTEGER); testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { iw.addDocument(singleton(new NumericDocValuesField(NUMBER_FIELD_NAME, 7))); @@ -215,8 +208,8 @@ public void testUnmappedWithMissingDate() throws IOException { .addRange(-2d, 5d) .missing("2020-02-13T10:11:12"); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName(NUMBER_FIELD_NAME); + MappedFieldType fieldType + = new NumberFieldMapper.NumberFieldType(NUMBER_FIELD_NAME, NumberFieldMapper.NumberType.INTEGER); expectThrows(NumberFormatException.class, () -> testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { @@ -230,15 +223,13 @@ public void testUnsupportedType() { .field("not_a_number") .addRange(-2d, 5d); - MappedFieldType fieldType = new KeywordFieldMapper.KeywordFieldType(); - fieldType.setName("not_a_number"); - fieldType.setHasDocValues(true); + MappedFieldType fieldType = new KeywordFieldMapper.KeywordFieldType("not_a_number"); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { iw.addDocument(singleton(new SortedSetDocValuesField("string", new BytesRef("foo")))); }, range -> fail("Should have thrown exception"), fieldType)); - assertEquals("Field [not_a_number] of type [keyword(indexed,tokenized)] is not supported for aggregation [range]", e.getMessage()); + assertEquals("Field [not_a_number] of type [keyword] is not supported for aggregation [range]", e.getMessage()); } public void testBadMissingField() { @@ -247,8 +238,8 @@ public void testBadMissingField() { .addRange(-2d, 5d) .missing("bogus"); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName(NUMBER_FIELD_NAME); + MappedFieldType fieldType + = new NumberFieldMapper.NumberFieldType(NUMBER_FIELD_NAME, NumberFieldMapper.NumberType.INTEGER); expectThrows(NumberFormatException.class, () -> testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { @@ -263,8 +254,8 @@ public void testUnmappedWithBadMissingField() { .addRange(-2d, 5d) .missing("bogus"); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName(NUMBER_FIELD_NAME); + MappedFieldType fieldType + = new NumberFieldMapper.NumberFieldType(NUMBER_FIELD_NAME, NumberFieldMapper.NumberType.INTEGER); expectThrows(NumberFormatException.class, () -> testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { @@ -276,8 +267,8 @@ public void testUnmappedWithBadMissingField() { private void testCase(Query query, CheckedConsumer buildIndex, Consumer> verify) throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName(NUMBER_FIELD_NAME); + MappedFieldType fieldType + = new NumberFieldMapper.NumberFieldType(NUMBER_FIELD_NAME, NumberFieldMapper.NumberType.INTEGER); RangeAggregationBuilder aggregationBuilder = new RangeAggregationBuilder("test_range_agg"); aggregationBuilder.field(NUMBER_FIELD_NAME); aggregationBuilder.addRange(0d, 5d); diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/sampler/DiversifiedSamplerTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/sampler/DiversifiedSamplerTests.java index 78d5830f5d3fd..abc60a5f7c321 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/sampler/DiversifiedSamplerTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/sampler/DiversifiedSamplerTests.java @@ -88,9 +88,7 @@ public void testDiversifiedSampler() throws Exception { IndexReader indexReader = DirectoryReader.open(directory); IndexSearcher indexSearcher = new IndexSearcher(indexReader); - MappedFieldType genreFieldType = new KeywordFieldMapper.KeywordFieldType(); - genreFieldType.setName("genre"); - genreFieldType.setHasDocValues(true); + MappedFieldType genreFieldType = new KeywordFieldMapper.KeywordFieldType("genre"); Consumer verify = result -> { Terms terms = result.getAggregations().get("terms"); assertEquals(2, terms.getBuckets().size()); @@ -101,14 +99,11 @@ public void testDiversifiedSampler() throws Exception { testCase(indexSearcher, genreFieldType, "global_ordinals", verify); testCase(indexSearcher, genreFieldType, "bytes_hash", verify); - genreFieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - genreFieldType.setName("genre_id"); + genreFieldType = new NumberFieldMapper.NumberFieldType("genre_id", NumberFieldMapper.NumberType.LONG); testCase(indexSearcher, genreFieldType, null, verify); // wrong field: - genreFieldType = new KeywordFieldMapper.KeywordFieldType(); - genreFieldType.setName("wrong_field"); - genreFieldType.setHasDocValues(true); + genreFieldType = new KeywordFieldMapper.KeywordFieldType("wrong_field"); testCase(indexSearcher, genreFieldType, null, result -> { Terms terms = result.getAggregations().get("terms"); assertEquals(1, terms.getBuckets().size()); @@ -156,9 +151,7 @@ public void testRidiculousSize() throws Exception { IndexReader indexReader = DirectoryReader.open(directory); IndexSearcher indexSearcher = new IndexSearcher(indexReader); - MappedFieldType genreFieldType = new KeywordFieldMapper.KeywordFieldType(); - genreFieldType.setName("genre"); - genreFieldType.setHasDocValues(true); + MappedFieldType genreFieldType = new KeywordFieldMapper.KeywordFieldType("genre"); Consumer verify = result -> { Terms terms = result.getAggregations().get("terms"); assertThat(terms.getBuckets().size(), greaterThan(0)); @@ -187,9 +180,7 @@ private void testCase(IndexSearcher indexSearcher, MappedFieldType genreFieldTyp private void testCase(IndexSearcher indexSearcher, MappedFieldType genreFieldType, String executionHint, Consumer verify, int shardSize, int maxDocsPerValue) throws IOException { - MappedFieldType idFieldType = new KeywordFieldMapper.KeywordFieldType(); - idFieldType.setName("id"); - idFieldType.setHasDocValues(true); + MappedFieldType idFieldType = new KeywordFieldMapper.KeywordFieldType("id"); SortedNumericIndexFieldData fieldData = new SortedNumericIndexFieldData(new Index("index", "index"), "price", IndexNumericFieldData.NumericType.DOUBLE); @@ -214,13 +205,9 @@ public void testDiversifiedSampler_noDocs() throws Exception { IndexReader indexReader = DirectoryReader.open(directory); IndexSearcher indexSearcher = new IndexSearcher(indexReader); - MappedFieldType idFieldType = new KeywordFieldMapper.KeywordFieldType(); - idFieldType.setName("id"); - idFieldType.setHasDocValues(true); + MappedFieldType idFieldType = new KeywordFieldMapper.KeywordFieldType("id"); - MappedFieldType genreFieldType = new KeywordFieldMapper.KeywordFieldType(); - genreFieldType.setName("genre"); - genreFieldType.setHasDocValues(true); + MappedFieldType genreFieldType = new KeywordFieldMapper.KeywordFieldType("genre"); DiversifiedAggregationBuilder builder = new DiversifiedAggregationBuilder("_name") .field(genreFieldType.name()) diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/sampler/SamplerAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/sampler/SamplerAggregatorTests.java index 329154e8fb839..4d4ee292ebb77 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/sampler/SamplerAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/sampler/SamplerAggregatorTests.java @@ -19,7 +19,6 @@ package org.elasticsearch.search.aggregations.bucket.sampler; -import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.document.SortedNumericDocValuesField; @@ -31,10 +30,9 @@ import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.TermQuery; import org.apache.lucene.store.Directory; -import org.elasticsearch.index.analysis.AnalyzerScope; -import org.elasticsearch.index.analysis.NamedAnalyzer; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.NumberFieldMapper; +import org.elasticsearch.index.mapper.TextFieldMapper; import org.elasticsearch.index.mapper.TextFieldMapper.TextFieldType; import org.elasticsearch.search.aggregations.AggregatorTestCase; import org.elasticsearch.search.aggregations.metrics.Min; @@ -48,10 +46,8 @@ public class SamplerAggregatorTests extends AggregatorTestCase { * Uses the sampler aggregation to find the minimum value of a field out of the top 3 scoring documents in a search. */ public void testSampler() throws IOException { - TextFieldType textFieldType = new TextFieldType(); - textFieldType.setIndexAnalyzer(new NamedAnalyzer("foo", AnalyzerScope.GLOBAL, new StandardAnalyzer())); - MappedFieldType numericFieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - numericFieldType.setName("int"); + TextFieldType textFieldType = new TextFieldType("text"); + MappedFieldType numericFieldType = new NumberFieldMapper.NumberFieldType("int", NumberFieldMapper.NumberType.LONG); IndexWriterConfig indexWriterConfig = newIndexWriterConfig(); indexWriterConfig.setMaxBufferedDocs(100); @@ -64,7 +60,7 @@ public void testSampler() throws IOException { for (int i = 0; i < value; i++) { text.append("good "); } - doc.add(new Field("text", text.toString(), textFieldType)); + doc.add(new Field("text", text.toString(), TextFieldMapper.Defaults.FIELD_TYPE)); doc.add(new SortedNumericDocValuesField("int", value)); w.addDocument(doc); } @@ -86,10 +82,8 @@ public void testSampler() throws IOException { } public void testRidiculousSize() throws IOException { - TextFieldType textFieldType = new TextFieldType(); - textFieldType.setIndexAnalyzer(new NamedAnalyzer("foo", AnalyzerScope.GLOBAL, new StandardAnalyzer())); - MappedFieldType numericFieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - numericFieldType.setName("int"); + TextFieldType textFieldType = new TextFieldType("text"); + MappedFieldType numericFieldType = new NumberFieldMapper.NumberFieldType("int", NumberFieldMapper.NumberType.LONG); IndexWriterConfig indexWriterConfig = newIndexWriterConfig(); indexWriterConfig.setMaxBufferedDocs(100); @@ -102,7 +96,7 @@ public void testRidiculousSize() throws IOException { for (int i = 0; i < value; i++) { text.append("good "); } - doc.add(new Field("text", text.toString(), textFieldType)); + doc.add(new Field("text", text.toString(), TextFieldMapper.Defaults.FIELD_TYPE)); doc.add(new SortedNumericDocValuesField("int", value)); w.addDocument(doc); } diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/BinaryTermsAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/BinaryTermsAggregatorTests.java index 6b971164aa8b0..8621475fa4c59 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/BinaryTermsAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/BinaryTermsAggregatorTests.java @@ -159,9 +159,7 @@ private void executeTestCase(boolean reduced, Query query, List dataset, configure.accept(aggregationBuilder); } - MappedFieldType binaryFieldType = new BinaryFieldMapper.Builder(BINARY_FIELD).fieldType(); - binaryFieldType.setName(BINARY_FIELD); - binaryFieldType.setHasDocValues(true); + MappedFieldType binaryFieldType = new BinaryFieldMapper.BinaryFieldType(BINARY_FIELD); InternalMappedTerms rareTerms; if (reduced) { diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/KeywordTermsAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/KeywordTermsAggregatorTests.java index 098e94d3d4450..c3a1be37d05ba 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/KeywordTermsAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/KeywordTermsAggregatorTests.java @@ -143,9 +143,7 @@ private void executeTestCase(boolean reduced, Query query, List dataset, configure.accept(aggregationBuilder); } - MappedFieldType keywordFieldType = new KeywordFieldMapper.KeywordFieldType(); - keywordFieldType.setName(KEYWORD_FIELD); - keywordFieldType.setHasDocValues(true); + MappedFieldType keywordFieldType = new KeywordFieldMapper.KeywordFieldType(KEYWORD_FIELD); InternalMappedTerms rareTerms; if (reduced) { diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/NumericTermsAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/NumericTermsAggregatorTests.java index b0790b9fa0413..30e32c7aa84bc 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/NumericTermsAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/NumericTermsAggregatorTests.java @@ -172,9 +172,8 @@ private void executeTestCase(boolean reduced, Query query, List dataset, configure.accept(aggregationBuilder); } - MappedFieldType longFieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - longFieldType.setName(LONG_FIELD); - longFieldType.setHasDocValues(true); + MappedFieldType longFieldType + = new NumberFieldMapper.NumberFieldType(LONG_FIELD, NumberFieldMapper.NumberType.LONG); InternalMappedTerms rareTerms; if (reduced) { diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/RareTermsAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/RareTermsAggregatorTests.java index 774baa80b8da8..17a61818e7768 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/RareTermsAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/RareTermsAggregatorTests.java @@ -257,14 +257,9 @@ public void testUnmapped() throws Exception { document.add(new SortedDocValuesField("string", new BytesRef("a"))); document.add(new NumericDocValuesField("long", 0L)); indexWriter.addDocument(document); - MappedFieldType fieldType1 = new KeywordFieldMapper.KeywordFieldType(); - fieldType1.setName("another_string"); - fieldType1.setHasDocValues(true); - - MappedFieldType fieldType2 = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - fieldType2.setName("another_long"); - fieldType2.setHasDocValues(true); - + MappedFieldType fieldType1 = new KeywordFieldMapper.KeywordFieldType("another_string"); + MappedFieldType fieldType2 + = new NumberFieldMapper.NumberFieldType("another_long", NumberFieldMapper.NumberType.LONG); try (IndexReader indexReader = maybeWrapReaderEs(indexWriter.getReader())) { IndexSearcher indexSearcher = newIndexSearcher(indexReader); @@ -300,8 +295,7 @@ public void testRangeField() throws Exception { doc.add(new BinaryDocValuesField("field", encodedRange)); indexWriter.addDocument(doc); } - MappedFieldType fieldType = new RangeFieldMapper.Builder("field", rangeType).fieldType(); - fieldType.setName("field"); + MappedFieldType fieldType = new RangeFieldMapper.RangeFieldType("field", rangeType); try (IndexReader indexReader = maybeWrapReaderEs(indexWriter.getReader())) { IndexSearcher indexSearcher = newIndexSearcher(indexReader); @@ -405,9 +399,7 @@ public void testGlobalAggregationWithScore() throws IOException { ) ); - MappedFieldType fieldType = new KeywordFieldMapper.KeywordFieldType(); - fieldType.setName("keyword"); - fieldType.setHasDocValues(true); + MappedFieldType fieldType = new KeywordFieldMapper.KeywordFieldType("keyword"); InternalGlobal result = searchAndReduce(indexSearcher, new MatchAllDocsQuery(), globalBuilder, fieldType); InternalMultiBucketAggregation terms = result.getAggregations().get("terms"); @@ -444,9 +436,8 @@ public void testWithNestedAggregations() throws IOException { .field("nested_value") .maxDocCount(1) ); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - fieldType.setHasDocValues(true); - fieldType.setName("nested_value"); + MappedFieldType fieldType + = new NumberFieldMapper.NumberFieldType("nested_value", NumberFieldMapper.NumberType.LONG); try (IndexReader indexReader = wrapInMockESDirectoryReader(DirectoryReader.open(directory))) { InternalNested result = searchAndReduce(newIndexSearcher(indexReader), // match root document only @@ -482,9 +473,8 @@ public void testWithNestedScoringAggregations() throws IOException { .storedField("_none_") ) ); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - fieldType.setHasDocValues(true); - fieldType.setName("nested_value"); + MappedFieldType fieldType + = new NumberFieldMapper.NumberFieldType("nested_value", NumberFieldMapper.NumberType.LONG); try (IndexReader indexReader = wrapInMockESDirectoryReader(DirectoryReader.open(directory))) { if (withScore) { @@ -626,14 +616,6 @@ private InternalAggregation executeTestCase( try (IndexReader indexReader = DirectoryReader.open(directory)) { IndexSearcher indexSearcher = newIndexSearcher(indexReader); - MappedFieldType keywordFieldType = new KeywordFieldMapper.KeywordFieldType(); - keywordFieldType.setName(KEYWORD_FIELD); - keywordFieldType.setHasDocValues(true); - - MappedFieldType longFieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - longFieldType.setName(LONG_FIELD); - longFieldType.setHasDocValues(true); - MappedFieldType[] types = new MappedFieldType[] { keywordField(KEYWORD_FIELD), longField(LONG_FIELD), diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/SignificantTermsAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/SignificantTermsAggregatorTests.java index 15a55e8cad9fb..28efafacff9bf 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/SignificantTermsAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/SignificantTermsAggregatorTests.java @@ -27,7 +27,6 @@ import org.apache.lucene.document.SortedNumericDocValuesField; import org.apache.lucene.document.StoredField; import org.apache.lucene.index.DirectoryReader; -import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; @@ -48,6 +47,7 @@ import org.elasticsearch.index.mapper.NumberFieldMapper.NumberType; import org.elasticsearch.index.mapper.RangeFieldMapper; import org.elasticsearch.index.mapper.RangeType; +import org.elasticsearch.index.mapper.TextFieldMapper; import org.elasticsearch.index.mapper.TextFieldMapper.TextFieldType; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; @@ -76,10 +76,7 @@ public class SignificantTermsAggregatorTests extends AggregatorTestCase { @Before public void setUpTest() throws Exception { super.setUp(); - fieldType = new KeywordFieldMapper.KeywordFieldType(); - fieldType.setHasDocValues(true); - fieldType.setIndexOptions(IndexOptions.DOCS); - fieldType.setName("field"); + fieldType = new KeywordFieldMapper.KeywordFieldType("field"); } @Override @@ -120,8 +117,7 @@ protected Map getFieldAliases(MappedFieldType... fieldT * Uses the significant terms aggregation to find the keywords in text fields */ public void testSignificance() throws IOException { - TextFieldType textFieldType = new TextFieldType(); - textFieldType.setName("text"); + TextFieldType textFieldType = new TextFieldType("text"); textFieldType.setFielddata(true); textFieldType.setIndexAnalyzer(new NamedAnalyzer("my_analyzer", AnalyzerScope.GLOBAL, new StandardAnalyzer())); @@ -207,12 +203,10 @@ public void testSignificance() throws IOException { * fields */ public void testNumericSignificance() throws IOException { - NumberFieldType longFieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - longFieldType.setName("long_field"); + NumberFieldType longFieldType + = new NumberFieldMapper.NumberFieldType("long_field", NumberFieldMapper.NumberType.LONG); - TextFieldType textFieldType = new TextFieldType(); - textFieldType.setName("text"); - textFieldType.setIndexAnalyzer(new NamedAnalyzer("my_analyzer", AnalyzerScope.GLOBAL, new StandardAnalyzer())); + TextFieldType textFieldType = new TextFieldType("text"); IndexWriterConfig indexWriterConfig = newIndexWriterConfig(); indexWriterConfig.setMaxBufferedDocs(100); @@ -227,10 +221,10 @@ public void testNumericSignificance() throws IOException { Document doc = new Document(); if (i % 2 == 0) { addFields(doc, NumberType.LONG.createFields("long_field", ODD_VALUE, true, true, false)); - doc.add(new Field("text", "odd", textFieldType)); + doc.add(new Field("text", "odd", TextFieldMapper.Defaults.FIELD_TYPE)); } else { addFields(doc, NumberType.LONG.createFields("long_field", EVEN_VALUE, true, true, false)); - doc.add(new Field("text", "even", textFieldType)); + doc.add(new Field("text", "even", TextFieldMapper.Defaults.FIELD_TYPE)); } addFields(doc, NumberType.LONG.createFields("long_field", COMMON_VALUE, true, true, false)); w.addDocument(doc); @@ -268,8 +262,7 @@ public void testNumericSignificance() throws IOException { * Uses the significant terms aggregation on an index with unmapped field */ public void testUnmapped() throws IOException { - TextFieldType textFieldType = new TextFieldType(); - textFieldType.setName("text"); + TextFieldType textFieldType = new TextFieldType("text"); textFieldType.setFielddata(true); textFieldType.setIndexAnalyzer(new NamedAnalyzer("my_analyzer", AnalyzerScope.GLOBAL, new StandardAnalyzer())); @@ -307,8 +300,7 @@ public void testRangeField() throws IOException { final RangeFieldMapper.Range range1 = new RangeFieldMapper.Range(rangeType, 1.0D, 5.0D, true, true); final RangeFieldMapper.Range range2 = new RangeFieldMapper.Range(rangeType, 6.0D, 10.0D, true, true); final String fieldName = "rangeField"; - MappedFieldType fieldType = new RangeFieldMapper.Builder(fieldName, rangeType).fieldType(); - fieldType.setName(fieldName); + MappedFieldType fieldType = new RangeFieldMapper.RangeFieldType(fieldName, rangeType); IndexWriterConfig indexWriterConfig = newIndexWriterConfig(); indexWriterConfig.setMaxBufferedDocs(100); @@ -338,8 +330,7 @@ public void testRangeField() throws IOException { } public void testFieldAlias() throws IOException { - TextFieldType textFieldType = new TextFieldType(); - textFieldType.setName("text"); + TextFieldType textFieldType = new TextFieldType("text"); textFieldType.setFielddata(true); textFieldType.setIndexAnalyzer(new NamedAnalyzer("my_analyzer", AnalyzerScope.GLOBAL, new StandardAnalyzer())); @@ -606,7 +597,7 @@ private void addMixedTextDocs(TextFieldType textFieldType, IndexWriter w) throws text.append("even "); } - doc.add(new Field("text", text.toString(), textFieldType)); + doc.add(new Field("text", text.toString(), TextFieldMapper.Defaults.FIELD_TYPE)); String json = "{ \"text\" : \"" + text.toString() + "\" }"; doc.add(new StoredField("_source", new BytesRef(json))); diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/SignificantTextAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/SignificantTextAggregatorTests.java index dab9b92f06dd2..db39925db9da9 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/SignificantTextAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/SignificantTextAggregatorTests.java @@ -37,13 +37,12 @@ import org.elasticsearch.index.mapper.BinaryFieldMapper; import org.elasticsearch.index.mapper.GeoPointFieldMapper; import org.elasticsearch.index.mapper.MappedFieldType; +import org.elasticsearch.index.mapper.TextFieldMapper; import org.elasticsearch.index.mapper.TextFieldMapper.TextFieldType; import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.AggregatorTestCase; import org.elasticsearch.search.aggregations.bucket.sampler.InternalSampler; import org.elasticsearch.search.aggregations.bucket.sampler.SamplerAggregationBuilder; -import org.elasticsearch.search.aggregations.bucket.terms.SignificantTerms; -import org.elasticsearch.search.aggregations.bucket.terms.SignificantTextAggregationBuilder; import org.elasticsearch.search.aggregations.support.AggregationInspectionHelper; import org.elasticsearch.search.aggregations.support.CoreValuesSourceType; import org.elasticsearch.search.aggregations.support.ValuesSourceType; @@ -81,10 +80,7 @@ protected List getSupportedValuesSourceTypes() { return List.of(CoreValuesSourceType.NUMERIC, CoreValuesSourceType.BYTES, CoreValuesSourceType.RANGE, - CoreValuesSourceType.GEOPOINT, - CoreValuesSourceType.BOOLEAN, - CoreValuesSourceType.DATE, - CoreValuesSourceType.IP); + CoreValuesSourceType.GEOPOINT); } @Override @@ -99,8 +95,7 @@ protected List unsupportedMappedFieldTypes() { * Uses the significant text aggregation to find the keywords in text fields */ public void testSignificance() throws IOException { - TextFieldType textFieldType = new TextFieldType(); - textFieldType.setName("text"); + TextFieldType textFieldType = new TextFieldType("text"); textFieldType.setIndexAnalyzer(new NamedAnalyzer("my_analyzer", AnalyzerScope.GLOBAL, new StandardAnalyzer())); IndexWriterConfig indexWriterConfig = newIndexWriterConfig(); @@ -148,8 +143,7 @@ public void testSignificance() throws IOException { } public void testFieldAlias() throws IOException { - TextFieldType textFieldType = new TextFieldType(); - textFieldType.setName("text"); + TextFieldType textFieldType = new TextFieldType("text"); textFieldType.setIndexAnalyzer(new NamedAnalyzer("my_analyzer", AnalyzerScope.GLOBAL, new StandardAnalyzer())); IndexWriterConfig indexWriterConfig = newIndexWriterConfig(); @@ -209,7 +203,7 @@ private void indexDocuments(IndexWriter writer, TextFieldType textFieldType) thr text.append("even separator" + i + " duplicate duplicate duplicate duplicate duplicate duplicate "); } - doc.add(new Field("text", text.toString(), textFieldType)); + doc.add(new Field("text", text.toString(), TextFieldMapper.Defaults.FIELD_TYPE)); String json ="{ \"text\" : \"" + text.toString() + "\","+ " \"json_only_field\" : \"" + text.toString() + "\"" + " }"; @@ -222,8 +216,7 @@ private void indexDocuments(IndexWriter writer, TextFieldType textFieldType) thr * Test documents with arrays of text */ public void testSignificanceOnTextArrays() throws IOException { - TextFieldType textFieldType = new TextFieldType(); - textFieldType.setName("text"); + TextFieldType textFieldType = new TextFieldType("text"); textFieldType.setIndexAnalyzer(new NamedAnalyzer("my_analyzer", AnalyzerScope.GLOBAL, new StandardAnalyzer())); IndexWriterConfig indexWriterConfig = newIndexWriterConfig(); @@ -232,7 +225,7 @@ public void testSignificanceOnTextArrays() throws IOException { try (Directory dir = newDirectory(); IndexWriter w = new IndexWriter(dir, indexWriterConfig)) { for (int i = 0; i < 10; i++) { Document doc = new Document(); - doc.add(new Field("text", "foo", textFieldType)); + doc.add(new Field("text", "foo", TextFieldMapper.Defaults.FIELD_TYPE)); String json ="{ \"text\" : [\"foo\",\"foo\"], \"title\" : [\"foo\", \"foo\"]}"; doc.add(new StoredField("_source", new BytesRef(json))); w.addDocument(doc); diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/TermsAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/TermsAggregatorTests.java index 04a4a47dd3fdc..ad2304c58d9f5 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/TermsAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/terms/TermsAggregatorTests.java @@ -178,9 +178,7 @@ public void testUsesGlobalOrdinalsByDefault() throws Exception { TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("_name").userValueTypeHint(ValueType.STRING) .field("string"); - MappedFieldType fieldType = new KeywordFieldMapper.KeywordFieldType(); - fieldType.setName("string"); - fieldType.setHasDocValues(true); + MappedFieldType fieldType = new KeywordFieldMapper.KeywordFieldType("string"); TermsAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType); assertThat(aggregator, instanceOf(GlobalOrdinalsStringTermsAggregator.class)); @@ -250,9 +248,7 @@ public void testSimple() throws Exception { .executionHint(executionMode.toString()) .field("string") .order(BucketOrder.key(true)); - MappedFieldType fieldType = new KeywordFieldMapper.KeywordFieldType(); - fieldType.setName("string"); - fieldType.setHasDocValues(true ); + MappedFieldType fieldType = new KeywordFieldMapper.KeywordFieldType("string"); TermsAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType); aggregator.preCollection(); @@ -312,9 +308,7 @@ public void testStringIncludeExclude() throws Exception { indexWriter.addDocument(document); try (IndexReader indexReader = maybeWrapReaderEs(indexWriter.getReader())) { IndexSearcher indexSearcher = newIndexSearcher(indexReader); - MappedFieldType fieldType = new KeywordFieldMapper.KeywordFieldType(); - fieldType.setName("mv_field"); - fieldType.setHasDocValues(true); + MappedFieldType fieldType = new KeywordFieldMapper.KeywordFieldType("mv_field"); String executionHint = randomFrom(TermsAggregatorFactory.ExecutionMode.values()).toString(); TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("_name") @@ -353,9 +347,7 @@ public void testStringIncludeExclude() throws Exception { assertEquals(1L, result.getBuckets().get(9).getDocCount()); assertTrue(AggregationInspectionHelper.hasValue((InternalTerms)result)); - MappedFieldType fieldType2 = new KeywordFieldMapper.KeywordFieldType(); - fieldType2.setName("sv_field"); - fieldType2.setHasDocValues(true); + MappedFieldType fieldType2 = new KeywordFieldMapper.KeywordFieldType("sv_field"); aggregationBuilder = new TermsAggregationBuilder("_name").userValueTypeHint(ValueType.STRING) .executionHint(executionHint) .includeExclude(new IncludeExclude("val00.+", null)) @@ -495,9 +487,8 @@ public void testNumericIncludeExclude() throws Exception { indexWriter.addDocument(document); try (IndexReader indexReader = maybeWrapReaderEs(indexWriter.getReader())) { IndexSearcher indexSearcher = newIndexSearcher(indexReader); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - fieldType.setName("long_field"); - fieldType.setHasDocValues(true ); + MappedFieldType fieldType + = new NumberFieldMapper.NumberFieldType("long_field", NumberFieldMapper.NumberType.LONG); String executionHint = randomFrom(TermsAggregatorFactory.ExecutionMode.values()).toString(); TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("_name") @@ -539,9 +530,8 @@ public void testNumericIncludeExclude() throws Exception { assertEquals(1L, result.getBuckets().get(3).getDocCount()); assertTrue(AggregationInspectionHelper.hasValue((InternalTerms)result)); - fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE); - fieldType.setName("double_field"); - fieldType.setHasDocValues(true ); + fieldType + = new NumberFieldMapper.NumberFieldType("double_field", NumberFieldMapper.NumberType.DOUBLE); aggregationBuilder = new TermsAggregationBuilder("_name").userValueTypeHint(ValueType.DOUBLE) .executionHint(executionHint) .includeExclude(new IncludeExclude(new double[]{0.0, 5.0}, null)) @@ -592,7 +582,7 @@ public void testStringTermsAggregator() throws Exception { return new SortedDocValuesField("field", new BytesRef(val)); } }; - MappedFieldType fieldType = new KeywordFieldMapper.KeywordFieldType(); + MappedFieldType fieldType = new KeywordFieldMapper.KeywordFieldType("field"); termsAggregator(ValueType.STRING, fieldType, i -> Integer.toString(i), String::compareTo, luceneFieldFactory); termsAggregatorWithNestedMaxAgg(ValueType.STRING, fieldType, i -> Integer.toString(i), @@ -607,7 +597,8 @@ public void testLongTermsAggregator() throws Exception { return new NumericDocValuesField("field", val); } }; - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); + MappedFieldType fieldType + = new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.LONG); termsAggregator(ValueType.LONG, fieldType, Integer::longValue, Long::compareTo, luceneFieldFactory); termsAggregatorWithNestedMaxAgg(ValueType.LONG, fieldType, Integer::longValue, val -> new NumericDocValuesField("field", val)); } @@ -620,7 +611,8 @@ public void testDoubleTermsAggregator() throws Exception { return new NumericDocValuesField("field", Double.doubleToRawLongBits(val)); } }; - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE); + MappedFieldType fieldType + = new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.DOUBLE); termsAggregator(ValueType.DOUBLE, fieldType, Integer::doubleValue, Double::compareTo, luceneFieldFactory); termsAggregatorWithNestedMaxAgg(ValueType.DOUBLE, fieldType, Integer::doubleValue, val -> new NumericDocValuesField("field", Double.doubleToRawLongBits(val))); @@ -640,7 +632,7 @@ public void testIpTermsAggregator() throws Exception { BytesRef b2 = new BytesRef(InetAddressPoint.encode(o2)); return b1.compareTo(b2); }; - termsAggregator(ValueType.IP, new IpFieldMapper.IpFieldType(), i -> {return base[0] = InetAddressPoint.nextUp(base[0]);}, + termsAggregator(ValueType.IP, new IpFieldMapper.IpFieldType("field"), i -> base[0] = InetAddressPoint.nextUp(base[0]), comparator, luceneFieldFactory); } @@ -727,8 +719,6 @@ private void termsAggregator(ValueType valueType, MappedFieldType fieldType, .shardSize(size) .field("field") .order(bucketOrder); - fieldType.setName("field"); - fieldType.setHasDocValues(true); Aggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType); aggregator.preCollection(); @@ -748,8 +738,7 @@ private void termsAggregator(ValueType valueType, MappedFieldType fieldType, } if (multiValued == false) { - MappedFieldType filterFieldType = new KeywordFieldMapper.KeywordFieldType(); - filterFieldType.setName("include"); + MappedFieldType filterFieldType = new KeywordFieldMapper.KeywordFieldType("include"); aggregationBuilder = new FilterAggregationBuilder("_name1", QueryBuilders.termQuery("include", "yes")); aggregationBuilder.subAggregation(new TermsAggregationBuilder("_name2") .userValueTypeHint(valueType) @@ -827,12 +816,9 @@ private void termsAggregatorWithNestedMaxAgg(ValueType valueType, MappedFiel .field("field") .order(bucketOrder) .subAggregation(AggregationBuilders.max("_max").field("value")); - fieldType.setName("field"); - fieldType.setHasDocValues(true); - MappedFieldType fieldType2 = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - fieldType2.setName("value"); - fieldType2.setHasDocValues(true); + MappedFieldType fieldType2 + = new NumberFieldMapper.NumberFieldType("value", NumberFieldMapper.NumberType.LONG); Aggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType, fieldType2); aggregator.preCollection(); indexSearcher.search(new MatchAllDocsQuery(), aggregator); @@ -852,17 +838,11 @@ private void termsAggregatorWithNestedMaxAgg(ValueType valueType, MappedFiel public void testEmpty() throws Exception { try (Directory directory = newDirectory()) { try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) { - MappedFieldType fieldType1 = new KeywordFieldMapper.KeywordFieldType(); - fieldType1.setName("string"); - fieldType1.setHasDocValues(true); - - MappedFieldType fieldType2 = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - fieldType1.setName("long"); - fieldType1.setHasDocValues(true); - - MappedFieldType fieldType3 = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE); - fieldType1.setName("double"); - fieldType1.setHasDocValues(true); + MappedFieldType fieldType1 = new KeywordFieldMapper.KeywordFieldType("string"); + MappedFieldType fieldType2 + = new NumberFieldMapper.NumberFieldType("long", NumberFieldMapper.NumberType.LONG); + MappedFieldType fieldType3 + = new NumberFieldMapper.NumberFieldType("double", NumberFieldMapper.NumberType.DOUBLE); try (IndexReader indexReader = maybeWrapReaderEs(indexWriter.getReader())) { IndexSearcher indexSearcher = newIndexSearcher(indexReader); TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("_name") @@ -935,9 +915,7 @@ public void testUnmappedWithMissing() throws Exception { try (IndexReader indexReader = maybeWrapReaderEs(indexWriter.getReader())) { - MappedFieldType fieldType1 = new KeywordFieldMapper.KeywordFieldType(); - fieldType1.setName("unrelated_value"); - fieldType1.setHasDocValues(true); + MappedFieldType fieldType1 = new KeywordFieldMapper.KeywordFieldType("unrelated_value"); IndexSearcher indexSearcher = newIndexSearcher(indexReader); ValueType[] valueTypes = new ValueType[]{ValueType.STRING, ValueType.LONG, ValueType.DOUBLE}; @@ -977,10 +955,7 @@ public void testRangeField() throws Exception { document.add(field); indexWriter.addDocument(document); try (IndexReader indexReader = maybeWrapReaderEs(indexWriter.getReader())) { - MappedFieldType fieldType = new RangeFieldMapper.Builder(fieldName, rangeType).fieldType(); - fieldType.setHasDocValues(true); - fieldType.setName(fieldName); - + MappedFieldType fieldType = new RangeFieldMapper.RangeFieldType(fieldName, rangeType); IndexSearcher indexSearcher = newIndexSearcher(indexReader); TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("_name") .field(fieldName); expectThrows(IllegalArgumentException.class, () -> { @@ -1000,10 +975,7 @@ public void testGeoPointField() throws Exception { document.add(new LatLonDocValuesField(field, point.getLat(), point.getLon())); indexWriter.addDocument(document); try (IndexReader indexReader = maybeWrapReaderEs(indexWriter.getReader())) { - MappedFieldType fieldType = new GeoPointFieldMapper.GeoPointFieldType(); - fieldType.setHasDocValues(true); - fieldType.setName("field"); - + MappedFieldType fieldType = new GeoPointFieldMapper.GeoPointFieldType("field"); IndexSearcher indexSearcher = newIndexSearcher(indexReader); TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("_name") .field(field); expectThrows(IllegalArgumentException.class, () -> { @@ -1023,10 +995,7 @@ public void testIpField() throws Exception { new BytesRef(InetAddressPoint.encode(InetAddresses.forString("192.168.100.42"))))); indexWriter.addDocument(document); try (IndexReader indexReader = maybeWrapReaderEs(indexWriter.getReader())) { - MappedFieldType fieldType = new IpFieldMapper.IpFieldType(); - fieldType.setHasDocValues(true); - fieldType.setName("field"); - + MappedFieldType fieldType = new IpFieldMapper.IpFieldType("field"); IndexSearcher indexSearcher = newIndexSearcher(indexReader); TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("_name") .field(field); // Note - other places we throw IllegalArgumentException @@ -1075,12 +1044,8 @@ public void testNestedTermsAgg() throws Exception { .field("field2") .order(BucketOrder.key(true)) ); - MappedFieldType fieldType1 = new KeywordFieldMapper.KeywordFieldType(); - fieldType1.setName("field1"); - fieldType1.setHasDocValues(true); - MappedFieldType fieldType2 = new KeywordFieldMapper.KeywordFieldType(); - fieldType2.setName("field2"); - fieldType2.setHasDocValues(true); + MappedFieldType fieldType1 = new KeywordFieldMapper.KeywordFieldType("field1"); + MappedFieldType fieldType2 = new KeywordFieldMapper.KeywordFieldType("field2"); Aggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType1, fieldType2); aggregator.preCollection(); @@ -1119,9 +1084,7 @@ public void testMixLongAndDouble() throws Exception { dir = ((DirectoryReader) reader).directory(); IndexSearcher searcher = new IndexSearcher(reader); MappedFieldType fieldType = - new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - fieldType.setName("number"); - fieldType.setHasDocValues(true); + new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.LONG); aggs.add(buildInternalAggregation(aggregationBuilder, fieldType, searcher)); } dir.close(); @@ -1133,9 +1096,7 @@ public void testMixLongAndDouble() throws Exception { dir = ((DirectoryReader) reader).directory(); IndexSearcher searcher = new IndexSearcher(reader); MappedFieldType fieldType = - new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE); - fieldType.setName("number"); - fieldType.setHasDocValues(true); + new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.DOUBLE); aggs.add(buildInternalAggregation(aggregationBuilder, fieldType, searcher)); } dir.close(); @@ -1196,9 +1157,7 @@ public void testGlobalAggregationWithScore() throws IOException { ) ); - MappedFieldType fieldType = new KeywordFieldMapper.KeywordFieldType(); - fieldType.setName("keyword"); - fieldType.setHasDocValues(true); + MappedFieldType fieldType = new KeywordFieldMapper.KeywordFieldType("keyword"); InternalGlobal result = searchAndReduce(indexSearcher, new MatchAllDocsQuery(), globalBuilder, fieldType); InternalMultiBucketAggregation terms = result.getAggregations().get("terms"); @@ -1243,9 +1202,8 @@ public void testWithNestedAggregations() throws IOException { .storedField("_none_") ) ); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - fieldType.setHasDocValues(true); - fieldType.setName("nested_value"); + MappedFieldType fieldType + = new NumberFieldMapper.NumberFieldType("nested_value", NumberFieldMapper.NumberType.LONG); try (IndexReader indexReader = wrapInMockESDirectoryReader(DirectoryReader.open(directory))) { { InternalNested result = search(newSearcher(indexReader, false, true), @@ -1273,8 +1231,8 @@ public void testWithNestedAggregations() throws IOException { } public void testNumberToStringValueScript() throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("number"); + MappedFieldType fieldType + = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER); TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("name") .userValueTypeHint(ValueType.STRING) @@ -1409,9 +1367,7 @@ public void testOrderByPipelineAggregation() throws Exception { .order(BucketOrder.aggregation("script", true)) .subAggregation(bucketScriptAgg); - MappedFieldType fieldType = new KeywordFieldMapper.KeywordFieldType(); - fieldType.setName("field"); - fieldType.setHasDocValues(true); + MappedFieldType fieldType = new KeywordFieldMapper.KeywordFieldType("field"); AggregationExecutionException e = expectThrows(AggregationExecutionException.class, () -> createAggregator(termsAgg, indexSearcher, fieldType)); diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/AvgAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/AvgAggregatorTests.java index 3b0b47e501409..ece0012e093fb 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/AvgAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/AvgAggregatorTests.java @@ -250,8 +250,7 @@ public void testUnmappedWithMissingField() throws IOException { private void verifyAvgOfDoubles(double[] values, double expected, double delta) throws IOException { AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name").field("number"); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE); - fieldType.setName("number"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.DOUBLE); testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> { for (double value : values) { @@ -281,8 +280,7 @@ public void testSingleValuedFieldPartiallyUnmapped() throws IOException { IndexSearcher indexSearcher = newSearcher(multiReader, true, true); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("number"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER); AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name").field("number"); AvgAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType); @@ -314,8 +312,7 @@ public void testSingleValuedField() throws IOException { } public void testSingleValuedField_WithFormatter() throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("value"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("value", NumberFieldMapper.NumberType.INTEGER); AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name") .format("#") @@ -335,8 +332,7 @@ public void testSingleValuedField_WithFormatter() throws IOException { } public void testSingleValuedFieldWithValueScript() throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("value"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("value", NumberFieldMapper.NumberType.INTEGER); AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name") .field("value") @@ -354,8 +350,7 @@ public void testSingleValuedFieldWithValueScript() throws IOException { } public void testScriptSingleValued() throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("value"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("value", NumberFieldMapper.NumberType.INTEGER); AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name") .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_FIELD_SCRIPT, Collections.emptyMap())); @@ -372,8 +367,7 @@ public void testScriptSingleValued() throws IOException { } public void testScriptSingleValuedWithParams() throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("value"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("value", NumberFieldMapper.NumberType.INTEGER); Map params = new HashMap<>(); params.put("inc", 1); @@ -409,8 +403,7 @@ public void testMultiValuedField() throws IOException { } public void testScriptMultiValued() throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("values"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("values", NumberFieldMapper.NumberType.INTEGER); AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name") .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, SUM_VALUES_FIELD_SCRIPT, Collections.emptyMap())); @@ -434,8 +427,7 @@ public void testScriptMultiValuedWithParams() throws Exception { params.put("inc", 1); params.put("field", "values"); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("values"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("values", NumberFieldMapper.NumberType.INTEGER); AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name") .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, SUM_FIELD_PARAMS_SCRIPT, params)); @@ -455,8 +447,7 @@ public void testScriptMultiValuedWithParams() throws Exception { } public void testSingleValuedFieldWithValueScriptWithParams() throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("value"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("value", NumberFieldMapper.NumberType.INTEGER); Map params = Collections.singletonMap("inc", 1); AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name") @@ -475,8 +466,7 @@ public void testSingleValuedFieldWithValueScriptWithParams() throws IOException } public void testMultiValuedFieldWithValueScriptWithParams() throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("values"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("values", NumberFieldMapper.NumberType.INTEGER); Map params = Collections.singletonMap("inc", 1); AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name") @@ -498,8 +488,7 @@ public void testMultiValuedFieldWithValueScriptWithParams() throws IOException { } public void testMultiValuedFieldWithValueScript() throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("values"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("values", NumberFieldMapper.NumberType.INTEGER); AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name") .field("values") @@ -520,9 +509,7 @@ public void testMultiValuedFieldWithValueScript() throws IOException { } public void testOrderByEmptyAggregation() throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("value"); - fieldType.setHasDocValues(true); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("value", NumberFieldMapper.NumberType.INTEGER); AggregationBuilder aggregationBuilder = new TermsAggregationBuilder("terms").userValueTypeHint(ValueType.NUMERIC) .field("value") @@ -574,8 +561,7 @@ public void testOrderByEmptyAggregation() throws IOException { private void testAggregation(Query query, CheckedConsumer buildIndex, Consumer verify) throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("number"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER); AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("_name").field("number"); testAggregation(aggregationBuilder, query, buildIndex, verify, fieldType); } @@ -610,8 +596,7 @@ public void testCacheAggregation() throws IOException { MultiReader multiReader = new MultiReader(indexReader, unamappedIndexReader); IndexSearcher indexSearcher = newSearcher(multiReader, true, true); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("value"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("value", NumberFieldMapper.NumberType.INTEGER); AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("avg") .field("value"); @@ -656,8 +641,7 @@ public void testScriptCaching() throws IOException { MultiReader multiReader = new MultiReader(indexReader, unamappedIndexReader); IndexSearcher indexSearcher = newSearcher(multiReader, true, true); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("value"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("value", NumberFieldMapper.NumberType.INTEGER); AvgAggregationBuilder aggregationBuilder = new AvgAggregationBuilder("avg") .field("value") .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_SCRIPT, Collections.emptyMap())); diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/CardinalityAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/CardinalityAggregatorTests.java index f8d302754197f..8b8c9f868d773 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/CardinalityAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/CardinalityAggregatorTests.java @@ -60,8 +60,7 @@ public void testRangeFieldValues() throws IOException { final RangeFieldMapper.Range range1 = new RangeFieldMapper.Range(rangeType, 1.0D, 5.0D, true, true); final RangeFieldMapper.Range range2 = new RangeFieldMapper.Range(rangeType, 6.0D, 10.0D, true, true); final String fieldName = "rangeField"; - MappedFieldType fieldType = new RangeFieldMapper.Builder(fieldName, rangeType).fieldType(); - fieldType.setName(fieldName); + MappedFieldType fieldType = new RangeFieldMapper.RangeFieldType(fieldName, rangeType); final CardinalityAggregationBuilder aggregationBuilder = new CardinalityAggregationBuilder("_name").field(fieldName); testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> { iw.addDocument(singleton(new BinaryDocValuesField(fieldName, rangeType.encodeRanges(singleton(range1))))); @@ -172,9 +171,7 @@ public void testUnmappedMissingGeoPoint() throws IOException { private void testAggregation(Query query, CheckedConsumer buildIndex, Consumer verify) throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType( - NumberFieldMapper.NumberType.LONG); - fieldType.setName("number"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.LONG); final CardinalityAggregationBuilder aggregationBuilder = new CardinalityAggregationBuilder("_name").field("number"); testAggregation(aggregationBuilder, query, buildIndex, verify, fieldType); } diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/ExtendedStatsAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/ExtendedStatsAggregatorTests.java index 9e781885e1eb4..cdcb17cbbfc92 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/ExtendedStatsAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/ExtendedStatsAggregatorTests.java @@ -46,8 +46,7 @@ public class ExtendedStatsAggregatorTests extends AggregatorTestCase { public void testEmpty() throws IOException { MappedFieldType ft = - new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - ft.setName("field"); + new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.LONG); testCase(ft, iw -> {}, stats -> { assertEquals(0d, stats.getCount(), 0); @@ -69,8 +68,7 @@ public void testEmpty() throws IOException { public void testRandomDoubles() throws IOException { MappedFieldType ft = - new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE); - ft.setName("field"); + new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.DOUBLE); final ExtendedSimpleStatsAggregator expected = new ExtendedSimpleStatsAggregator(); testCase(ft, iw -> { @@ -122,8 +120,7 @@ public void testRandomDoubles() throws IOException { */ public void testVarianceNonNegative() throws IOException { MappedFieldType ft = - new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE); - ft.setName("field"); + new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.DOUBLE); final ExtendedSimpleStatsAggregator expected = new ExtendedSimpleStatsAggregator(); testCase(ft, iw -> { @@ -151,8 +148,7 @@ public void testVarianceNonNegative() throws IOException { public void testRandomLongs() throws IOException { MappedFieldType ft = - new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - ft.setName("field"); + new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.LONG); final ExtendedSimpleStatsAggregator expected = new ExtendedSimpleStatsAggregator(); testCase(ft, iw -> { @@ -232,9 +228,8 @@ public void testSummationAccuracy() throws IOException { private void verifyStatsOfDoubles(double[] values, double expectedSum, double expectedSumOfSqrs, double delta) throws IOException { - MappedFieldType ft = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE); final String fieldName = "field"; - ft.setName(fieldName); + MappedFieldType ft = new NumberFieldMapper.NumberFieldType(fieldName, NumberFieldMapper.NumberType.DOUBLE); double max = Double.NEGATIVE_INFINITY; double min = Double.POSITIVE_INFINITY; for (double value : values) { diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/GeoBoundsAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/GeoBoundsAggregatorTests.java index ef42a2d210eed..ca1cb92e0fd5c 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/GeoBoundsAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/GeoBoundsAggregatorTests.java @@ -54,9 +54,7 @@ public void testEmpty() throws Exception { .field("field") .wrapLongitude(false); - MappedFieldType fieldType = new GeoPointFieldMapper.GeoPointFieldType(); - fieldType.setHasDocValues(true); - fieldType.setName("field"); + MappedFieldType fieldType = new GeoPointFieldMapper.GeoPointFieldType("field"); try (IndexReader reader = w.getReader()) { IndexSearcher searcher = new IndexSearcher(reader); InternalGeoBounds bounds = search(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType); @@ -83,9 +81,7 @@ public void testUnmappedFieldWithDocs() throws Exception { .field("non_existent") .wrapLongitude(false); - MappedFieldType fieldType = new GeoPointFieldMapper.GeoPointFieldType(); - fieldType.setHasDocValues(true); - fieldType.setName("field"); + MappedFieldType fieldType = new GeoPointFieldMapper.GeoPointFieldType("field"); try (IndexReader reader = w.getReader()) { IndexSearcher searcher = new IndexSearcher(reader); InternalGeoBounds bounds = search(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType); @@ -106,9 +102,7 @@ public void testMissing() throws Exception { doc.add(new NumericDocValuesField("not_field", 1000L)); w.addDocument(doc); - MappedFieldType fieldType = new GeoPointFieldMapper.GeoPointFieldType(); - fieldType.setHasDocValues(true); - fieldType.setName("field"); + MappedFieldType fieldType = new GeoPointFieldMapper.GeoPointFieldType("field"); Point point = GeometryTestUtils.randomPoint(false); double lon = point.getX(); @@ -141,9 +135,7 @@ public void testInvalidMissing() throws Exception { doc.add(new NumericDocValuesField("not_field", 1000L)); w.addDocument(doc); - MappedFieldType fieldType = new GeoPointFieldMapper.GeoPointFieldType(); - fieldType.setHasDocValues(true); - fieldType.setName("field"); + MappedFieldType fieldType = new GeoPointFieldMapper.GeoPointFieldType("field"); GeoBoundsAggregationBuilder aggBuilder = new GeoBoundsAggregationBuilder("my_agg") .field("field") @@ -199,9 +191,7 @@ public void testRandom() throws Exception { .field("field") .wrapLongitude(false); - MappedFieldType fieldType = new GeoPointFieldMapper.GeoPointFieldType(); - fieldType.setHasDocValues(true); - fieldType.setName("field"); + MappedFieldType fieldType = new GeoPointFieldMapper.GeoPointFieldType("field"); try (IndexReader reader = w.getReader()) { IndexSearcher searcher = new IndexSearcher(reader); InternalGeoBounds bounds = search(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType); diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/GeoCentroidAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/GeoCentroidAggregatorTests.java index c692d45371039..ab4a1fdd4a982 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/GeoCentroidAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/GeoCentroidAggregatorTests.java @@ -48,9 +48,7 @@ public void testEmpty() throws Exception { GeoCentroidAggregationBuilder aggBuilder = new GeoCentroidAggregationBuilder("my_agg") .field("field"); - MappedFieldType fieldType = new GeoPointFieldMapper.GeoPointFieldType(); - fieldType.setHasDocValues(true); - fieldType.setName("field"); + MappedFieldType fieldType = new GeoPointFieldMapper.GeoPointFieldType("field"); try (IndexReader reader = w.getReader()) { IndexSearcher searcher = new IndexSearcher(reader); InternalGeoCentroid result = search(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType); @@ -72,15 +70,11 @@ public void testUnmapped() throws Exception { try (IndexReader reader = w.getReader()) { IndexSearcher searcher = new IndexSearcher(reader); - MappedFieldType fieldType = new GeoPointFieldMapper.GeoPointFieldType(); - fieldType.setHasDocValues(true); - fieldType.setName("another_field"); + MappedFieldType fieldType = new GeoPointFieldMapper.GeoPointFieldType("another_field"); InternalGeoCentroid result = search(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType); assertNull(result.centroid()); - fieldType = new GeoPointFieldMapper.GeoPointFieldType(); - fieldType.setHasDocValues(true); - fieldType.setName("field"); + fieldType = new GeoPointFieldMapper.GeoPointFieldType("another_field"); result = search(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType); assertNull(result.centroid()); assertFalse(AggregationInspectionHelper.hasValue(result)); @@ -102,9 +96,7 @@ public void testUnmappedWithMissing() throws Exception { try (IndexReader reader = w.getReader()) { IndexSearcher searcher = new IndexSearcher(reader); - MappedFieldType fieldType = new GeoPointFieldMapper.GeoPointFieldType(); - fieldType.setHasDocValues(true); - fieldType.setName("another_field"); + MappedFieldType fieldType = new GeoPointFieldMapper.GeoPointFieldType("another_field"); InternalGeoCentroid result = search(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType); assertEquals(result.centroid(), expectedCentroid); assertTrue(AggregationInspectionHelper.hasValue(result)); @@ -164,9 +156,7 @@ public void testMultiValuedField() throws Exception { } private void assertCentroid(RandomIndexWriter w, GeoPoint expectedCentroid) throws IOException { - MappedFieldType fieldType = new GeoPointFieldMapper.GeoPointFieldType(); - fieldType.setHasDocValues(true); - fieldType.setName("field"); + MappedFieldType fieldType = new GeoPointFieldMapper.GeoPointFieldType("field"); GeoCentroidAggregationBuilder aggBuilder = new GeoCentroidAggregationBuilder("my_agg") .field("field"); try (IndexReader reader = w.getReader()) { diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/HDRPercentileRanksAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/HDRPercentileRanksAggregatorTests.java index 6c34ba7430a38..1c16c8b4d24ff 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/HDRPercentileRanksAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/HDRPercentileRanksAggregatorTests.java @@ -62,8 +62,8 @@ public void testEmpty() throws IOException { PercentileRanksAggregationBuilder aggBuilder = new PercentileRanksAggregationBuilder("my_agg", new double[]{0.5}) .field("field") .method(PercentilesMethod.HDR); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE); - fieldType.setName("field"); + MappedFieldType fieldType + = new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.DOUBLE); try (IndexReader reader = new MultiReader()) { IndexSearcher searcher = new IndexSearcher(reader); PercentileRanks ranks = search(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType); @@ -86,8 +86,8 @@ public void testSimple() throws IOException { PercentileRanksAggregationBuilder aggBuilder = new PercentileRanksAggregationBuilder("my_agg", new double[]{0.1, 0.5, 12}) .field("field") .method(PercentilesMethod.HDR); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE); - fieldType.setName("field"); + MappedFieldType fieldType + = new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.DOUBLE); try (IndexReader reader = w.getReader()) { IndexSearcher searcher = new IndexSearcher(reader); PercentileRanks ranks = search(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType); diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/HDRPercentilesAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/HDRPercentilesAggregatorTests.java index a0a0c5a9118a0..6f1196e7cd2c5 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/HDRPercentilesAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/HDRPercentilesAggregatorTests.java @@ -85,9 +85,7 @@ public void testNoDocs() throws IOException { */ public void testStringField() throws IOException { final String fieldName = "string"; - MappedFieldType fieldType = new KeywordFieldMapper.KeywordFieldType(); - fieldType.setName(fieldName); - fieldType.setHasDocValues(true); + MappedFieldType fieldType = new KeywordFieldMapper.KeywordFieldType(fieldName); expectThrows(IllegalArgumentException.class, () -> testCase(new DocValuesFieldExistsQuery(fieldName), iw -> { iw.addDocument(singleton(new SortedSetDocValuesField("string", new BytesRef("bogus")))); @@ -103,8 +101,7 @@ public void testStringField() throws IOException { public void testRangeField() throws IOException { // Currently fails (throws ClassCast exception), but should be fixed once HDRPercentileAggregation uses the ValuesSource registry final String fieldName = "range"; - MappedFieldType fieldType = new RangeFieldMapper.Builder(fieldName, RangeType.DOUBLE).fieldType(); - fieldType.setName(fieldName); + MappedFieldType fieldType = new RangeFieldMapper.RangeFieldType(fieldName, RangeType.DOUBLE); RangeFieldMapper.Range range =new RangeFieldMapper.Range(RangeType.DOUBLE, 1.0D, 5.0D, true, true); BytesRef encodedRange = RangeType.DOUBLE.encodeRanges(Collections.singleton(range)); expectThrows(IllegalArgumentException.class, @@ -191,8 +188,7 @@ public void testHdrThenTdigestSettings() throws Exception { private void testCase(Query query, CheckedConsumer buildIndex, Consumer verify) throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - fieldType.setName("number"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.LONG); testCase(query, buildIndex, verify, fieldType, "number"); } diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/MaxAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/MaxAggregatorTests.java index 7db08546b11cf..d4bc4c4c179e0 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/MaxAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/MaxAggregatorTests.java @@ -256,8 +256,7 @@ public void testUnmappedWithMissingField() throws IOException { } public void testMissingFieldOptimization() throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("number"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER); AggregationBuilder aggregationBuilder = new MaxAggregationBuilder("_name").field("number").missing(19L); testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> { @@ -271,8 +270,7 @@ public void testMissingFieldOptimization() throws IOException { } public void testScript() throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("number"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER); AggregationBuilder aggregationBuilder = new MaxAggregationBuilder("_name") .field("number") @@ -290,8 +288,7 @@ public void testScript() throws IOException { private void testAggregation(Query query, CheckedConsumer buildIndex, Consumer verify) throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("number"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER); MaxAggregationBuilder aggregationBuilder = new MaxAggregationBuilder("_name").field("number"); testAggregation(aggregationBuilder, query, buildIndex, verify, fieldType); } @@ -421,8 +418,7 @@ public void testSingleValuedField() throws IOException { } public void testSingleValuedFieldWithFormatter() throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("value"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("value", NumberFieldMapper.NumberType.INTEGER); MaxAggregationBuilder aggregationBuilder = new MaxAggregationBuilder("_name") .format("0000.0") @@ -441,9 +437,7 @@ public void testSingleValuedFieldWithFormatter() throws IOException { } public void testSingleValuedFieldGetProperty() throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("value"); - fieldType.setHasDocValues(true); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("value", NumberFieldMapper.NumberType.INTEGER); AggregationBuilder aggregationBuilder = AggregationBuilders.global("global") .subAggregation(AggregationBuilders.max("max").field("value")); @@ -502,8 +496,7 @@ public void testSingleValuedFieldPartiallyUnmapped() throws IOException { IndexSearcher indexSearcher = newSearcher(multiReader, true, true); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("value"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("value", NumberFieldMapper.NumberType.INTEGER); AggregationBuilder aggregationBuilder = new MaxAggregationBuilder("max").field("value"); MaxAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType); @@ -523,8 +516,7 @@ public void testSingleValuedFieldPartiallyUnmapped() throws IOException { } public void testSingleValuedFieldWithValueScript() throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("value"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("value", NumberFieldMapper.NumberType.INTEGER); MaxAggregationBuilder aggregationBuilder = new MaxAggregationBuilder("max") .field("value") @@ -543,8 +535,7 @@ public void testSingleValuedFieldWithValueScript() throws IOException { } public void testSingleValuedFieldWithValueScriptWithParams() throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("value"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("value", NumberFieldMapper.NumberType.INTEGER); Map params = Collections.singletonMap("inc", 1); MaxAggregationBuilder aggregationBuilder = new MaxAggregationBuilder("max") @@ -579,8 +570,7 @@ public void testMultiValuedField() throws IOException { } public void testMultiValuedFieldWithValueScript() throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("values"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("values", NumberFieldMapper.NumberType.INTEGER); MaxAggregationBuilder aggregationBuilder = new MaxAggregationBuilder("max") .field("values") @@ -601,8 +591,7 @@ public void testMultiValuedFieldWithValueScript() throws IOException { } public void testMultiValuedFieldWithValueScriptWithParams() throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("values"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("values", NumberFieldMapper.NumberType.INTEGER); Map params = Collections.singletonMap("inc", 1); MaxAggregationBuilder aggregationBuilder = new MaxAggregationBuilder("max") @@ -624,8 +613,7 @@ public void testMultiValuedFieldWithValueScriptWithParams() throws IOException { } public void testScriptSingleValued() throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("value"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("value", NumberFieldMapper.NumberType.INTEGER); MaxAggregationBuilder aggregationBuilder = new MaxAggregationBuilder("max") .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_FIELD_SCRIPT, Collections.emptyMap())); @@ -642,8 +630,7 @@ public void testScriptSingleValued() throws IOException { } public void testScriptSingleValuedWithParams() throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("value"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("value", NumberFieldMapper.NumberType.INTEGER); Map params = new HashMap<>(); params.put("inc", 1); @@ -664,8 +651,7 @@ public void testScriptSingleValuedWithParams() throws IOException { } public void testScriptMultiValued() throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("values"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("values", NumberFieldMapper.NumberType.INTEGER); MaxAggregationBuilder aggregationBuilder = new MaxAggregationBuilder("max") .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, SUM_VALUES_FIELD_SCRIPT, Collections.emptyMap())); @@ -689,8 +675,7 @@ public void testScriptMultiValuedWithParams() throws IOException { params.put("inc", 1); params.put("field", "values"); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("values"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("values", NumberFieldMapper.NumberType.INTEGER); MaxAggregationBuilder aggregationBuilder = new MaxAggregationBuilder("max") .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, SUM_FIELD_PARAMS_SCRIPT, params)); @@ -710,9 +695,7 @@ public void testScriptMultiValuedWithParams() throws IOException { } public void testEmptyAggregation() throws Exception { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("value"); - fieldType.setHasDocValues(true); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("value", NumberFieldMapper.NumberType.INTEGER); AggregationBuilder aggregationBuilder = AggregationBuilders.global("global") .subAggregation(AggregationBuilders.max("max").field("value")); @@ -747,9 +730,7 @@ public void testEmptyAggregation() throws Exception { } public void testOrderByEmptyAggregation() throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("value"); - fieldType.setHasDocValues(true); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("value", NumberFieldMapper.NumberType.INTEGER); TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("terms").userValueTypeHint(ValueType.NUMERIC) .field("value") @@ -799,8 +780,7 @@ public void testOrderByEmptyAggregation() throws IOException { } public void testEarlyTermination() throws Exception { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("values"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("values", NumberFieldMapper.NumberType.INTEGER); Directory directory = newDirectory(); RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory); @@ -843,11 +823,8 @@ public void testEarlyTermination() throws Exception { } public void testNestedEarlyTermination() throws Exception { - MappedFieldType multiValuesfieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - multiValuesfieldType.setName("values"); - - MappedFieldType singleValueFieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - singleValueFieldType.setName("value"); + MappedFieldType multiValuesfieldType = new NumberFieldMapper.NumberFieldType("values", NumberFieldMapper.NumberType.INTEGER); + MappedFieldType singleValueFieldType = new NumberFieldMapper.NumberFieldType("value", NumberFieldMapper.NumberType.INTEGER); Directory directory = newDirectory(); RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory); @@ -931,8 +908,7 @@ public void testCacheAggregation() throws IOException { IndexSearcher indexSearcher = newSearcher(multiReader, true, true); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("value"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("value", NumberFieldMapper.NumberType.INTEGER); MaxAggregationBuilder aggregationBuilder = new MaxAggregationBuilder("max") .field("value"); @@ -977,8 +953,7 @@ public void testScriptCaching() throws Exception { MultiReader multiReader = new MultiReader(indexReader, unamappedIndexReader); IndexSearcher indexSearcher = newSearcher(multiReader, true, true); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("value"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("value", NumberFieldMapper.NumberType.INTEGER); MaxAggregationBuilder aggregationBuilder = new MaxAggregationBuilder("max") .field("value") .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_SCRIPT, Collections.emptyMap())); diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/MedianAbsoluteDeviationAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/MedianAbsoluteDeviationAggregatorTests.java index 732c52ac32bf2..0a14ebfdd7102 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/MedianAbsoluteDeviationAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/MedianAbsoluteDeviationAggregatorTests.java @@ -201,9 +201,8 @@ public void testUnmappedMissing() throws IOException { } public void testValueScript() throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - fieldType.setName(FIELD_NAME); - fieldType.setHasDocValues(true); + MappedFieldType fieldType + = new NumberFieldMapper.NumberFieldType(FIELD_NAME, NumberFieldMapper.NumberType.LONG); MedianAbsoluteDeviationAggregationBuilder aggregationBuilder = new MedianAbsoluteDeviationAggregationBuilder("foo") .field(FIELD_NAME) @@ -227,8 +226,8 @@ public void testSingleScript() throws IOException { MedianAbsoluteDeviationAggregationBuilder aggregationBuilder = new MedianAbsoluteDeviationAggregationBuilder("foo") .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, SINGLE_SCRIPT, Collections.emptyMap())); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - fieldType.setName(FIELD_NAME); + MappedFieldType fieldType + = new NumberFieldMapper.NumberFieldType(FIELD_NAME, NumberFieldMapper.NumberType.LONG); final int size = randomIntBetween(100, 1000); final List sample = new ArrayList<>(size); @@ -253,8 +252,8 @@ private void testAggregation(Query query, .field(FIELD_NAME) .compression(randomDoubleBetween(20, 1000, true)); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - fieldType.setName(FIELD_NAME); + MappedFieldType fieldType + = new NumberFieldMapper.NumberFieldType(FIELD_NAME, NumberFieldMapper.NumberType.LONG); testAggregation(builder, query, buildIndex, verify, fieldType); } diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/MinAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/MinAggregatorTests.java index 15186fef5748b..8991639f5ac7e 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/MinAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/MinAggregatorTests.java @@ -19,26 +19,6 @@ package org.elasticsearch.search.aggregations.metrics; -import static java.util.Collections.singleton; -import static org.elasticsearch.index.query.QueryBuilders.termQuery; -import static org.hamcrest.Matchers.equalTo; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import java.io.IOException; -import java.time.Instant; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.function.BiFunction; -import java.util.function.Consumer; -import java.util.function.Function; -import java.util.function.Supplier; - import org.apache.lucene.document.Document; import org.apache.lucene.document.DoublePoint; import org.apache.lucene.document.Field; @@ -51,7 +31,6 @@ import org.apache.lucene.document.SortedSetDocValuesField; import org.apache.lucene.document.StringField; import org.apache.lucene.index.DirectoryReader; -import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; @@ -110,6 +89,26 @@ import org.elasticsearch.search.internal.SearchContext; import org.elasticsearch.search.lookup.LeafDocLookup; +import java.io.IOException; +import java.time.Instant; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.BiFunction; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.function.Supplier; + +import static java.util.Collections.singleton; +import static org.elasticsearch.index.query.QueryBuilders.termQuery; +import static org.hamcrest.Matchers.equalTo; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + public class MinAggregatorTests extends AggregatorTestCase { private final String SCRIPT_NAME = "script_name"; @@ -266,9 +265,7 @@ public void testIpField() throws IOException { final String fieldName = "IP_field"; MinAggregationBuilder aggregationBuilder = new MinAggregationBuilder("min").field(fieldName); - MappedFieldType fieldType = new IpFieldMapper.IpFieldType(); - fieldType.setName(fieldName); - + MappedFieldType fieldType = new IpFieldMapper.IpFieldType(fieldName); boolean v4 = randomBoolean(); expectThrows(IllegalArgumentException.class, () -> testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { iw.addDocument(singleton(new SortedSetDocValuesField(fieldName, new BytesRef(InetAddressPoint.encode(randomIp(v4)))))); @@ -279,8 +276,7 @@ public void testIpField() throws IOException { public void testUnmappedWithMissingField() throws IOException { MinAggregationBuilder aggregationBuilder = new MinAggregationBuilder("min").field("does_not_exist").missing(0L); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("number"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER); testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { iw.addDocument(singleton(new NumericDocValuesField("number", 7))); @@ -294,9 +290,7 @@ public void testUnmappedWithMissingField() throws IOException { public void testUnsupportedType() { MinAggregationBuilder aggregationBuilder = new MinAggregationBuilder("min").field("not_a_number"); - MappedFieldType fieldType = new KeywordFieldMapper.KeywordFieldType(); - fieldType.setName("not_a_number"); - fieldType.setHasDocValues(true); + MappedFieldType fieldType = new KeywordFieldMapper.KeywordFieldType("not_a_number"); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { @@ -304,14 +298,13 @@ public void testUnsupportedType() { }, (Consumer) min -> { fail("Should have thrown exception"); }, fieldType)); - assertEquals("Field [not_a_number] of type [keyword(indexed,tokenized)] is not supported for aggregation [min]", e.getMessage()); + assertEquals("Field [not_a_number] of type [keyword] is not supported for aggregation [min]", e.getMessage()); } public void testBadMissingField() { MinAggregationBuilder aggregationBuilder = new MinAggregationBuilder("min").field("number").missing("not_a_number"); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("number"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER); expectThrows(NumberFormatException.class, () -> testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { @@ -325,8 +318,7 @@ public void testBadMissingField() { public void testUnmappedWithBadMissingField() { MinAggregationBuilder aggregationBuilder = new MinAggregationBuilder("min").field("does_not_exist").missing("not_a_number"); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("number"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER); expectThrows(NumberFormatException.class, () -> testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { @@ -341,8 +333,7 @@ public void testEmptyBucket() throws IOException { HistogramAggregationBuilder histogram = new HistogramAggregationBuilder("histo").field("number").interval(1).minDocCount(0) .subAggregation(new MinAggregationBuilder("min").field("number")); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("number"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER); testCase(histogram, new MatchAllDocsQuery(), iw -> { iw.addDocument(singleton(new NumericDocValuesField("number", 1))); @@ -372,8 +363,7 @@ public void testEmptyBucket() throws IOException { public void testFormatter() throws IOException { MinAggregationBuilder aggregationBuilder = new MinAggregationBuilder("min").field("number").format("0000.0"); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("number"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER); testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { iw.addDocument(singleton(new NumericDocValuesField("number", 7))); @@ -389,8 +379,7 @@ public void testGetProperty() throws IOException { GlobalAggregationBuilder globalBuilder = new GlobalAggregationBuilder("global") .subAggregation(new MinAggregationBuilder("min").field("number")); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("number"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER); testCase(globalBuilder, new MatchAllDocsQuery(), iw -> { iw.addDocument(singleton(new NumericDocValuesField("number", 7))); @@ -410,8 +399,7 @@ public void testGetProperty() throws IOException { public void testSingleValuedFieldPartiallyUnmapped() throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("number"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER); MinAggregationBuilder aggregationBuilder = new MinAggregationBuilder("min").field("number"); try (Directory directory = newDirectory(); @@ -441,8 +429,7 @@ public void testSingleValuedFieldPartiallyUnmapped() throws IOException { public void testSingleValuedFieldPartiallyUnmappedWithMissing() throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("number"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER); MinAggregationBuilder aggregationBuilder = new MinAggregationBuilder("min").field("number").missing(-19L); try (Directory directory = newDirectory(); @@ -472,8 +459,7 @@ public void testSingleValuedFieldPartiallyUnmappedWithMissing() throws IOExcepti } public void testSingleValuedFieldWithValueScript() throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("number"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER); MinAggregationBuilder aggregationBuilder = new MinAggregationBuilder("min") .field("number") @@ -491,8 +477,7 @@ public void testSingleValuedFieldWithValueScript() throws IOException { } public void testSingleValuedFieldWithValueScriptAndMissing() throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("number"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER); MinAggregationBuilder aggregationBuilder = new MinAggregationBuilder("min") .field("number") @@ -512,8 +497,7 @@ public void testSingleValuedFieldWithValueScriptAndMissing() throws IOException } public void testSingleValuedFieldWithValueScriptAndParams() throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("number"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER); MinAggregationBuilder aggregationBuilder = new MinAggregationBuilder("min") .field("number") @@ -531,8 +515,7 @@ public void testSingleValuedFieldWithValueScriptAndParams() throws IOException { } public void testScript() throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("number"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER); MinAggregationBuilder aggregationBuilder = new MinAggregationBuilder("min") .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, SCRIPT_NAME, Collections.emptyMap())); @@ -551,8 +534,7 @@ public void testScript() throws IOException { public void testMultiValuedField() throws IOException { MinAggregationBuilder aggregationBuilder = new MinAggregationBuilder("min").field("number"); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("number"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER); testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { final int numDocs = 10; @@ -573,8 +555,7 @@ public void testMultiValuedFieldWithScript() throws IOException { .field("number") .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, INVERT_SCRIPT, Collections.emptyMap())); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("number"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER); testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { final int numDocs = 10; @@ -595,8 +576,7 @@ public void testMultiValuedFieldWithScriptParams() throws IOException { .field("number") .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_SCRIPT, Collections.singletonMap("inc", 5))); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("number"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER); testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { final int numDocs = 10; @@ -619,8 +599,7 @@ public void testOrderByEmptyAggregation() throws IOException { .subAggregation(new FilterAggregationBuilder("filter", termQuery("number", 100)) .subAggregation(new MinAggregationBuilder("min").field("number"))); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("number"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER); int numDocs = 10; testCase(termsBuilder, new MatchAllDocsQuery(), iw -> { @@ -649,8 +628,7 @@ public void testOrderByEmptyAggregation() throws IOException { public void testCaching() throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("number"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER); MinAggregationBuilder aggregationBuilder = new MinAggregationBuilder("min").field("number"); try (Directory directory = newDirectory()) { @@ -675,8 +653,7 @@ public void testCaching() throws IOException { public void testScriptCaching() throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("number"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER); MinAggregationBuilder aggregationBuilder = new MinAggregationBuilder("min") .field("number") .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, INVERT_SCRIPT, Collections.emptyMap())); @@ -899,10 +876,7 @@ private ValuesSourceConfig mockNumericValuesSourceConfig(String fieldName, NumberFieldMapper.NumberType numType, boolean indexed) { ValuesSourceConfig config = mock(ValuesSourceConfig.class); - MappedFieldType ft = new NumberFieldMapper.NumberFieldType(numType); - ft.setName(fieldName); - ft.setIndexOptions(indexed ? IndexOptions.DOCS : IndexOptions.NONE); - ft.freeze(); + MappedFieldType ft = new NumberFieldMapper.NumberFieldType(fieldName, numType, indexed, true, Collections.emptyMap()); when(config.fieldContext()).thenReturn(new FieldContext(fieldName, null, ft)); return config; } @@ -925,8 +899,7 @@ private ValuesSourceConfig mockDateValuesSourceConfig(String fieldName, boolean private void testCase(Query query, CheckedConsumer buildIndex, Consumer verify) throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("number"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER); MinAggregationBuilder aggregationBuilder = new MinAggregationBuilder("min").field("number"); testCase(aggregationBuilder, query, buildIndex, verify, fieldType); } diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/StatsAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/StatsAggregatorTests.java index 6df5b477d7081..5e11dbf867bad 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/StatsAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/StatsAggregatorTests.java @@ -74,8 +74,7 @@ public class StatsAggregatorTests extends AggregatorTestCase { // TODO: Script tests, should fail with defaultValuesSourceType disabled. public void testEmpty() throws IOException { - final MappedFieldType ft = new NumberFieldMapper.NumberFieldType(NumberType.LONG); - ft.setName("field"); + final MappedFieldType ft = new NumberFieldMapper.NumberFieldType("field", NumberType.LONG); testCase( stats("_name").field(ft.name()), iw -> {}, @@ -92,8 +91,7 @@ public void testEmpty() throws IOException { } public void testRandomDoubles() throws IOException { - final MappedFieldType ft = new NumberFieldMapper.NumberFieldType(NumberType.DOUBLE); - ft.setName("field"); + final MappedFieldType ft = new NumberFieldMapper.NumberFieldType("field", NumberType.DOUBLE); final SimpleStatsAggregator expected = new SimpleStatsAggregator(); testCase( stats("_name").field(ft.name()), @@ -171,8 +169,7 @@ public void testSummationAccuracy() throws IOException { private void verifySummationOfDoubles(double[] values, double expectedSum, double expectedAvg, double delta) throws IOException { - MappedFieldType ft = new NumberFieldMapper.NumberFieldType(NumberType.DOUBLE); - ft.setName("field"); + MappedFieldType ft = new NumberFieldMapper.NumberFieldType("field", NumberType.DOUBLE); double max = Double.NEGATIVE_INFINITY; double min = Double.POSITIVE_INFINITY; @@ -222,8 +219,7 @@ public void testPartiallyUnmapped() throws IOException { RandomIndexWriter mappedWriter = new RandomIndexWriter(random(), mappedDirectory); RandomIndexWriter unmappedWriter = new RandomIndexWriter(random(), unmappedDirectory)) { - final MappedFieldType ft = new NumberFieldMapper.NumberFieldType(NumberType.LONG); - ft.setName("field"); + final MappedFieldType ft = new NumberFieldMapper.NumberFieldType("field", NumberType.LONG); final SimpleStatsAggregator expected = new SimpleStatsAggregator(); final int numDocs = randomIntBetween(10, 50); for (int i = 0; i < numDocs; i++) { @@ -347,8 +343,7 @@ public void testFieldScriptMultiValuedField() throws IOException { } public void testMissing() throws IOException { - final MappedFieldType ft = new NumberFieldMapper.NumberFieldType(NumberType.LONG); - ft.setName("field"); + final MappedFieldType ft = new NumberFieldMapper.NumberFieldType("field", NumberType.LONG); final long missingValue = randomIntBetween(-100, 100); @@ -407,8 +402,7 @@ private void randomLongsTestCase(int valuesPerField, StatsAggregationBuilder builder, BiConsumer verify) throws IOException { - final MappedFieldType ft = new NumberFieldMapper.NumberFieldType(NumberType.LONG); - ft.setName("field"); + final MappedFieldType ft = new NumberFieldMapper.NumberFieldType("field", NumberType.LONG); final int numDocs = randomIntBetween(10, 50); final List> docs = new ArrayList<>(numDocs); diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/SumAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/SumAggregatorTests.java index f50880a8d17a1..ca1aebb6be135 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/SumAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/SumAggregatorTests.java @@ -228,9 +228,8 @@ public void testUnmapped() throws IOException { } public void testPartiallyUnmapped() throws IOException { - final MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberType.LONG); - fieldType.setName(FIELD_NAME); - fieldType.setHasDocValues(true); + final MappedFieldType fieldType + = new NumberFieldMapper.NumberFieldType(FIELD_NAME, NumberType.LONG); final SumAggregationBuilder builder = sum("_name") .field(fieldType.name()); @@ -314,8 +313,8 @@ public void testFieldScriptMultiValuedField() throws IOException { public void testMissing() throws IOException { final MappedFieldType aggField = defaultFieldType(); - final MappedFieldType irrelevantField = new NumberFieldMapper.NumberFieldType(NumberType.LONG); - irrelevantField.setName("irrelevant_field"); + final MappedFieldType irrelevantField + = new NumberFieldMapper.NumberFieldType("irrelevant_field", NumberType.LONG); final int numDocs = randomIntBetween(10, 100); final long missingValue = randomLongBetween(1, 1000); @@ -435,9 +434,6 @@ private static MappedFieldType defaultFieldType() { } private static MappedFieldType defaultFieldType(NumberType numberType) { - final MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(numberType); - fieldType.setName(FIELD_NAME); - fieldType.setHasDocValues(true); - return fieldType; + return new NumberFieldMapper.NumberFieldType(FIELD_NAME, numberType); } } diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/TDigestPercentileRanksAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/TDigestPercentileRanksAggregatorTests.java index 043554bcc4dc3..52c2bf92373ab 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/TDigestPercentileRanksAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/TDigestPercentileRanksAggregatorTests.java @@ -62,8 +62,7 @@ public void testEmpty() throws IOException { PercentileRanksAggregationBuilder aggBuilder = new PercentileRanksAggregationBuilder("my_agg", new double[]{0.5}) .field("field") .method(PercentilesMethod.TDIGEST); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE); - fieldType.setName("field"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.DOUBLE); try (IndexReader reader = new MultiReader()) { IndexSearcher searcher = new IndexSearcher(reader); PercentileRanks ranks = search(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType); @@ -86,8 +85,7 @@ public void testSimple() throws IOException { PercentileRanksAggregationBuilder aggBuilder = new PercentileRanksAggregationBuilder("my_agg", new double[]{0.1, 0.5, 12}) .field("field") .method(PercentilesMethod.TDIGEST); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE); - fieldType.setName("field"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.DOUBLE); try (IndexReader reader = w.getReader()) { IndexSearcher searcher = new IndexSearcher(reader); PercentileRanks ranks = search(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType); diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/TDigestPercentilesAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/TDigestPercentilesAggregatorTests.java index 63f8e799fb796..2f24d1c04bc85 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/TDigestPercentilesAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/TDigestPercentilesAggregatorTests.java @@ -190,8 +190,8 @@ private void testCase(Query query, CheckedConsumer { iw.addDocument(singleton(new BinaryDocValuesField(fieldName, rangeType.encodeRanges(singleton(range1))))); @@ -275,9 +274,7 @@ public void testValueScriptNumber() throws IOException { .field(FIELD_NAME) .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, NUMBER_VALUE_SCRIPT, Collections.emptyMap())); - MappedFieldType fieldType = createMappedFieldType(ValueType.NUMERIC); - fieldType.setName(FIELD_NAME); - fieldType.setHasDocValues(true); + MappedFieldType fieldType = createMappedFieldType(FIELD_NAME, ValueType.NUMERIC); testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> { iw.addDocument(singleton(new NumericDocValuesField(FIELD_NAME, 7))); @@ -293,9 +290,7 @@ public void testSingleScriptNumber() throws IOException { ValueCountAggregationBuilder aggregationBuilder = new ValueCountAggregationBuilder("name") .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, SINGLE_SCRIPT, Collections.emptyMap())); - MappedFieldType fieldType = createMappedFieldType(ValueType.NUMERIC); - fieldType.setName(FIELD_NAME); - fieldType.setHasDocValues(true); + MappedFieldType fieldType = createMappedFieldType(FIELD_NAME, ValueType.NUMERIC); testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> { Document doc = new Document(); @@ -325,9 +320,7 @@ public void testValueScriptString() throws IOException { .field(FIELD_NAME) .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, STRING_VALUE_SCRIPT, Collections.emptyMap())); - MappedFieldType fieldType = createMappedFieldType(ValueType.STRING); - fieldType.setName(FIELD_NAME); - fieldType.setHasDocValues(true); + MappedFieldType fieldType = createMappedFieldType(FIELD_NAME, ValueType.STRING); testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> { iw.addDocument(singleton(new SortedDocValuesField(FIELD_NAME, new BytesRef("1")))); @@ -343,9 +336,7 @@ public void testSingleScriptString() throws IOException { ValueCountAggregationBuilder aggregationBuilder = new ValueCountAggregationBuilder("name") .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, SINGLE_SCRIPT, Collections.emptyMap())); - MappedFieldType fieldType = createMappedFieldType(ValueType.STRING); - fieldType.setName(FIELD_NAME); - fieldType.setHasDocValues(true); + MappedFieldType fieldType = createMappedFieldType(FIELD_NAME, ValueType.STRING); testAggregation(aggregationBuilder, new MatchAllDocsQuery(), iw -> { Document doc = new Document(); @@ -384,9 +375,7 @@ private void testAggregation(Query query, ValueType valueType, CheckedConsumer indexer, Consumer verify, boolean testWithHint) throws IOException { - MappedFieldType fieldType = createMappedFieldType(valueType); - fieldType.setName(FIELD_NAME); - fieldType.setHasDocValues(true); + MappedFieldType fieldType = createMappedFieldType(FIELD_NAME, valueType); ValueCountAggregationBuilder aggregationBuilder = new ValueCountAggregationBuilder("_name"); if (valueType != null && testWithHint) { @@ -406,26 +395,26 @@ private void testAggregation( testCase(aggregationBuilder, query, buildIndex, verify, fieldType); } - private static MappedFieldType createMappedFieldType(ValueType valueType) { + private static MappedFieldType createMappedFieldType(String name, ValueType valueType) { switch (valueType) { case BOOLEAN: - return new BooleanFieldMapper.BooleanFieldType(); + return new BooleanFieldMapper.BooleanFieldType(name); case STRING: - return new KeywordFieldMapper.KeywordFieldType(); + return new KeywordFieldMapper.KeywordFieldType(name); case DOUBLE: - return new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE); + return new NumberFieldMapper.NumberFieldType(name, NumberFieldMapper.NumberType.DOUBLE); case NUMBER: case NUMERIC: case LONG: - return new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); + return new NumberFieldMapper.NumberFieldType(name, NumberFieldMapper.NumberType.LONG); case DATE: - return new DateFieldMapper.Builder("_name").fieldType(); + return new DateFieldMapper.DateFieldType(name); case IP: - return new IpFieldMapper.Builder("_name").fieldType(); + return new IpFieldMapper.IpFieldType(name); case GEOPOINT: - return new GeoPointFieldMapper.Builder("_name").fieldType(); + return new GeoPointFieldMapper.GeoPointFieldType(name); case RANGE: - return new RangeFieldMapper.Builder("_name", RangeType.DOUBLE).fieldType(); + return new RangeFieldMapper.RangeFieldType(name, RangeType.DOUBLE); default: throw new IllegalArgumentException("Test does not support value type [" + valueType + "]"); } diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/WeightedAvgAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/WeightedAvgAggregatorTests.java index d4360ce39056f..7ad2cecbd8041 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/WeightedAvgAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/WeightedAvgAggregatorTests.java @@ -36,9 +36,6 @@ import org.elasticsearch.index.mapper.NumberFieldMapper; import org.elasticsearch.search.aggregations.AggregationExecutionException; import org.elasticsearch.search.aggregations.AggregatorTestCase; -import org.elasticsearch.search.aggregations.metrics.InternalWeightedAvg; -import org.elasticsearch.search.aggregations.metrics.WeightedAvgAggregationBuilder; -import org.elasticsearch.search.aggregations.metrics.WeightedAvgAggregator; import org.elasticsearch.search.aggregations.support.AggregationInspectionHelper; import org.elasticsearch.search.aggregations.support.MultiValuesSourceFieldConfig; @@ -473,14 +470,8 @@ private void testCase(Query query, WeightedAvgAggregationBuilder aggregationBuil IndexSearcher indexSearcher = newSearcher(indexReader, true, true); try { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(fieldNumberType); - fieldType.setName("value_field"); - fieldType.setHasDocValues(true); - - MappedFieldType fieldType2 = new NumberFieldMapper.NumberFieldType(fieldNumberType); - fieldType2.setName("weight_field"); - fieldType2.setHasDocValues(true); - + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("value_field", fieldNumberType); + MappedFieldType fieldType2 = new NumberFieldMapper.NumberFieldType("weight_field", fieldNumberType); WeightedAvgAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType, fieldType2); aggregator.preCollection(); indexSearcher.search(query, aggregator); diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/pipeline/AvgBucketAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/pipeline/AvgBucketAggregatorTests.java index b8e9f1444dd6a..12e41cc73e672 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/pipeline/AvgBucketAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/pipeline/AvgBucketAggregatorTests.java @@ -111,14 +111,10 @@ public void testSameAggNames() throws IOException { try (IndexReader indexReader = DirectoryReader.open(directory)) { IndexSearcher indexSearcher = newSearcher(indexReader, true, true); - DateFieldMapper.Builder builder = new DateFieldMapper.Builder("histo"); - DateFieldMapper.DateFieldType fieldType = builder.fieldType(); - fieldType.setHasDocValues(true); - fieldType.setName(DATE_FIELD); - - MappedFieldType valueFieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - valueFieldType.setName(VALUE_FIELD); - valueFieldType.setHasDocValues(true); + DateFieldMapper.DateFieldType fieldType = new DateFieldMapper.DateFieldType(DATE_FIELD); + + MappedFieldType valueFieldType + = new NumberFieldMapper.NumberFieldType(VALUE_FIELD, NumberFieldMapper.NumberType.LONG); avgResult = searchAndReduce(indexSearcher, query, avgBuilder, 10000, new MappedFieldType[]{fieldType, valueFieldType}); diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/pipeline/BucketScriptAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/pipeline/BucketScriptAggregatorTests.java index 8f2eaa83509ce..e1a187b0e1327 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/pipeline/BucketScriptAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/pipeline/BucketScriptAggregatorTests.java @@ -70,12 +70,9 @@ protected ScriptService getMockScriptService() { } public void testScript() throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("number_field"); - fieldType.setHasDocValues(true); - MappedFieldType fieldType1 = new KeywordFieldMapper.KeywordFieldType(); - fieldType1.setName("the_field"); - fieldType1.setHasDocValues(true); + MappedFieldType fieldType + = new NumberFieldMapper.NumberFieldType("number_field", NumberFieldMapper.NumberType.INTEGER); + MappedFieldType fieldType1 = new KeywordFieldMapper.KeywordFieldType("the_field"); FiltersAggregationBuilder filters = new FiltersAggregationBuilder("placeholder", new MatchAllQueryBuilder()) .subAggregation(new TermsAggregationBuilder("the_terms").userValueTypeHint(ValueType.STRING).field("the_field") diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/pipeline/CumulativeSumAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/pipeline/CumulativeSumAggregatorTests.java index dd3e971f4400b..37a75ff7551a3 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/pipeline/CumulativeSumAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/pipeline/CumulativeSumAggregatorTests.java @@ -317,14 +317,9 @@ private void executeTestCase(Query query, AggregationBuilder aggBuilder, Consume try (IndexReader indexReader = DirectoryReader.open(directory)) { IndexSearcher indexSearcher = newSearcher(indexReader, true, true); - DateFieldMapper.Builder builder = new DateFieldMapper.Builder("_name"); - DateFieldMapper.DateFieldType fieldType = builder.fieldType(); - fieldType.setHasDocValues(true); - fieldType.setName(HISTO_FIELD); - - MappedFieldType valueFieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - valueFieldType.setHasDocValues(true); - valueFieldType.setName("value_field"); + DateFieldMapper.DateFieldType fieldType = new DateFieldMapper.DateFieldType(HISTO_FIELD); + MappedFieldType valueFieldType + = new NumberFieldMapper.NumberFieldType("value_field", NumberFieldMapper.NumberType.LONG); InternalAggregation histogram; histogram = searchAndReduce(indexSearcher, query, aggBuilder, new MappedFieldType[]{fieldType, valueFieldType}); diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/pipeline/DerivativeAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/pipeline/DerivativeAggregatorTests.java index 637a2db947dd2..632ba6f6f48d9 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/pipeline/DerivativeAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/pipeline/DerivativeAggregatorTests.java @@ -756,17 +756,11 @@ private void executeTestCase(Query query, AggregationBuilder aggBuilder, Consume try (IndexReader indexReader = DirectoryReader.open(directory)) { IndexSearcher indexSearcher = newSearcher(indexReader, true, true); - DateFieldMapper.Builder builder = new DateFieldMapper.Builder("_name"); - DateFieldMapper.DateFieldType fieldType = builder.fieldType(); - fieldType.setHasDocValues(true); - fieldType.setName(SINGLE_VALUED_FIELD_NAME); + DateFieldMapper.DateFieldType fieldType = new DateFieldMapper.DateFieldType(SINGLE_VALUED_FIELD_NAME); + MappedFieldType valueFieldType + = new NumberFieldMapper.NumberFieldType("value_field", NumberFieldMapper.NumberType.LONG); - MappedFieldType valueFieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - valueFieldType.setHasDocValues(true); - valueFieldType.setName("value_field"); - - InternalAggregation histogram; - histogram = searchAndReduce(indexSearcher, query, aggBuilder, new MappedFieldType[]{fieldType, valueFieldType}); + InternalAggregation histogram = searchAndReduce(indexSearcher, query, aggBuilder, fieldType, valueFieldType); verify.accept(histogram); } diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/pipeline/MovFnAggrgatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/pipeline/MovFnAggrgatorTests.java index e297e8a473f5f..9d07a3deffe0c 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/pipeline/MovFnAggrgatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/pipeline/MovFnAggrgatorTests.java @@ -148,14 +148,9 @@ private void executeTestCase(Query query, try (IndexReader indexReader = DirectoryReader.open(directory)) { IndexSearcher indexSearcher = newSearcher(indexReader, true, true); - DateFieldMapper.Builder builder = new DateFieldMapper.Builder("_name"); - DateFieldMapper.DateFieldType fieldType = builder.fieldType(); - fieldType.setHasDocValues(true); - fieldType.setName(aggBuilder.field()); - - MappedFieldType valueFieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - valueFieldType.setHasDocValues(true); - valueFieldType.setName("value_field"); + DateFieldMapper.DateFieldType fieldType = new DateFieldMapper.DateFieldType(aggBuilder.field()); + MappedFieldType valueFieldType + = new NumberFieldMapper.NumberFieldType("value_field", NumberFieldMapper.NumberType.LONG); InternalDateHistogram histogram; histogram = searchAndReduce(indexSearcher, query, aggBuilder, 1000, diff --git a/server/src/test/java/org/elasticsearch/search/collapse/CollapseBuilderTests.java b/server/src/test/java/org/elasticsearch/search/collapse/CollapseBuilderTests.java index 5956306fcad75..c815c8d274c62 100644 --- a/server/src/test/java/org/elasticsearch/search/collapse/CollapseBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/search/collapse/CollapseBuilderTests.java @@ -20,7 +20,6 @@ import org.apache.lucene.analysis.MockAnalyzer; import org.apache.lucene.index.DirectoryReader; -import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.search.Query; @@ -44,6 +43,7 @@ import java.io.IOException; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import static java.util.Collections.emptyList; @@ -144,33 +144,46 @@ public void testBuild() throws IOException { QueryShardContext shardContext = mock(QueryShardContext.class); try (IndexReader reader = DirectoryReader.open(dir)) { when(shardContext.getIndexReader()).thenReturn(reader); + MappedFieldType numberFieldType = - new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - MappedFieldType keywordFieldType = - new KeywordFieldMapper.KeywordFieldType(); - for (MappedFieldType fieldType : new MappedFieldType[] {numberFieldType, keywordFieldType}) { - fieldType.setName("field"); - fieldType.setHasDocValues(true); - when(shardContext.fieldMapper("field")).thenReturn(fieldType); - CollapseBuilder builder = new CollapseBuilder("field"); - CollapseContext collapseContext = builder.build(shardContext); - assertEquals(collapseContext.getFieldType(), fieldType); - - fieldType.setIndexOptions(IndexOptions.NONE); - collapseContext = builder.build(shardContext); - assertEquals(collapseContext.getFieldType(), fieldType); - - fieldType.setHasDocValues(false); - IllegalArgumentException exc = expectThrows(IllegalArgumentException.class, () -> builder.build(shardContext)); - assertEquals(exc.getMessage(), "cannot collapse on field `field` without `doc_values`"); - - fieldType.setHasDocValues(true); - builder.setInnerHits(new InnerHitBuilder()); - exc = expectThrows(IllegalArgumentException.class, () -> builder.build(shardContext)); - assertEquals(exc.getMessage(), - "cannot expand `inner_hits` for collapse field `field`, " + - "only indexed field can retrieve `inner_hits`"); - } + new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.LONG); + when(shardContext.fieldMapper("field")).thenReturn(numberFieldType); + CollapseBuilder builder = new CollapseBuilder("field"); + CollapseContext collapseContext = builder.build(shardContext); + assertEquals(collapseContext.getFieldType(), numberFieldType); + + numberFieldType = + new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.LONG, true, false, Collections.emptyMap()); + when(shardContext.fieldMapper("field")).thenReturn(numberFieldType); + IllegalArgumentException exc = expectThrows(IllegalArgumentException.class, () -> builder.build(shardContext)); + assertEquals(exc.getMessage(), "cannot collapse on field `field` without `doc_values`"); + + numberFieldType = + new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.LONG, false, true, Collections.emptyMap()); + when(shardContext.fieldMapper("field")).thenReturn(numberFieldType); + builder.setInnerHits(new InnerHitBuilder()); + exc = expectThrows(IllegalArgumentException.class, () -> builder.build(shardContext)); + assertEquals(exc.getMessage(), + "cannot expand `inner_hits` for collapse field `field`, only indexed field can retrieve `inner_hits`"); + + MappedFieldType keywordFieldType = new KeywordFieldMapper.KeywordFieldType("field"); + when(shardContext.fieldMapper("field")).thenReturn(keywordFieldType); + CollapseBuilder kbuilder = new CollapseBuilder("field"); + collapseContext = kbuilder.build(shardContext); + assertEquals(collapseContext.getFieldType(), keywordFieldType); + + keywordFieldType = new KeywordFieldMapper.KeywordFieldType("field", true, false, Collections.emptyMap()); + when(shardContext.fieldMapper("field")).thenReturn(keywordFieldType); + exc = expectThrows(IllegalArgumentException.class, () -> kbuilder.build(shardContext)); + assertEquals(exc.getMessage(), "cannot collapse on field `field` without `doc_values`"); + + keywordFieldType = new KeywordFieldMapper.KeywordFieldType("field", false, true, Collections.emptyMap()); + when(shardContext.fieldMapper("field")).thenReturn(keywordFieldType); + kbuilder.setInnerHits(new InnerHitBuilder()); + exc = expectThrows(IllegalArgumentException.class, () -> builder.build(shardContext)); + assertEquals(exc.getMessage(), + "cannot expand `inner_hits` for collapse field `field`, only indexed field can retrieve `inner_hits`"); + } } @@ -183,7 +196,7 @@ public void testBuildWithExceptions() { } { - MappedFieldType fieldType = new MappedFieldType() { + MappedFieldType fieldType = new MappedFieldType("field", true, true, Collections.emptyMap()) { @Override public MappedFieldType clone() { return null; @@ -203,8 +216,6 @@ public Query existsQuery(QueryShardContext context) { return null; } }; - fieldType.setName("field"); - fieldType.setHasDocValues(true); when(shardContext.fieldMapper("field")).thenReturn(fieldType); CollapseBuilder builder = new CollapseBuilder("field"); IllegalArgumentException exc = expectThrows(IllegalArgumentException.class, () -> builder.build(shardContext)); diff --git a/server/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/CustomHighlighter.java b/server/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/CustomHighlighter.java index 6ece69999f7fc..b2221304fb83c 100644 --- a/server/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/CustomHighlighter.java +++ b/server/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/CustomHighlighter.java @@ -18,8 +18,8 @@ */ package org.elasticsearch.search.fetch.subphase.highlight; +import org.apache.lucene.document.FieldType; import org.elasticsearch.common.text.Text; -import org.elasticsearch.index.mapper.MappedFieldType; import java.util.ArrayList; import java.util.List; @@ -64,7 +64,7 @@ public HighlightField highlight(HighlighterContext highlighterContext) { } @Override - public boolean canHighlight(MappedFieldType fieldType) { + public boolean canHighlight(FieldType fieldType) { return true; } diff --git a/server/src/test/java/org/elasticsearch/search/geo/GeoShapeQueryTests.java b/server/src/test/java/org/elasticsearch/search/geo/GeoShapeQueryTests.java index eca23aba9eab0..bc0541853ba6f 100644 --- a/server/src/test/java/org/elasticsearch/search/geo/GeoShapeQueryTests.java +++ b/server/src/test/java/org/elasticsearch/search/geo/GeoShapeQueryTests.java @@ -20,7 +20,6 @@ package org.elasticsearch.search.geo; import com.carrotsearch.randomizedtesting.generators.RandomNumbers; - import org.apache.lucene.geo.GeoTestUtil; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.search.SearchResponse; diff --git a/server/src/test/java/org/elasticsearch/search/query/QueryPhaseTests.java b/server/src/test/java/org/elasticsearch/search/query/QueryPhaseTests.java index 7cfa661010924..313e12d94ef0f 100644 --- a/server/src/test/java/org/elasticsearch/search/query/QueryPhaseTests.java +++ b/server/src/test/java/org/elasticsearch/search/query/QueryPhaseTests.java @@ -641,8 +641,8 @@ public void testDisableTopScoreCollection() throws Exception { public void testNumericLongOrDateSortOptimization() throws Exception { final String fieldNameLong = "long-field"; final String fieldNameDate = "date-field"; - MappedFieldType fieldTypeLong = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - MappedFieldType fieldTypeDate = new DateFieldMapper.Builder(fieldNameDate).fieldType(); + MappedFieldType fieldTypeLong = new NumberFieldMapper.NumberFieldType(fieldNameLong, NumberFieldMapper.NumberType.LONG); + MappedFieldType fieldTypeDate = new DateFieldMapper.DateFieldType(fieldNameDate); MapperService mapperService = mock(MapperService.class); when(mapperService.fieldType(fieldNameLong)).thenReturn(fieldTypeLong); when(mapperService.fieldType(fieldNameDate)).thenReturn(fieldTypeDate); diff --git a/server/src/test/java/org/elasticsearch/search/slice/SliceBuilderTests.java b/server/src/test/java/org/elasticsearch/search/slice/SliceBuilderTests.java index c234df58b384a..903bf6e2ace80 100644 --- a/server/src/test/java/org/elasticsearch/search/slice/SliceBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/search/slice/SliceBuilderTests.java @@ -118,7 +118,7 @@ private ShardSearchRequest createRequest(int shardId, String[] routings, String private QueryShardContext createShardContext(Version indexVersionCreated, IndexReader reader, String fieldName, DocValuesType dvType, int numShards, int shardId) { - MappedFieldType fieldType = new MappedFieldType() { + MappedFieldType fieldType = new MappedFieldType(fieldName, true, dvType != null, Collections.emptyMap()) { @Override public MappedFieldType clone() { return null; @@ -138,7 +138,6 @@ public Query existsQuery(QueryShardContext context) { return null; } }; - fieldType.setName(fieldName); QueryShardContext context = mock(QueryShardContext.class); when(context.fieldMapper(fieldName)).thenReturn(fieldType); when(context.getIndexReader()).thenReturn(reader); @@ -146,8 +145,6 @@ public Query existsQuery(QueryShardContext context) { IndexSettings indexSettings = createIndexSettings(indexVersionCreated, numShards); when(context.getIndexSettings()).thenReturn(indexSettings); if (dvType != null) { - fieldType.setHasDocValues(true); - fieldType.setDocValuesType(dvType); IndexNumericFieldData fd = mock(IndexNumericFieldData.class); when(context.getForField(fieldType)).thenReturn(fd); } diff --git a/server/src/test/java/org/elasticsearch/search/sort/AbstractSortTestCase.java b/server/src/test/java/org/elasticsearch/search/sort/AbstractSortTestCase.java index b1dd7b7cc807a..8a20cb2d4c8cd 100644 --- a/server/src/test/java/org/elasticsearch/search/sort/AbstractSortTestCase.java +++ b/server/src/test/java/org/elasticsearch/search/sort/AbstractSortTestCase.java @@ -220,9 +220,8 @@ public ObjectMapper getObjectMapper(String name) { * Tests that require other field types can override this. */ protected MappedFieldType provideMappedFieldType(String name) { - NumberFieldMapper.NumberFieldType doubleFieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE); - doubleFieldType.setName(name); - doubleFieldType.setHasDocValues(true); + NumberFieldMapper.NumberFieldType doubleFieldType + = new NumberFieldMapper.NumberFieldType(name, NumberFieldMapper.NumberType.DOUBLE); return doubleFieldType; } diff --git a/server/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java b/server/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java index b457e560c65d4..00e5a4a5e33a6 100644 --- a/server/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java @@ -30,7 +30,6 @@ import org.apache.lucene.document.SortedNumericDocValuesField; import org.apache.lucene.document.TextField; import org.apache.lucene.index.DirectoryReader; -import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.index.Term; import org.apache.lucene.search.AssertingIndexSearcher; @@ -328,34 +327,20 @@ public void testUnknownOptionFails() throws IOException { @Override protected MappedFieldType provideMappedFieldType(String name) { if (name.equals(MAPPED_STRING_FIELDNAME)) { - KeywordFieldMapper.KeywordFieldType fieldType = new KeywordFieldMapper.KeywordFieldType(); - fieldType.setName(name); - fieldType.setHasDocValues(true); - return fieldType; + return new KeywordFieldMapper.KeywordFieldType(name); } else if (name.startsWith("custom-")) { final MappedFieldType fieldType; if (name.startsWith("custom-keyword")) { - fieldType = new KeywordFieldMapper.KeywordFieldType(); + fieldType = new KeywordFieldMapper.KeywordFieldType(name); } else if (name.startsWith("custom-date")) { - fieldType = new DateFieldMapper.DateFieldType(); + fieldType = new DateFieldMapper.DateFieldType(name); } else { String type = name.split("-")[1]; if (type.equals("INT")) { type = "integer"; } NumberFieldMapper.NumberType numberType = NumberFieldMapper.NumberType.valueOf(type.toUpperCase(Locale.ENGLISH)); - if (numberType != null) { - fieldType = new NumberFieldMapper.NumberFieldType(numberType); - } else { - fieldType = new KeywordFieldMapper.KeywordFieldType(); - } - } - fieldType.setName(name); - fieldType.setHasDocValues(true); - if (name.endsWith("-ni")) { - fieldType.setIndexOptions(IndexOptions.NONE); - } else { - fieldType.setIndexOptions(IndexOptions.DOCS); + fieldType = new NumberFieldMapper.NumberFieldType(name, numberType); } return fieldType; } else { diff --git a/server/src/test/java/org/elasticsearch/search/sort/GeoDistanceSortBuilderTests.java b/server/src/test/java/org/elasticsearch/search/sort/GeoDistanceSortBuilderTests.java index 453b3fdbb09a8..8e5f156ed0eec 100644 --- a/server/src/test/java/org/elasticsearch/search/sort/GeoDistanceSortBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/search/sort/GeoDistanceSortBuilderTests.java @@ -117,9 +117,7 @@ public static GeoDistanceSortBuilder randomGeoDistanceSortBuilder() { @Override protected MappedFieldType provideMappedFieldType(String name) { - MappedFieldType clone = new GeoPointFieldMapper.GeoPointFieldType(); - clone.setName(name); - return clone; + return new GeoPointFieldMapper.GeoPointFieldType(name); } private static GeoPoint[] points(GeoPoint[] original) { diff --git a/server/src/test/java/org/elasticsearch/search/suggest/completion/CategoryContextMappingTests.java b/server/src/test/java/org/elasticsearch/search/suggest/completion/CategoryContextMappingTests.java index 0f6780faea38f..4cbb0bc28d77c 100644 --- a/server/src/test/java/org/elasticsearch/search/suggest/completion/CategoryContextMappingTests.java +++ b/server/src/test/java/org/elasticsearch/search/suggest/completion/CategoryContextMappingTests.java @@ -20,6 +20,7 @@ package org.elasticsearch.search.suggest.completion; import org.apache.lucene.document.Field; +import org.apache.lucene.document.FieldType; import org.apache.lucene.document.IntPoint; import org.apache.lucene.document.SortedDocValuesField; import org.apache.lucene.document.SortedSetDocValuesField; @@ -728,9 +729,8 @@ public void testParsingContextFromDocument() throws Exception { CategoryContextMapping mapping = ContextBuilder.category("cat").field("category").build(); ParseContext.Document document = new ParseContext.Document(); - KeywordFieldMapper.KeywordFieldType keyword = new KeywordFieldMapper.KeywordFieldType(); - keyword.setName("category"); - document.add(new Field(keyword.name(), new BytesRef("category1"), keyword)); + KeywordFieldMapper.KeywordFieldType keyword = new KeywordFieldMapper.KeywordFieldType("category"); + document.add(new KeywordFieldMapper.KeywordField(keyword.name(), new BytesRef("category1"), new FieldType())); // Ignore doc values document.add(new SortedSetDocValuesField(keyword.name(), new BytesRef("category1"))); Set context = mapping.parseContext(document); @@ -739,11 +739,10 @@ public void testParsingContextFromDocument() throws Exception { document = new ParseContext.Document(); - TextFieldMapper.TextFieldType text = new TextFieldMapper.TextFieldType(); - text.setName("category"); - document.add(new Field(text.name(), "category1", text)); + TextFieldMapper.TextFieldType text = new TextFieldMapper.TextFieldType("category"); + document.add(new Field(text.name(), "category1", TextFieldMapper.Defaults.FIELD_TYPE)); // Ignore stored field - document.add(new StoredField(text.name(), "category1", text)); + document.add(new StoredField(text.name(), "category1", TextFieldMapper.Defaults.FIELD_TYPE)); context = mapping.parseContext(document); assertThat(context.size(), equalTo(1)); assertTrue(context.contains("category1")); diff --git a/server/src/test/java/org/elasticsearch/search/suggest/completion/CompletionSuggesterBuilderTests.java b/server/src/test/java/org/elasticsearch/search/suggest/completion/CompletionSuggesterBuilderTests.java index f7423d3f55a93..13f85df7c46d2 100644 --- a/server/src/test/java/org/elasticsearch/search/suggest/completion/CompletionSuggesterBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/search/suggest/completion/CompletionSuggesterBuilderTests.java @@ -164,8 +164,7 @@ protected void mutateSpecificParameters(CompletionSuggestionBuilder builder) thr @Override protected MappedFieldType mockFieldType(String fieldName) { - CompletionFieldType completionFieldType = new CompletionFieldType(); - completionFieldType.setName(fieldName); + CompletionFieldType completionFieldType = new CompletionFieldType(fieldName); completionFieldType.setContextMappings(new ContextMappings(contextMappings)); return completionFieldType; } diff --git a/test/framework/src/main/java/org/elasticsearch/index/mapper/FieldMapperTestCase.java b/test/framework/src/main/java/org/elasticsearch/index/mapper/FieldMapperTestCase.java index b4c6e6dbd1c6a..0609e218b6a67 100644 --- a/test/framework/src/main/java/org/elasticsearch/index/mapper/FieldMapperTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/index/mapper/FieldMapperTestCase.java @@ -19,21 +19,34 @@ package org.elasticsearch.index.mapper; +import org.apache.lucene.analysis.core.KeywordAnalyzer; +import org.apache.lucene.analysis.core.WhitespaceAnalyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.search.similarities.BM25Similarity; +import org.apache.lucene.search.similarities.BooleanSimilarity; import org.elasticsearch.Version; +import org.elasticsearch.common.Strings; +import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.json.JsonXContent; +import org.elasticsearch.index.IndexService; import org.elasticsearch.index.analysis.AnalyzerScope; import org.elasticsearch.index.analysis.NamedAnalyzer; import org.elasticsearch.index.similarity.SimilarityProvider; import org.elasticsearch.test.ESSingleNodeTestCase; +import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; +import java.util.Set; import java.util.function.BiConsumer; import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; public abstract class FieldMapperTestCase> extends ESSingleNodeTestCase { @@ -64,58 +77,40 @@ private Modifier booleanModifier(String name, boolean updateable, BiConsumer unsupportedProperties() { + return Collections.emptySet(); } private final List modifiers = new ArrayList<>(Arrays.asList( new Modifier("analyzer", false, (a, b) -> { - a.indexAnalyzer(new NamedAnalyzer("a", AnalyzerScope.INDEX, new StandardAnalyzer())); - a.indexAnalyzer(new NamedAnalyzer("b", AnalyzerScope.INDEX, new StandardAnalyzer())); + a.indexAnalyzer(new NamedAnalyzer("standard", AnalyzerScope.INDEX, new StandardAnalyzer())); + a.indexAnalyzer(new NamedAnalyzer("keyword", AnalyzerScope.INDEX, new KeywordAnalyzer())); }), new Modifier("boost", true, (a, b) -> { - a.fieldType().setBoost(1.1f); - b.fieldType().setBoost(1.2f); + a.boost(1.1f); + b.boost(1.2f); }), - new Modifier("doc_values", supportsDocValues() == false, (a, b) -> { - if (supportsDocValues()) { - a.docValues(true); - b.docValues(false); - } + new Modifier("doc_values", false, (a, b) -> { + a.docValues(true); + b.docValues(false); }), - booleanModifier("eager_global_ordinals", true, (a, t) -> a.fieldType().setEagerGlobalOrdinals(t)), + booleanModifier("eager_global_ordinals", true, (a, t) -> a.setEagerGlobalOrdinals(t)), booleanModifier("norms", false, FieldMapper.Builder::omitNorms), - new Modifier("null_value", true, (a, b) -> { - a.fieldType().setNullValue(dummyNullValue); - }), new Modifier("search_analyzer", true, (a, b) -> { - a.searchAnalyzer(new NamedAnalyzer("a", AnalyzerScope.INDEX, new StandardAnalyzer())); - a.searchAnalyzer(new NamedAnalyzer("b", AnalyzerScope.INDEX, new StandardAnalyzer())); + a.searchAnalyzer(new NamedAnalyzer("standard", AnalyzerScope.INDEX, new StandardAnalyzer())); + a.searchAnalyzer(new NamedAnalyzer("keyword", AnalyzerScope.INDEX, new KeywordAnalyzer())); }), new Modifier("search_quote_analyzer", true, (a, b) -> { - a.searchQuoteAnalyzer(new NamedAnalyzer("a", AnalyzerScope.INDEX, new StandardAnalyzer())); - a.searchQuoteAnalyzer(new NamedAnalyzer("b", AnalyzerScope.INDEX, new StandardAnalyzer())); + a.searchQuoteAnalyzer(new NamedAnalyzer("standard", AnalyzerScope.INDEX, new StandardAnalyzer())); + a.searchQuoteAnalyzer(new NamedAnalyzer("whitespace", AnalyzerScope.INDEX, new WhitespaceAnalyzer())); }), new Modifier("similarity", false, (a, b) -> { - a.similarity(new SimilarityProvider("a", new BM25Similarity())); - b.similarity(new SimilarityProvider("b", new BM25Similarity())); + a.similarity(new SimilarityProvider("BM25", new BM25Similarity())); + b.similarity(new SimilarityProvider("boolean", new BooleanSimilarity())); }), - new Modifier("store", supportsStore() == false, (a, b) -> { - if (supportsStore()) { - a.store(true); - b.store(false); - } + new Modifier("store", false, (a, b) -> { + a.store(true); + b.store(false); }), new Modifier("term_vector", false, (a, b) -> { a.storeTermVectors(true); @@ -190,6 +185,9 @@ protected String contentType() { assertThat(e.getMessage(), containsString("bogustype")); } for (Modifier modifier : modifiers) { + if (unsupportedProperties().contains(modifier.property)) { + continue; + } builder1 = newBuilder(); builder2 = newBuilder(); modifier.apply(builder1, builder2); @@ -205,4 +203,47 @@ protected String contentType() { } } + public void testSerialization() throws IOException { + for (Modifier modifier : modifiers) { + if (unsupportedProperties().contains(modifier.property)) { + continue; + } + T builder1 = newBuilder(); + T builder2 = newBuilder(); + modifier.apply(builder1, builder2); + assertSerializes(modifier.property + "-a", builder1); + assertSerializes(modifier.property + "-b", builder2); + } + } + + protected Settings getIndexMapperSettings() { + return Settings.EMPTY; + } + + protected void assertSerializes(String indexname, T builder) throws IOException { + + // TODO can we do this without building an entire index? + IndexService index = createIndex("serialize-" + indexname, getIndexMapperSettings()); + MapperService mapperService = index.mapperService(); + + Mapper.BuilderContext context = new Mapper.BuilderContext(SETTINGS, new ContentPath(1)); + + XContentBuilder x = JsonXContent.contentBuilder(); + x.startObject().startObject("properties"); + builder.build(context).toXContent(x, ToXContent.EMPTY_PARAMS); + x.endObject().endObject(); + String mappings = Strings.toString(x); + + mapperService.merge("_doc", new CompressedXContent(mappings), MapperService.MergeReason.MAPPING_UPDATE); + + Mapper rebuilt = mapperService.documentMapper().mappers().getMapper(builder.name); + x = JsonXContent.contentBuilder(); + x.startObject().startObject("properties"); + rebuilt.toXContent(x, ToXContent.EMPTY_PARAMS); + x.endObject().endObject(); + String reparsed = Strings.toString(x); + + assertThat(reparsed, equalTo(mappings)); + } + } diff --git a/test/framework/src/main/java/org/elasticsearch/index/mapper/FieldTypeTestCase.java b/test/framework/src/main/java/org/elasticsearch/index/mapper/FieldTypeTestCase.java index c2c8227c8d06f..a48695535164d 100644 --- a/test/framework/src/main/java/org/elasticsearch/index/mapper/FieldTypeTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/index/mapper/FieldTypeTestCase.java @@ -26,6 +26,7 @@ import org.elasticsearch.test.EqualsHashCodeTestUtils; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -40,21 +41,11 @@ public abstract class FieldTypeTestCase extends ESTes public static final QueryShardContext MOCK_QSC_DISALLOW_EXPENSIVE = createMockQueryShardContext(false); /** Create a default constructed fieldtype */ - protected abstract T createDefaultFieldType(); - - T createNamedDefaultFieldType() { - T fieldType = createDefaultFieldType(); - fieldType.setName("foo"); - return fieldType; - } + protected abstract T createDefaultFieldType(String name, Map meta); @SuppressWarnings("unchecked") private final List> modifiers = new ArrayList<>(List.of( - t -> { - MappedFieldType copy = t.clone(); - copy.setName(t.name() + "-mutated"); - return (T) copy; - }, + t -> createDefaultFieldType(t.name() + "-mutated", t.meta()), t -> { MappedFieldType copy = t.clone(); copy.setBoost(t.boost() + 1); @@ -80,22 +71,15 @@ T createNamedDefaultFieldType() { copy.setSearchQuoteAnalyzer(new NamedAnalyzer(a.name() + "-mutated", a.scope(), a.analyzer())); return (T) copy; }, - t -> { - MappedFieldType copy = t.clone(); - copy.setNullValue(new Object()); - return (T) copy; - }, t -> { MappedFieldType copy = t.clone(); copy.setEagerGlobalOrdinals(t.eagerGlobalOrdinals() == false); return (T) copy; }, t -> { - MappedFieldType copy = t.clone(); Map meta = new HashMap<>(t.meta()); meta.put("bogus", "bogus"); - copy.setMeta(meta); - return (T) copy; + return createDefaultFieldType(t.name(), meta); } )); @@ -114,14 +98,14 @@ static QueryShardContext createMockQueryShardContext(boolean allowExpensiveQueri } public void testClone() { - MappedFieldType fieldType = createNamedDefaultFieldType(); + MappedFieldType fieldType = createDefaultFieldType("foo", Collections.emptyMap()); EqualsHashCodeTestUtils.checkEqualsAndHashCode(fieldType, MappedFieldType::clone); } @SuppressWarnings("unchecked") public void testEquals() { for (EqualsHashCodeTestUtils.MutateFunction modifier : modifiers) { - EqualsHashCodeTestUtils.checkEqualsAndHashCode(createNamedDefaultFieldType(), + EqualsHashCodeTestUtils.checkEqualsAndHashCode(createDefaultFieldType("foo", Collections.emptyMap()), t -> (T) t.clone(), modifier); } } diff --git a/test/framework/src/main/java/org/elasticsearch/index/mapper/MockFieldMapper.java b/test/framework/src/main/java/org/elasticsearch/index/mapper/MockFieldMapper.java index 311e3dd625e40..f1186427d96c3 100644 --- a/test/framework/src/main/java/org/elasticsearch/index/mapper/MockFieldMapper.java +++ b/test/framework/src/main/java/org/elasticsearch/index/mapper/MockFieldMapper.java @@ -19,6 +19,7 @@ package org.elasticsearch.index.mapper; +import org.apache.lucene.document.FieldType; import org.apache.lucene.index.Term; import org.apache.lucene.search.DocValuesFieldExistsQuery; import org.apache.lucene.search.Query; @@ -29,6 +30,7 @@ import org.elasticsearch.index.query.QueryShardContext; import java.io.IOException; +import java.util.Collections; import java.util.List; // this sucks how much must be overridden just do get a dummy field mapper... @@ -36,26 +38,22 @@ public class MockFieldMapper extends FieldMapper { static Settings dummySettings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT.id).build(); public MockFieldMapper(String fullName) { - this(fullName, new FakeFieldType()); + this(new FakeFieldType(fullName)); } - public MockFieldMapper(String fullName, MappedFieldType fieldType) { - super(findSimpleName(fullName), setName(fullName, fieldType), setName(fullName, fieldType), dummySettings, + public MockFieldMapper(MappedFieldType fieldType) { + super(findSimpleName(fieldType.name()), new FieldType(), fieldType, dummySettings, MultiFields.empty(), new CopyTo.Builder().build()); } - static MappedFieldType setName(String fullName, MappedFieldType fieldType) { - fieldType.setName(fullName); - return fieldType; - } - static String findSimpleName(String fullName) { int ndx = fullName.lastIndexOf('.'); return fullName.substring(ndx + 1); } public static class FakeFieldType extends TermBasedFieldType { - public FakeFieldType() { + public FakeFieldType(String name) { + super(name, true, false, Collections.emptyMap()); } protected FakeFieldType(FakeFieldType ref) { diff --git a/test/framework/src/main/java/org/elasticsearch/search/aggregations/AggregatorTestCase.java b/test/framework/src/main/java/org/elasticsearch/search/aggregations/AggregatorTestCase.java index 13b6b81ef3150..b40dfa5315dc3 100644 --- a/test/framework/src/main/java/org/elasticsearch/search/aggregations/AggregatorTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/search/aggregations/AggregatorTestCase.java @@ -888,46 +888,37 @@ protected DateFieldMapper.DateFieldType dateField(String name, DateFieldMapper.R * Make a {@linkplain NumberFieldMapper.NumberFieldType} for a {@code double}. */ protected NumberFieldMapper.NumberFieldType doubleField(String name) { - NumberFieldMapper.NumberFieldType result = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE); - result.setName(name); - return result; + return new NumberFieldMapper.NumberFieldType(name, NumberFieldMapper.NumberType.DOUBLE); } /** * Make a {@linkplain GeoPointFieldMapper.GeoPointFieldType} for a {@code geo_point}. */ protected GeoPointFieldMapper.GeoPointFieldType geoPointField(String name) { - GeoPointFieldMapper.GeoPointFieldType result = new GeoPointFieldMapper.GeoPointFieldType(); - result.setHasDocValues(true); - result.setName(name); - return result; + return new GeoPointFieldMapper.GeoPointFieldType(name); } /** * Make a {@linkplain DateFieldMapper.DateFieldType} for a {@code date}. */ protected KeywordFieldMapper.KeywordFieldType keywordField(String name) { - KeywordFieldMapper.KeywordFieldType result = new KeywordFieldMapper.KeywordFieldType(); - result.setName(name); - result.setHasDocValues(true); - return result; + return new KeywordFieldMapper.KeywordFieldType(name); } /** * Make a {@linkplain NumberFieldMapper.NumberFieldType} for a {@code long}. */ protected NumberFieldMapper.NumberFieldType longField(String name) { - NumberFieldMapper.NumberFieldType result = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - result.setName(name); - return result; + return new NumberFieldMapper.NumberFieldType(name, NumberFieldMapper.NumberType.LONG); } /** * Make a {@linkplain NumberFieldMapper.NumberFieldType} for a {@code range}. */ protected RangeFieldMapper.RangeFieldType rangeField(String name, RangeType rangeType) { - RangeFieldMapper.RangeFieldType result = new RangeFieldMapper.Builder(name, rangeType).fieldType(); - result.setName(name); - return result; + if (rangeType == RangeType.DATE) { + return new RangeFieldMapper.RangeFieldType(name, RangeFieldMapper.Defaults.DATE_FORMATTER); + } + return new RangeFieldMapper.RangeFieldType(name, rangeType); } } diff --git a/x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/mapper/HistogramFieldMapper.java b/x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/mapper/HistogramFieldMapper.java index 5423bd4a8c578..de6f706249291 100644 --- a/x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/mapper/HistogramFieldMapper.java +++ b/x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/mapper/HistogramFieldMapper.java @@ -10,6 +10,7 @@ import com.carrotsearch.hppc.IntArrayList; import org.apache.lucene.document.BinaryDocValuesField; import org.apache.lucene.document.Field; +import org.apache.lucene.document.FieldType; import org.apache.lucene.index.BinaryDocValues; import org.apache.lucene.index.DocValues; import org.apache.lucene.index.IndexOptions; @@ -73,11 +74,10 @@ public static class Names { public static class Defaults { public static final Explicit IGNORE_MALFORMED = new Explicit<>(false, false); - public static final HistogramFieldType FIELD_TYPE = new HistogramFieldType(); + public static final FieldType FIELD_TYPE = new FieldType(); static { FIELD_TYPE.setTokenized(false); - FIELD_TYPE.setHasDocValues(true); FIELD_TYPE.setIndexOptions(IndexOptions.NONE); FIELD_TYPE.freeze(); } @@ -90,7 +90,7 @@ public static class Builder extends FieldMapper.Builder { protected Boolean ignoreMalformed; public Builder(String name) { - super(name, Defaults.FIELD_TYPE, Defaults.FIELD_TYPE); + super(name, Defaults.FIELD_TYPE); builder = this; } @@ -109,18 +109,10 @@ protected Explicit ignoreMalformed(BuilderContext context) { return HistogramFieldMapper.Defaults.IGNORE_MALFORMED; } - public HistogramFieldMapper build(BuilderContext context, String simpleName, MappedFieldType fieldType, - MappedFieldType defaultFieldType, Settings indexSettings, - MultiFields multiFields, Explicit ignoreMalformed, CopyTo copyTo) { - setupFieldType(context); - return new HistogramFieldMapper(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, - ignoreMalformed, copyTo); - } - @Override public HistogramFieldMapper build(BuilderContext context) { - return build(context, name, fieldType, defaultFieldType, context.indexSettings(), - multiFieldsBuilder.build(this, context), ignoreMalformed(context), copyTo); + return new HistogramFieldMapper(name, fieldType, new HistogramFieldType(buildFullName(context), hasDocValues, meta), + context.indexSettings(), multiFieldsBuilder.build(this, context), ignoreMalformed(context), copyTo); } } @@ -145,9 +137,9 @@ public Mapper.Builder parse(String name, Map node, Pars protected Explicit ignoreMalformed; - public HistogramFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, + public HistogramFieldMapper(String simpleName, FieldType fieldType, MappedFieldType mappedFieldType, Settings indexSettings, MultiFields multiFields, Explicit ignoreMalformed, CopyTo copyTo) { - super(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, copyTo); + super(simpleName, fieldType, mappedFieldType, indexSettings, multiFields, copyTo); this.ignoreMalformed = ignoreMalformed; } @@ -171,7 +163,8 @@ protected void parseCreateField(ParseContext context) throws IOException { public static class HistogramFieldType extends MappedFieldType { - public HistogramFieldType() { + public HistogramFieldType(String name, boolean hasDocValues, Map meta) { + super(name, false, hasDocValues, meta); } HistogramFieldType(HistogramFieldType ref) { @@ -413,6 +406,11 @@ protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, } } + @Override + protected boolean indexedByDefault() { + return false; + } + /** re-usable {@link HistogramValue} implementation */ private static class InternalHistogramValue extends HistogramValue { double value; diff --git a/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/aggregations/metrics/HDRPreAggregatedPercentileRanksAggregatorTests.java b/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/aggregations/metrics/HDRPreAggregatedPercentileRanksAggregatorTests.java index 3571adaa083ea..3012d1afffa95 100644 --- a/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/aggregations/metrics/HDRPreAggregatedPercentileRanksAggregatorTests.java +++ b/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/aggregations/metrics/HDRPreAggregatedPercentileRanksAggregatorTests.java @@ -34,6 +34,7 @@ import org.hamcrest.Matchers; import java.io.IOException; +import java.util.Collections; import java.util.Iterator; import java.util.List; @@ -88,8 +89,7 @@ public void testSimple() throws IOException { PercentileRanksAggregationBuilder aggBuilder = new PercentileRanksAggregationBuilder("my_agg", new double[]{0.1, 0.5, 12}) .field("field") .method(PercentilesMethod.HDR); - MappedFieldType fieldType = new HistogramFieldMapper.Builder("field").fieldType(); - fieldType.setName("field"); + MappedFieldType fieldType = new HistogramFieldMapper.HistogramFieldType("field", true, Collections.emptyMap()); try (IndexReader reader = w.getReader()) { IndexSearcher searcher = new IndexSearcher(reader); PercentileRanks ranks = searchAndReduce(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType); diff --git a/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/aggregations/metrics/HDRPreAggregatedPercentilesAggregatorTests.java b/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/aggregations/metrics/HDRPreAggregatedPercentilesAggregatorTests.java index cd2ebe3ff8b3c..8f9f0c900995c 100644 --- a/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/aggregations/metrics/HDRPreAggregatedPercentilesAggregatorTests.java +++ b/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/aggregations/metrics/HDRPreAggregatedPercentilesAggregatorTests.java @@ -35,6 +35,7 @@ import org.elasticsearch.xpack.analytics.mapper.HistogramFieldMapper; import java.io.IOException; +import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.function.Consumer; @@ -147,8 +148,7 @@ private void testCase(Query query, CheckedConsumer indexer, Consumer verify) throws IOException { - testCase(avg("_name").field(FIELD_NAME), query, indexer, verify, defaultFieldType(FIELD_NAME)); + testCase(avg("_name").field(FIELD_NAME), query, indexer, verify, defaultFieldType()); } private BinaryDocValuesField getDocValue(String fieldName, double[] values) throws IOException { @@ -147,9 +148,7 @@ protected AggregationBuilder createAggBuilderForTypeTest(MappedFieldType fieldTy return new AvgAggregationBuilder("_name").field(fieldName); } - private MappedFieldType defaultFieldType(String fieldName) { - MappedFieldType fieldType = new HistogramFieldMapper.Builder("field").fieldType(); - fieldType.setName("field"); - return fieldType; + private MappedFieldType defaultFieldType() { + return new HistogramFieldMapper.HistogramFieldType(HistoBackedAvgAggregatorTests.FIELD_NAME, true, Collections.emptyMap()); } } diff --git a/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/aggregations/metrics/HistoBackedSumAggregatorTests.java b/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/aggregations/metrics/HistoBackedSumAggregatorTests.java index 0b15942d9280e..33f589f077932 100644 --- a/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/aggregations/metrics/HistoBackedSumAggregatorTests.java +++ b/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/aggregations/metrics/HistoBackedSumAggregatorTests.java @@ -34,6 +34,7 @@ import java.io.IOException; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.function.Consumer; @@ -106,7 +107,7 @@ public void testQueryFiltering() throws IOException { private void testCase(Query query, CheckedConsumer indexer, Consumer verify) throws IOException { - testCase(sum("_name").field(FIELD_NAME), query, indexer, verify, defaultFieldType(FIELD_NAME)); + testCase(sum("_name").field(FIELD_NAME), query, indexer, verify, defaultFieldType()); } private BinaryDocValuesField getDocValue(String fieldName, double[] values) throws IOException { @@ -147,9 +148,7 @@ protected AggregationBuilder createAggBuilderForTypeTest(MappedFieldType fieldTy return new SumAggregationBuilder("_name").field(fieldName); } - private MappedFieldType defaultFieldType(String fieldName) { - MappedFieldType fieldType = new HistogramFieldMapper.Builder("field").fieldType(); - fieldType.setName("field"); - return fieldType; + private MappedFieldType defaultFieldType() { + return new HistogramFieldMapper.HistogramFieldType(HistoBackedSumAggregatorTests.FIELD_NAME, true, Collections.emptyMap()); } } diff --git a/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/aggregations/metrics/HistoBackedValueCountAggregatorTests.java b/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/aggregations/metrics/HistoBackedValueCountAggregatorTests.java index d2121d38c94d9..d4a52a49a14ff 100644 --- a/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/aggregations/metrics/HistoBackedValueCountAggregatorTests.java +++ b/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/aggregations/metrics/HistoBackedValueCountAggregatorTests.java @@ -34,6 +34,7 @@ import java.io.IOException; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.function.Consumer; @@ -107,7 +108,7 @@ private void testCase( Query query, CheckedConsumer indexer, Consumer verify) throws IOException { - testCase(count("_name").field(FIELD_NAME), query, indexer, verify, defaultFieldType(FIELD_NAME)); + testCase(count("_name").field(FIELD_NAME), query, indexer, verify, defaultFieldType()); } private BinaryDocValuesField getDocValue(String fieldName, double[] values) throws IOException { @@ -152,9 +153,7 @@ protected AggregationBuilder createAggBuilderForTypeTest(MappedFieldType fieldTy return new ValueCountAggregationBuilder("_name").field(fieldName); } - private MappedFieldType defaultFieldType(String fieldName) { - MappedFieldType fieldType = new HistogramFieldMapper.Builder("field").fieldType(); - fieldType.setName("field"); - return fieldType; + private MappedFieldType defaultFieldType() { + return new HistogramFieldMapper.HistogramFieldType("field", true, Collections.emptyMap()); } } diff --git a/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/aggregations/metrics/TDigestPreAggregatedPercentileRanksAggregatorTests.java b/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/aggregations/metrics/TDigestPreAggregatedPercentileRanksAggregatorTests.java index 0de987c46a0db..bddea56afb376 100644 --- a/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/aggregations/metrics/TDigestPreAggregatedPercentileRanksAggregatorTests.java +++ b/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/aggregations/metrics/TDigestPreAggregatedPercentileRanksAggregatorTests.java @@ -36,6 +36,7 @@ import java.io.IOException; import java.util.Collection; +import java.util.Collections; import java.util.Iterator; import java.util.List; @@ -90,8 +91,7 @@ public void testSimple() throws IOException { PercentileRanksAggregationBuilder aggBuilder = new PercentileRanksAggregationBuilder("my_agg", new double[] {0.1, 0.5, 12}) .field("field") .method(PercentilesMethod.TDIGEST); - MappedFieldType fieldType = new HistogramFieldMapper.Builder("number").fieldType(); - fieldType.setName("field"); + MappedFieldType fieldType = new HistogramFieldMapper.HistogramFieldType("field", true, Collections.emptyMap()); try (IndexReader reader = w.getReader()) { IndexSearcher searcher = new IndexSearcher(reader); PercentileRanks ranks = search(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType); diff --git a/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/aggregations/metrics/TDigestPreAggregatedPercentilesAggregatorTests.java b/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/aggregations/metrics/TDigestPreAggregatedPercentilesAggregatorTests.java index 6f89f61d211ae..9902275989fc2 100644 --- a/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/aggregations/metrics/TDigestPreAggregatedPercentilesAggregatorTests.java +++ b/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/aggregations/metrics/TDigestPreAggregatedPercentilesAggregatorTests.java @@ -37,6 +37,7 @@ import java.io.IOException; import java.util.Collection; +import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.function.Consumer; @@ -144,8 +145,7 @@ private void testCase(Query query, CheckedConsumer { iw.addDocument(singleton(new NumericDocValuesField("other", 2))); @@ -193,8 +192,7 @@ public void testUnmappedWithMissingField() throws IOException { BoxplotAggregationBuilder aggregationBuilder = new BoxplotAggregationBuilder("boxplot") .field("does_not_exist").missing(0L); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("number"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER); testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { iw.addDocument(singleton(new NumericDocValuesField("number", 7))); @@ -211,9 +209,7 @@ public void testUnmappedWithMissingField() throws IOException { public void testUnsupportedType() { BoxplotAggregationBuilder aggregationBuilder = new BoxplotAggregationBuilder("boxplot").field("not_a_number"); - MappedFieldType fieldType = new KeywordFieldMapper.KeywordFieldType(); - fieldType.setName("not_a_number"); - fieldType.setHasDocValues(true); + MappedFieldType fieldType = new KeywordFieldMapper.KeywordFieldType("not_a_number"); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { @@ -221,7 +217,7 @@ public void testUnsupportedType() { }, (Consumer) boxplot -> { fail("Should have thrown exception"); }, fieldType)); - assertEquals(e.getMessage(), "Field [not_a_number] of type [keyword(indexed,tokenized)] " + + assertEquals(e.getMessage(), "Field [not_a_number] of type [keyword] " + "is not supported for aggregation [boxplot]"); } @@ -229,8 +225,7 @@ public void testBadMissingField() { BoxplotAggregationBuilder aggregationBuilder = new BoxplotAggregationBuilder("boxplot").field("number") .missing("not_a_number"); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("number"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER); expectThrows(NumberFormatException.class, () -> testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { @@ -249,8 +244,7 @@ public void testUnmappedWithBadMissingField() { BoxplotAggregationBuilder aggregationBuilder = new BoxplotAggregationBuilder("boxplot") .field("does_not_exist").missing("not_a_number"); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("number"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER); expectThrows(NumberFormatException.class, () -> testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { @@ -269,8 +263,7 @@ public void testEmptyBucket() throws IOException { HistogramAggregationBuilder histogram = new HistogramAggregationBuilder("histo").field("number").interval(10).minDocCount(0) .subAggregation(new BoxplotAggregationBuilder("boxplot").field("number")); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("number"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER); testCase(histogram, new MatchAllDocsQuery(), iw -> { iw.addDocument(singleton(new NumericDocValuesField("number", 1))); @@ -310,8 +303,7 @@ public void testFormatter() throws IOException { BoxplotAggregationBuilder aggregationBuilder = new BoxplotAggregationBuilder("boxplot").field("number") .format("0000.0"); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("number"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER); testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { iw.addDocument(singleton(new NumericDocValuesField("number", 1))); @@ -337,8 +329,7 @@ public void testGetProperty() throws IOException { GlobalAggregationBuilder globalBuilder = new GlobalAggregationBuilder("global") .subAggregation(new BoxplotAggregationBuilder("boxplot").field("number")); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("number"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER); testCase(globalBuilder, new MatchAllDocsQuery(), iw -> { iw.addDocument(singleton(new NumericDocValuesField("number", 1))); @@ -364,8 +355,7 @@ public void testValueScript() throws IOException { .field("number") .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_SCRIPT, Collections.emptyMap())); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("number"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER); testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { iw.addDocument(singleton(new NumericDocValuesField("number", 7))); @@ -384,8 +374,7 @@ public void testValueScriptUnmapped() throws IOException { .field("does_not_exist") .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_SCRIPT, Collections.emptyMap())); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("number"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER); testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { iw.addDocument(singleton(new NumericDocValuesField("number", 7))); @@ -405,8 +394,7 @@ public void testValueScriptUnmappedMissing() throws IOException { .script(new Script(ScriptType.INLINE, MockScriptEngine.NAME, VALUE_SCRIPT, Collections.emptyMap())) .missing(1.0); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("number"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER); testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> { iw.addDocument(singleton(new NumericDocValuesField("number", 7))); @@ -424,8 +412,7 @@ public void testValueScriptUnmappedMissing() throws IOException { private void testCase(Query query, CheckedConsumer buildIndex, Consumer verify) throws IOException { - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("number"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.INTEGER); BoxplotAggregationBuilder aggregationBuilder = new BoxplotAggregationBuilder("boxplot").field("number"); testCase(aggregationBuilder, query, buildIndex, verify, fieldType); } diff --git a/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/cumulativecardinality/CumulativeCardinalityAggregatorTests.java b/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/cumulativecardinality/CumulativeCardinalityAggregatorTests.java index a8894e3c13a92..5ffb5a5ca1efe 100644 --- a/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/cumulativecardinality/CumulativeCardinalityAggregatorTests.java +++ b/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/cumulativecardinality/CumulativeCardinalityAggregatorTests.java @@ -140,14 +140,9 @@ private void executeTestCase(Query query, AggregationBuilder aggBuilder, Consume try (IndexReader indexReader = DirectoryReader.open(directory)) { IndexSearcher indexSearcher = newSearcher(indexReader, true, true); - DateFieldMapper.Builder builder = new DateFieldMapper.Builder("_name"); - DateFieldMapper.DateFieldType fieldType = builder.fieldType(); - fieldType.setHasDocValues(true); - fieldType.setName(HISTO_FIELD); - - MappedFieldType valueFieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - valueFieldType.setHasDocValues(true); - valueFieldType.setName("value_field"); + DateFieldMapper.DateFieldType fieldType = new DateFieldMapper.DateFieldType(HISTO_FIELD); + MappedFieldType valueFieldType + = new NumberFieldMapper.NumberFieldType("value_field", NumberFieldMapper.NumberType.LONG); InternalAggregation histogram; histogram = searchAndReduce(indexSearcher, query, aggBuilder, fieldType, valueFieldType); diff --git a/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/mapper/HistogramFieldMapperTests.java b/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/mapper/HistogramFieldMapperTests.java index 76aa163d7e98e..2aa2bcd273f6a 100644 --- a/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/mapper/HistogramFieldMapperTests.java +++ b/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/mapper/HistogramFieldMapperTests.java @@ -13,12 +13,12 @@ import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.IndexService; import org.elasticsearch.index.mapper.DocumentMapper; +import org.elasticsearch.index.mapper.FieldMapperTestCase; import org.elasticsearch.index.mapper.MapperParsingException; +import org.elasticsearch.index.mapper.MapperService.MergeReason; import org.elasticsearch.index.mapper.ParsedDocument; import org.elasticsearch.index.mapper.SourceToParse; -import org.elasticsearch.index.mapper.MapperService.MergeReason; import org.elasticsearch.plugins.Plugin; -import org.elasticsearch.test.ESSingleNodeTestCase; import org.elasticsearch.xpack.analytics.AnalyticsPlugin; import org.elasticsearch.xpack.core.LocalStateCompositeXPackPlugin; @@ -26,13 +26,19 @@ import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.Set; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; -public class HistogramFieldMapperTests extends ESSingleNodeTestCase { +public class HistogramFieldMapperTests extends FieldMapperTestCase { + + @Override + protected Set unsupportedProperties() { + return Set.of("analyzer", "similarity", "doc_values", "store"); + } public void testParseValue() throws Exception { ensureGreen(); @@ -536,4 +542,8 @@ protected Collection> getPlugins() { return plugins; } + @Override + protected HistogramFieldMapper.Builder newBuilder() { + return new HistogramFieldMapper.Builder("histo"); + } } diff --git a/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/mapper/HistogramFieldTypeTests.java b/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/mapper/HistogramFieldTypeTests.java index 8cecc4b9c989a..78acdb9fa7c73 100644 --- a/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/mapper/HistogramFieldTypeTests.java +++ b/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/mapper/HistogramFieldTypeTests.java @@ -10,10 +10,12 @@ import org.elasticsearch.index.mapper.FieldTypeTestCase; import org.elasticsearch.index.mapper.MappedFieldType; +import java.util.Map; + public class HistogramFieldTypeTests extends FieldTypeTestCase { @Override - protected MappedFieldType createDefaultFieldType() { - return new HistogramFieldMapper.HistogramFieldType(); + protected MappedFieldType createDefaultFieldType(String name, Map meta) { + return new HistogramFieldMapper.HistogramFieldType(name, true, meta); } } diff --git a/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/movingPercentiles/MovingPercentilesHDRAggregatorTests.java b/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/movingPercentiles/MovingPercentilesHDRAggregatorTests.java index 7d6035a217bfc..7eac53df02fb1 100644 --- a/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/movingPercentiles/MovingPercentilesHDRAggregatorTests.java +++ b/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/movingPercentiles/MovingPercentilesHDRAggregatorTests.java @@ -67,14 +67,9 @@ protected void executeTestCase(int window, int shift, Query query, try (IndexReader indexReader = DirectoryReader.open(directory)) { IndexSearcher indexSearcher = newSearcher(indexReader, true, true); - DateFieldMapper.Builder builder = new DateFieldMapper.Builder("_name"); - DateFieldMapper.DateFieldType fieldType = builder.fieldType(); - fieldType.setHasDocValues(true); - fieldType.setName(aggBuilder.field()); - - MappedFieldType valueFieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE); - valueFieldType.setHasDocValues(true); - valueFieldType.setName("value_field"); + DateFieldMapper.DateFieldType fieldType = new DateFieldMapper.DateFieldType(aggBuilder.field()); + MappedFieldType valueFieldType + = new NumberFieldMapper.NumberFieldType("value_field", NumberFieldMapper.NumberType.DOUBLE); InternalDateHistogram histogram; histogram = searchAndReduce(indexSearcher, query, aggBuilder, 1000, diff --git a/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/movingPercentiles/MovingPercentilesTDigestAggregatorTests.java b/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/movingPercentiles/MovingPercentilesTDigestAggregatorTests.java index 601b32acb2ea0..d290f7309640a 100644 --- a/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/movingPercentiles/MovingPercentilesTDigestAggregatorTests.java +++ b/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/movingPercentiles/MovingPercentilesTDigestAggregatorTests.java @@ -68,14 +68,9 @@ protected void executeTestCase(int window, int shift, Query query, try (IndexReader indexReader = DirectoryReader.open(directory)) { IndexSearcher indexSearcher = newSearcher(indexReader, true, true); - DateFieldMapper.Builder builder = new DateFieldMapper.Builder("_name"); - DateFieldMapper.DateFieldType fieldType = builder.fieldType(); - fieldType.setHasDocValues(true); - fieldType.setName(aggBuilder.field()); - - MappedFieldType valueFieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE); - valueFieldType.setHasDocValues(true); - valueFieldType.setName("value_field"); + DateFieldMapper.DateFieldType fieldType = new DateFieldMapper.DateFieldType(aggBuilder.field()); + MappedFieldType valueFieldType + = new NumberFieldMapper.NumberFieldType("value_field", NumberFieldMapper.NumberType.DOUBLE); InternalDateHistogram histogram; histogram = searchAndReduce(indexSearcher, query, aggBuilder, 1000, diff --git a/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/normalize/NormalizeAggregatorTests.java b/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/normalize/NormalizeAggregatorTests.java index aebff64661b0c..fd83bef4f7664 100644 --- a/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/normalize/NormalizeAggregatorTests.java +++ b/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/normalize/NormalizeAggregatorTests.java @@ -149,15 +149,9 @@ private void testCase(ValuesSourceAggregationBuilder aggBuilder, Consumer buildIndex, Consumer verify) throws IOException { - TextFieldMapper.TextFieldType fieldType = new TextFieldMapper.TextFieldType(); - fieldType.setName("text"); + TextFieldMapper.TextFieldType fieldType = new TextFieldMapper.TextFieldType("text"); fieldType.setFielddata(true); AggregationBuilder aggregationBuilder = new StringStatsAggregationBuilder("_name").field("text"); diff --git a/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/topmetrics/TopMetricsAggregatorTests.java b/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/topmetrics/TopMetricsAggregatorTests.java index 55a8ffdf4a559..a3adacec42e7f 100644 --- a/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/topmetrics/TopMetricsAggregatorTests.java +++ b/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/topmetrics/TopMetricsAggregatorTests.java @@ -9,6 +9,7 @@ import org.apache.lucene.document.Field; import org.apache.lucene.document.LatLonDocValuesField; import org.apache.lucene.document.SortedNumericDocValuesField; +import org.apache.lucene.document.TextField; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexableField; @@ -479,22 +480,15 @@ private MappedFieldType[] geoPointAndDoubleField() { } private MappedFieldType numberFieldType(NumberType numberType, String name) { - NumberFieldMapper.NumberFieldType type = new NumberFieldMapper.NumberFieldType(numberType); - type.setName(name); - return type; + return new NumberFieldMapper.NumberFieldType(name, numberType); } private MappedFieldType textFieldType(String name) { - TextFieldMapper.TextFieldType type = new TextFieldMapper.TextFieldType(); - type.setName(name); - return type; + return new TextFieldMapper.TextFieldType(name); } private MappedFieldType geoPointFieldType(String name) { - GeoPointFieldMapper.GeoPointFieldType type = new GeoPointFieldMapper.GeoPointFieldType(); - type.setName(name); - type.setHasDocValues(true); - return type; + return new GeoPointFieldMapper.GeoPointFieldType(name); } private IndexableField doubleField(String name, double value) { @@ -510,7 +504,7 @@ private IndexableField longField(String name, long value) { } private IndexableField textField(String name, String value) { - return new Field(name, value, textFieldType(name)); + return new TextField(name, value, Field.Store.NO); } private IndexableField geoPointField(String name, double lat, double lon) { diff --git a/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/ttest/TTestAggregatorTests.java b/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/ttest/TTestAggregatorTests.java index 313030e468706..a2b847cdc7507 100644 --- a/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/ttest/TTestAggregatorTests.java +++ b/x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/ttest/TTestAggregatorTests.java @@ -181,8 +181,7 @@ public void testMultiplePairedValues() { public void testSameFieldAndNoFilters() { TTestType tTestType = randomFrom(TTestType.values()); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("field"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.INTEGER); TTestAggregationBuilder aggregationBuilder = new TTestAggregationBuilder("t_test") .a(new MultiValuesSourceFieldConfig.Builder().setFieldName("field").setMissing(100).build()) .b(new MultiValuesSourceFieldConfig.Builder().setFieldName("field").setMissing(100).build()) @@ -247,10 +246,8 @@ public void testUnmappedWithMissingField() throws IOException { TTestType tTestType = randomFrom(TTestType.values()); boolean missA = randomBoolean(); boolean missB = missA == false || randomBoolean(); // at least one of the fields should be missing - MappedFieldType fieldType1 = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType1.setName(missA ? "not_a" : "a"); - MappedFieldType fieldType2 = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType2.setName(missB ? "not_b" : "b"); + MappedFieldType fieldType1 = new NumberFieldMapper.NumberFieldType(missA ? "not_a" : "a", NumberFieldMapper.NumberType.INTEGER); + MappedFieldType fieldType2 = new NumberFieldMapper.NumberFieldType(missB ? "not_b" : "b", NumberFieldMapper.NumberType.INTEGER); TTestAggregationBuilder aggregationBuilder = new TTestAggregationBuilder("t_test") .a(new MultiValuesSourceFieldConfig.Builder().setFieldName("a").setMissing(100).build()) .b(new MultiValuesSourceFieldConfig.Builder().setFieldName("b").setMissing(100).build()) @@ -301,20 +298,16 @@ public void testUnsupportedType() { boolean wrongB = wrongA == false || randomBoolean(); // at least one of the fields should have unsupported type MappedFieldType fieldType1; if (wrongA) { - fieldType1 = new KeywordFieldMapper.KeywordFieldType(); - fieldType1.setHasDocValues(true); + fieldType1 = new KeywordFieldMapper.KeywordFieldType("a"); } else { - fieldType1 = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); + fieldType1 = new NumberFieldMapper.NumberFieldType("a", NumberFieldMapper.NumberType.INTEGER); } - fieldType1.setName("a"); MappedFieldType fieldType2; if (wrongB) { - fieldType2 = new KeywordFieldMapper.KeywordFieldType(); - fieldType2.setHasDocValues(true); + fieldType2 = new KeywordFieldMapper.KeywordFieldType("b"); } else { - fieldType2 = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); + fieldType2 = new NumberFieldMapper.NumberFieldType("b", NumberFieldMapper.NumberType.INTEGER); } - fieldType2.setName("b"); TTestAggregationBuilder aggregationBuilder = new TTestAggregationBuilder("t_test") .a(new MultiValuesSourceFieldConfig.Builder().setFieldName("a").build()) .b(new MultiValuesSourceFieldConfig.Builder().setFieldName("b").build()) @@ -336,14 +329,12 @@ public void testBadMissingField() { TTestType tTestType = randomFrom(TTestType.values()); boolean missA = randomBoolean(); boolean missB = missA == false || randomBoolean(); // at least one of the fields should be have bad missing - MappedFieldType fieldType1 = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType1.setName("a"); + MappedFieldType fieldType1 = new NumberFieldMapper.NumberFieldType("a", NumberFieldMapper.NumberType.INTEGER); MultiValuesSourceFieldConfig.Builder a = new MultiValuesSourceFieldConfig.Builder().setFieldName("a"); if (missA) { a.setMissing("bad_number"); } - MappedFieldType fieldType2 = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType2.setName("b"); + MappedFieldType fieldType2 = new NumberFieldMapper.NumberFieldType("b", NumberFieldMapper.NumberType.INTEGER); MultiValuesSourceFieldConfig.Builder b = new MultiValuesSourceFieldConfig.Builder().setFieldName("b"); if (missB) { b.setMissing("bad_number"); @@ -364,15 +355,14 @@ public void testUnmappedWithBadMissingField() { TTestType tTestType = randomFrom(TTestType.values()); boolean missA = randomBoolean(); boolean missB = missA == false || randomBoolean(); // at least one of the fields should be have bad missing - MappedFieldType fieldType1 = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType1.setName("a"); + MappedFieldType fieldType1 = new NumberFieldMapper.NumberFieldType("a", NumberFieldMapper.NumberType.INTEGER); MultiValuesSourceFieldConfig.Builder a = new MultiValuesSourceFieldConfig.Builder(); if (missA) { a.setFieldName("not_a").setMissing("bad_number"); } else { a.setFieldName("a"); } - MappedFieldType fieldType2 = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); + MappedFieldType fieldType2 = new NumberFieldMapper.NumberFieldType(missB ? "not_b" : "b", NumberFieldMapper.NumberType.INTEGER); MultiValuesSourceFieldConfig.Builder b = new MultiValuesSourceFieldConfig.Builder(); if (missB) { @@ -393,12 +383,9 @@ public void testUnmappedWithBadMissingField() { public void testEmptyBucket() throws IOException { TTestType tTestType = randomFrom(TTestType.values()); - MappedFieldType fieldType1 = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType1.setName("a"); - MappedFieldType fieldType2 = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType2.setName("b"); - MappedFieldType fieldTypePart = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldTypePart.setName("part"); + MappedFieldType fieldType1 = new NumberFieldMapper.NumberFieldType("a", NumberFieldMapper.NumberType.INTEGER); + MappedFieldType fieldType2 = new NumberFieldMapper.NumberFieldType("b", NumberFieldMapper.NumberType.INTEGER); + MappedFieldType fieldTypePart = new NumberFieldMapper.NumberFieldType("part", NumberFieldMapper.NumberType.INTEGER); HistogramAggregationBuilder histogram = new HistogramAggregationBuilder("histo").field("part").interval(10).minDocCount(0) .subAggregation(new TTestAggregationBuilder("t_test") .a(new MultiValuesSourceFieldConfig.Builder().setFieldName("a").build()) @@ -440,10 +427,8 @@ public void testEmptyBucket() throws IOException { @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/54365") public void testFormatter() throws IOException { TTestType tTestType = randomFrom(TTestType.values()); - MappedFieldType fieldType1 = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType1.setName("a"); - MappedFieldType fieldType2 = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType2.setName("b"); + MappedFieldType fieldType1 = new NumberFieldMapper.NumberFieldType("a", NumberFieldMapper.NumberType.INTEGER); + MappedFieldType fieldType2 = new NumberFieldMapper.NumberFieldType("b", NumberFieldMapper.NumberType.INTEGER); TTestAggregationBuilder aggregationBuilder = new TTestAggregationBuilder("t_test") .a(new MultiValuesSourceFieldConfig.Builder().setFieldName("a").build()) .b(new MultiValuesSourceFieldConfig.Builder().setFieldName("b").build()) @@ -462,10 +447,8 @@ public void testFormatter() throws IOException { } public void testGetProperty() throws IOException { - MappedFieldType fieldType1 = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType1.setName("a"); - MappedFieldType fieldType2 = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType2.setName("b"); + MappedFieldType fieldType1 = new NumberFieldMapper.NumberFieldType("a", NumberFieldMapper.NumberType.INTEGER); + MappedFieldType fieldType2 = new NumberFieldMapper.NumberFieldType("b", NumberFieldMapper.NumberType.INTEGER); GlobalAggregationBuilder globalBuilder = new GlobalAggregationBuilder("global") .subAggregation(new TTestAggregationBuilder("t_test") .a(new MultiValuesSourceFieldConfig.Builder().setFieldName("a").build()) @@ -490,8 +473,7 @@ public void testScript() throws IOException { boolean fieldInA = randomBoolean(); TTestType tTestType = randomFrom(TTestType.values()); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType.setName("field"); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.INTEGER); MultiValuesSourceFieldConfig a = new MultiValuesSourceFieldConfig.Builder().setFieldName("field").build(); MultiValuesSourceFieldConfig b = new MultiValuesSourceFieldConfig.Builder().setScript( @@ -509,10 +491,8 @@ public void testScript() throws IOException { } public void testPaired() throws IOException { - MappedFieldType fieldType1 = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType1.setName("a"); - MappedFieldType fieldType2 = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType2.setName("b"); + MappedFieldType fieldType1 = new NumberFieldMapper.NumberFieldType("a", NumberFieldMapper.NumberType.INTEGER); + MappedFieldType fieldType2 = new NumberFieldMapper.NumberFieldType("b", NumberFieldMapper.NumberType.INTEGER); TTestAggregationBuilder aggregationBuilder = new TTestAggregationBuilder("t_test") .a(new MultiValuesSourceFieldConfig.Builder().setFieldName("a").build()) .b(new MultiValuesSourceFieldConfig.Builder().setFieldName("b").build()) @@ -534,10 +514,8 @@ public void testPaired() throws IOException { } public void testHomoscedastic() throws IOException { - MappedFieldType fieldType1 = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType1.setName("a"); - MappedFieldType fieldType2 = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType2.setName("b"); + MappedFieldType fieldType1 = new NumberFieldMapper.NumberFieldType("a", NumberFieldMapper.NumberType.INTEGER); + MappedFieldType fieldType2 = new NumberFieldMapper.NumberFieldType("b", NumberFieldMapper.NumberType.INTEGER); TTestAggregationBuilder aggregationBuilder = new TTestAggregationBuilder("t_test") .a(new MultiValuesSourceFieldConfig.Builder().setFieldName("a").build()) .b(new MultiValuesSourceFieldConfig.Builder().setFieldName("b").build()) @@ -559,10 +537,8 @@ public void testHomoscedastic() throws IOException { } public void testHeteroscedastic() throws IOException { - MappedFieldType fieldType1 = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType1.setName("a"); - MappedFieldType fieldType2 = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType2.setName("b"); + MappedFieldType fieldType1 = new NumberFieldMapper.NumberFieldType("a", NumberFieldMapper.NumberType.INTEGER); + MappedFieldType fieldType2 = new NumberFieldMapper.NumberFieldType("b", NumberFieldMapper.NumberType.INTEGER); TTestAggregationBuilder aggregationBuilder = new TTestAggregationBuilder("t_test") .a(new MultiValuesSourceFieldConfig.Builder().setFieldName("a").build()) .b(new MultiValuesSourceFieldConfig.Builder().setFieldName("b").build()); @@ -587,10 +563,8 @@ public void testHeteroscedastic() throws IOException { public void testFiltered() throws IOException { TTestType tTestType = randomFrom(TTestType.values()); - MappedFieldType fieldType1 = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType1.setName("a"); - MappedFieldType fieldType2 = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType2.setName("b"); + MappedFieldType fieldType1 = new NumberFieldMapper.NumberFieldType("a", NumberFieldMapper.NumberType.INTEGER); + MappedFieldType fieldType2 = new NumberFieldMapper.NumberFieldType("b", NumberFieldMapper.NumberType.INTEGER); TTestAggregationBuilder aggregationBuilder = new TTestAggregationBuilder("t_test") .a(new MultiValuesSourceFieldConfig.Builder().setFieldName("a").setFilter(QueryBuilders.termQuery("b", 1)).build()) .b(new MultiValuesSourceFieldConfig.Builder().setFieldName("a").setFilter(QueryBuilders.termQuery("b", 2)).build()) @@ -642,10 +616,8 @@ public void testFilterByFilterOrScript() throws IOException { boolean fieldInA = randomBoolean(); TTestType tTestType = randomFrom(TTestType.HOMOSCEDASTIC, TTestType.HETEROSCEDASTIC); - MappedFieldType fieldType1 = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType1.setName("field"); - MappedFieldType fieldType2 = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType2.setName("term"); + MappedFieldType fieldType1 = new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.INTEGER); + MappedFieldType fieldType2 = new NumberFieldMapper.NumberFieldType("term", NumberFieldMapper.NumberType.INTEGER); boolean filterTermOne = randomBoolean(); @@ -675,10 +647,8 @@ public void testFilterByFilterOrScript() throws IOException { private void testCase(Query query, TTestType type, CheckedConsumer buildIndex, Consumer verify) throws IOException { - MappedFieldType fieldType1 = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType1.setName("a"); - MappedFieldType fieldType2 = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.INTEGER); - fieldType2.setName("b"); + MappedFieldType fieldType1 = new NumberFieldMapper.NumberFieldType("a", NumberFieldMapper.NumberType.INTEGER); + MappedFieldType fieldType2 = new NumberFieldMapper.NumberFieldType("b", NumberFieldMapper.NumberType.INTEGER); TTestAggregationBuilder aggregationBuilder = new TTestAggregationBuilder("t_test") .a(new MultiValuesSourceFieldConfig.Builder().setFieldName("a").build()) diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/DocumentSubsetBitsetCacheTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/DocumentSubsetBitsetCacheTests.java index a51af35e8f618..31f2e096a1cee 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/DocumentSubsetBitsetCacheTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/DocumentSubsetBitsetCacheTests.java @@ -549,10 +549,7 @@ private void runTestOnIndices(int numberIndices, CheckedConsumer Collections.singletonList((String) invocationOnMock.getArguments()[0])); when(mapperService.fieldType(Mockito.anyString())).then(invocation -> { final String fieldName = (String) invocation.getArguments()[0]; - KeywordFieldMapper.KeywordFieldType ft = new KeywordFieldMapper.KeywordFieldType(); - ft.setName(fieldName); - ft.freeze(); - return ft; + return new KeywordFieldMapper.KeywordFieldType(fieldName); }); final ThreadContext threadContext = new ThreadContext(Settings.EMPTY); @@ -191,10 +188,7 @@ public void testDLSWithLimitedPermissions() throws Exception { .then(invocationOnMock -> Collections.singletonList((String) invocationOnMock.getArguments()[0])); when(mapperService.fieldType(Mockito.anyString())).then(invocation -> { final String fieldName = (String) invocation.getArguments()[0]; - KeywordFieldMapper.KeywordFieldType ft = new KeywordFieldMapper.KeywordFieldType(); - ft.setName(fieldName); - ft.freeze(); - return ft; + return new KeywordFieldMapper.KeywordFieldType(fieldName); }); final ThreadContext threadContext = new ThreadContext(Settings.EMPTY); diff --git a/x-pack/plugin/frozen-indices/src/test/java/org/elasticsearch/index/engine/RewriteCachingDirectoryReaderTests.java b/x-pack/plugin/frozen-indices/src/test/java/org/elasticsearch/index/engine/RewriteCachingDirectoryReaderTests.java index 2219a78055544..4367f5a085cda 100644 --- a/x-pack/plugin/frozen-indices/src/test/java/org/elasticsearch/index/engine/RewriteCachingDirectoryReaderTests.java +++ b/x-pack/plugin/frozen-indices/src/test/java/org/elasticsearch/index/engine/RewriteCachingDirectoryReaderTests.java @@ -87,9 +87,7 @@ public void testIsWithinQuery() throws IOException { writer.addDocument(doc); try (DirectoryReader reader = DirectoryReader.open(writer)) { RewriteCachingDirectoryReader cachingDirectoryReader = new RewriteCachingDirectoryReader(dir, reader.leaves()); - DateFieldMapper.Builder b = new DateFieldMapper.Builder("test"); - DateFieldMapper.DateFieldType dateFieldType = b.fieldType(); - dateFieldType.setName("test"); + DateFieldMapper.DateFieldType dateFieldType = new DateFieldMapper.DateFieldType("test"); QueryRewriteContext context = new QueryRewriteContext(xContentRegistry(), writableRegistry(), null, () -> 0); MappedFieldType.Relation relation = dateFieldType.isFieldWithinQuery(cachingDirectoryReader, 0, 10, true, true, ZoneOffset.UTC, null, context); diff --git a/x-pack/plugin/mapper-constant-keyword/src/main/java/org/elasticsearch/xpack/constantkeyword/mapper/ConstantKeywordFieldMapper.java b/x-pack/plugin/mapper-constant-keyword/src/main/java/org/elasticsearch/xpack/constantkeyword/mapper/ConstantKeywordFieldMapper.java index 635ec9a181a55..d854d8405c46f 100644 --- a/x-pack/plugin/mapper-constant-keyword/src/main/java/org/elasticsearch/xpack/constantkeyword/mapper/ConstantKeywordFieldMapper.java +++ b/x-pack/plugin/mapper-constant-keyword/src/main/java/org/elasticsearch/xpack/constantkeyword/mapper/ConstantKeywordFieldMapper.java @@ -7,6 +7,7 @@ package org.elasticsearch.xpack.constantkeyword.mapper; +import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.search.MatchAllDocsQuery; import org.apache.lucene.search.MatchNoDocsQuery; @@ -40,6 +41,7 @@ import java.io.IOException; import java.time.ZoneId; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Objects; @@ -52,7 +54,7 @@ public class ConstantKeywordFieldMapper extends FieldMapper { public static final String CONTENT_TYPE = "constant_keyword"; public static class Defaults { - public static final MappedFieldType FIELD_TYPE = new ConstantKeywordFieldType(); + public static final FieldType FIELD_TYPE = new FieldType(); static { FIELD_TYPE.setIndexOptions(IndexOptions.NONE); FIELD_TYPE.freeze(); @@ -61,26 +63,22 @@ public static class Defaults { public static class Builder extends FieldMapper.Builder { + String value; + public Builder(String name) { - super(name, Defaults.FIELD_TYPE, Defaults.FIELD_TYPE); + super(name, Defaults.FIELD_TYPE); builder = this; } public Builder setValue(String value) { - fieldType().setValue(value); + this.value = value; return this; } - @Override - public ConstantKeywordFieldType fieldType() { - return (ConstantKeywordFieldType) super.fieldType(); - } - @Override public ConstantKeywordFieldMapper build(BuilderContext context) { - setupFieldType(context); return new ConstantKeywordFieldMapper( - name, fieldType, defaultFieldType, + name, fieldType, new ConstantKeywordFieldType(buildFullName(context), value, meta), context.indexSettings()); } } @@ -110,10 +108,15 @@ public Mapper.Builder parse(String name, Map node, ParserCont public static final class ConstantKeywordFieldType extends ConstantFieldType { - private String value; + private final String value; - public ConstantKeywordFieldType() { - super(); + public ConstantKeywordFieldType(String name, String value, Map meta) { + super(name, meta); + this.value = value; + } + + public ConstantKeywordFieldType(String name, String value) { + this(name, value, Collections.emptyMap()); } protected ConstantKeywordFieldType(ConstantKeywordFieldType ref) { @@ -144,12 +147,6 @@ public String value() { return value; } - /** Set the value. */ - public void setValue(String value) { - checkIfFrozen(); - this.value = Objects.requireNonNull(value); - } - @Override public String typeName() { return CONTENT_TYPE; @@ -241,9 +238,9 @@ public Query regexpQuery(String value, int flags, int maxDeterminizedStates, } - ConstantKeywordFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, + ConstantKeywordFieldMapper(String simpleName, FieldType fieldType, MappedFieldType mappedFieldType, Settings indexSettings) { - super(simpleName, fieldType, defaultFieldType, indexSettings, MultiFields.empty(), CopyTo.empty()); + super(simpleName, fieldType, mappedFieldType, indexSettings, MultiFields.empty(), CopyTo.empty()); } @Override @@ -271,11 +268,9 @@ protected void parseCreateField(ParseContext context) throws IOException { } if (fieldType().value == null) { - ConstantKeywordFieldType newFieldType = new ConstantKeywordFieldType(fieldType()); - newFieldType.setValue(value); - newFieldType.freeze(); + ConstantKeywordFieldType newFieldType = new ConstantKeywordFieldType(fieldType().name(), value, fieldType().meta()); Mapper update = new ConstantKeywordFieldMapper( - simpleName(), newFieldType, defaultFieldType, context.indexSettings().getSettings()); + simpleName(), fieldType, newFieldType, context.indexSettings().getSettings()); context.addDynamicMapper(update); } else if (Objects.equals(fieldType().value, value) == false) { throw new IllegalArgumentException("[constant_keyword] field [" + name() + diff --git a/x-pack/plugin/mapper-constant-keyword/src/test/java/org/elasticsearch/xpack/constantkeyword/mapper/ConstantKeywordFieldMapperTests.java b/x-pack/plugin/mapper-constant-keyword/src/test/java/org/elasticsearch/xpack/constantkeyword/mapper/ConstantKeywordFieldMapperTests.java index 0e6b22b5a7a01..490c98195b64f 100644 --- a/x-pack/plugin/mapper-constant-keyword/src/test/java/org/elasticsearch/xpack/constantkeyword/mapper/ConstantKeywordFieldMapperTests.java +++ b/x-pack/plugin/mapper-constant-keyword/src/test/java/org/elasticsearch/xpack/constantkeyword/mapper/ConstantKeywordFieldMapperTests.java @@ -25,6 +25,7 @@ import java.util.Collection; import java.util.Collections; +import java.util.Set; public class ConstantKeywordFieldMapperTests extends FieldMapperTestCase { @@ -33,6 +34,11 @@ protected Collection> getPlugins() { return pluginList(ConstantKeywordMapperPlugin.class, LocalStateCompositeXPackPlugin.class); } + @Override + protected Set unsupportedProperties() { + return Set.of("analyzer", "similarity", "store", "doc_values"); + } + @Override protected ConstantKeywordFieldMapper.Builder newBuilder() { return new ConstantKeywordFieldMapper.Builder("constant"); diff --git a/x-pack/plugin/mapper-constant-keyword/src/test/java/org/elasticsearch/xpack/constantkeyword/mapper/ConstantKeywordFieldTypeTests.java b/x-pack/plugin/mapper-constant-keyword/src/test/java/org/elasticsearch/xpack/constantkeyword/mapper/ConstantKeywordFieldTypeTests.java index 9ca8ba2eb4cff..2baf48b802043 100644 --- a/x-pack/plugin/mapper-constant-keyword/src/test/java/org/elasticsearch/xpack/constantkeyword/mapper/ConstantKeywordFieldTypeTests.java +++ b/x-pack/plugin/mapper-constant-keyword/src/test/java/org/elasticsearch/xpack/constantkeyword/mapper/ConstantKeywordFieldTypeTests.java @@ -16,28 +16,27 @@ import java.util.Arrays; import java.util.Collections; +import java.util.Map; public class ConstantKeywordFieldTypeTests extends FieldTypeTestCase { @Override - protected MappedFieldType createDefaultFieldType() { - ConstantKeywordFieldType ft = new ConstantKeywordFieldType(); - ft.setValue("foo"); - return ft; + protected MappedFieldType createDefaultFieldType(String name, Map meta) { + return new ConstantKeywordFieldType(name, "foo", meta); } public void testTermQuery() { - ConstantKeywordFieldType ft = new ConstantKeywordFieldType(); - assertEquals(new MatchNoDocsQuery(), ft.termQuery("foo", null)); - ft.setValue("foo"); + ConstantKeywordFieldType ft = new ConstantKeywordFieldType("f", "foo"); assertEquals(new MatchAllDocsQuery(), ft.termQuery("foo", null)); assertEquals(new MatchNoDocsQuery(), ft.termQuery("bar", null)); + ConstantKeywordFieldType bar = new ConstantKeywordFieldType("f", "bar"); + assertEquals(new MatchNoDocsQuery(), bar.termQuery("foo", null)); } public void testTermsQuery() { - ConstantKeywordFieldType ft = new ConstantKeywordFieldType(); - assertEquals(new MatchNoDocsQuery(), ft.termsQuery(Collections.singletonList("foo"), null)); - ft.setValue("foo"); + ConstantKeywordFieldType bar = new ConstantKeywordFieldType("f", "bar"); + assertEquals(new MatchNoDocsQuery(), bar.termsQuery(Collections.singletonList("foo"), null)); + ConstantKeywordFieldType ft = new ConstantKeywordFieldType("f", "foo"); assertEquals(new MatchAllDocsQuery(), ft.termsQuery(Collections.singletonList("foo"), null)); assertEquals(new MatchAllDocsQuery(), ft.termsQuery(Arrays.asList("bar", "foo", "quux"), null)); assertEquals(new MatchNoDocsQuery(), ft.termsQuery(Collections.emptyList(), null)); @@ -46,34 +45,34 @@ public void testTermsQuery() { } public void testWildcardQuery() { - ConstantKeywordFieldType ft = new ConstantKeywordFieldType(); - assertEquals(new MatchNoDocsQuery(), ft.wildcardQuery("f*o", null, null)); - ft.setValue("foo"); + ConstantKeywordFieldType bar = new ConstantKeywordFieldType("f", "bar"); + assertEquals(new MatchNoDocsQuery(), bar.wildcardQuery("f*o", null, null)); + ConstantKeywordFieldType ft = new ConstantKeywordFieldType("f", "foo"); assertEquals(new MatchAllDocsQuery(), ft.wildcardQuery("f*o", null, null)); assertEquals(new MatchNoDocsQuery(), ft.wildcardQuery("b*r", null, null)); } public void testPrefixQuery() { - ConstantKeywordFieldType ft = new ConstantKeywordFieldType(); - assertEquals(new MatchNoDocsQuery(), ft.prefixQuery("fo", null, null)); - ft.setValue("foo"); + ConstantKeywordFieldType bar = new ConstantKeywordFieldType("f", "bar"); + assertEquals(new MatchNoDocsQuery(), bar.prefixQuery("fo", null, null)); + ConstantKeywordFieldType ft = new ConstantKeywordFieldType("f", "foo"); assertEquals(new MatchAllDocsQuery(), ft.prefixQuery("fo", null, null)); assertEquals(new MatchNoDocsQuery(), ft.prefixQuery("ba", null, null)); } public void testExistsQuery() { - ConstantKeywordFieldType ft = new ConstantKeywordFieldType(); - assertEquals(new MatchNoDocsQuery(), ft.existsQuery(null)); - ft.setValue("foo"); + ConstantKeywordFieldType none = new ConstantKeywordFieldType("f", null); + assertEquals(new MatchNoDocsQuery(), none.existsQuery(null)); + ConstantKeywordFieldType ft = new ConstantKeywordFieldType("f", "foo"); assertEquals(new MatchAllDocsQuery(), ft.existsQuery(null)); } public void testRangeQuery() { - ConstantKeywordFieldType ft = new ConstantKeywordFieldType(); - assertEquals(new MatchNoDocsQuery(), ft.rangeQuery(null, null, randomBoolean(), randomBoolean(), null, null, null, null)); - assertEquals(new MatchNoDocsQuery(), ft.rangeQuery(null, "foo", randomBoolean(), randomBoolean(), null, null, null, null)); - assertEquals(new MatchNoDocsQuery(), ft.rangeQuery("foo", null, randomBoolean(), randomBoolean(), null, null, null, null)); - ft.setValue("foo"); + ConstantKeywordFieldType none = new ConstantKeywordFieldType("f", null); + assertEquals(new MatchNoDocsQuery(), none.rangeQuery(null, null, randomBoolean(), randomBoolean(), null, null, null, null)); + assertEquals(new MatchNoDocsQuery(), none.rangeQuery(null, "foo", randomBoolean(), randomBoolean(), null, null, null, null)); + assertEquals(new MatchNoDocsQuery(), none.rangeQuery("foo", null, randomBoolean(), randomBoolean(), null, null, null, null)); + ConstantKeywordFieldType ft = new ConstantKeywordFieldType("f", "foo"); assertEquals(new MatchAllDocsQuery(), ft.rangeQuery(null, null, randomBoolean(), randomBoolean(), null, null, null, null)); assertEquals(new MatchAllDocsQuery(), ft.rangeQuery("foo", null, true, randomBoolean(), null, null, null, null)); assertEquals(new MatchNoDocsQuery(), ft.rangeQuery("foo", null, false, randomBoolean(), null, null, null, null)); @@ -85,17 +84,17 @@ public void testRangeQuery() { } public void testFuzzyQuery() { - ConstantKeywordFieldType ft = new ConstantKeywordFieldType(); - assertEquals(new MatchNoDocsQuery(), ft.fuzzyQuery("fooquux", Fuzziness.AUTO, 3, 50, randomBoolean(), null)); - ft.setValue("foobar"); + ConstantKeywordFieldType none = new ConstantKeywordFieldType("f", null); + assertEquals(new MatchNoDocsQuery(), none.fuzzyQuery("fooquux", Fuzziness.AUTO, 3, 50, randomBoolean(), null)); + ConstantKeywordFieldType ft = new ConstantKeywordFieldType("f", "foobar"); assertEquals(new MatchAllDocsQuery(), ft.fuzzyQuery("foobaz", Fuzziness.AUTO, 3, 50, randomBoolean(), null)); assertEquals(new MatchNoDocsQuery(), ft.fuzzyQuery("fooquux", Fuzziness.AUTO, 3, 50, randomBoolean(), null)); } public void testRegexpQuery() { - ConstantKeywordFieldType ft = new ConstantKeywordFieldType(); - assertEquals(new MatchNoDocsQuery(), ft.regexpQuery("f..o", RegExp.ALL, 10, null, null)); - ft.setValue("foo"); + ConstantKeywordFieldType none = new ConstantKeywordFieldType("f", null); + assertEquals(new MatchNoDocsQuery(), none.regexpQuery("f..o", RegExp.ALL, 10, null, null)); + ConstantKeywordFieldType ft = new ConstantKeywordFieldType("f", "foo"); assertEquals(new MatchAllDocsQuery(), ft.regexpQuery("f.o", RegExp.ALL, 10, null, null)); assertEquals(new MatchNoDocsQuery(), ft.regexpQuery("f..o", RegExp.ALL, 10, null, null)); } diff --git a/x-pack/plugin/mapper-flattened/src/main/java/org/elasticsearch/xpack/flattened/mapper/FlatObjectFieldMapper.java b/x-pack/plugin/mapper-flattened/src/main/java/org/elasticsearch/xpack/flattened/mapper/FlatObjectFieldMapper.java index 45f67688bf85b..21425cdc39b60 100644 --- a/x-pack/plugin/mapper-flattened/src/main/java/org/elasticsearch/xpack/flattened/mapper/FlatObjectFieldMapper.java +++ b/x-pack/plugin/mapper-flattened/src/main/java/org/elasticsearch/xpack/flattened/mapper/FlatObjectFieldMapper.java @@ -7,6 +7,7 @@ package org.elasticsearch.xpack.flattened.mapper; import org.apache.lucene.analysis.core.WhitespaceAnalyzer; +import org.apache.lucene.document.FieldType; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.LeafReaderContext; @@ -95,12 +96,11 @@ public final class FlatObjectFieldMapper extends DynamicKeyFieldMapper { private static final String KEYED_FIELD_SUFFIX = "._keyed"; private static class Defaults { - public static final MappedFieldType FIELD_TYPE = new RootFlatObjectFieldType(); + public static final FieldType FIELD_TYPE = new FieldType(); static { FIELD_TYPE.setTokenized(false); FIELD_TYPE.setStored(false); - FIELD_TYPE.setHasDocValues(true); FIELD_TYPE.setIndexOptions(IndexOptions.DOCS); FIELD_TYPE.setOmitNorms(true); FIELD_TYPE.freeze(); @@ -113,17 +113,15 @@ private static class Defaults { public static class Builder extends FieldMapper.Builder { private int depthLimit = Defaults.DEPTH_LIMIT; private int ignoreAbove = Defaults.IGNORE_ABOVE; + private String nullValue = null; + private boolean eagerGlobalOrdinals = false; + private boolean splitQueriesOnWhitespace = false; public Builder(String name) { - super(name, Defaults.FIELD_TYPE, Defaults.FIELD_TYPE); + super(name, Defaults.FIELD_TYPE); builder = this; } - @Override - public RootFlatObjectFieldType fieldType() { - return (RootFlatObjectFieldType) super.fieldType(); - } - @Override public Builder indexOptions(IndexOptions indexOptions) { if (indexOptions.compareTo(IndexOptions.DOCS_AND_FREQS) > 0) { @@ -143,7 +141,7 @@ public Builder depthLimit(int depthLimit) { } public Builder eagerGlobalOrdinals(boolean eagerGlobalOrdinals) { - fieldType().setEagerGlobalOrdinals(eagerGlobalOrdinals); + this.eagerGlobalOrdinals = eagerGlobalOrdinals; return builder; } @@ -155,8 +153,13 @@ public Builder ignoreAbove(int ignoreAbove) { return this; } + public Builder nullValue(String nullValue) { + this.nullValue = nullValue; + return this; + } + public Builder splitQueriesOnWhitespace(boolean splitQueriesOnWhitespace) { - fieldType().setSplitQueriesOnWhitespace(splitQueriesOnWhitespace); + this.splitQueriesOnWhitespace = splitQueriesOnWhitespace; return builder; } @@ -177,13 +180,11 @@ public Builder store(boolean store) { @Override public FlatObjectFieldMapper build(BuilderContext context) { - setupFieldType(context); - if (fieldType().splitQueriesOnWhitespace()) { - NamedAnalyzer whitespaceAnalyzer = new NamedAnalyzer("whitespace", AnalyzerScope.INDEX, new WhitespaceAnalyzer()); - fieldType().setSearchAnalyzer(whitespaceAnalyzer); + MappedFieldType ft = new RootFlatObjectFieldType(buildFullName(context), indexed, hasDocValues, meta, splitQueriesOnWhitespace); + if (eagerGlobalOrdinals) { + ft.setEagerGlobalOrdinals(true); } - return new FlatObjectFieldMapper(name, fieldType, defaultFieldType, - ignoreAbove, depthLimit, context.indexSettings()); + return new FlatObjectFieldMapper(name, fieldType, ft, ignoreAbove, depthLimit, nullValue, context.indexSettings()); } } @@ -229,10 +230,17 @@ public static final class KeyedFlatObjectFieldType extends StringFieldType { private final String key; private boolean splitQueriesOnWhitespace; - public KeyedFlatObjectFieldType(String key) { + public KeyedFlatObjectFieldType(String name, boolean indexed, boolean hasDocValues, String key, + boolean splitQueriesOnWhitespace, Map meta) { + super(name, indexed, hasDocValues, meta); setIndexAnalyzer(Lucene.KEYWORD_ANALYZER); - setSearchAnalyzer(Lucene.KEYWORD_ANALYZER); + if (splitQueriesOnWhitespace == false) { + setSearchAnalyzer(Lucene.KEYWORD_ANALYZER); + } else { + setSearchAnalyzer(new NamedAnalyzer("whitespace", AnalyzerScope.INDEX, new WhitespaceAnalyzer())); + } this.key = key; + this.splitQueriesOnWhitespace = splitQueriesOnWhitespace; } public KeyedFlatObjectFieldType clone() { @@ -246,10 +254,7 @@ private KeyedFlatObjectFieldType(KeyedFlatObjectFieldType ref) { } private KeyedFlatObjectFieldType(String name, String key, RootFlatObjectFieldType ref) { - super(ref); - setName(name); - this.key = key; - this.splitQueriesOnWhitespace = ref.splitQueriesOnWhitespace; + this(name, ref.isSearchable(), ref.hasDocValues(), key, ref.splitQueriesOnWhitespace, ref.meta()); } @Override @@ -280,7 +285,6 @@ public boolean splitQueriesOnWhitespace() { } public void setSplitQueriesOnWhitespace(boolean splitQueriesOnWhitespace) { - checkIfFrozen(); this.splitQueriesOnWhitespace = splitQueriesOnWhitespace; } @@ -477,9 +481,16 @@ public IndexFieldData build(IndexSettings indexSettings, public static final class RootFlatObjectFieldType extends StringFieldType { private boolean splitQueriesOnWhitespace; - public RootFlatObjectFieldType() { + public RootFlatObjectFieldType(String name, boolean indexed, boolean hasDocValues, Map meta, + boolean splitQueriesOnWhitespace) { + super(name, indexed, hasDocValues, meta); + this.splitQueriesOnWhitespace = splitQueriesOnWhitespace; setIndexAnalyzer(Lucene.KEYWORD_ANALYZER); - setSearchAnalyzer(Lucene.KEYWORD_ANALYZER); + if (splitQueriesOnWhitespace) { + setSearchAnalyzer(new NamedAnalyzer("whitespace", AnalyzerScope.INDEX, new WhitespaceAnalyzer())); + } else { + setSearchAnalyzer(Lucene.KEYWORD_ANALYZER); + } } private RootFlatObjectFieldType(RootFlatObjectFieldType ref) { @@ -515,7 +526,6 @@ public boolean splitQueriesOnWhitespace() { } public void setSplitQueriesOnWhitespace(boolean splitQueriesOnWhitespace) { - checkIfFrozen(); this.splitQueriesOnWhitespace = splitQueriesOnWhitespace; } @@ -546,22 +556,25 @@ public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName) { } private FlatObjectFieldParser fieldParser; + private final String nullValue; private int depthLimit; private int ignoreAbove; private FlatObjectFieldMapper(String simpleName, - MappedFieldType fieldType, - MappedFieldType defaultFieldType, + FieldType fieldType, + MappedFieldType mappedFieldType, int ignoreAbove, int depthLimit, + String nullValue, Settings indexSettings) { - super(simpleName, fieldType, defaultFieldType, indexSettings, CopyTo.empty()); + super(simpleName, fieldType, mappedFieldType, indexSettings, CopyTo.empty()); assert fieldType.indexOptions().compareTo(IndexOptions.DOCS_AND_FREQS) <= 0; this.depthLimit = depthLimit; this.ignoreAbove = ignoreAbove; - this.fieldParser = new FlatObjectFieldParser(fieldType.name(), keyedFieldName(), - fieldType, depthLimit, ignoreAbove); + this.nullValue = nullValue; + this.fieldParser = new FlatObjectFieldParser(mappedFieldType.name(), keyedFieldName(), + mappedFieldType, depthLimit, ignoreAbove, nullValue); } @Override @@ -572,10 +585,13 @@ protected String contentType() { @Override protected void mergeOptions(FieldMapper mergeWith, List conflicts) { FlatObjectFieldMapper other = ((FlatObjectFieldMapper) mergeWith); + if (Objects.equals(this.nullValue, other.nullValue) == false) { + conflicts.add("mapper [" + name() + "] has different [null_value] settings"); + } this.depthLimit = other.depthLimit; this.ignoreAbove = other.ignoreAbove; - this.fieldParser = new FlatObjectFieldParser(fieldType.name(), keyedFieldName(), - fieldType, depthLimit, ignoreAbove); + this.fieldParser = new FlatObjectFieldParser(mappedFieldType.name(), keyedFieldName(), + mappedFieldType, depthLimit, ignoreAbove, nullValue); } @Override @@ -594,7 +610,7 @@ public KeyedFlatObjectFieldType keyedFieldType(String key) { } public String keyedFieldName() { - return fieldType.name() + KEYED_FIELD_SUFFIX; + return mappedFieldType.name() + KEYED_FIELD_SUFFIX; } @Override @@ -603,7 +619,7 @@ protected void parseCreateField(ParseContext context) throws IOException { return; } - if (fieldType.indexOptions() == IndexOptions.NONE && !fieldType.hasDocValues()) { + if (fieldType.indexOptions() == IndexOptions.NONE && mappedFieldType.hasDocValues() == false) { context.parser().skipChildren(); return; } @@ -611,7 +627,7 @@ protected void parseCreateField(ParseContext context) throws IOException { XContentParser xContentParser = context.parser(); context.doc().addAll(fieldParser.parse(xContentParser)); - if (!fieldType.hasDocValues()) { + if (mappedFieldType.hasDocValues() == false) { createFieldNamesField(context); } } @@ -619,17 +635,20 @@ protected void parseCreateField(ParseContext context) throws IOException { @Override protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException { super.doXContentBody(builder, includeDefaults, params); - + if (includeDefaults || mappedFieldType.isSearchable() && fieldType.indexOptions() != Defaults.FIELD_TYPE.indexOptions()) { + builder.field("index_options", indexOptionToString(fieldType.indexOptions())); + } if (includeDefaults || depthLimit != Defaults.DEPTH_LIMIT) { builder.field("depth_limit", depthLimit); } - if (includeDefaults || ignoreAbove != Defaults.IGNORE_ABOVE) { builder.field("ignore_above", ignoreAbove); } - - if (includeDefaults || fieldType().nullValue() != null) { - builder.field("null_value", fieldType().nullValue()); + if (includeDefaults || fieldType().eagerGlobalOrdinals()) { + builder.field("eager_global_ordinals", fieldType().eagerGlobalOrdinals()); + } + if (nullValue != null) { + builder.field("null_value", nullValue); } if (includeDefaults || fieldType().splitQueriesOnWhitespace()) { diff --git a/x-pack/plugin/mapper-flattened/src/main/java/org/elasticsearch/xpack/flattened/mapper/FlatObjectFieldParser.java b/x-pack/plugin/mapper-flattened/src/main/java/org/elasticsearch/xpack/flattened/mapper/FlatObjectFieldParser.java index a855440b2be71..d7bf14232ef58 100644 --- a/x-pack/plugin/mapper-flattened/src/main/java/org/elasticsearch/xpack/flattened/mapper/FlatObjectFieldParser.java +++ b/x-pack/plugin/mapper-flattened/src/main/java/org/elasticsearch/xpack/flattened/mapper/FlatObjectFieldParser.java @@ -9,7 +9,6 @@ import org.apache.lucene.document.Field; import org.apache.lucene.document.SortedSetDocValuesField; import org.apache.lucene.document.StringField; -import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.IndexableField; import org.apache.lucene.util.BytesRef; import org.elasticsearch.common.xcontent.XContentParser; @@ -35,17 +34,20 @@ class FlatObjectFieldParser { private final MappedFieldType fieldType; private final int depthLimit; private final int ignoreAbove; + private final String nullValue; FlatObjectFieldParser(String rootFieldName, String keyedFieldName, MappedFieldType fieldType, int depthLimit, - int ignoreAbove) { + int ignoreAbove, + String nullValue) { this.rootFieldName = rootFieldName; this.keyedFieldName = keyedFieldName; this.fieldType = fieldType; this.depthLimit = depthLimit; this.ignoreAbove = ignoreAbove; + this.nullValue = nullValue; } public List parse(XContentParser parser) throws IOException { @@ -108,8 +110,8 @@ private void parseFieldValue(XContentParser.Token token, String value = parser.text(); addField(path, currentName, value, fields); } else if (token == XContentParser.Token.VALUE_NULL) { - if (fieldType.nullValueAsString() != null) { - addField(path, currentName, fieldType.nullValueAsString(), fields); + if (nullValue != null) { + addField(path, currentName, nullValue, fields); } } else { // Note that we throw an exception here just to be safe. We don't actually expect to reach @@ -133,7 +135,7 @@ private void addField(ContentPath path, } String keyedValue = createKeyedValue(key, value); - if (fieldType.indexOptions() != IndexOptions.NONE) { + if (fieldType.isSearchable()) { fields.add(new StringField(rootFieldName, new BytesRef(value), Field.Store.NO)); fields.add(new StringField(keyedFieldName, new BytesRef(keyedValue), Field.Store.NO)); } diff --git a/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/index/mapper/FlatObjectFieldLookupTests.java b/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/index/mapper/FlatObjectFieldLookupTests.java index c821a9ea0df83..c58a5b270ce34 100644 --- a/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/index/mapper/FlatObjectFieldLookupTests.java +++ b/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/index/mapper/FlatObjectFieldLookupTests.java @@ -18,6 +18,7 @@ import org.elasticsearch.xpack.flattened.mapper.FlatObjectFieldMapper.KeyedFlatObjectFieldType; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -156,10 +157,12 @@ public void testScriptDocValuesLookup() { ScriptDocValues docValues2 = mock(ScriptDocValues.class); IndexFieldData fieldData2 = createFieldData(docValues2); - KeyedFlatObjectFieldType fieldType1 = new KeyedFlatObjectFieldType("key1"); + KeyedFlatObjectFieldType fieldType1 + = new KeyedFlatObjectFieldType("field", true, true, "key1", false, Collections.emptyMap()); when(mapperService.fieldType("json.key1")).thenReturn(fieldType1); - KeyedFlatObjectFieldType fieldType2 = new KeyedFlatObjectFieldType( "key2"); + KeyedFlatObjectFieldType fieldType2 + = new KeyedFlatObjectFieldType( "field", true, true, "key2", false, Collections.emptyMap()); when(mapperService.fieldType("json.key2")).thenReturn(fieldType2); Function> fieldDataSupplier = fieldType -> { diff --git a/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/FlatObjectFieldMapperTests.java b/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/FlatObjectFieldMapperTests.java index 7599f0bb3da28..e8e4116f08d31 100644 --- a/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/FlatObjectFieldMapperTests.java +++ b/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/FlatObjectFieldMapperTests.java @@ -35,6 +35,7 @@ import java.io.IOException; import java.util.Arrays; import java.util.Collection; +import java.util.Set; import static org.apache.lucene.analysis.BaseTokenStreamTestCase.assertTokenStreamContents; import static org.hamcrest.Matchers.equalTo; @@ -48,11 +49,6 @@ protected FlatObjectFieldMapper.Builder newBuilder() { return new FlatObjectFieldMapper.Builder("flat-object"); } - @Override - protected boolean supportsStore() { - return false; - } - @Before public void setup() { indexService = createIndex("test"); @@ -60,6 +56,11 @@ public void setup() { addBooleanModifier("split_queries_on_whitespace", true, FlatObjectFieldMapper.Builder::splitQueriesOnWhitespace); } + @Override + protected Set unsupportedProperties() { + return Set.of("store", "analyzer", "similarity"); + } + @Override protected Collection> getPlugins() { return pluginList(FlattenedMapperPlugin.class, LocalStateCompositeXPackPlugin.class); diff --git a/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/FlatObjectFieldParserTests.java b/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/FlatObjectFieldParserTests.java index ff4de0d85cee1..74b2d822c2320 100644 --- a/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/FlatObjectFieldParserTests.java +++ b/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/FlatObjectFieldParserTests.java @@ -30,9 +30,10 @@ public class FlatObjectFieldParserTests extends ESTestCase { public void setUp() throws Exception { super.setUp(); parser = new FlatObjectFieldParser("field", "field._keyed", - new FakeFieldType(), + new FakeFieldType("field"), Integer.MAX_VALUE, - Integer.MAX_VALUE); + Integer.MAX_VALUE, + null); } public void testTextValues() throws Exception { @@ -209,7 +210,7 @@ public void testDepthLimit() throws Exception { "\"parent2\": [{ \"key\" : { \"key\" : \"value\" }}]}"; XContentParser xContentParser = createXContentParser(input); FlatObjectFieldParser configuredParser = new FlatObjectFieldParser("field", "field._keyed", - new FakeFieldType(), 2, Integer.MAX_VALUE); + new FakeFieldType("field"), 2, Integer.MAX_VALUE, null); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> configuredParser.parse(xContentParser)); @@ -221,7 +222,7 @@ public void testDepthLimitBoundary() throws Exception { "\"parent2\": [{ \"key\" : { \"key\" : \"value\" }}]}"; XContentParser xContentParser = createXContentParser(input); FlatObjectFieldParser configuredParser = new FlatObjectFieldParser("field", "field._keyed", - new FakeFieldType(), 3, Integer.MAX_VALUE); + new FakeFieldType("field"), 3, Integer.MAX_VALUE, null); List fields = configuredParser.parse(xContentParser); assertEquals(4, fields.size()); @@ -231,7 +232,7 @@ public void testIgnoreAbove() throws Exception { String input = "{ \"key\": \"a longer field than usual\" }"; XContentParser xContentParser = createXContentParser(input); FlatObjectFieldParser configuredParser = new FlatObjectFieldParser("field", "field._keyed", - new FakeFieldType(), Integer.MAX_VALUE, 10); + new FakeFieldType("field"), Integer.MAX_VALUE, 10, null); List fields = configuredParser.parse(xContentParser); assertEquals(0, fields.size()); @@ -246,10 +247,9 @@ public void testNullValues() throws Exception { xContentParser = createXContentParser(input); - MappedFieldType fieldType = new FakeFieldType(); - fieldType.setNullValue("placeholder"); + MappedFieldType fieldType = new FakeFieldType("field"); FlatObjectFieldParser configuredParser = new FlatObjectFieldParser("field", "field._keyed", - fieldType, Integer.MAX_VALUE, Integer.MAX_VALUE); + fieldType, Integer.MAX_VALUE, Integer.MAX_VALUE, "placeholder"); fields = configuredParser.parse(xContentParser); assertEquals(2, fields.size()); diff --git a/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/KeyedFlatObjectFieldTypeTests.java b/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/KeyedFlatObjectFieldTypeTests.java index 407b51b08b38e..7622b4d2ad3f1 100644 --- a/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/KeyedFlatObjectFieldTypeTests.java +++ b/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/KeyedFlatObjectFieldTypeTests.java @@ -6,7 +6,6 @@ package org.elasticsearch.xpack.flattened.mapper; -import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.Term; import org.apache.lucene.search.MultiTermQuery; import org.apache.lucene.search.PrefixQuery; @@ -22,7 +21,9 @@ import org.junit.Before; import java.util.ArrayList; +import java.util.Collections; import java.util.List; +import java.util.Map; public class KeyedFlatObjectFieldTypeTests extends FieldTypeTestCase { @@ -36,13 +37,12 @@ public void addModifiers() { } @Override - protected KeyedFlatObjectFieldType createDefaultFieldType() { - return new KeyedFlatObjectFieldType("key"); + protected KeyedFlatObjectFieldType createDefaultFieldType(String name, Map meta) { + return new KeyedFlatObjectFieldType(name, true, true, "key", false, meta); } public void testIndexedValueForSearch() { - KeyedFlatObjectFieldType ft = createDefaultFieldType(); - ft.setName("field"); + KeyedFlatObjectFieldType ft = createDefaultFieldType("field", Collections.emptyMap()); BytesRef keywordValue = ft.indexedValueForSearch("value"); assertEquals(new BytesRef("key\0value"), keywordValue); @@ -55,21 +55,20 @@ public void testIndexedValueForSearch() { } public void testTermQuery() { - KeyedFlatObjectFieldType ft = createDefaultFieldType(); - ft.setName("field"); + KeyedFlatObjectFieldType ft = createDefaultFieldType("field", Collections.emptyMap()); Query expected = new TermQuery(new Term("field", "key\0value")); assertEquals(expected, ft.termQuery("value", null)); - ft.setIndexOptions(IndexOptions.NONE); + KeyedFlatObjectFieldType unsearchable = new KeyedFlatObjectFieldType("field", false, true, "key", + false, Collections.emptyMap()); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> ft.termQuery("field", null)); + () -> unsearchable.termQuery("field", null)); assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage()); } public void testTermsQuery() { - KeyedFlatObjectFieldType ft = createDefaultFieldType(); - ft.setName("field"); + KeyedFlatObjectFieldType ft = createDefaultFieldType("field", Collections.emptyMap()); Query expected = new TermInSetQuery("field", new BytesRef("key\0value1"), @@ -84,16 +83,14 @@ public void testTermsQuery() { } public void testExistsQuery() { - KeyedFlatObjectFieldType ft = createDefaultFieldType(); - ft.setName("field"); + KeyedFlatObjectFieldType ft = createDefaultFieldType("field", Collections.emptyMap()); Query expected = new PrefixQuery(new Term("field", "key\0")); assertEquals(expected, ft.existsQuery(null)); } public void testPrefixQuery() { - KeyedFlatObjectFieldType ft = createDefaultFieldType(); - ft.setName("field"); + KeyedFlatObjectFieldType ft = createDefaultFieldType("field", Collections.emptyMap()); Query expected = new PrefixQuery(new Term("field", "key\0val")); assertEquals(expected, ft.prefixQuery("val", MultiTermQuery.CONSTANT_SCORE_REWRITE, MOCK_QSC)); @@ -105,8 +102,7 @@ public void testPrefixQuery() { } public void testFuzzyQuery() { - KeyedFlatObjectFieldType ft = createDefaultFieldType(); - ft.setName("field"); + KeyedFlatObjectFieldType ft = createDefaultFieldType("field", Collections.emptyMap()); UnsupportedOperationException e = expectThrows(UnsupportedOperationException.class, () -> ft.fuzzyQuery("value", Fuzziness.fromEdits(2), 1, 50, true, randomMockShardContext())); @@ -114,8 +110,7 @@ public void testFuzzyQuery() { } public void testRangeQuery() { - KeyedFlatObjectFieldType ft = createDefaultFieldType(); - ft.setName("field"); + KeyedFlatObjectFieldType ft = createDefaultFieldType("field", Collections.emptyMap()); TermRangeQuery expected = new TermRangeQuery("field", new BytesRef("key\0lower"), @@ -144,8 +139,7 @@ public void testRangeQuery() { } public void testRegexpQuery() { - KeyedFlatObjectFieldType ft = createDefaultFieldType(); - ft.setName("field"); + KeyedFlatObjectFieldType ft = createDefaultFieldType("field", Collections.emptyMap()); UnsupportedOperationException e = expectThrows(UnsupportedOperationException.class, () -> ft.regexpQuery("valu*", 0, 10, null, randomMockShardContext())); @@ -153,8 +147,7 @@ public void testRegexpQuery() { } public void testWildcardQuery() { - KeyedFlatObjectFieldType ft = createDefaultFieldType(); - ft.setName("field"); + KeyedFlatObjectFieldType ft = createDefaultFieldType("field", Collections.emptyMap()); UnsupportedOperationException e = expectThrows(UnsupportedOperationException.class, () -> ft.wildcardQuery("valu*", null, randomMockShardContext())); diff --git a/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/RootFlatObjectFieldTypeTests.java b/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/RootFlatObjectFieldTypeTests.java index cf61612bed98d..b49fda1ea9fc3 100644 --- a/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/RootFlatObjectFieldTypeTests.java +++ b/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/RootFlatObjectFieldTypeTests.java @@ -6,7 +6,6 @@ package org.elasticsearch.xpack.flattened.mapper; -import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.Term; import org.apache.lucene.search.DocValuesFieldExistsQuery; import org.apache.lucene.search.FuzzyQuery; @@ -23,6 +22,9 @@ import org.elasticsearch.xpack.flattened.mapper.FlatObjectFieldMapper.RootFlatObjectFieldType; import org.junit.Before; +import java.util.Collections; +import java.util.Map; + public class RootFlatObjectFieldTypeTests extends FieldTypeTestCase { @Before @@ -35,12 +37,12 @@ public void addModifiers() { } @Override - protected RootFlatObjectFieldType createDefaultFieldType() { - return new RootFlatObjectFieldType(); + protected RootFlatObjectFieldType createDefaultFieldType(String name, Map meta) { + return new RootFlatObjectFieldType(name, true, true, meta, false); } public void testValueForDisplay() { - RootFlatObjectFieldType ft = createDefaultFieldType(); + RootFlatObjectFieldType ft = createDefaultFieldType("field", Collections.emptyMap()); String fieldValue = "{ \"key\": \"value\" }"; BytesRef storedValue = new BytesRef(fieldValue); @@ -48,32 +50,30 @@ public void testValueForDisplay() { } public void testTermQuery() { - RootFlatObjectFieldType ft = createDefaultFieldType(); - ft.setName("field"); + RootFlatObjectFieldType ft = createDefaultFieldType("field", Collections.emptyMap()); Query expected = new TermQuery(new Term("field", "value")); assertEquals(expected, ft.termQuery("value", null)); - ft.setIndexOptions(IndexOptions.NONE); + RootFlatObjectFieldType unsearchable = new RootFlatObjectFieldType("field", false, true, + Collections.emptyMap(), false); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> ft.termQuery("field", null)); + () -> unsearchable.termQuery("field", null)); assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage()); } public void testExistsQuery() { - RootFlatObjectFieldType ft = new RootFlatObjectFieldType(); - ft.setName("field"); + RootFlatObjectFieldType ft = new RootFlatObjectFieldType("field", true, false, Collections.emptyMap(), false); assertEquals( new TermQuery(new Term(FieldNamesFieldMapper.NAME, new BytesRef("field"))), ft.existsQuery(null)); - ft.setHasDocValues(true); - assertEquals(new DocValuesFieldExistsQuery("field"), ft.existsQuery(null)); + RootFlatObjectFieldType withDv = new RootFlatObjectFieldType("field", true, true, Collections.emptyMap(), false); + assertEquals(new DocValuesFieldExistsQuery("field"), withDv.existsQuery(null)); } public void testFuzzyQuery() { - RootFlatObjectFieldType ft = createDefaultFieldType(); - ft.setName("field"); + RootFlatObjectFieldType ft = createDefaultFieldType("field", Collections.emptyMap()); Query expected = new FuzzyQuery(new Term("field", "value"), 2, 1, 50, true); Query actual = ft.fuzzyQuery("value", Fuzziness.fromEdits(2), 1, 50, true, MOCK_QSC); @@ -87,8 +87,7 @@ public void testFuzzyQuery() { } public void testRangeQuery() { - RootFlatObjectFieldType ft = createDefaultFieldType(); - ft.setName("field"); + RootFlatObjectFieldType ft = createDefaultFieldType("field", Collections.emptyMap()); TermRangeQuery expected = new TermRangeQuery("field", new BytesRef("lower"), @@ -107,8 +106,7 @@ public void testRangeQuery() { } public void testRegexpQuery() { - RootFlatObjectFieldType ft = createDefaultFieldType(); - ft.setName("field"); + RootFlatObjectFieldType ft = createDefaultFieldType("field", Collections.emptyMap()); Query expected = new RegexpQuery(new Term("field", "val.*")); Query actual = ft.regexpQuery("val.*", 0, 10, null, MOCK_QSC); @@ -121,8 +119,7 @@ public void testRegexpQuery() { } public void testWildcardQuery() { - RootFlatObjectFieldType ft = createDefaultFieldType(); - ft.setName("field"); + RootFlatObjectFieldType ft = createDefaultFieldType("field", Collections.emptyMap()); Query expected = new WildcardQuery(new Term("field", new BytesRef("valu*"))); assertEquals(expected, ft.wildcardQuery("valu*", null, MOCK_QSC)); diff --git a/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/RollupResponseTranslationTests.java b/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/RollupResponseTranslationTests.java index ffda75ea09183..b6a11f4e4bd7e 100644 --- a/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/RollupResponseTranslationTests.java +++ b/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/RollupResponseTranslationTests.java @@ -12,7 +12,6 @@ import org.apache.lucene.document.SortedSetDocValuesField; import org.apache.lucene.document.TextField; import org.apache.lucene.index.DirectoryReader; -import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.RandomIndexWriter; import org.apache.lucene.index.Term; @@ -417,7 +416,7 @@ public void testUnsupported() throws IOException { GeoBoundsAggregationBuilder geo2 = new GeoBoundsAggregationBuilder("foo").field("bar"); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.LONG); List responses = doQueries(new MatchAllDocsQuery(), iw -> { @@ -440,10 +439,7 @@ public void testUnsupported() throws IOException { public void testUnsupportedMultiBucket() throws IOException { - MappedFieldType fieldType = new KeywordFieldMapper.KeywordFieldType(); - fieldType.setHasDocValues(true); - fieldType.setIndexOptions(IndexOptions.DOCS); - fieldType.setName("foo"); + MappedFieldType fieldType = new KeywordFieldMapper.KeywordFieldType("foo"); QueryBuilder filter = QueryBuilders.boolQuery() .must(QueryBuilders.termQuery("field", "foo")) .should(QueryBuilders.termQuery("field", "bar")); @@ -481,7 +477,7 @@ public void testMismatch() throws IOException { FilterAggregationBuilder filterBuilder = new FilterAggregationBuilder("filter", new TermQueryBuilder("foo", "bar")); filterBuilder.subAggregation(histoBuilder); - MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); + MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.LONG); List responses = doQueries(new MatchAllDocsQuery(), iw -> { @@ -530,21 +526,11 @@ public void testDateHisto() throws IOException { .subAggregation(new SumAggregationBuilder("histo." + RollupField.COUNT_FIELD) .field("timestamp.date_histogram." + RollupField.COUNT_FIELD)); - DateFieldMapper.Builder nrBuilder = new DateFieldMapper.Builder("histo"); - DateFieldMapper.DateFieldType nrFTtimestamp = nrBuilder.fieldType(); - nrFTtimestamp.setHasDocValues(true); - nrFTtimestamp.setName(nonRollupHisto.field()); + DateFieldMapper.DateFieldType nrFTtimestamp = new DateFieldMapper.DateFieldType(nonRollupHisto.field()); + DateFieldMapper.DateFieldType rFTtimestamp = new DateFieldMapper.DateFieldType(rollupHisto.field()); - DateFieldMapper.Builder rBuilder = new DateFieldMapper.Builder("histo"); - DateFieldMapper.DateFieldType rFTtimestamp = rBuilder.fieldType(); - rFTtimestamp.setHasDocValues(true); - rFTtimestamp.setName(rollupHisto.field()); - - NumberFieldMapper.Builder valueBuilder = new NumberFieldMapper.Builder("histo." + RollupField.COUNT_FIELD, - NumberFieldMapper.NumberType.LONG); - MappedFieldType rFTvalue = valueBuilder.fieldType(); - rFTvalue.setHasDocValues(true); - rFTvalue.setName("timestamp.date_histogram." + RollupField.COUNT_FIELD); + MappedFieldType rFTvalue = new NumberFieldMapper.NumberFieldType("timestamp.date_histogram." + RollupField.COUNT_FIELD, + NumberFieldMapper.NumberType.LONG); List responses = doQueries(new MatchAllDocsQuery(), iw -> { @@ -576,21 +562,11 @@ public void testDateHistoWithGap() throws IOException { .subAggregation(new SumAggregationBuilder("histo." + RollupField.COUNT_FIELD) .field("timestamp.date_histogram." + RollupField.COUNT_FIELD)); - DateFieldMapper.Builder nrBuilder = new DateFieldMapper.Builder("histo"); - DateFieldMapper.DateFieldType nrFTtimestamp = nrBuilder.fieldType(); - nrFTtimestamp.setHasDocValues(true); - nrFTtimestamp.setName(nonRollupHisto.field()); + DateFieldMapper.DateFieldType nrFTtimestamp = new DateFieldMapper.DateFieldType(nonRollupHisto.field()); + DateFieldMapper.DateFieldType rFTtimestamp = new DateFieldMapper.DateFieldType(rollupHisto.field()); - DateFieldMapper.Builder rBuilder = new DateFieldMapper.Builder("histo"); - DateFieldMapper.DateFieldType rFTtimestamp = rBuilder.fieldType(); - rFTtimestamp.setHasDocValues(true); - rFTtimestamp.setName(rollupHisto.field()); - - NumberFieldMapper.Builder valueBuilder = new NumberFieldMapper.Builder("histo." + RollupField.COUNT_FIELD, - NumberFieldMapper.NumberType.LONG); - MappedFieldType rFTvalue = valueBuilder.fieldType(); - rFTvalue.setHasDocValues(true); - rFTvalue.setName("timestamp.date_histogram." + RollupField.COUNT_FIELD); + MappedFieldType rFTvalue = new NumberFieldMapper.NumberFieldType("timestamp.date_histogram." + RollupField.COUNT_FIELD, + NumberFieldMapper.NumberType.LONG); List responses = doQueries(new MatchAllDocsQuery(), iw -> { @@ -634,31 +610,14 @@ public void testNonMatchingPartition() throws IOException { .subAggregation(new SumAggregationBuilder("histo." + RollupField.COUNT_FIELD) .field("timestamp.date_histogram." + RollupField.COUNT_FIELD)); - DateFieldMapper.Builder nrBuilder = new DateFieldMapper.Builder("histo"); - DateFieldMapper.DateFieldType nrFTtimestamp = nrBuilder.fieldType(); - nrFTtimestamp.setHasDocValues(true); - nrFTtimestamp.setName(nonRollupHisto.field()); - - DateFieldMapper.Builder rBuilder = new DateFieldMapper.Builder("histo"); - DateFieldMapper.DateFieldType rFTtimestamp = rBuilder.fieldType(); - rFTtimestamp.setHasDocValues(true); - rFTtimestamp.setName(rollupHisto.field()); + DateFieldMapper.DateFieldType nrFTtimestamp = new DateFieldMapper.DateFieldType(nonRollupHisto.field()); + DateFieldMapper.DateFieldType rFTtimestamp = new DateFieldMapper.DateFieldType(rollupHisto.field()); - NumberFieldMapper.Builder valueBuilder = new NumberFieldMapper.Builder("histo." + RollupField.COUNT_FIELD, - NumberFieldMapper.NumberType.LONG); - MappedFieldType rFTvalue = valueBuilder.fieldType(); - rFTvalue.setHasDocValues(true); - rFTvalue.setName("timestamp.date_histogram." + RollupField.COUNT_FIELD); - - KeywordFieldMapper.Builder nrKeywordBuilder = new KeywordFieldMapper.Builder("partition"); - KeywordFieldMapper.KeywordFieldType nrKeywordFT = nrKeywordBuilder.fieldType(); - nrKeywordFT.setHasDocValues(true); - nrKeywordFT.setName("partition"); + MappedFieldType rFTvalue = new NumberFieldMapper.NumberFieldType("timestamp.date_histogram." + RollupField.COUNT_FIELD, + NumberFieldMapper.NumberType.LONG); - KeywordFieldMapper.Builder rKeywordBuilder = new KeywordFieldMapper.Builder("partition"); - KeywordFieldMapper.KeywordFieldType rKeywordFT = rKeywordBuilder.fieldType(); - rKeywordFT.setHasDocValues(true); - rKeywordFT.setName("partition"); + KeywordFieldMapper.KeywordFieldType nrKeywordFT = new KeywordFieldMapper.KeywordFieldType("partition"); + KeywordFieldMapper.KeywordFieldType rKeywordFT = new KeywordFieldMapper.KeywordFieldType("partition"); // Note: term query for "a" List results = new ArrayList<>(2); @@ -745,21 +704,11 @@ public void testDateHistoOverlappingAggTrees() throws IOException { .subAggregation(new SumAggregationBuilder("histo." + RollupField.COUNT_FIELD) .field("timestamp.date_histogram." + RollupField.COUNT_FIELD)); - DateFieldMapper.Builder nrBuilder = new DateFieldMapper.Builder("histo"); - DateFieldMapper.DateFieldType nrFTtimestamp = nrBuilder.fieldType(); - nrFTtimestamp.setHasDocValues(true); - nrFTtimestamp.setName(nonRollupHisto.field()); + DateFieldMapper.DateFieldType nrFTtimestamp = new DateFieldMapper.DateFieldType(nonRollupHisto.field()); + DateFieldMapper.DateFieldType rFTtimestamp = new DateFieldMapper.DateFieldType(rollupHisto.field()); - DateFieldMapper.Builder rBuilder = new DateFieldMapper.Builder("histo"); - DateFieldMapper.DateFieldType rFTtimestamp = rBuilder.fieldType(); - rFTtimestamp.setHasDocValues(true); - rFTtimestamp.setName(rollupHisto.field()); - - NumberFieldMapper.Builder valueBuilder = new NumberFieldMapper.Builder("histo." + RollupField.COUNT_FIELD, - NumberFieldMapper.NumberType.LONG); - MappedFieldType rFTvalue = valueBuilder.fieldType(); - rFTvalue.setHasDocValues(true); - rFTvalue.setName("timestamp.date_histogram." + RollupField.COUNT_FIELD); + MappedFieldType rFTvalue = new NumberFieldMapper.NumberFieldType("timestamp.date_histogram." + RollupField.COUNT_FIELD, + NumberFieldMapper.NumberType.LONG); List responses = doQueries(new MatchAllDocsQuery(), iw -> { @@ -806,21 +755,11 @@ public void testDateHistoOverlappingMergeRealIntoZero() throws IOException { .subAggregation(new SumAggregationBuilder("histo." + RollupField.COUNT_FIELD) .field("timestamp.date_histogram." + RollupField.COUNT_FIELD)); - DateFieldMapper.Builder nrBuilder = new DateFieldMapper.Builder("histo"); - DateFieldMapper.DateFieldType nrFTtimestamp = nrBuilder.fieldType(); - nrFTtimestamp.setHasDocValues(true); - nrFTtimestamp.setName(nonRollupHisto.field()); + DateFieldMapper.DateFieldType nrFTtimestamp = new DateFieldMapper.DateFieldType(nonRollupHisto.field()); + DateFieldMapper.DateFieldType rFTtimestamp = new DateFieldMapper.DateFieldType(rollupHisto.field()); - DateFieldMapper.Builder rBuilder = new DateFieldMapper.Builder("histo"); - DateFieldMapper.DateFieldType rFTtimestamp = rBuilder.fieldType(); - rFTtimestamp.setHasDocValues(true); - rFTtimestamp.setName(rollupHisto.field()); - - NumberFieldMapper.Builder valueBuilder = new NumberFieldMapper.Builder("histo." + RollupField.COUNT_FIELD, + MappedFieldType rFTvalue = new NumberFieldMapper.NumberFieldType("timestamp.date_histogram." + RollupField.COUNT_FIELD, NumberFieldMapper.NumberType.LONG); - MappedFieldType rFTvalue = valueBuilder.fieldType(); - rFTvalue.setHasDocValues(true); - rFTvalue.setName("timestamp.date_histogram." + RollupField.COUNT_FIELD); List responses = doQueries(new MatchAllDocsQuery(), iw -> { @@ -874,21 +813,11 @@ public void testDateHistoOverlappingMergeZeroIntoReal() throws IOException { .subAggregation(new SumAggregationBuilder("histo." + RollupField.COUNT_FIELD) .field("timestamp.date_histogram." + RollupField.COUNT_FIELD)); - DateFieldMapper.Builder nrBuilder = new DateFieldMapper.Builder("histo"); - DateFieldMapper.DateFieldType nrFTtimestamp = nrBuilder.fieldType(); - nrFTtimestamp.setHasDocValues(true); - nrFTtimestamp.setName(nonRollupHisto.field()); - - DateFieldMapper.Builder rBuilder = new DateFieldMapper.Builder("histo"); - DateFieldMapper.DateFieldType rFTtimestamp = rBuilder.fieldType(); - rFTtimestamp.setHasDocValues(true); - rFTtimestamp.setName(rollupHisto.field()); + DateFieldMapper.DateFieldType nrFTtimestamp = new DateFieldMapper.DateFieldType(nonRollupHisto.field()); + DateFieldMapper.DateFieldType rFTtimestamp = new DateFieldMapper.DateFieldType(rollupHisto.field()); - NumberFieldMapper.Builder valueBuilder = new NumberFieldMapper.Builder("histo." + RollupField.COUNT_FIELD, - NumberFieldMapper.NumberType.LONG); - MappedFieldType rFTvalue = valueBuilder.fieldType(); - rFTvalue.setHasDocValues(true); - rFTvalue.setName("timestamp.date_histogram." + RollupField.COUNT_FIELD); + MappedFieldType rFTvalue = new NumberFieldMapper.NumberFieldType("timestamp.date_histogram." + RollupField.COUNT_FIELD, + NumberFieldMapper.NumberType.LONG); InternalAggregation currentTree = doQuery(new MatchAllDocsQuery(), iw -> { @@ -929,17 +858,9 @@ public void testAvg() throws IOException { SumAggregationBuilder rollup = new SumAggregationBuilder("avg") .field("foo.avg." + RollupField.VALUE); - NumberFieldMapper.Builder nrValueMapper = new NumberFieldMapper.Builder("foo", - NumberFieldMapper.NumberType.LONG); - MappedFieldType nrFTvalue = nrValueMapper.fieldType(); - nrFTvalue.setHasDocValues(true); - nrFTvalue.setName("foo"); - - NumberFieldMapper.Builder rValueMapper = new NumberFieldMapper.Builder("avg.foo", - NumberFieldMapper.NumberType.LONG); - MappedFieldType rFTvalue = rValueMapper.fieldType(); - rFTvalue.setHasDocValues(true); - rFTvalue.setName("foo.avg." + RollupField.VALUE); + MappedFieldType nrFTvalue = new NumberFieldMapper.NumberFieldType("foo", NumberFieldMapper.NumberType.LONG); + MappedFieldType rFTvalue = new NumberFieldMapper.NumberFieldType("foo.avg." + RollupField.VALUE, + NumberFieldMapper.NumberType.LONG); List responses = doQueries(new MatchAllDocsQuery(), iw -> { @@ -985,17 +906,8 @@ public void testMetric() throws IOException { rollupValue = 0; } - NumberFieldMapper.Builder nrValueMapper = new NumberFieldMapper.Builder("foo", - NumberFieldMapper.NumberType.LONG); - MappedFieldType nrFTvalue = nrValueMapper.fieldType(); - nrFTvalue.setHasDocValues(true); - nrFTvalue.setName("foo"); - - NumberFieldMapper.Builder rValueMapper = new NumberFieldMapper.Builder("foo", - NumberFieldMapper.NumberType.LONG); - MappedFieldType rFTvalue = rValueMapper.fieldType(); - rFTvalue.setHasDocValues(true); - rFTvalue.setName(fieldName); + MappedFieldType nrFTvalue = new NumberFieldMapper.NumberFieldType("foo", NumberFieldMapper.NumberType.LONG); + MappedFieldType rFTvalue = new NumberFieldMapper.NumberFieldType(fieldName, NumberFieldMapper.NumberType.LONG); List responses = doQueries(new MatchAllDocsQuery(), iw -> { @@ -1021,17 +933,9 @@ public void testUnsupportedMetric() throws IOException { AggregationBuilder rollup = new CardinalityAggregationBuilder("test_metric").userValueTypeHint(ValueType.LONG) .field(fieldName); - NumberFieldMapper.Builder nrValueMapper = new NumberFieldMapper.Builder("foo", - NumberFieldMapper.NumberType.LONG); - MappedFieldType nrFTvalue = nrValueMapper.fieldType(); - nrFTvalue.setHasDocValues(true); - nrFTvalue.setName("foo"); + MappedFieldType nrFTvalue = new NumberFieldMapper.NumberFieldType("foo", NumberFieldMapper.NumberType.LONG); - NumberFieldMapper.Builder rValueMapper = new NumberFieldMapper.Builder("foo", - NumberFieldMapper.NumberType.LONG); - MappedFieldType rFTvalue = rValueMapper.fieldType(); - rFTvalue.setHasDocValues(true); - rFTvalue.setName(fieldName); + MappedFieldType rFTvalue = new NumberFieldMapper.NumberFieldType(fieldName, NumberFieldMapper.NumberType.LONG); List responses = doQueries(new MatchAllDocsQuery(), iw -> { @@ -1059,21 +963,12 @@ public void testStringTerms() throws IOException { .subAggregation(new SumAggregationBuilder("terms." + RollupField.COUNT_FIELD) .field("stringfield.terms." + RollupField.COUNT_FIELD)); - KeywordFieldMapper.Builder nrBuilder = new KeywordFieldMapper.Builder("terms"); - KeywordFieldMapper.KeywordFieldType nrFTterm = nrBuilder.fieldType(); - nrFTterm.setHasDocValues(true); - nrFTterm.setName(nonRollupTerms.field()); + KeywordFieldMapper.KeywordFieldType nrFTterm = new KeywordFieldMapper.KeywordFieldType(nonRollupTerms.field()); - KeywordFieldMapper.Builder rBuilder = new KeywordFieldMapper.Builder("terms"); - KeywordFieldMapper.KeywordFieldType rFTterm = rBuilder.fieldType(); - rFTterm.setHasDocValues(true); - rFTterm.setName(rollupTerms.field()); + KeywordFieldMapper.KeywordFieldType rFTterm = new KeywordFieldMapper.KeywordFieldType(rollupTerms.field()); - NumberFieldMapper.Builder valueBuilder = new NumberFieldMapper.Builder("terms." + RollupField.COUNT_FIELD, - NumberFieldMapper.NumberType.LONG); - MappedFieldType rFTvalue = valueBuilder.fieldType(); - rFTvalue.setHasDocValues(true); - rFTvalue.setName("stringfield.terms." + RollupField.COUNT_FIELD); + MappedFieldType rFTvalue = new NumberFieldMapper.NumberFieldType("stringfield.terms." + RollupField.COUNT_FIELD, + NumberFieldMapper.NumberType.LONG); List responses = doQueries(new MatchAllDocsQuery(), iw -> { @@ -1100,21 +995,11 @@ public void testStringTermsNullValue() throws IOException { .subAggregation(new SumAggregationBuilder("terms." + RollupField.COUNT_FIELD) .field("stringfield.terms." + RollupField.COUNT_FIELD)); - KeywordFieldMapper.Builder nrBuilder = new KeywordFieldMapper.Builder("terms"); - KeywordFieldMapper.KeywordFieldType nrFTterm = nrBuilder.fieldType(); - nrFTterm.setHasDocValues(true); - nrFTterm.setName(nonRollupTerms.field()); + KeywordFieldMapper.KeywordFieldType nrFTterm = new KeywordFieldMapper.KeywordFieldType(nonRollupTerms.field()); + KeywordFieldMapper.KeywordFieldType rFTterm = new KeywordFieldMapper.KeywordFieldType(rollupTerms.field()); - KeywordFieldMapper.Builder rBuilder = new KeywordFieldMapper.Builder("terms"); - KeywordFieldMapper.KeywordFieldType rFTterm = rBuilder.fieldType(); - rFTterm.setHasDocValues(true); - rFTterm.setName(rollupTerms.field()); - - NumberFieldMapper.Builder valueBuilder = new NumberFieldMapper.Builder("terms." + RollupField.COUNT_FIELD, + MappedFieldType rFTvalue = new NumberFieldMapper.NumberFieldType("stringfield.terms." + RollupField.COUNT_FIELD, NumberFieldMapper.NumberType.LONG); - MappedFieldType rFTvalue = valueBuilder.fieldType(); - rFTvalue.setHasDocValues(true); - rFTvalue.setName("stringfield.terms." + RollupField.COUNT_FIELD); List responses = doQueries(new MatchAllDocsQuery(), iw -> { @@ -1148,21 +1033,11 @@ public void testLongTerms() throws IOException { .subAggregation(new SumAggregationBuilder("terms." + RollupField.COUNT_FIELD) .field("longfield.terms." + RollupField.COUNT_FIELD)); - NumberFieldMapper.Builder nrBuilder = new NumberFieldMapper.Builder("terms", NumberFieldMapper.NumberType.LONG); - MappedFieldType nrFTterm = nrBuilder.fieldType(); - nrFTterm.setHasDocValues(true); - nrFTterm.setName(nonRollupTerms.field()); - - NumberFieldMapper.Builder rBuilder = new NumberFieldMapper.Builder("terms", NumberFieldMapper.NumberType.LONG); - MappedFieldType rFTterm = rBuilder.fieldType(); - rFTterm.setHasDocValues(true); - rFTterm.setName(rollupTerms.field()); + MappedFieldType nrFTterm = new NumberFieldMapper.NumberFieldType(nonRollupTerms.field(), NumberFieldMapper.NumberType.LONG); + MappedFieldType rFTterm = new NumberFieldMapper.NumberFieldType(rollupTerms.field(), NumberFieldMapper.NumberType.LONG); - NumberFieldMapper.Builder valueBuilder = new NumberFieldMapper.Builder("terms." + RollupField.COUNT_FIELD, - NumberFieldMapper.NumberType.LONG); - MappedFieldType rFTvalue = valueBuilder.fieldType(); - rFTvalue.setHasDocValues(true); - rFTvalue.setName("longfield.terms." + RollupField.COUNT_FIELD); + MappedFieldType rFTvalue = new NumberFieldMapper.NumberFieldType("longfield.terms." + RollupField.COUNT_FIELD, + NumberFieldMapper.NumberType.LONG); List responses = doQueries(new MatchAllDocsQuery(), iw -> { @@ -1190,21 +1065,11 @@ public void testHisto() throws IOException { .subAggregation(new SumAggregationBuilder("histo." + RollupField.COUNT_FIELD) .field("bar.histogram." + RollupField.COUNT_FIELD)); - NumberFieldMapper.Builder nrBuilder = new NumberFieldMapper.Builder("histo", NumberFieldMapper.NumberType.LONG); - MappedFieldType nrFTbar = nrBuilder.fieldType(); - nrFTbar.setHasDocValues(true); - nrFTbar.setName(nonRollupHisto.field()); - - NumberFieldMapper.Builder rBuilder = new NumberFieldMapper.Builder("histo", NumberFieldMapper.NumberType.LONG); - MappedFieldType rFTbar = rBuilder.fieldType(); - rFTbar.setHasDocValues(true); - rFTbar.setName(rollupHisto.field()); + MappedFieldType nrFTbar = new NumberFieldMapper.NumberFieldType(nonRollupHisto.field(), NumberFieldMapper.NumberType.LONG); + MappedFieldType rFTbar = new NumberFieldMapper.NumberFieldType(rollupHisto.field(), NumberFieldMapper.NumberType.LONG); - NumberFieldMapper.Builder valueBuilder = new NumberFieldMapper.Builder("histo." + RollupField.COUNT_FIELD, - NumberFieldMapper.NumberType.LONG); - MappedFieldType rFTvalue = valueBuilder.fieldType(); - rFTvalue.setHasDocValues(true); - rFTvalue.setName("bar.histogram." + RollupField.COUNT_FIELD); + MappedFieldType rFTvalue = new NumberFieldMapper.NumberFieldType("bar.histogram." + RollupField.COUNT_FIELD, + NumberFieldMapper.NumberType.LONG); List responses = doQueries(new MatchAllDocsQuery(), iw -> { @@ -1234,21 +1099,12 @@ public void testOverlappingBuckets() throws IOException { .subAggregation(new SumAggregationBuilder("histo." + RollupField.COUNT_FIELD) .field("timestamp.date_histogram." + RollupField.COUNT_FIELD)); - DateFieldMapper.Builder nrBuilder = new DateFieldMapper.Builder("histo"); - DateFieldMapper.DateFieldType nrFTtimestamp = nrBuilder.fieldType(); - nrFTtimestamp.setHasDocValues(true); - nrFTtimestamp.setName(nonRollupHisto.field()); + DateFieldMapper.DateFieldType nrFTtimestamp = new DateFieldMapper.DateFieldType(nonRollupHisto.field()); - DateFieldMapper.Builder rBuilder = new DateFieldMapper.Builder("histo"); - DateFieldMapper.DateFieldType rFTtimestamp = rBuilder.fieldType(); - rFTtimestamp.setHasDocValues(true); - rFTtimestamp.setName(rollupHisto.field()); + DateFieldMapper.DateFieldType rFTtimestamp = new DateFieldMapper.DateFieldType(rollupHisto.field()); - NumberFieldMapper.Builder valueBuilder = new NumberFieldMapper.Builder("histo." + RollupField.COUNT_FIELD, - NumberFieldMapper.NumberType.LONG); - MappedFieldType rFTvalue = valueBuilder.fieldType(); - rFTvalue.setHasDocValues(true); - rFTvalue.setName("timestamp.date_histogram." + RollupField.COUNT_FIELD); + MappedFieldType rFTvalue = new NumberFieldMapper.NumberFieldType("timestamp.date_histogram." + RollupField.COUNT_FIELD, + NumberFieldMapper.NumberType.LONG); List responses = doQueries(new MatchAllDocsQuery(), iw -> { diff --git a/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/job/IndexerUtilsTests.java b/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/job/IndexerUtilsTests.java index 4e5f636455caa..14d0104582f69 100644 --- a/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/job/IndexerUtilsTests.java +++ b/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/job/IndexerUtilsTests.java @@ -90,15 +90,8 @@ public void testMissingFields() throws IOException { IndexReader indexReader = DirectoryReader.open(directory); IndexSearcher indexSearcher = newIndexSearcher(indexReader); - DateFieldMapper.Builder builder = new DateFieldMapper.Builder(timestampField); - DateFieldMapper.DateFieldType timestampFieldType = builder.fieldType(); - timestampFieldType.setHasDocValues(true); - timestampFieldType.setName(timestampField); - - MappedFieldType valueFieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - valueFieldType.setName(valueField); - valueFieldType.setHasDocValues(true); - valueFieldType.setName(valueField); + DateFieldMapper.DateFieldType timestampFieldType = new DateFieldMapper.DateFieldType(timestampField); + MappedFieldType valueFieldType = new NumberFieldMapper.NumberFieldType(valueField, NumberFieldMapper.NumberType.LONG); // Setup the composite agg DateHistogramGroupConfig dateHistoGroupConfig @@ -154,15 +147,8 @@ public void testCorrectFields() throws IOException { IndexReader indexReader = DirectoryReader.open(directory); IndexSearcher indexSearcher = newIndexSearcher(indexReader); - DateFieldMapper.Builder builder = new DateFieldMapper.Builder(timestampField); - DateFieldMapper.DateFieldType timestampFieldType = builder.fieldType(); - timestampFieldType.setHasDocValues(true); - timestampFieldType.setName(timestampField); - - MappedFieldType valueFieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - valueFieldType.setName(valueField); - valueFieldType.setHasDocValues(true); - valueFieldType.setName(valueField); + DateFieldMapper.DateFieldType timestampFieldType = new DateFieldMapper.DateFieldType(timestampField); + MappedFieldType valueFieldType = new NumberFieldMapper.NumberFieldType(valueField, NumberFieldMapper.NumberType.LONG); // Setup the composite agg //TODO swap this over to DateHistoConfig.Builder once DateInterval is in @@ -219,10 +205,7 @@ public void testNumericTerms() throws IOException { IndexReader indexReader = DirectoryReader.open(directory); IndexSearcher indexSearcher = newIndexSearcher(indexReader); - MappedFieldType valueFieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - valueFieldType.setName(valueField); - valueFieldType.setHasDocValues(true); - valueFieldType.setName(valueField); + MappedFieldType valueFieldType = new NumberFieldMapper.NumberFieldType(valueField, NumberFieldMapper.NumberType.LONG); // Setup the composite agg TermsValuesSourceBuilder terms @@ -278,15 +261,8 @@ public void testEmptyCounts() throws IOException { IndexReader indexReader = DirectoryReader.open(directory); IndexSearcher indexSearcher = newIndexSearcher(indexReader); - DateFieldMapper.Builder builder = new DateFieldMapper.Builder(timestampField); - DateFieldMapper.DateFieldType timestampFieldType = builder.fieldType(); - timestampFieldType.setHasDocValues(true); - timestampFieldType.setName(timestampField); - - MappedFieldType valueFieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - valueFieldType.setName(valueField); - valueFieldType.setHasDocValues(true); - valueFieldType.setName(valueField); + DateFieldMapper.DateFieldType timestampFieldType = new DateFieldMapper.DateFieldType(timestampField); + MappedFieldType valueFieldType = new NumberFieldMapper.NumberFieldType(valueField, NumberFieldMapper.NumberType.LONG); // Setup the composite agg DateHistogramValuesSourceBuilder dateHisto @@ -472,15 +448,8 @@ public void testMissingBuckets() throws IOException { IndexReader indexReader = DirectoryReader.open(directory); IndexSearcher indexSearcher = newIndexSearcher(indexReader); - MappedFieldType valueFieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - valueFieldType.setName(valueField); - valueFieldType.setHasDocValues(true); - valueFieldType.setName(valueField); - - MappedFieldType metricFieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - metricFieldType.setName(metricField); - metricFieldType.setHasDocValues(true); - metricFieldType.setName(metricField); + MappedFieldType valueFieldType = new NumberFieldMapper.NumberFieldType(valueField, NumberFieldMapper.NumberType.LONG); + MappedFieldType metricFieldType = new NumberFieldMapper.NumberFieldType(metricField, NumberFieldMapper.NumberType.LONG); // Setup the composite agg TermsGroupConfig termsGroupConfig = new TermsGroupConfig(valueField); @@ -546,15 +515,8 @@ public void testTimezone() throws IOException { IndexReader indexReader = DirectoryReader.open(directory); IndexSearcher indexSearcher = newIndexSearcher(indexReader); - DateFieldMapper.Builder builder = new DateFieldMapper.Builder(timestampField); - DateFieldMapper.DateFieldType timestampFieldType = builder.fieldType(); - timestampFieldType.setHasDocValues(true); - timestampFieldType.setName(timestampField); - - MappedFieldType valueFieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG); - valueFieldType.setName(valueField); - valueFieldType.setHasDocValues(true); - valueFieldType.setName(valueField); + DateFieldMapper.DateFieldType timestampFieldType = new DateFieldMapper.DateFieldType(timestampField); + MappedFieldType valueFieldType = new NumberFieldMapper.NumberFieldType(valueField, NumberFieldMapper.NumberType.LONG); // Setup the composite agg DateHistogramValuesSourceBuilder dateHisto diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/GeoShapeWithDocValuesFieldMapper.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/GeoShapeWithDocValuesFieldMapper.java index ac05f5acfbd39..69c370b7b96aa 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/GeoShapeWithDocValuesFieldMapper.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/GeoShapeWithDocValuesFieldMapper.java @@ -6,6 +6,7 @@ package org.elasticsearch.xpack.spatial.index.mapper; +import org.apache.lucene.document.FieldType; import org.apache.lucene.document.LatLonShape; import org.apache.lucene.document.ShapeField; import org.apache.lucene.index.IndexableField; @@ -60,54 +61,40 @@ */ public class GeoShapeWithDocValuesFieldMapper extends GeoShapeFieldMapper { public static final String CONTENT_TYPE = "geo_shape"; - private Explicit docValues; - @SuppressWarnings("rawtypes") - public static class Builder extends AbstractShapeGeometryFieldMapper.Builder { + public static class Builder extends AbstractShapeGeometryFieldMapper.Builder { + + private boolean docValuesSet = false; + public Builder(String name) { - super (name, new GeoShapeWithDocValuesFieldType(), new GeoShapeWithDocValuesFieldType()); + super (name, new FieldType()); + this.hasDocValues = true; } @Override - public GeoShapeWithDocValuesFieldMapper build(BuilderContext context) { - setupFieldType(context); - return new GeoShapeWithDocValuesFieldMapper(name, fieldType, defaultFieldType, ignoreMalformed(context), coerce(context), - ignoreZValue(), orientation(), docValues(), context.indexSettings(), - multiFieldsBuilder.build(this, context), copyTo); + public Builder docValues(boolean docValues) { + docValuesSet = true; + return super.docValues(docValues); } @Override - public boolean defaultDocValues(Version indexCreated) { - return Version.V_7_8_0.onOrBefore(indexCreated); - } - - protected Explicit docValues() { - if (docValuesSet && fieldType.hasDocValues()) { - return new Explicit<>(true, true); - } else if (docValuesSet) { - return new Explicit<>(false, true); + public GeoShapeWithDocValuesFieldMapper build(BuilderContext context) { + if (docValuesSet == false) { + hasDocValues = Version.V_7_8_0.onOrBefore(context.indexCreatedVersion()); } - return new Explicit<>(fieldType.hasDocValues(), false); - } - - @Override - protected void setGeometryParser(GeoShapeWithDocValuesFieldType ft) { + GeoShapeWithDocValuesFieldType ft = new GeoShapeWithDocValuesFieldType(buildFullName(context), indexed, hasDocValues, meta); // @todo check coerce GeometryParser geometryParser = new GeometryParser(ft.orientation().getAsBoolean(), coerce().value(), ignoreZValue().value()); - ft.setGeometryParser( (parser, mapper) -> geometryParser.parse(parser)); - } - - @Override - protected void setGeometryIndexer(GeoShapeWithDocValuesFieldType fieldType) { - fieldType.setGeometryIndexer(new GeoShapeIndexer(fieldType.orientation().getAsBoolean(), fieldType.name())); + ft.setGeometryParser((parser, mapper) -> geometryParser.parse(parser)); + ft.setGeometryIndexer(new GeoShapeIndexer(orientation().value().getAsBoolean(), ft.name())); + ft.setGeometryQueryBuilder(new VectorGeoShapeQueryProcessor()); + ft.setOrientation(orientation().value()); + return new GeoShapeWithDocValuesFieldMapper(name, fieldType, ft, ignoreMalformed(context), coerce(context), + ignoreZValue(), orientation(), context.indexSettings(), Version.V_7_8_0.onOrBefore(context.indexCreatedVersion()), + multiFieldsBuilder.build(this, context), copyTo); } - @Override - protected void setGeometryQueryBuilder(GeoShapeWithDocValuesFieldType fieldType) { - fieldType.setGeometryQueryBuilder(new VectorGeoShapeQueryProcessor()); - } } @Override @@ -135,8 +122,8 @@ protected void addDocValuesFields(String name, Geometry shape, List fields, Pars } public static final class GeoShapeWithDocValuesFieldType extends GeoShapeFieldMapper.GeoShapeFieldType { - public GeoShapeWithDocValuesFieldType() { - super(); + public GeoShapeWithDocValuesFieldType(String name, boolean indexed, boolean hasDocValues, Map meta) { + super(name, indexed, hasDocValues, meta); } protected GeoShapeWithDocValuesFieldType(GeoShapeWithDocValuesFieldType ref) { @@ -189,18 +176,21 @@ public AbstractShapeGeometryFieldMapper.Builder parse(String name, Map ignoreMalformed, Explicit coerce, Explicit ignoreZValue, Explicit orientation, - Explicit docValues, Settings indexSettings, + Settings indexSettings, boolean defaultDocValues, MultiFields multiFields, CopyTo copyTo) { - super(simpleName, fieldType, defaultFieldType, ignoreMalformed, coerce, ignoreZValue, orientation, indexSettings, + super(simpleName, fieldType, mappedFieldType, ignoreMalformed, coerce, ignoreZValue, orientation, indexSettings, multiFields, copyTo); - this.docValues = docValues; + this.defaultDocValues = defaultDocValues; } - public Explicit docValues() { - return docValues; + @Override + protected boolean docValuesByDefault() { + return defaultDocValues; } @Override diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/PointFieldMapper.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/PointFieldMapper.java index e16f1d348d70c..6440147615ba4 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/PointFieldMapper.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/PointFieldMapper.java @@ -5,6 +5,7 @@ */ package org.elasticsearch.xpack.spatial.index.mapper; +import org.apache.lucene.document.FieldType; import org.apache.lucene.document.StoredField; import org.apache.lucene.document.XYDocValuesField; import org.apache.lucene.document.XYPointField; @@ -34,36 +35,23 @@ public class PointFieldMapper extends AbstractPointGeometryFieldMapper { public Builder(String name) { - super(name, new PointFieldType(), new PointFieldType()); + super(name, new FieldType()); builder = this; } @Override - public PointFieldMapper build(BuilderContext context, String simpleName, MappedFieldType fieldType, - MappedFieldType defaultFieldType, Settings indexSettings, + public PointFieldMapper build(BuilderContext context, String simpleName, FieldType fieldType, + Settings indexSettings, MultiFields multiFields, Explicit ignoreMalformed, - Explicit ignoreZValue, CopyTo copyTo) { - setupFieldType(context); - return new PointFieldMapper(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, - ignoreMalformed, ignoreZValue(context), copyTo); + Explicit ignoreZValue, ParsedPoint nullValue, CopyTo copyTo) { + PointFieldType ft = new PointFieldType(buildFullName(context), indexed, hasDocValues, meta); + ft.setGeometryParser(new PointParser<>()); + ft.setGeometryIndexer(new PointIndexer(ft)); + ft.setGeometryQueryBuilder(new ShapeQueryPointProcessor()); + return new PointFieldMapper(simpleName, fieldType, ft, indexSettings, multiFields, + ignoreMalformed, ignoreZValue(context), nullValue, copyTo); } - @Override - protected void setGeometryParser() { - PointParser pointParser = new PointParser<>(); - fieldType().setGeometryParser((parser, mapper) -> pointParser.parse(parser, mapper)); - } - - @Override - @SuppressWarnings("unchecked") - protected void setGeometryIndexer(PointFieldType fieldType) { - fieldType.setGeometryIndexer(new PointIndexer(fieldType)); - } - - @Override - protected void setGeometryQueryBuilder(PointFieldType fieldType) { - fieldType.setGeometryQueryBuilder(new ShapeQueryPointProcessor()); - } } @Override @@ -71,14 +59,14 @@ protected ParsedPoint newParsedPoint() { return new ParsedCartesianPoint(); } - public static class TypeParser extends AbstractPointGeometryFieldMapper.TypeParser { + public static class TypeParser extends AbstractPointGeometryFieldMapper.TypeParser { @Override protected Builder newBuilder(String name, Map params) { return new PointFieldMapper.Builder(name); } @Override - protected CartesianPoint parseNullValue(Object nullValue, boolean ignoreZValue, boolean ignoreMalformed) { + protected ParsedPoint parseNullValue(Object nullValue, boolean ignoreZValue, boolean ignoreMalformed) { ParsedCartesianPoint point = new ParsedCartesianPoint(); CartesianPoint.parsePoint(nullValue, point, ignoreZValue); if (ignoreMalformed == false) { @@ -102,10 +90,10 @@ protected void parsePointIgnoringMalformed(XContentParser parser, ParsedPoint po CartesianPoint.parsePoint(parser, (CartesianPoint)point, ignoreZValue().value()); } - public PointFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, + public PointFieldMapper(String simpleName, FieldType fieldType, MappedFieldType mappedFieldType, Settings indexSettings, MultiFields multiFields, Explicit ignoreMalformed, - Explicit ignoreZValue, CopyTo copyTo) { - super(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, ignoreMalformed, ignoreZValue, copyTo); + Explicit ignoreZValue, ParsedPoint nullValue, CopyTo copyTo) { + super(simpleName, fieldType, mappedFieldType, indexSettings, multiFields, ignoreMalformed, ignoreZValue, nullValue, copyTo); } @Override @@ -137,12 +125,12 @@ protected String contentType() { @Override public PointFieldType fieldType() { - return (PointFieldType)fieldType; + return (PointFieldType)mappedFieldType; } public static class PointFieldType extends AbstractPointGeometryFieldType, List> { - public PointFieldType() { - super(); + public PointFieldType(String name, boolean indexed, boolean hasDocValues, Map meta) { + super(name, indexed, hasDocValues, meta); } PointFieldType(PointFieldType ref) { diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/ShapeFieldMapper.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/ShapeFieldMapper.java index 4578b8bc64032..656acf4289818 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/ShapeFieldMapper.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/ShapeFieldMapper.java @@ -5,6 +5,7 @@ */ package org.elasticsearch.xpack.spatial.index.mapper; +import org.apache.lucene.document.FieldType; import org.apache.lucene.document.XYShape; import org.elasticsearch.common.Explicit; import org.elasticsearch.common.geo.GeometryParser; @@ -39,42 +40,27 @@ public class ShapeFieldMapper extends AbstractShapeGeometryFieldMapper { public static final String CONTENT_TYPE = "shape"; - public static class Defaults extends AbstractShapeGeometryFieldMapper.Defaults { - public static final ShapeFieldType FIELD_TYPE = new ShapeFieldType(); - } - - @SuppressWarnings({"unchecked", "rawtypes"}) - public static class Builder extends AbstractShapeGeometryFieldMapper.Builder { + @SuppressWarnings({"unchecked"}) + public static class Builder extends AbstractShapeGeometryFieldMapper.Builder { public Builder(String name) { - super(name, Defaults.FIELD_TYPE, Defaults.FIELD_TYPE); + super(name, new FieldType()); + fieldType.setDimensions(7, 4, Integer.BYTES); builder = this; } @Override public ShapeFieldMapper build(BuilderContext context) { - setupFieldType(context); - return new ShapeFieldMapper(name, fieldType, defaultFieldType, ignoreMalformed(context), coerce(context), + ShapeFieldType ft = new ShapeFieldType(buildFullName(context), indexed, hasDocValues, meta); + GeometryParser geometryParser + = new GeometryParser(orientation().value().getAsBoolean(), coerce().value(), ignoreZValue().value()); + ft.setGeometryParser((parser, mapper) -> geometryParser.parse(parser)); + ft.setGeometryIndexer(new ShapeIndexer(ft.name())); + ft.setGeometryQueryBuilder(new ShapeQueryProcessor()); + ft.setOrientation(orientation().value()); + return new ShapeFieldMapper(name, fieldType, ft, ignoreMalformed(context), coerce(context), ignoreZValue(), orientation(), context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); } - - @Override - protected void setGeometryParser(ShapeFieldType fieldType) { - GeometryParser geometryParser = new GeometryParser(fieldType.orientation().getAsBoolean(), - coerce().value(), ignoreZValue().value()); - fieldType().setGeometryParser((parser, mapper) -> geometryParser.parse(parser)); - } - - @Override - protected void setGeometryIndexer(ShapeFieldType fieldType) { - fieldType.setGeometryIndexer(new ShapeIndexer(fieldType.name())); - } - - @Override - protected void setGeometryQueryBuilder(ShapeFieldType fieldType) { - fieldType.setGeometryQueryBuilder(new ShapeQueryProcessor()); - } } public static class TypeParser extends AbstractShapeGeometryFieldMapper.TypeParser { @@ -92,9 +78,8 @@ public Builder newBuilder(String name, Map params) { @SuppressWarnings({"unchecked", "rawtypes"}) public static final class ShapeFieldType extends AbstractShapeGeometryFieldType { - public ShapeFieldType() { - super(); - setDimensions(7, 4, Integer.BYTES); + public ShapeFieldType(String name, boolean indexed, boolean hasDocValues, Map meta) { + super(name, indexed, hasDocValues, meta); } public ShapeFieldType(ShapeFieldType ref) { @@ -117,11 +102,11 @@ protected Indexer geometryIndexer() { } } - public ShapeFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, + public ShapeFieldMapper(String simpleName, FieldType fieldType, MappedFieldType mappedFieldType, Explicit ignoreMalformed, Explicit coerce, Explicit ignoreZValue, Explicit orientation, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { - super(simpleName, fieldType, defaultFieldType, ignoreMalformed, coerce, ignoreZValue, orientation, indexSettings, + super(simpleName, fieldType, mappedFieldType, ignoreMalformed, coerce, ignoreZValue, orientation, indexSettings, multiFields, copyTo); } diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/aggregations/metrics/GeoShapeCentroidAggregatorTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/aggregations/metrics/GeoShapeCentroidAggregatorTests.java index eeb2c5bc35363..a83ac51c5fc7c 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/aggregations/metrics/GeoShapeCentroidAggregatorTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/aggregations/metrics/GeoShapeCentroidAggregatorTests.java @@ -40,6 +40,7 @@ import java.io.IOException; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.function.Function; @@ -60,9 +61,8 @@ public void testEmpty() throws Exception { GeoCentroidAggregationBuilder aggBuilder = new GeoCentroidAggregationBuilder("my_agg") .field("field"); - MappedFieldType fieldType = new GeoShapeWithDocValuesFieldMapper.GeoShapeWithDocValuesFieldType(); - fieldType.setHasDocValues(true); - fieldType.setName("field"); + MappedFieldType fieldType + = new GeoShapeWithDocValuesFieldMapper.GeoShapeWithDocValuesFieldType("field", true, true, Collections.emptyMap()); try (IndexReader reader = w.getReader()) { IndexSearcher searcher = new IndexSearcher(reader); InternalGeoCentroid result = search(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType); @@ -84,15 +84,13 @@ public void testUnmapped() throws Exception { try (IndexReader reader = w.getReader()) { IndexSearcher searcher = new IndexSearcher(reader); - MappedFieldType fieldType = new GeoShapeWithDocValuesFieldMapper.GeoShapeWithDocValuesFieldType(); - fieldType.setHasDocValues(true); - fieldType.setName("another_field"); + MappedFieldType fieldType = new GeoShapeWithDocValuesFieldMapper.GeoShapeWithDocValuesFieldType("another_field", + true, true, Collections.emptyMap()); InternalGeoCentroid result = search(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType); assertNull(result.centroid()); - fieldType = new GeoShapeWithDocValuesFieldMapper.GeoShapeWithDocValuesFieldType(); - fieldType.setHasDocValues(true); - fieldType.setName("field"); + fieldType = new GeoShapeWithDocValuesFieldMapper.GeoShapeWithDocValuesFieldType("field", + true, true, Collections.emptyMap()); result = search(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType); assertNull(result.centroid()); assertFalse(AggregationInspectionHelper.hasValue(result)); @@ -116,9 +114,8 @@ public void testUnmappedWithMissing() throws Exception { try (IndexReader reader = w.getReader()) { IndexSearcher searcher = new IndexSearcher(reader); - MappedFieldType fieldType = new GeoShapeWithDocValuesFieldMapper.GeoShapeWithDocValuesFieldType(); - fieldType.setHasDocValues(true); - fieldType.setName("another_field"); + MappedFieldType fieldType = new GeoShapeWithDocValuesFieldMapper.GeoShapeWithDocValuesFieldType("another_field", + true, true, Collections.emptyMap()); InternalGeoCentroid result = search(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType); assertThat(result.centroid(), equalTo(expectedCentroid)); assertTrue(AggregationInspectionHelper.hasValue(result)); @@ -176,9 +173,8 @@ public void testSingleValuedField() throws Exception { } private void assertCentroid(RandomIndexWriter w, GeoPoint expectedCentroid) throws IOException { - MappedFieldType fieldType = new GeoShapeWithDocValuesFieldMapper.GeoShapeWithDocValuesFieldType(); - fieldType.setHasDocValues(true); - fieldType.setName("field"); + MappedFieldType fieldType = new GeoShapeWithDocValuesFieldMapper.GeoShapeWithDocValuesFieldType("field", + true, true, Collections.emptyMap()); GeoCentroidAggregationBuilder aggBuilder = new GeoCentroidAggregationBuilder("my_agg") .field("field"); try (IndexReader reader = w.getReader()) { diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/GeoShapeWithDocValuesFieldMapperTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/GeoShapeWithDocValuesFieldMapperTests.java index bde6c01b6a75c..874d1de94653c 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/GeoShapeWithDocValuesFieldMapperTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/GeoShapeWithDocValuesFieldMapperTests.java @@ -48,6 +48,7 @@ import java.io.IOException; import java.util.Collection; import java.util.Collections; +import java.util.Set; import static org.elasticsearch.index.mapper.AbstractPointGeometryFieldMapper.Names.IGNORE_Z_VALUE; import static org.hamcrest.Matchers.containsString; @@ -61,6 +62,11 @@ protected GeoShapeWithDocValuesFieldMapper.Builder newBuilder() { return new GeoShapeWithDocValuesFieldMapper.Builder("geoshape"); } + @Override + protected Set unsupportedProperties() { + return Set.of("analyzer", "similarity", "store"); + } + @Before public void addModifiers() { addModifier("orientation", true, (a, b) -> { @@ -94,8 +100,6 @@ public void testDefaultConfiguration() throws IOException { GeoShapeWithDocValuesFieldMapper geoShapeFieldMapper = (GeoShapeWithDocValuesFieldMapper) fieldMapper; assertThat(geoShapeFieldMapper.fieldType().orientation(), equalTo(org.elasticsearch.index.mapper.GeoShapeFieldMapper.Defaults.ORIENTATION.value())); - assertFalse(geoShapeFieldMapper.docValues().explicit()); - assertTrue(geoShapeFieldMapper.docValues().value()); assertTrue(geoShapeFieldMapper.fieldType().hasDocValues()); } @@ -113,8 +117,6 @@ public void testDefaultDocValueConfigurationOnPre7_8() throws IOException { assertThat(fieldMapper, instanceOf(GeoShapeWithDocValuesFieldMapper.class)); GeoShapeWithDocValuesFieldMapper geoShapeFieldMapper = (GeoShapeWithDocValuesFieldMapper) fieldMapper; - assertFalse(geoShapeFieldMapper.docValues().explicit()); - assertFalse(geoShapeFieldMapper.docValues().value()); assertFalse(geoShapeFieldMapper.fieldType().hasDocValues()); } @@ -285,8 +287,6 @@ public void testDocValues() throws IOException { Mapper fieldMapper = defaultMapper.mappers().getMapper("location"); assertThat(fieldMapper, instanceOf(GeoShapeWithDocValuesFieldMapper.class)); - assertTrue(((GeoShapeWithDocValuesFieldMapper)fieldMapper).docValues().explicit()); - assertTrue(((GeoShapeWithDocValuesFieldMapper)fieldMapper).docValues().value()); boolean hasDocValues = ((GeoShapeWithDocValuesFieldMapper)fieldMapper).fieldType().hasDocValues(); assertTrue(hasDocValues); @@ -303,8 +303,6 @@ public void testDocValues() throws IOException { fieldMapper = defaultMapper.mappers().getMapper("location"); assertThat(fieldMapper, instanceOf(GeoShapeWithDocValuesFieldMapper.class)); - assertTrue(((GeoShapeWithDocValuesFieldMapper)fieldMapper).docValues().explicit()); - assertFalse(((GeoShapeWithDocValuesFieldMapper)fieldMapper).docValues().value()); hasDocValues = ((GeoShapeWithDocValuesFieldMapper)fieldMapper).fieldType().hasDocValues(); assertFalse(hasDocValues); } diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/GeoShapeWithDocValuesFieldTypeTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/GeoShapeWithDocValuesFieldTypeTests.java index a6d030f1b5151..b0024ab52904d 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/GeoShapeWithDocValuesFieldTypeTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/GeoShapeWithDocValuesFieldTypeTests.java @@ -11,6 +11,8 @@ import org.elasticsearch.xpack.spatial.index.mapper.GeoShapeWithDocValuesFieldMapper.GeoShapeWithDocValuesFieldType; import org.junit.Before; +import java.util.Map; + public class GeoShapeWithDocValuesFieldTypeTests extends FieldTypeTestCase { @Before @@ -27,7 +29,7 @@ public void addModifiers() { } @Override - protected GeoShapeWithDocValuesFieldType createDefaultFieldType() { - return new GeoShapeWithDocValuesFieldType(); + protected GeoShapeWithDocValuesFieldType createDefaultFieldType(String name, Map meta) { + return new GeoShapeWithDocValuesFieldType(name, true, true, meta); } } diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/PointFieldMapperTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/PointFieldMapperTests.java index cf6c7b399e98d..2c1998f12aa64 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/PointFieldMapperTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/PointFieldMapperTests.java @@ -228,7 +228,7 @@ public void testNullValue() throws Exception { Mapper fieldMapper = defaultMapper.mappers().getMapper("location"); assertThat(fieldMapper, instanceOf(PointFieldMapper.class)); - Object nullValue = ((PointFieldMapper) fieldMapper).fieldType().nullValue(); + Object nullValue = ((PointFieldMapper) fieldMapper).getNullValue(); assertThat(nullValue, equalTo(new CartesianPoint(1, 2))); ParsedDocument doc = defaultMapper.parse(new SourceToParse("test", "1", diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/PointFieldTypeTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/PointFieldTypeTests.java index 3fdfa4b36f0db..9c851988e35f9 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/PointFieldTypeTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/mapper/PointFieldTypeTests.java @@ -8,9 +8,11 @@ import org.elasticsearch.index.mapper.FieldTypeTestCase; import org.elasticsearch.index.mapper.MappedFieldType; +import java.util.Map; + public class PointFieldTypeTests extends FieldTypeTestCase { @Override - protected MappedFieldType createDefaultFieldType() { - return new PointFieldMapper.PointFieldType(); + protected MappedFieldType createDefaultFieldType(String name, Map meta) { + return new PointFieldMapper.PointFieldType(name, true, true, meta); } } diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/ingest/CircleProcessorTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/ingest/CircleProcessorTests.java index 65d71f2b7f6ce..abfcbdc356df6 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/ingest/CircleProcessorTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/ingest/CircleProcessorTests.java @@ -213,9 +213,8 @@ public void testGeoShapeQueryAcrossDateline() throws IOException { int numSides = randomIntBetween(4, 1000); Geometry geometry = SpatialUtils.createRegularGeoShapePolygon(circle, numSides); - MappedFieldType shapeType = new GeoShapeWithDocValuesFieldMapper.GeoShapeWithDocValuesFieldType(); - shapeType.setHasDocValues(false); - shapeType.setName(fieldName); + MappedFieldType shapeType + = new GeoShapeWithDocValuesFieldMapper.GeoShapeWithDocValuesFieldType(fieldName, true, false, Collections.emptyMap()); VectorGeoShapeQueryProcessor processor = new VectorGeoShapeQueryProcessor(); QueryShardContext mockedContext = mock(QueryShardContext.class); @@ -247,9 +246,7 @@ public void testShapeQuery() throws IOException { int numSides = randomIntBetween(4, 1000); Geometry geometry = SpatialUtils.createRegularShapePolygon(circle, numSides); - MappedFieldType shapeType = new ShapeFieldMapper.ShapeFieldType(); - shapeType.setHasDocValues(false); - shapeType.setName(fieldName); + MappedFieldType shapeType = new ShapeFieldMapper.ShapeFieldType(fieldName, true, false, Collections.emptyMap()); ShapeQueryProcessor processor = new ShapeQueryProcessor(); QueryShardContext mockedContext = mock(QueryShardContext.class); diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoShapeGeoGridTestCase.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoShapeGeoGridTestCase.java index 9311c62352660..973adf34672a1 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoShapeGeoGridTestCase.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/bucket/geogrid/GeoShapeGeoGridTestCase.java @@ -298,9 +298,8 @@ private void testCase(Query query, int precision, GeoBoundingBox geoBoundingBox, assertThat(aggregationBuilder.geoBoundingBox(), equalTo(geoBoundingBox)); } - MappedFieldType fieldType = new GeoShapeWithDocValuesFieldMapper.GeoShapeWithDocValuesFieldType(); - fieldType.setHasDocValues(true); - fieldType.setName(FIELD_NAME); + MappedFieldType fieldType + = new GeoShapeWithDocValuesFieldMapper.GeoShapeWithDocValuesFieldType(FIELD_NAME, true, true, Collections.emptyMap()); Aggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType); aggregator.preCollection(); diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/metrics/GeoShapeBoundsAggregatorTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/metrics/GeoShapeBoundsAggregatorTests.java index 218974f8c6a2c..6eb076cba6d7b 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/metrics/GeoShapeBoundsAggregatorTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/search/aggregations/metrics/GeoShapeBoundsAggregatorTests.java @@ -36,6 +36,7 @@ import org.elasticsearch.xpack.spatial.util.GeoTestUtils; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import static org.hamcrest.Matchers.closeTo; @@ -56,9 +57,8 @@ public void testEmpty() throws Exception { .field("field") .wrapLongitude(false); - MappedFieldType fieldType = new GeoShapeWithDocValuesFieldMapper.GeoShapeWithDocValuesFieldType(); - fieldType.setHasDocValues(true); - fieldType.setName("field"); + MappedFieldType fieldType + = new GeoShapeWithDocValuesFieldMapper.GeoShapeWithDocValuesFieldType("field", true, true, Collections.emptyMap()); try (IndexReader reader = w.getReader()) { IndexSearcher searcher = new IndexSearcher(reader); InternalGeoBounds bounds = search(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType); @@ -85,9 +85,8 @@ public void testUnmappedFieldWithDocs() throws Exception { .field("non_existent") .wrapLongitude(false); - MappedFieldType fieldType = new GeoShapeWithDocValuesFieldMapper.GeoShapeWithDocValuesFieldType(); - fieldType.setHasDocValues(true); - fieldType.setName("field"); + MappedFieldType fieldType + = new GeoShapeWithDocValuesFieldMapper.GeoShapeWithDocValuesFieldType("field", true, true, Collections.emptyMap()); try (IndexReader reader = w.getReader()) { IndexSearcher searcher = new IndexSearcher(reader); InternalGeoBounds bounds = search(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType); @@ -108,9 +107,8 @@ public void testMissing() throws Exception { doc.add(new NumericDocValuesField("not_field", 1000L)); w.addDocument(doc); - MappedFieldType fieldType = new GeoShapeWithDocValuesFieldMapper.GeoShapeWithDocValuesFieldType(); - fieldType.setHasDocValues(true); - fieldType.setName("field"); + MappedFieldType fieldType + = new GeoShapeWithDocValuesFieldMapper.GeoShapeWithDocValuesFieldType("field", true, true, Collections.emptyMap()); Point point = GeometryTestUtils.randomPoint(false); double lon = GeoEncodingUtils.decodeLongitude(GeoEncodingUtils.encodeLongitude(point.getX())); @@ -141,9 +139,8 @@ public void testInvalidMissing() throws Exception { doc.add(new NumericDocValuesField("not_field", 1000L)); w.addDocument(doc); - MappedFieldType fieldType = new GeoShapeWithDocValuesFieldMapper.GeoShapeWithDocValuesFieldType(); - fieldType.setHasDocValues(true); - fieldType.setName("field"); + MappedFieldType fieldType + = new GeoShapeWithDocValuesFieldMapper.GeoShapeWithDocValuesFieldType("field", true, true, Collections.emptyMap()); GeoBoundsAggregationBuilder aggBuilder = new GeoBoundsAggregationBuilder("my_agg") .field("field") @@ -203,9 +200,8 @@ public void testRandomShapes() throws Exception { .field("field") .wrapLongitude(false); - MappedFieldType fieldType = new GeoShapeWithDocValuesFieldMapper.GeoShapeWithDocValuesFieldType(); - fieldType.setHasDocValues(true); - fieldType.setName("field"); + MappedFieldType fieldType + = new GeoShapeWithDocValuesFieldMapper.GeoShapeWithDocValuesFieldType("field", true, true, Collections.emptyMap()); try (IndexReader reader = w.getReader()) { IndexSearcher searcher = new IndexSearcher(reader); InternalGeoBounds bounds = search(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType); diff --git a/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/mapper/DenseVectorFieldMapper.java b/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/mapper/DenseVectorFieldMapper.java index acfe77002b21e..9482c0fa83e7d 100644 --- a/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/mapper/DenseVectorFieldMapper.java +++ b/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/mapper/DenseVectorFieldMapper.java @@ -8,6 +8,7 @@ package org.elasticsearch.xpack.vectors.mapper; import org.apache.lucene.document.BinaryDocValuesField; +import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.search.DocValuesFieldExistsQuery; import org.apache.lucene.search.Query; @@ -46,12 +47,11 @@ public class DenseVectorFieldMapper extends FieldMapper { private static final byte INT_BYTES = 4; public static class Defaults { - public static final MappedFieldType FIELD_TYPE = new DenseVectorFieldType(); + public static final FieldType FIELD_TYPE = new FieldType(); static { FIELD_TYPE.setTokenized(false); FIELD_TYPE.setIndexOptions(IndexOptions.NONE); - FIELD_TYPE.setHasDocValues(true); FIELD_TYPE.setOmitNorms(true); FIELD_TYPE.freeze(); } @@ -61,35 +61,23 @@ public static class Builder extends FieldMapper.Builder { private int dims = 0; public Builder(String name) { - super(name, Defaults.FIELD_TYPE, Defaults.FIELD_TYPE); + super(name, Defaults.FIELD_TYPE); builder = this; } public Builder dims(int dims) { if ((dims > MAX_DIMS_COUNT) || (dims < 1)) { throw new MapperParsingException("The number of dimensions for field [" + name + - "] should be in the range [1, " + MAX_DIMS_COUNT + "]"); + "] should be in the range [1, " + MAX_DIMS_COUNT + "] but was [" + dims + "]"); } this.dims = dims; return this; } - @Override - protected void setupFieldType(BuilderContext context) { - super.setupFieldType(context); - fieldType().setDims(dims); - } - - @Override - public DenseVectorFieldType fieldType() { - return (DenseVectorFieldType) super.fieldType(); - } - @Override public DenseVectorFieldMapper build(BuilderContext context) { - setupFieldType(context); return new DenseVectorFieldMapper( - name, fieldType, defaultFieldType, + name, fieldType, new DenseVectorFieldType(buildFullName(context), dims, meta), context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); } } @@ -108,12 +96,16 @@ public Mapper.Builder parse(String name, Map node, ParserCont } public static final class DenseVectorFieldType extends MappedFieldType { - private int dims; + private final int dims; - public DenseVectorFieldType() {} + public DenseVectorFieldType(String name, int dims, Map meta) { + super(name, false, false, meta); + this.dims = dims; + } protected DenseVectorFieldType(DenseVectorFieldType ref) { super(ref); + this.dims = ref.dims; } public DenseVectorFieldType clone() { @@ -124,10 +116,6 @@ int dims() { return dims; } - void setDims(int dims) { - this.dims = dims; - } - @Override public String typeName() { return CONTENT_TYPE; @@ -156,9 +144,9 @@ public Query termQuery(Object value, QueryShardContext context) { } } - private DenseVectorFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, + private DenseVectorFieldMapper(String simpleName, FieldType fieldType, MappedFieldType mappedFieldType, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { - super(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, copyTo); + super(simpleName, fieldType, mappedFieldType, indexSettings, multiFields, copyTo); assert fieldType.indexOptions() == IndexOptions.NONE; } @@ -222,6 +210,16 @@ public void parse(ParseContext context) throws IOException { context.doc().addWithKey(fieldType().name(), field); } + @Override + protected boolean indexedByDefault() { + return false; + } + + @Override + protected boolean docValuesByDefault() { + return false; + } + @Override protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException { super.doXContentBody(builder, includeDefaults, params); diff --git a/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/mapper/SparseVectorFieldMapper.java b/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/mapper/SparseVectorFieldMapper.java index d8f67c3ee9112..4757c9b36e334 100644 --- a/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/mapper/SparseVectorFieldMapper.java +++ b/x-pack/plugin/vectors/src/main/java/org/elasticsearch/xpack/vectors/mapper/SparseVectorFieldMapper.java @@ -8,6 +8,7 @@ package org.elasticsearch.xpack.vectors.mapper; import org.apache.logging.log4j.LogManager; +import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.search.DocValuesFieldExistsQuery; import org.apache.lucene.search.Query; @@ -43,12 +44,11 @@ public class SparseVectorFieldMapper extends FieldMapper { public static final String CONTENT_TYPE = "sparse_vector"; public static class Defaults { - public static final MappedFieldType FIELD_TYPE = new SparseVectorFieldType(); + public static final FieldType FIELD_TYPE = new FieldType(); static { FIELD_TYPE.setTokenized(false); FIELD_TYPE.setIndexOptions(IndexOptions.NONE); - FIELD_TYPE.setHasDocValues(true); FIELD_TYPE.setOmitNorms(true); FIELD_TYPE.freeze(); } @@ -57,15 +57,14 @@ public static class Defaults { public static class Builder extends FieldMapper.Builder { public Builder(String name) { - super(name, Defaults.FIELD_TYPE, Defaults.FIELD_TYPE); + super(name, Defaults.FIELD_TYPE); builder = this; } @Override public SparseVectorFieldMapper build(BuilderContext context) { - setupFieldType(context); return new SparseVectorFieldMapper( - name, fieldType, defaultFieldType, + name, fieldType, new SparseVectorFieldType(buildFullName(context), meta), context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); } } @@ -85,7 +84,9 @@ public Mapper.Builder parse(String name, Map node, ParserCont public static final class SparseVectorFieldType extends MappedFieldType { - public SparseVectorFieldType() {} + public SparseVectorFieldType(String name, Map meta) { + super(name, false, false, meta); + } protected SparseVectorFieldType(SparseVectorFieldType ref) { super(ref); @@ -119,9 +120,9 @@ public Query termQuery(Object value, QueryShardContext context) { } - private SparseVectorFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, + private SparseVectorFieldMapper(String simpleName, FieldType fieldType, MappedFieldType mappedFieldType, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { - super(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, copyTo); + super(simpleName, fieldType, mappedFieldType, indexSettings, multiFields, copyTo); assert fieldType.indexOptions() == IndexOptions.NONE; } diff --git a/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/mapper/DenseVectorFieldMapperTests.java b/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/mapper/DenseVectorFieldMapperTests.java index 6d1e163859b61..8804c8292bb4c 100644 --- a/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/mapper/DenseVectorFieldMapperTests.java +++ b/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/mapper/DenseVectorFieldMapperTests.java @@ -33,6 +33,7 @@ import java.io.IOException; import java.nio.ByteBuffer; import java.util.Collection; +import java.util.Set; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.instanceOf; @@ -41,7 +42,7 @@ public class DenseVectorFieldMapperTests extends FieldMapperTestCase> getPlugins() { return pluginList(Vectors.class, LocalStateCompositeXPackPlugin.class); } + @Override + protected Set unsupportedProperties() { + return Set.of("analyzer", "similarity", "doc_values", "store"); + } + @Before public void addModifiers() { addModifier("dims", false, (a, b) -> { @@ -76,7 +82,8 @@ public void testMappingExceedDimsLimit() throws IOException { .endObject() .endObject()); MapperParsingException e = expectThrows(MapperParsingException.class, () -> parser.parse("_doc", new CompressedXContent(mapping))); - assertEquals(e.getMessage(), "The number of dimensions for field [my-dense-vector] should be in the range [1, 2048]"); + assertEquals(e.getMessage(), + "The number of dimensions for field [my-dense-vector] should be in the range [1, 2048] but was [2049]"); } public void testDefaults() throws Exception { diff --git a/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/mapper/DenseVectorFieldTypeTests.java b/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/mapper/DenseVectorFieldTypeTests.java index 0d178c92f6b66..8a82208fb36cc 100644 --- a/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/mapper/DenseVectorFieldTypeTests.java +++ b/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/mapper/DenseVectorFieldTypeTests.java @@ -10,10 +10,12 @@ import org.elasticsearch.index.mapper.FieldTypeTestCase; import org.elasticsearch.index.mapper.MappedFieldType; +import java.util.Map; + public class DenseVectorFieldTypeTests extends FieldTypeTestCase { @Override - protected MappedFieldType createDefaultFieldType() { - return new DenseVectorFieldMapper.DenseVectorFieldType(); + protected MappedFieldType createDefaultFieldType(String name, Map meta) { + return new DenseVectorFieldMapper.DenseVectorFieldType(name, 4, meta); } } diff --git a/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/mapper/SparseVectorFieldTypeTests.java b/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/mapper/SparseVectorFieldTypeTests.java index ca1da28d93c16..7420e5174e5b6 100644 --- a/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/mapper/SparseVectorFieldTypeTests.java +++ b/x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/mapper/SparseVectorFieldTypeTests.java @@ -10,15 +10,18 @@ import org.elasticsearch.index.mapper.FieldTypeTestCase; import org.elasticsearch.index.mapper.MappedFieldType; +import java.util.Collections; +import java.util.Map; + public class SparseVectorFieldTypeTests extends FieldTypeTestCase { @Override - protected MappedFieldType createDefaultFieldType() { - return new SparseVectorFieldMapper.SparseVectorFieldType(); + protected MappedFieldType createDefaultFieldType(String name, Map meta) { + return new SparseVectorFieldMapper.SparseVectorFieldType(name, meta); } public void testDocValuesDisabled() { - MappedFieldType fieldType = createDefaultFieldType(); + MappedFieldType fieldType = createDefaultFieldType("field", Collections.emptyMap()); expectThrows(IllegalArgumentException.class, () -> fieldType.fielddataBuilder("index")); } } diff --git a/x-pack/plugin/wildcard/src/main/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldMapper.java b/x-pack/plugin/wildcard/src/main/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldMapper.java index 5f040fee7a617..1d81f6e19fe19 100644 --- a/x-pack/plugin/wildcard/src/main/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldMapper.java +++ b/x-pack/plugin/wildcard/src/main/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldMapper.java @@ -13,6 +13,7 @@ import org.apache.lucene.analysis.ngram.NGramTokenizer; import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; import org.apache.lucene.document.Field; +import org.apache.lucene.document.FieldType; import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.IndexableField; import org.apache.lucene.index.Term; @@ -104,12 +105,10 @@ public TokenStreamComponents createComponents(String fieldName) { }); public static class Defaults { - public static final MappedFieldType FIELD_TYPE = new WildcardFieldType(); + public static final FieldType FIELD_TYPE = new FieldType(); static { FIELD_TYPE.setTokenized(false); - FIELD_TYPE.setIndexAnalyzer(WILDCARD_ANALYZER); - FIELD_TYPE.setSearchAnalyzer(Lucene.KEYWORD_ANALYZER); FIELD_TYPE.setIndexOptions(IndexOptions.DOCS); FIELD_TYPE.setStoreTermVectorOffsets(false); FIELD_TYPE.setOmitNorms(true); @@ -124,7 +123,7 @@ public static class Builder extends FieldMapper.Builder { protected String nullValue = Defaults.NULL_VALUE; public Builder(String name) { - super(name, Defaults.FIELD_TYPE, Defaults.FIELD_TYPE); + super(name, Defaults.FIELD_TYPE); builder = this; } @@ -165,6 +164,11 @@ public Builder index(boolean index) { return this; } + public Builder nullValue(String nullValue) { + this.nullValue = nullValue; + return this; + } + public Builder ignoreAbove(int ignoreAbove) { if (ignoreAbove < 0) { throw new IllegalArgumentException("[ignore_above] must be positive, got " + ignoreAbove); @@ -174,25 +178,12 @@ public Builder ignoreAbove(int ignoreAbove) { } - @Override - protected void setupFieldType(BuilderContext context) { - super.setupFieldType(context); - fieldType().setHasDocValues(true); - fieldType().setTokenized(false); - fieldType().setIndexOptions(IndexOptions.DOCS); - } - - @Override - public WildcardFieldType fieldType() { - return (WildcardFieldType) super.fieldType(); - } @Override public WildcardFieldMapper build(BuilderContext context) { - setupFieldType(context); return new WildcardFieldMapper( - name, fieldType, defaultFieldType, ignoreAbove, - context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); + name, fieldType, new WildcardFieldType(buildFullName(context), meta), ignoreAbove, + context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo, nullValue); } } @@ -228,11 +219,12 @@ public Mapper.Builder parse(String name, Map node, ParserCont public static final String TOKEN_END_STRING = TOKEN_START_STRING + TOKEN_START_STRING; public static final class WildcardFieldType extends MappedFieldType { - + static Analyzer lowercaseNormalizer = new LowercaseNormalizer(); - public WildcardFieldType() { - setIndexAnalyzer(Lucene.KEYWORD_ANALYZER); + public WildcardFieldType(String name, Map meta) { + super(name, true, true, meta); + setIndexAnalyzer(WILDCARD_ANALYZER); setSearchAnalyzer(Lucene.KEYWORD_ANALYZER); } @@ -316,13 +308,13 @@ public Query wildcardQuery(String wildcardPattern, RewriteMethod method, QuerySh verifyingBuilder.add(new BooleanClause(verifyingQuery, Occur.MUST)); return verifyingBuilder.build(); } else if (numWildcardChars == 0 || numWildcardStrings > 0) { - // We have no concrete characters and we're not a pure length query e.g. ??? + // We have no concrete characters and we're not a pure length query e.g. ??? return new DocValuesFieldExistsQuery(name()); } return verifyingQuery; } - + @Override public Query regexpQuery(String value, int flags, int maxDeterminizedStates, RewriteMethod method, QueryShardContext context) { if (value.length() == 0) { @@ -334,13 +326,13 @@ public Query regexpQuery(String value, int flags, int maxDeterminizedStates, Rew "[regexp] queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false." ); } - + RegExp ngramRegex = new RegExp(addLineEndChars(toLowerCase(value)), flags); Query approxBooleanQuery = toApproximationQuery(ngramRegex); Query approxNgramQuery = rewriteBoolToNgramQuery(approxBooleanQuery); - - // MatchAll is a special case meaning the regex is known to match everything .* and + + // MatchAll is a special case meaning the regex is known to match everything .* and // there is no need for verification. if (approxNgramQuery instanceof MatchAllDocsQuery) { return existsQuery(context); @@ -351,28 +343,28 @@ public Query regexpQuery(String value, int flags, int maxDeterminizedStates, Rew }; AutomatonQueryOnBinaryDv verifyingQuery = new AutomatonQueryOnBinaryDv(name(), value, deferredAutomatonSupplier); - + // MatchAllButRequireVerificationQuery is a special case meaning the regex is reduced to a single - // clause which we can't accelerate at all and needs verification. Example would be ".." + // clause which we can't accelerate at all and needs verification. Example would be ".." if (approxNgramQuery instanceof MatchAllButRequireVerificationQuery) { return verifyingQuery; } - + // We can accelerate execution with the ngram query BooleanQuery.Builder verifyingBuilder = new BooleanQuery.Builder(); verifyingBuilder.add(new BooleanClause(approxNgramQuery, Occur.MUST)); verifyingBuilder.add(new BooleanClause(verifyingQuery, Occur.MUST)); return verifyingBuilder.build(); } - + // Convert a regular expression to a simplified query consisting of BooleanQuery and TermQuery objects // which captures as much of the logic as possible. Query can produce some false positives but shouldn't // produce any false negatives. // In addition to Term and BooleanQuery clauses there are MatchAllDocsQuery objects (e.g for .*) and - // a RegExpQuery if we can't resolve to any of the above. - // * If an expression resolves to a single MatchAllDocsQuery eg .* then a match all shortcut is possible with - // no verification needed. - // * If an expression resolves to a RegExpQuery eg ?? then only the verification + // a RegExpQuery if we can't resolve to any of the above. + // * If an expression resolves to a single MatchAllDocsQuery eg .* then a match all shortcut is possible with + // no verification needed. + // * If an expression resolves to a RegExpQuery eg ?? then only the verification // query is run. // * Anything else is a concrete query that should be run on the ngram index. public static Query toApproximationQuery(RegExp r) throws IllegalArgumentException { @@ -397,7 +389,7 @@ public static Query toApproximationQuery(RegExp r) throws IllegalArgumentExcepti // Repeat is zero or more times so zero matches = match all result = new MatchAllDocsQuery(); break; - + case REGEXP_REPEAT_MIN: case REGEXP_REPEAT_MINMAX: if (r.min > 0) { @@ -419,7 +411,7 @@ public static Query toApproximationQuery(RegExp r) throws IllegalArgumentExcepti // optimisation for .* queries - match all and no verification stage required. result = new MatchAllDocsQuery(); break; - // All other kinds of expression cannot be represented as a boolean or term query so return an object + // All other kinds of expression cannot be represented as a boolean or term query so return an object // that indicates verification is required case REGEXP_OPTIONAL: case REGEXP_INTERSECTION: @@ -427,7 +419,7 @@ public static Query toApproximationQuery(RegExp r) throws IllegalArgumentExcepti case REGEXP_CHAR_RANGE: case REGEXP_ANYCHAR: case REGEXP_INTERVAL: - case REGEXP_EMPTY: + case REGEXP_EMPTY: case REGEXP_AUTOMATON: result = new MatchAllButRequireVerificationQuery(); break; @@ -435,7 +427,7 @@ public static Query toApproximationQuery(RegExp r) throws IllegalArgumentExcepti assert result != null; // All regex types are understood and translated to a query. return result; } - + private static Query createConcatenationQuery(RegExp r) { // Create ANDs of expressions plus collapse consecutive TermQuerys into single longer ones ArrayList queries = new ArrayList<>(); @@ -452,7 +444,7 @@ private static Query createConcatenationQuery(RegExp r) { bAnd.add(new TermQuery(new Term("", sequence.toString())), Occur.MUST); sequence = new StringBuilder(); } - bAnd.add(query, Occur.MUST); + bAnd.add(query, Occur.MUST); } } if (sequence.length() > 0) { @@ -462,9 +454,9 @@ private static Query createConcatenationQuery(RegExp r) { if (combined.clauses().size() > 0) { return combined; } - // There's something in the regex we couldn't represent as a query - resort to a match all with verification + // There's something in the regex we couldn't represent as a query - resort to a match all with verification return new MatchAllButRequireVerificationQuery(); - + } private static Query createUnionQuery(RegExp r) { @@ -490,7 +482,7 @@ private static Query createUnionQuery(RegExp r) { return bOr.build(); } } - // There's something in the regex we couldn't represent as a query - resort to a match all with verification + // There's something in the regex we couldn't represent as a query - resort to a match all with verification return new MatchAllButRequireVerificationQuery(); } @@ -501,15 +493,15 @@ private static void findLeaves(RegExp exp, Kind kind, List queries) { } else { queries.add(toApproximationQuery(exp)); } - } - + } + private static String toLowerCase(String string) { return lowercaseNormalizer.normalize(null, string).utf8ToString(); } - + // Takes a BooleanQuery + TermQuery tree representing query logic and rewrites using ngrams of appropriate size. private Query rewriteBoolToNgramQuery(Query approxQuery) { - //TODO optimise more intelligently so we: + //TODO optimise more intelligently so we: // 1) favour full-length term queries eg abc over short eg a* when pruning too many clauses. // 2) make MAX_CLAUSES_IN_APPROXIMATION_QUERY a global cap rather than per-boolean clause. if (approxQuery == null) { @@ -538,13 +530,13 @@ private Query rewriteBoolToNgramQuery(Query approxQuery) { } if (approxQuery instanceof TermQuery) { TermQuery tq = (TermQuery) approxQuery; - + //Remove simple terms that are only string beginnings or ends. String s = tq.getTerm().text(); if (s.equals(WildcardFieldMapper.TOKEN_START_STRING) || s.equals(WildcardFieldMapper.TOKEN_END_STRING)) { return new MatchAllButRequireVerificationQuery(); } - + // Break term into tokens Set tokens = new LinkedHashSet<>(); getNgramTokens(tokens, s); @@ -558,8 +550,8 @@ private Query rewriteBoolToNgramQuery(Query approxQuery) { return approxQuery; } throw new IllegalStateException("Invalid query type found parsing regex query:" + approxQuery); - } - + } + static Query simplify(Query input) { if (input instanceof BooleanQuery == false) { return input; @@ -613,8 +605,8 @@ static Query simplify(Query input) { } return result; } - - + + static boolean isMatchAll(Query q) { return q instanceof MatchAllDocsQuery || q instanceof MatchAllButRequireVerificationQuery; } @@ -668,7 +660,7 @@ protected void getNgramTokens(Set tokens, String fragment) { throw new ElasticsearchParseException("Error parsing wildcard regex pattern fragment [" + fragment + "]"); } } - + private void addClause(String token, BooleanQuery.Builder bqBuilder, Occur occur) { assert token.codePointCount(0, token.length()) <= NGRAM_SIZE; @@ -788,9 +780,9 @@ public Query fuzzyQuery( BooleanQuery.Builder bqBuilder = new BooleanQuery.Builder(); //The approximation query can have a prefix and any number of ngrams. BooleanQuery.Builder approxBuilder = new BooleanQuery.Builder(); - + String postPrefixString = lowerSearchTerm; - + // Add all content prior to prefixLength as a MUST clause to the ngram index query if (prefixLength > 0) { Set prefixTokens = new LinkedHashSet<>(); @@ -825,7 +817,7 @@ public Query fuzzyQuery( } tokenizer.end(); tokenizer.close(); - + BooleanQuery.Builder ngramBuilder = new BooleanQuery.Builder(); int numClauses = 0; for (String token : postPrefixTokens) { @@ -839,7 +831,7 @@ public Query fuzzyQuery( ngramBuilder.setMinimumNumberShouldMatch(numClauses - fuzziness.asDistance(searchTerm)); approxBuilder.add(ngramBuilder.build(), Occur.MUST); } - + BooleanQuery ngramQ = approxBuilder.build(); if (ngramQ.clauses().size()>0) { bqBuilder.add(ngramQ, Occur.MUST); @@ -923,16 +915,19 @@ public SortField sortField(Object missingValue, MultiValueMode sortMode, Nested } private int ignoreAbove; - - private WildcardFieldMapper(String simpleName, MappedFieldType fieldType, MappedFieldType defaultFieldType, - int ignoreAbove, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { - super(simpleName, fieldType, defaultFieldType, indexSettings, multiFields, copyTo); + private final String nullValue; + private final FieldType ngramFieldType; + + private WildcardFieldMapper(String simpleName, FieldType fieldType, MappedFieldType mappedFieldType, + int ignoreAbove, Settings indexSettings, MultiFields multiFields, CopyTo copyTo, + String nullValue) { + super(simpleName, fieldType, mappedFieldType, indexSettings, multiFields, copyTo); + this.nullValue = nullValue; this.ignoreAbove = ignoreAbove; + this.ngramFieldType = new FieldType(fieldType); + this.ngramFieldType.setTokenized(true); + this.ngramFieldType.freeze();; assert fieldType.indexOptions() == IndexOptions.DOCS; - - ngramFieldType = fieldType.clone(); - ngramFieldType.setTokenized(true); - ngramFieldType.freeze(); } /** Values that have more chars than the return value of this method will @@ -955,8 +950,8 @@ public WildcardFieldType fieldType() { @Override protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException { super.doXContentBody(builder, includeDefaults, params); - if (includeDefaults || fieldType().nullValue() != null) { - builder.field("null_value", fieldType().nullValue()); + if (nullValue != null) { + builder.field("null_value", nullValue); } if (includeDefaults || ignoreAbove != Defaults.IGNORE_ABOVE) { builder.field("ignore_above", ignoreAbove); @@ -971,7 +966,7 @@ protected void parseCreateField(ParseContext context) throws IOException { } else { XContentParser parser = context.parser(); if (parser.currentToken() == XContentParser.Token.VALUE_NULL) { - value = fieldType().nullValueAsString(); + value = nullValue; } else { value = parser.textOrNull(); } @@ -983,15 +978,12 @@ protected void parseCreateField(ParseContext context) throws IOException { parseDoc.addAll(fields); } - // For internal use by Lucene only - used to define ngram index - final MappedFieldType ngramFieldType; - void createFields(String value, Document parseDoc, Listfields) throws IOException { if (value == null || value.length() > ignoreAbove) { return; } - // Always lower case the ngram index and value - helps with - // a) speed (less ngram variations to explore on disk and in RAM-based automaton) and + // Always lower case the ngram index and value - helps with + // a) speed (less ngram variations to explore on disk and in RAM-based automaton) and // b) uses less disk space String ngramValue = addLineEndChars(WildcardFieldType.toLowerCase(value)); Field ngramField = new Field(fieldType().name(), ngramValue, ngramFieldType); @@ -1005,7 +997,7 @@ void createFields(String value, Document parseDoc, Listfields) t dvField.add(value.getBytes(StandardCharsets.UTF_8)); } } - + // Values held in the ngram index are encoded with special characters to denote start and end of values. static String addLineEndChars(String value) { return TOKEN_START_OR_END_CHAR + value + TOKEN_START_OR_END_CHAR + TOKEN_START_OR_END_CHAR; diff --git a/x-pack/plugin/wildcard/src/test/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldMapperTests.java b/x-pack/plugin/wildcard/src/test/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldMapperTests.java index a65297bfd889c..0942a93e67c12 100644 --- a/x-pack/plugin/wildcard/src/test/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldMapperTests.java +++ b/x-pack/plugin/wildcard/src/test/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldMapperTests.java @@ -72,17 +72,17 @@ import static org.mockito.Mockito.when; public class WildcardFieldMapperTests extends ESTestCase { - + static QueryShardContext createMockQueryShardContext(boolean allowExpensiveQueries) { QueryShardContext queryShardContext = mock(QueryShardContext.class); when(queryShardContext.allowExpensiveQueries()).thenReturn(allowExpensiveQueries); return queryShardContext; - } + } private static final String KEYWORD_FIELD_NAME = "keyword_field"; private static final String WILDCARD_FIELD_NAME = "wildcard_field"; public static final QueryShardContext MOCK_QSC = createMockQueryShardContext(true); - + static final int MAX_FIELD_LENGTH = 30; static WildcardFieldMapper wildcardFieldType; static KeywordFieldMapper keywordFieldType; @@ -169,8 +169,8 @@ public void testTooBigQueryField() throws IOException { wildcardFieldQuery = wildcardFieldType.fieldType().regexpQuery(queryString, RegExp.ALL, 20000, null, MOCK_QSC); wildcardFieldTopDocs = searcher.search(wildcardFieldQuery, 10, Sort.INDEXORDER); assertThat(wildcardFieldTopDocs.totalHits.value, equalTo(0L)); - - + + reader.close(); dir.close(); } @@ -217,7 +217,7 @@ public void testSearchResultsVersusKeywordField() throws IOException { String pattern = null; switch (randomInt(4)) { case 0: - pattern = getRandomWildcardPattern(); + pattern = getRandomWildcardPattern(); wildcardFieldQuery = wildcardFieldType.fieldType().wildcardQuery(pattern, null, MOCK_QSC); keywordFieldQuery = keywordFieldType.fieldType().wildcardQuery(pattern, null, MOCK_QSC); break; @@ -252,26 +252,27 @@ public void testSearchResultsVersusKeywordField() throws IOException { // Prefix length shouldn't be longer than selected search string // BUT keyword field has a bug with prefix length when equal - see https://github.com/elastic/elasticsearch/issues/55790 // so we opt for one less - prefixLength = Math.min(pattern.length() - 1 , prefixLength); + prefixLength = Math.min(pattern.length() - 1 , prefixLength); boolean transpositions = randomBoolean(); - - wildcardFieldQuery = wildcardFieldType.fieldType().fuzzyQuery(pattern, fuzziness, prefixLength, 50, + + wildcardFieldQuery = wildcardFieldType.fieldType().fuzzyQuery(pattern, fuzziness, prefixLength, 50, transpositions, MOCK_QSC); - keywordFieldQuery = keywordFieldType.fieldType().fuzzyQuery(pattern, fuzziness, prefixLength, 50, + keywordFieldQuery = keywordFieldType.fieldType().fuzzyQuery(pattern, fuzziness, prefixLength, 50, transpositions, MOCK_QSC); break; case 4: TermRangeQuery trq = getRandomRange(values); - wildcardFieldQuery = wildcardFieldType.fieldType().rangeQuery(trq.getLowerTerm(),trq.getUpperTerm(), trq.includesLower(), + wildcardFieldQuery = wildcardFieldType.fieldType().rangeQuery(trq.getLowerTerm(),trq.getUpperTerm(), trq.includesLower(), trq.includesUpper(), null, null, null, MOCK_QSC); - keywordFieldQuery = keywordFieldType.fieldType().rangeQuery(trq.getLowerTerm(),trq.getUpperTerm(), trq.includesLower(), + keywordFieldQuery = keywordFieldType.fieldType().rangeQuery(trq.getLowerTerm(),trq.getUpperTerm(), trq.includesLower(), trq.includesUpper(), null, null, null, MOCK_QSC); break; - + } TopDocs kwTopDocs = searcher.search(keywordFieldQuery, values.size() + 1, Sort.RELEVANCE); TopDocs wildcardFieldTopDocs = searcher.search(wildcardFieldQuery, values.size() + 1, Sort.RELEVANCE); - assertThat(wildcardFieldTopDocs.totalHits.value, equalTo(kwTopDocs.totalHits.value)); + assertThat(keywordFieldQuery + "\n" + wildcardFieldQuery, + wildcardFieldTopDocs.totalHits.value, equalTo(kwTopDocs.totalHits.value)); HashSet expectedDocs = new HashSet<>(); for (ScoreDoc topDoc : kwTopDocs.scoreDocs) { @@ -302,14 +303,14 @@ public void testSearchResultsVersusKeywordField() throws IOException { reader.close(); dir.close(); } - + private void indexDoc(RandomIndexWriter iw, String value) throws IOException { Document doc = new Document(); ParseContext.Document parseDoc = new ParseContext.Document(); addFields(parseDoc, doc, value); - indexDoc(parseDoc, doc, iw); + indexDoc(parseDoc, doc, iw); } - + public void testRangeQueryVersusKeywordField() throws IOException { Directory dir = newDirectory(); IndexWriterConfig iwc = newIndexWriterConfig(WildcardFieldMapper.WILDCARD_ANALYZER); @@ -331,29 +332,29 @@ public void testRangeQueryVersusKeywordField() throws IOException { IndexSearcher searcher = newSearcher(reader); iw.close(); - + String [][] rangeTests = { - {"C:\\Program Files\\a", "C:\\Program Files\\z"}, - {"C:\\Program Files\\a", "C:\\Program Files\\n"}, - {null, "C:\\Program Files\\z"}, + {"C:\\Program Files\\a", "C:\\Program Files\\z"}, + {"C:\\Program Files\\a", "C:\\Program Files\\n"}, + {null, "C:\\Program Files\\z"}, {"C:\\Program Files\\a", null}, - - {"a.txt", "z.txt"}, - {"a.txt", "n.txt"}, - {null, "z.txt"}, - {"a.txt", null} + + {"a.txt", "z.txt"}, + {"a.txt", "n.txt"}, + {null, "z.txt"}, + {"a.txt", null} }; - + for (String[] bounds : rangeTests) { BytesRef lower = bounds[0] == null ? null :new BytesRef(bounds[0]); BytesRef upper = bounds[1] == null ? null :new BytesRef(bounds[1]); TermRangeQuery trq = new TermRangeQuery(WILDCARD_FIELD_NAME, lower, upper, randomBoolean(), randomBoolean()); - Query wildcardFieldQuery = wildcardFieldType.fieldType().rangeQuery(trq.getLowerTerm(),trq.getUpperTerm(), trq.includesLower(), + Query wildcardFieldQuery = wildcardFieldType.fieldType().rangeQuery(trq.getLowerTerm(),trq.getUpperTerm(), trq.includesLower(), trq.includesUpper(), null, null, null, MOCK_QSC); - Query keywordFieldQuery = keywordFieldType.fieldType().rangeQuery(trq.getLowerTerm(),trq.getUpperTerm(), trq.includesLower(), + Query keywordFieldQuery = keywordFieldType.fieldType().rangeQuery(trq.getLowerTerm(),trq.getUpperTerm(), trq.includesLower(), trq.includesUpper(), null, null, null, MOCK_QSC); - + TopDocs kwTopDocs = searcher.search(keywordFieldQuery, 10, Sort.RELEVANCE); TopDocs wildcardFieldTopDocs = searcher.search(wildcardFieldQuery, 10, Sort.RELEVANCE); assertThat(wildcardFieldTopDocs.totalHits.value, equalTo(kwTopDocs.totalHits.value)); @@ -371,30 +372,29 @@ public void testRangeQueryVersusKeywordField() throws IOException { reader.close(); dir.close(); } - - + public void testRegexAcceleration() throws IOException, ParseException { // All these expressions should rewrite to a match all with no verification step required at all String superfastRegexes[]= { ".*", "...*..", "(foo|bar|.*)", "@"}; for (String regex : superfastRegexes) { Query wildcardFieldQuery = wildcardFieldType.fieldType().regexpQuery(regex, RegExp.ALL, 20000, null, MOCK_QSC); assertTrue(wildcardFieldQuery instanceof DocValuesFieldExistsQuery); - } + } String matchNoDocsRegexes[]= { ""}; for (String regex : matchNoDocsRegexes) { Query wildcardFieldQuery = wildcardFieldType.fieldType().regexpQuery(regex, RegExp.ALL, 20000, null, MOCK_QSC); assertTrue(wildcardFieldQuery instanceof MatchNoDocsQuery); - } + } - // All of these regexes should be accelerated as the equivalent of the given QueryString query - String acceleratedTests[][] = { - {".*foo.*", "foo"}, + // All of these regexes should be accelerated as the equivalent of the given QueryString query + String acceleratedTests[][] = { + {".*foo.*", "foo"}, {"..foobar","+foo +oba +ar_ +r__"}, {"(maynotexist)?foobar","+foo +oba +ar_ +r__"}, - {".*/etc/passw.*", "+\\/et +tc\\/ +\\/pa +ass +ssw"}, - {".*etc/passwd", "+etc +c\\/p +pas +ssw +wd_ +d__"}, - {"(http|ftp)://foo.*", "+((+htt +ttp) ftp) +(+\\:\\/\\/ +\\/fo +foo)"}, - {"[Pp][Oo][Ww][Ee][Rr][Ss][Hh][Ee][Ll][Ll]\\.[Ee][Xx][Ee]", "+_po +owe +ers +she +ell +l\\.e +exe +e__"}, + {".*/etc/passw.*", "+\\/et +tc\\/ +\\/pa +ass +ssw"}, + {".*etc/passwd", "+etc +c\\/p +pas +ssw +wd_ +d__"}, + {"(http|ftp)://foo.*", "+((+htt +ttp) ftp) +(+\\:\\/\\/ +\\/fo +foo)"}, + {"[Pp][Oo][Ww][Ee][Rr][Ss][Hh][Ee][Ll][Ll]\\.[Ee][Xx][Ee]", "+_po +owe +ers +she +ell +l\\.e +exe +e__"}, {"foo<1-100>bar", "+(+_fo +foo) +(+bar +r__ )"}, {"(aaa.+&.+bbb)cat", "+cat +t__"}, {".a", "a__"} @@ -402,24 +402,24 @@ public void testRegexAcceleration() throws IOException, ParseException { for (String[] test : acceleratedTests) { String regex = test[0]; String expectedAccelerationQueryString = test[1].replaceAll("_", ""+WildcardFieldMapper.TOKEN_START_OR_END_CHAR); - Query wildcardFieldQuery = wildcardFieldType.fieldType().regexpQuery(regex, RegExp.ALL, 20000, null, MOCK_QSC); + Query wildcardFieldQuery = wildcardFieldType.fieldType().regexpQuery(regex, RegExp.ALL, 20000, null, MOCK_QSC); testExpectedAccelerationQuery(regex, wildcardFieldQuery, expectedAccelerationQueryString); } - - // All these expressions should rewrite to just the verification query (there's no ngram acceleration) - // TODO we can possibly improve on some of these + + // All these expressions should rewrite to just the verification query (there's no ngram acceleration) + // TODO we can possibly improve on some of these String matchAllButVerifyTests[]= { "..", "(a)?","(a|b){0,3}", "((foo)?|(foo|bar)?)", "@&~(abc.+)", "aaa.+&.+bbb"}; for (String regex : matchAllButVerifyTests) { Query wildcardFieldQuery = wildcardFieldType.fieldType().regexpQuery(regex, RegExp.ALL, 20000, null, MOCK_QSC); - assertTrue(regex +" was not a pure verify query " +formatQuery(wildcardFieldQuery), + assertTrue(regex +" was not a pure verify query " +formatQuery(wildcardFieldQuery), wildcardFieldQuery instanceof AutomatonQueryOnBinaryDv); - } - - - - // Documentation - regexes that do try accelerate but we would like to improve in future versions. - String suboptimalTests[][] = { - // TODO short wildcards like a* OR b* aren't great so we just drop them. + } + + + + // Documentation - regexes that do try accelerate but we would like to improve in future versions. + String suboptimalTests[][] = { + // TODO short wildcards like a* OR b* aren't great so we just drop them. // Ideally we would attach to successors to create (acd OR bcd) { "[ab]cd", "+cd_ +d__"} }; @@ -427,18 +427,18 @@ public void testRegexAcceleration() throws IOException, ParseException { String regex = test[0]; String expectedAccelerationQueryString = test[1].replaceAll("_", ""+WildcardFieldMapper.TOKEN_START_OR_END_CHAR); Query wildcardFieldQuery = wildcardFieldType.fieldType().regexpQuery(regex, RegExp.ALL, 20000, null, MOCK_QSC); - + testExpectedAccelerationQuery(regex, wildcardFieldQuery, expectedAccelerationQueryString); - } + } - } + } // Make error messages more readable String formatQuery(Query q) { return q.toString().replaceAll(WILDCARD_FIELD_NAME+":", "").replaceAll(WildcardFieldMapper.TOKEN_START_STRING, "_"); } - + public void testWildcardAcceleration() throws IOException, ParseException { - + // All these expressions should rewrite to MatchAll with no verification step required at all String superfastPattern[] = { "*", "**", "*?" }; for (String pattern : superfastPattern) { @@ -478,7 +478,7 @@ public void testWildcardAcceleration() throws IOException, ParseException { } } - + static class FuzzyTest { String pattern; int prefixLength; @@ -549,7 +549,7 @@ Query getExpectedApproxQuery() throws ParseException { return bq.build(); } } - + public void testFuzzyAcceleration() throws IOException, ParseException { FuzzyTest[] tests = { @@ -562,9 +562,8 @@ public void testFuzzyAcceleration() throws IOException, ParseException { Query wildcardFieldQuery = test.getFuzzyQuery(); testExpectedAccelerationQuery(test.pattern, wildcardFieldQuery, test.getExpectedApproxQuery()); } - } - - + } + static class RangeTest { String lower; String upper; @@ -598,8 +597,8 @@ Query getExpectedApproxQuery() throws ParseException { } return bq.build(); } - } - + } + public void testRangeAcceleration() throws IOException, ParseException { RangeTest[] tests = { @@ -610,8 +609,8 @@ public void testRangeAcceleration() throws IOException, ParseException { Query wildcardFieldQuery = test.getRangeQuery(); testExpectedAccelerationQuery(test.lower + "-" + test.upper, wildcardFieldQuery, test.getExpectedApproxQuery()); } - } - + } + void testExpectedAccelerationQuery(String regex, Query combinedQuery, String expectedAccelerationQueryString) throws ParseException { QueryParser qsp = new QueryParser(WILDCARD_FIELD_NAME, new KeywordAnalyzer()); @@ -632,32 +631,32 @@ void testExpectedAccelerationQuery(String regex, Query combinedQuery, Query expe } } assert verifyQueryFound; - - String message = "regex: "+ regex +"\nactual query: " + formatQuery(approximationQuery) + + + String message = "regex: "+ regex +"\nactual query: " + formatQuery(approximationQuery) + "\nexpected query: " + formatQuery(expectedAccelerationQuery) + "\n"; assertEquals(message, expectedAccelerationQuery, approximationQuery); - } - + } + private String getRandomFuzzyPattern(HashSet values, int edits, int prefixLength) { assert edits >=0 && edits <=2; // Pick one of the indexed document values to focus our queries on. String randomValue = values.toArray(new String[0])[randomIntBetween(0, values.size()-1)]; - + if (edits == 0) { return randomValue; } - + if (randomValue.length() > prefixLength) { randomValue = randomValue.substring(0,prefixLength) + "C" + randomValue.substring(prefixLength); edits--; } - + if(edits > 0) { randomValue = randomValue + "a"; } return randomValue; - } - + } + private TermRangeQuery getRandomRange(HashSet values) { // Pick one of the indexed document values to focus our queries on. String randomValue = values.toArray(new String[0])[randomIntBetween(0, values.size()-1)]; @@ -670,24 +669,23 @@ private TermRangeQuery getRandomRange(HashSet values) { if(substitutionPoint >0) { upper.append(randomValue.substring(0,substitutionPoint)); } - + // Modify the middle... String replacementPart = randomValue.substring(substitutionPoint, substitutionPoint+substitutionLength); // .-replace all a chars with z - upper.append(replacementPart.replaceAll("a", "z")); - + upper.append(replacementPart.replaceAll("a", "z")); + //add any remaining tail, unchanged if(substitutionPoint + substitutionLength <= randomValue.length()-1) { upper.append(randomValue.substring(substitutionPoint + substitutionLength)); } - return new TermRangeQuery(WILDCARD_FIELD_NAME, new BytesRef(randomValue), new BytesRef(upper.toString()), + return new TermRangeQuery(WILDCARD_FIELD_NAME, new BytesRef(randomValue), new BytesRef(upper.toString()), randomBoolean(), randomBoolean()); - } - + } private String getRandomRegexPattern(HashSet values) { // Pick one of the indexed document values to focus our queries on. - String randomValue = values.toArray(new String[0])[randomIntBetween(0, values.size()-1)]; + String randomValue = values.toArray(new String[0])[randomIntBetween(0, values.size()-1)]; return convertToRandomRegex(randomValue); } @@ -702,38 +700,38 @@ protected String convertToRandomRegex(String randomValue) { if(substitutionPoint >0) { result.append(randomValue.substring(0,substitutionPoint)); } - + // Modify the middle... String replacementPart = randomValue.substring(substitutionPoint, substitutionPoint+substitutionLength); int mutation = randomIntBetween(0, 11); switch (mutation) { case 0: // OR with random alpha of same length - result.append("("+replacementPart+"|c"+ randomABString(replacementPart.length())+")"); + result.append("("+replacementPart+"|c"+ randomABString(replacementPart.length())+")"); break; case 1: // OR with non-existant value - result.append("("+replacementPart+"|doesnotexist)"); + result.append("("+replacementPart+"|doesnotexist)"); break; case 2: // OR with another randomised regex (used to create nested levels of expression). - result.append("(" + convertToRandomRegex(replacementPart) +"|doesnotexist)"); + result.append("(" + convertToRandomRegex(replacementPart) +"|doesnotexist)"); break; case 3: // Star-replace all ab sequences. - result.append(replacementPart.replaceAll("ab", ".*")); + result.append(replacementPart.replaceAll("ab", ".*")); break; case 4: // .-replace all b chars - result.append(replacementPart.replaceAll("b", ".")); + result.append(replacementPart.replaceAll("b", ".")); break; case 5: // length-limited stars {1,2} - result.append(".{1,"+replacementPart.length()+"}"); + result.append(".{1,"+replacementPart.length()+"}"); break; case 6: // replace all chars with . - result.append(replacementPart.replaceAll(".", ".")); + result.append(replacementPart.replaceAll(".", ".")); break; case 7: // OR with uppercase chars eg [aA] (many of these sorts of expression in the wild.. @@ -765,7 +763,7 @@ protected String convertToRandomRegex(String randomValue) { if(substitutionPoint + substitutionLength <= randomValue.length()-1) { result.append(randomValue.substring(substitutionPoint + substitutionLength)); } - + //Assert our randomly generated regex actually matches the provided raw input. RegExp regex = new RegExp(result.toString()); Automaton automaton = regex.toAutomaton(); @@ -839,7 +837,7 @@ static String randomABString(int minLength) { if (randomBoolean()) { sb.append("a"); } else { - sb.append("A"); + sb.append("A"); } } else { sb.append("b"); diff --git a/x-pack/plugin/wildcard/src/test/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldTypeTests.java b/x-pack/plugin/wildcard/src/test/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldTypeTests.java index 374b2023bfb53..37d6232e96ce0 100644 --- a/x-pack/plugin/wildcard/src/test/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldTypeTests.java +++ b/x-pack/plugin/wildcard/src/test/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldTypeTests.java @@ -10,10 +10,12 @@ import org.elasticsearch.index.mapper.FieldTypeTestCase; import org.elasticsearch.index.mapper.MappedFieldType; +import java.util.Map; + public class WildcardFieldTypeTests extends FieldTypeTestCase { @Override - protected MappedFieldType createDefaultFieldType() { - return new WildcardFieldMapper.WildcardFieldType(); + protected MappedFieldType createDefaultFieldType(String name, Map meta) { + return new WildcardFieldMapper.WildcardFieldType(name, meta); } }