Mapping: Move null value handling into MappedFieldType

In order for exists queries to use the null value for
a field, null value needs to be part of the field type (should
differ between document types). This change moves null value
into the field type, as well as simplifies the null value
methods available to remove supportsNullValue().
This commit is contained in:
Ryan Ernst 2015-06-08 15:03:50 -07:00
parent 16d9480d78
commit ba15a2f6cb
20 changed files with 263 additions and 368 deletions

View File

@ -432,11 +432,7 @@ class DocumentParser implements Closeable {
// we can only handle null values if we have mappings for them // we can only handle null values if we have mappings for them
Mapper mapper = parentMapper.getMapper(lastFieldName); Mapper mapper = parentMapper.getMapper(lastFieldName);
if (mapper != null) { if (mapper != null) {
if (mapper instanceof FieldMapper) { // TODO: passing null to an object seems bogus?
if (!((FieldMapper) mapper).supportsNullValue()) {
throw new MapperParsingException("no object mapping found for null value in [" + lastFieldName + "]");
}
}
parseObjectOrField(context, mapper); parseObjectOrField(context, mapper);
} else if (parentMapper.dynamic() == ObjectMapper.Dynamic.STRICT) { } else if (parentMapper.dynamic() == ObjectMapper.Dynamic.STRICT) {
throw new StrictDynamicMappingException(parentMapper.fullPath(), lastFieldName); throw new StrictDynamicMappingException(parentMapper.fullPath(), lastFieldName);

View File

@ -96,8 +96,6 @@ public interface FieldMapper extends Mapper {
boolean isSortable(); boolean isSortable();
boolean supportsNullValue();
/** /**
* Fields might not be available before indexing, for example _all, token_count,... * Fields might not be available before indexing, for example _all, token_count,...
* When get is called and these fields are requested, this case needs special treatment. * When get is called and these fields are requested, this case needs special treatment.

View File

@ -25,6 +25,7 @@ import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.index.Terms; import org.apache.lucene.index.Terms;
import org.apache.lucene.queries.TermsQuery; import org.apache.lucene.queries.TermsQuery;
import org.apache.lucene.search.ConstantScoreQuery;
import org.apache.lucene.search.FuzzyQuery; import org.apache.lucene.search.FuzzyQuery;
import org.apache.lucene.search.MultiTermQuery; import org.apache.lucene.search.MultiTermQuery;
import org.apache.lucene.search.PrefixQuery; import org.apache.lucene.search.PrefixQuery;
@ -175,6 +176,8 @@ public class MappedFieldType extends FieldType {
private SimilarityProvider similarity; private SimilarityProvider similarity;
private Loading normsLoading; private Loading normsLoading;
private FieldDataType fieldDataType; private FieldDataType fieldDataType;
private Object nullValue;
private String nullValueAsString; // for sending null value to _all field
protected MappedFieldType(MappedFieldType ref) { protected MappedFieldType(MappedFieldType ref) {
super(ref); super(ref);
@ -187,6 +190,8 @@ public class MappedFieldType extends FieldType {
this.similarity = ref.similarity(); this.similarity = ref.similarity();
this.normsLoading = ref.normsLoading(); this.normsLoading = ref.normsLoading();
this.fieldDataType = ref.fieldDataType(); this.fieldDataType = ref.fieldDataType();
this.nullValue = ref.nullValue();
this.nullValueAsString = ref.nullValueAsString();
} }
public MappedFieldType() {} public MappedFieldType() {}
@ -286,6 +291,20 @@ public class MappedFieldType extends FieldType {
this.similarity = similarity; this.similarity = similarity;
} }
public Object nullValue() {
return nullValue;
}
public String nullValueAsString() {
return nullValueAsString;
}
public void setNullValue(Object nullValue) {
checkIfFrozen();
this.nullValue = nullValue;
this.nullValueAsString = nullValue == null ? null : nullValue.toString();
}
/** Returns the actual value of the field. */ /** Returns the actual value of the field. */
public Object value(Object value) { public Object value(Object value) {
return value; return value;
@ -353,6 +372,13 @@ public class MappedFieldType extends FieldType {
return query; return query;
} }
public Query nullValueQuery() {
if (nullValue == null) {
return null;
}
return new ConstantScoreQuery(termQuery(nullValue, null));
}
/** /**
* @return a {@link FieldStats} instance that maps to the type of this field based on the provided {@link Terms} instance. * @return a {@link FieldStats} instance that maps to the type of this field based on the provided {@link Terms} instance.
*/ */

View File

@ -228,6 +228,11 @@ public abstract class AbstractFieldMapper implements FieldMapper {
return builder; return builder;
} }
public Builder nullValue(Object nullValue) {
this.fieldType.setNullValue(nullValue);
return this;
}
public T multiFieldPathType(ContentPath.Type pathType) { public T multiFieldPathType(ContentPath.Type pathType) {
multiFieldsBuilder.pathType(pathType); multiFieldsBuilder.pathType(pathType);
return builder; return builder;
@ -440,8 +445,8 @@ public abstract class AbstractFieldMapper implements FieldMapper {
} }
@Override @Override
public Query nullValueFilter() { public final Query nullValueFilter() {
return null; return fieldType().nullValueQuery();
} }
@Override @Override
@ -689,11 +694,6 @@ public abstract class AbstractFieldMapper implements FieldMapper {
return fieldType().isSortable(); return fieldType().isSortable();
} }
@Override
public boolean supportsNullValue() {
return true;
}
public static class MultiFields { public static class MultiFields {
public static MultiFields empty() { public static MultiFields empty() {

View File

@ -22,8 +22,6 @@ package org.elasticsearch.index.mapper.core;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.SortedNumericDocValuesField; import org.apache.lucene.document.SortedNumericDocValuesField;
import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.IndexOptions;
import org.apache.lucene.search.ConstantScoreQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.Booleans; import org.elasticsearch.common.Booleans;
import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Nullable;
@ -39,7 +37,6 @@ import org.elasticsearch.index.mapper.MapperParsingException;
import org.elasticsearch.index.mapper.MergeMappingException; import org.elasticsearch.index.mapper.MergeMappingException;
import org.elasticsearch.index.mapper.MergeResult; import org.elasticsearch.index.mapper.MergeResult;
import org.elasticsearch.index.mapper.ParseContext; import org.elasticsearch.index.mapper.ParseContext;
import org.elasticsearch.index.similarity.SimilarityProvider;
import java.io.IOException; import java.io.IOException;
import java.util.Iterator; import java.util.Iterator;
@ -68,8 +65,6 @@ public class BooleanFieldMapper extends AbstractFieldMapper {
FIELD_TYPE.setSearchAnalyzer(Lucene.KEYWORD_ANALYZER); FIELD_TYPE.setSearchAnalyzer(Lucene.KEYWORD_ANALYZER);
FIELD_TYPE.freeze(); FIELD_TYPE.freeze();
} }
public static final Boolean NULL_VALUE = null;
} }
public static class Values { public static class Values {
@ -79,18 +74,11 @@ public class BooleanFieldMapper extends AbstractFieldMapper {
public static class Builder extends AbstractFieldMapper.Builder<Builder, BooleanFieldMapper> { public static class Builder extends AbstractFieldMapper.Builder<Builder, BooleanFieldMapper> {
private Boolean nullValue = Defaults.NULL_VALUE;
public Builder(String name) { public Builder(String name) {
super(name, Defaults.FIELD_TYPE); super(name, Defaults.FIELD_TYPE);
this.builder = this; this.builder = this;
} }
public Builder nullValue(boolean nullValue) {
this.nullValue = nullValue;
return this;
}
@Override @Override
public Builder tokenized(boolean tokenized) { public Builder tokenized(boolean tokenized) {
if (tokenized) { if (tokenized) {
@ -102,7 +90,7 @@ public class BooleanFieldMapper extends AbstractFieldMapper {
@Override @Override
public BooleanFieldMapper build(BuilderContext context) { public BooleanFieldMapper build(BuilderContext context) {
setupFieldType(context); setupFieldType(context);
return new BooleanFieldMapper(fieldType, docValues, nullValue, return new BooleanFieldMapper(fieldType, docValues,
fieldDataSettings, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); fieldDataSettings, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo);
} }
} }
@ -143,6 +131,11 @@ public class BooleanFieldMapper extends AbstractFieldMapper {
return new BooleanFieldType(this); return new BooleanFieldType(this);
} }
@Override
public Boolean nullValue() {
return (Boolean)super.nullValue();
}
@Override @Override
public BytesRef indexedValueForSearch(Object value) { public BytesRef indexedValueForSearch(Object value) {
if (value == null) { if (value == null) {
@ -198,12 +191,14 @@ public class BooleanFieldMapper extends AbstractFieldMapper {
} }
} }
private Boolean nullValue; protected BooleanFieldMapper(MappedFieldType fieldType, Boolean docValues,
protected BooleanFieldMapper(MappedFieldType fieldType, Boolean docValues, Boolean nullValue,
@Nullable Settings fieldDataSettings, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { @Nullable Settings fieldDataSettings, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) {
super(fieldType, docValues, fieldDataSettings, indexSettings, multiFields, copyTo); super(fieldType, docValues, fieldDataSettings, indexSettings, multiFields, copyTo);
this.nullValue = nullValue; }
@Override
public BooleanFieldType fieldType() {
return (BooleanFieldType)fieldType;
} }
@Override @Override
@ -217,14 +212,6 @@ public class BooleanFieldMapper extends AbstractFieldMapper {
return new FieldDataType(CONTENT_TYPE); return new FieldDataType(CONTENT_TYPE);
} }
@Override
public Query nullValueFilter() {
if (nullValue == null) {
return null;
}
return new ConstantScoreQuery(termQuery(nullValue, null));
}
@Override @Override
protected void parseCreateField(ParseContext context, List<Field> fields) throws IOException { protected void parseCreateField(ParseContext context, List<Field> fields) throws IOException {
if (fieldType().indexOptions() == IndexOptions.NONE && !fieldType().stored() && !fieldType().hasDocValues()) { if (fieldType().indexOptions() == IndexOptions.NONE && !fieldType().stored() && !fieldType().hasDocValues()) {
@ -235,8 +222,8 @@ public class BooleanFieldMapper extends AbstractFieldMapper {
if (value == null) { if (value == null) {
XContentParser.Token token = context.parser().currentToken(); XContentParser.Token token = context.parser().currentToken();
if (token == XContentParser.Token.VALUE_NULL) { if (token == XContentParser.Token.VALUE_NULL) {
if (nullValue != null) { if (fieldType().nullValue() != null) {
value = nullValue; value = fieldType().nullValue();
} }
} else { } else {
value = context.parser().booleanValue(); value = context.parser().booleanValue();
@ -260,7 +247,9 @@ public class BooleanFieldMapper extends AbstractFieldMapper {
} }
if (!mergeResult.simulate()) { if (!mergeResult.simulate()) {
this.nullValue = ((BooleanFieldMapper) mergeWith).nullValue; this.fieldType = this.fieldType.clone();
this.fieldType.setNullValue(((BooleanFieldMapper) mergeWith).fieldType().nullValue());
this.fieldType.freeze();
} }
} }
@ -272,8 +261,8 @@ public class BooleanFieldMapper extends AbstractFieldMapper {
@Override @Override
protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException { protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException {
super.doXContentBody(builder, includeDefaults, params); super.doXContentBody(builder, includeDefaults, params);
if (includeDefaults || nullValue != null) { if (includeDefaults || fieldType().nullValue() != null) {
builder.field("null_value", nullValue); builder.field("null_value", fieldType().nullValue());
} }
} }
} }

View File

@ -70,28 +70,19 @@ public class ByteFieldMapper extends NumberFieldMapper {
static { static {
FIELD_TYPE.freeze(); FIELD_TYPE.freeze();
} }
public static final Byte NULL_VALUE = null;
} }
public static class Builder extends NumberFieldMapper.Builder<Builder, ByteFieldMapper> { public static class Builder extends NumberFieldMapper.Builder<Builder, ByteFieldMapper> {
protected Byte nullValue = Defaults.NULL_VALUE;
public Builder(String name) { public Builder(String name) {
super(name, Defaults.FIELD_TYPE, Defaults.PRECISION_STEP_8_BIT); super(name, Defaults.FIELD_TYPE, Defaults.PRECISION_STEP_8_BIT);
builder = this; builder = this;
} }
public Builder nullValue(byte nullValue) {
this.nullValue = nullValue;
return this;
}
@Override @Override
public ByteFieldMapper build(BuilderContext context) { public ByteFieldMapper build(BuilderContext context) {
setupFieldType(context); setupFieldType(context);
ByteFieldMapper fieldMapper = new ByteFieldMapper(fieldType, docValues, nullValue, ignoreMalformed(context), ByteFieldMapper fieldMapper = new ByteFieldMapper(fieldType, docValues, ignoreMalformed(context),
coerce(context), fieldDataSettings, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); coerce(context), fieldDataSettings, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo);
fieldMapper.includeInAll(includeInAll); fieldMapper.includeInAll(includeInAll);
return fieldMapper; return fieldMapper;
@ -142,6 +133,11 @@ public class ByteFieldMapper extends NumberFieldMapper {
return new ByteFieldType(this); return new ByteFieldType(this);
} }
@Override
public Byte nullValue() {
return (Byte)super.nullValue();
}
@Override @Override
public Byte value(Object value) { public Byte value(Object value) {
if (value == null) { if (value == null) {
@ -191,16 +187,15 @@ public class ByteFieldMapper extends NumberFieldMapper {
} }
} }
private Byte nullValue;
private String nullValueAsString;
protected ByteFieldMapper(MappedFieldType fieldType, Boolean docValues, protected ByteFieldMapper(MappedFieldType fieldType, Boolean docValues,
Byte nullValue, Explicit<Boolean> ignoreMalformed, Explicit<Boolean> coerce, Explicit<Boolean> ignoreMalformed, Explicit<Boolean> coerce,
@Nullable Settings fieldDataSettings, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { @Nullable Settings fieldDataSettings, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) {
super(fieldType, docValues, ignoreMalformed, coerce, fieldDataSettings, indexSettings, multiFields, copyTo); super(fieldType, docValues, ignoreMalformed, coerce, fieldDataSettings, indexSettings, multiFields, copyTo);
this.nullValue = nullValue; }
this.nullValueAsString = nullValue == null ? null : nullValue.toString();
@Override
public ByteFieldType fieldType() {
return (ByteFieldType)fieldType;
} }
@Override @Override
@ -223,14 +218,6 @@ public class ByteFieldMapper extends NumberFieldMapper {
return Byte.parseByte(value.toString()); return Byte.parseByte(value.toString());
} }
@Override
public Query nullValueFilter() {
if (nullValue == null) {
return null;
}
return new ConstantScoreQuery(termQuery(nullValue, null));
}
@Override @Override
protected boolean customBoost() { protected boolean customBoost() {
return true; return true;
@ -243,17 +230,17 @@ public class ByteFieldMapper extends NumberFieldMapper {
if (context.externalValueSet()) { if (context.externalValueSet()) {
Object externalValue = context.externalValue(); Object externalValue = context.externalValue();
if (externalValue == null) { if (externalValue == null) {
if (nullValue == null) { if (fieldType().nullValue() == null) {
return; return;
} }
value = nullValue; value = fieldType().nullValue();
} else if (externalValue instanceof String) { } else if (externalValue instanceof String) {
String sExternalValue = (String) externalValue; String sExternalValue = (String) externalValue;
if (sExternalValue.length() == 0) { if (sExternalValue.length() == 0) {
if (nullValue == null) { if (fieldType().nullValue() == null) {
return; return;
} }
value = nullValue; value = fieldType().nullValue();
} else { } else {
value = Byte.parseByte(sExternalValue); value = Byte.parseByte(sExternalValue);
} }
@ -267,17 +254,17 @@ public class ByteFieldMapper extends NumberFieldMapper {
XContentParser parser = context.parser(); XContentParser parser = context.parser();
if (parser.currentToken() == XContentParser.Token.VALUE_NULL || if (parser.currentToken() == XContentParser.Token.VALUE_NULL ||
(parser.currentToken() == XContentParser.Token.VALUE_STRING && parser.textLength() == 0)) { (parser.currentToken() == XContentParser.Token.VALUE_STRING && parser.textLength() == 0)) {
if (nullValue == null) { if (fieldType().nullValue() == null) {
return; return;
} }
value = nullValue; value = fieldType().nullValue();
if (nullValueAsString != null && (context.includeInAll(includeInAll, this))) { if (fieldType().nullValueAsString() != null && (context.includeInAll(includeInAll, this))) {
context.allEntries().addText(fieldType.names().fullName(), nullValueAsString, boost); context.allEntries().addText(fieldType.names().fullName(), fieldType().nullValueAsString(), boost);
} }
} else if (parser.currentToken() == XContentParser.Token.START_OBJECT) { } else if (parser.currentToken() == XContentParser.Token.START_OBJECT) {
XContentParser.Token token; XContentParser.Token token;
String currentFieldName = null; String currentFieldName = null;
Byte objValue = nullValue; Byte objValue = fieldType().nullValue();
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) { if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName(); currentFieldName = parser.currentName();
@ -327,8 +314,9 @@ public class ByteFieldMapper extends NumberFieldMapper {
return; return;
} }
if (!mergeResult.simulate()) { if (!mergeResult.simulate()) {
this.nullValue = ((ByteFieldMapper) mergeWith).nullValue; this.fieldType = this.fieldType.clone();
this.nullValueAsString = ((ByteFieldMapper) mergeWith).nullValueAsString; this.fieldType.setNullValue(((ByteFieldMapper) mergeWith).fieldType().nullValue());
this.fieldType.freeze();
} }
} }
@ -339,8 +327,8 @@ public class ByteFieldMapper extends NumberFieldMapper {
if (includeDefaults || fieldType.numericPrecisionStep() != Defaults.PRECISION_STEP_8_BIT) { if (includeDefaults || fieldType.numericPrecisionStep() != Defaults.PRECISION_STEP_8_BIT) {
builder.field("precision_step", fieldType.numericPrecisionStep()); builder.field("precision_step", fieldType.numericPrecisionStep());
} }
if (includeDefaults || nullValue != null) { if (includeDefaults || fieldType().nullValue() != null) {
builder.field("null_value", nullValue); builder.field("null_value", fieldType().nullValue());
} }
if (includeInAll != null) { if (includeInAll != null) {
builder.field("include_in_all", includeInAll); builder.field("include_in_all", includeInAll);

View File

@ -297,6 +297,9 @@ public class CompletionFieldMapper extends AbstractFieldMapper {
public Mapper parse(ParseContext context) throws IOException { public Mapper parse(ParseContext context) throws IOException {
XContentParser parser = context.parser(); XContentParser parser = context.parser();
XContentParser.Token token = parser.currentToken(); XContentParser.Token token = parser.currentToken();
if (token == XContentParser.Token.VALUE_NULL) {
throw new MapperParsingException("completion field [" + fieldType().names().fullName() + "] does not support null values");
}
String surfaceForm = null; String surfaceForm = null;
BytesRef payload = null; BytesRef payload = null;
@ -524,11 +527,6 @@ public class CompletionFieldMapper extends AbstractFieldMapper {
return CONTENT_TYPE; return CONTENT_TYPE;
} }
@Override
public boolean supportsNullValue() {
return false;
}
@Override @Override
public MappedFieldType defaultFieldType() { public MappedFieldType defaultFieldType() {
return Defaults.FIELD_TYPE; return Defaults.FIELD_TYPE;

View File

@ -115,8 +115,9 @@ public class DateFieldMapper extends NumberFieldMapper {
@Override @Override
public DateFieldMapper build(BuilderContext context) { public DateFieldMapper build(BuilderContext context) {
setupFieldType(context); setupFieldType(context);
fieldType.setNullValue(nullValue);
DateFieldMapper fieldMapper = new DateFieldMapper(fieldType, DateFieldMapper fieldMapper = new DateFieldMapper(fieldType,
docValues, nullValue, ignoreMalformed(context), coerce(context), docValues, ignoreMalformed(context), coerce(context),
fieldDataSettings, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); fieldDataSettings, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo);
fieldMapper.includeInAll(includeInAll); fieldMapper.includeInAll(includeInAll);
return fieldMapper; return fieldMapper;
@ -374,12 +375,9 @@ public class DateFieldMapper extends NumberFieldMapper {
} }
} }
private String nullValue; protected DateFieldMapper(MappedFieldType fieldType, Boolean docValues, Explicit<Boolean> ignoreMalformed,Explicit<Boolean> coerce,
protected DateFieldMapper(MappedFieldType fieldType, Boolean docValues, String nullValue, Explicit<Boolean> ignoreMalformed,Explicit<Boolean> coerce,
@Nullable Settings fieldDataSettings, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { @Nullable Settings fieldDataSettings, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) {
super(fieldType, docValues, ignoreMalformed, coerce, fieldDataSettings, indexSettings, multiFields, copyTo); super(fieldType, docValues, ignoreMalformed, coerce, fieldDataSettings, indexSettings, multiFields, copyTo);
this.nullValue = nullValue;
} }
@Override @Override
@ -409,15 +407,6 @@ public class DateFieldMapper extends NumberFieldMapper {
}; };
} }
@Override
public Query nullValueFilter() {
if (nullValue == null) {
return null;
}
return new ConstantScoreQuery(termQuery(nullValue, null));
}
@Override @Override
protected boolean customBoost() { protected boolean customBoost() {
return true; return true;
@ -431,13 +420,13 @@ public class DateFieldMapper extends NumberFieldMapper {
Object externalValue = context.externalValue(); Object externalValue = context.externalValue();
dateAsString = (String) externalValue; dateAsString = (String) externalValue;
if (dateAsString == null) { if (dateAsString == null) {
dateAsString = nullValue; dateAsString = fieldType.nullValueAsString();
} }
} else { } else {
XContentParser parser = context.parser(); XContentParser parser = context.parser();
XContentParser.Token token = parser.currentToken(); XContentParser.Token token = parser.currentToken();
if (token == XContentParser.Token.VALUE_NULL) { if (token == XContentParser.Token.VALUE_NULL) {
dateAsString = nullValue; dateAsString = fieldType.nullValueAsString();
} else if (token == XContentParser.Token.VALUE_NUMBER) { } else if (token == XContentParser.Token.VALUE_NUMBER) {
dateAsString = parser.text(); dateAsString = parser.text();
} else if (token == XContentParser.Token.START_OBJECT) { } else if (token == XContentParser.Token.START_OBJECT) {
@ -448,7 +437,7 @@ public class DateFieldMapper extends NumberFieldMapper {
} else { } else {
if ("value".equals(currentFieldName) || "_value".equals(currentFieldName)) { if ("value".equals(currentFieldName) || "_value".equals(currentFieldName)) {
if (token == XContentParser.Token.VALUE_NULL) { if (token == XContentParser.Token.VALUE_NULL) {
dateAsString = nullValue; dateAsString = fieldType.nullValueAsString();
} else { } else {
dateAsString = parser.text(); dateAsString = parser.text();
} }
@ -496,9 +485,9 @@ public class DateFieldMapper extends NumberFieldMapper {
return; return;
} }
if (!mergeResult.simulate()) { if (!mergeResult.simulate()) {
this.nullValue = ((DateFieldMapper) mergeWith).nullValue;
this.fieldType = this.fieldType.clone(); this.fieldType = this.fieldType.clone();
fieldType().setDateTimeFormatter(((DateFieldMapper) mergeWith).fieldType().dateTimeFormatter()); fieldType().setDateTimeFormatter(((DateFieldMapper) mergeWith).fieldType().dateTimeFormatter());
this.fieldType.setNullValue(((DateFieldMapper) mergeWith).fieldType().nullValue());
this.fieldType.freeze(); this.fieldType.freeze();
} }
} }
@ -511,8 +500,8 @@ public class DateFieldMapper extends NumberFieldMapper {
builder.field("precision_step", fieldType.numericPrecisionStep()); builder.field("precision_step", fieldType.numericPrecisionStep());
} }
builder.field("format", fieldType().dateTimeFormatter().format()); builder.field("format", fieldType().dateTimeFormatter().format());
if (includeDefaults || nullValue != null) { if (includeDefaults || fieldType.nullValueAsString() != null) {
builder.field("null_value", nullValue); builder.field("null_value", fieldType.nullValueAsString());
} }
if (includeInAll != null) { if (includeInAll != null) {
builder.field("include_in_all", includeInAll); builder.field("include_in_all", includeInAll);

View File

@ -23,10 +23,8 @@ import com.carrotsearch.hppc.DoubleArrayList;
import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.IndexOptions;
import org.apache.lucene.index.Terms; import org.apache.lucene.index.Terms;
import org.apache.lucene.search.ConstantScoreQuery;
import org.apache.lucene.search.NumericRangeQuery; import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRef;
@ -76,28 +74,19 @@ public class DoubleFieldMapper extends NumberFieldMapper {
static { static {
FIELD_TYPE.freeze(); FIELD_TYPE.freeze();
} }
public static final Double NULL_VALUE = null;
} }
public static class Builder extends NumberFieldMapper.Builder<Builder, DoubleFieldMapper> { public static class Builder extends NumberFieldMapper.Builder<Builder, DoubleFieldMapper> {
protected Double nullValue = Defaults.NULL_VALUE;
public Builder(String name) { public Builder(String name) {
super(name, Defaults.FIELD_TYPE, Defaults.PRECISION_STEP_64_BIT); super(name, Defaults.FIELD_TYPE, Defaults.PRECISION_STEP_64_BIT);
builder = this; builder = this;
} }
public Builder nullValue(double nullValue) {
this.nullValue = nullValue;
return this;
}
@Override @Override
public DoubleFieldMapper build(BuilderContext context) { public DoubleFieldMapper build(BuilderContext context) {
setupFieldType(context); setupFieldType(context);
DoubleFieldMapper fieldMapper = new DoubleFieldMapper(fieldType, docValues, nullValue, ignoreMalformed(context), coerce(context), DoubleFieldMapper fieldMapper = new DoubleFieldMapper(fieldType, docValues, ignoreMalformed(context), coerce(context),
fieldDataSettings, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); fieldDataSettings, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo);
fieldMapper.includeInAll(includeInAll); fieldMapper.includeInAll(includeInAll);
return fieldMapper; return fieldMapper;
@ -148,6 +137,11 @@ public class DoubleFieldMapper extends NumberFieldMapper {
return new DoubleFieldType(this); return new DoubleFieldType(this);
} }
@Override
public Double nullValue() {
return (Double)super.nullValue();
}
@Override @Override
public Double value(Object value) { public Double value(Object value) {
if (value == null) { if (value == null) {
@ -198,15 +192,14 @@ public class DoubleFieldMapper extends NumberFieldMapper {
} }
} }
private Double nullValue; protected DoubleFieldMapper(MappedFieldType fieldType, Boolean docValues, Explicit<Boolean> ignoreMalformed, Explicit<Boolean> coerce,
private String nullValueAsString;
protected DoubleFieldMapper(MappedFieldType fieldType, Boolean docValues, Double nullValue, Explicit<Boolean> ignoreMalformed, Explicit<Boolean> coerce,
@Nullable Settings fieldDataSettings, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { @Nullable Settings fieldDataSettings, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) {
super(fieldType, docValues, ignoreMalformed, coerce, fieldDataSettings, indexSettings, multiFields, copyTo); super(fieldType, docValues, ignoreMalformed, coerce, fieldDataSettings, indexSettings, multiFields, copyTo);
this.nullValue = nullValue; }
this.nullValueAsString = nullValue == null ? null : nullValue.toString();
@Override
public DoubleFieldType fieldType() {
return (DoubleFieldType)fieldType;
} }
@Override @Override
@ -219,18 +212,6 @@ public class DoubleFieldMapper extends NumberFieldMapper {
return new FieldDataType("double"); return new FieldDataType("double");
} }
public Query rangeFilter(Double lowerTerm, Double upperTerm, boolean includeLower, boolean includeUpper) {
return NumericRangeQuery.newDoubleRange(fieldType.names().indexName(), fieldType.numericPrecisionStep(), lowerTerm, upperTerm, includeLower, includeUpper);
}
@Override
public Query nullValueFilter() {
if (nullValue == null) {
return null;
}
return new ConstantScoreQuery(termQuery(nullValue, null));
}
@Override @Override
protected boolean customBoost() { protected boolean customBoost() {
return true; return true;
@ -243,17 +224,17 @@ public class DoubleFieldMapper extends NumberFieldMapper {
if (context.externalValueSet()) { if (context.externalValueSet()) {
Object externalValue = context.externalValue(); Object externalValue = context.externalValue();
if (externalValue == null) { if (externalValue == null) {
if (nullValue == null) { if (fieldType().nullValue() == null) {
return; return;
} }
value = nullValue; value = fieldType().nullValue();
} else if (externalValue instanceof String) { } else if (externalValue instanceof String) {
String sExternalValue = (String) externalValue; String sExternalValue = (String) externalValue;
if (sExternalValue.length() == 0) { if (sExternalValue.length() == 0) {
if (nullValue == null) { if (fieldType().nullValue() == null) {
return; return;
} }
value = nullValue; value = fieldType().nullValue();
} else { } else {
value = Double.parseDouble(sExternalValue); value = Double.parseDouble(sExternalValue);
} }
@ -267,17 +248,17 @@ public class DoubleFieldMapper extends NumberFieldMapper {
XContentParser parser = context.parser(); XContentParser parser = context.parser();
if (parser.currentToken() == XContentParser.Token.VALUE_NULL || if (parser.currentToken() == XContentParser.Token.VALUE_NULL ||
(parser.currentToken() == XContentParser.Token.VALUE_STRING && parser.textLength() == 0)) { (parser.currentToken() == XContentParser.Token.VALUE_STRING && parser.textLength() == 0)) {
if (nullValue == null) { if (fieldType().nullValue() == null) {
return; return;
} }
value = nullValue; value = fieldType().nullValue();
if (nullValueAsString != null && (context.includeInAll(includeInAll, this))) { if (fieldType().nullValueAsString() != null && (context.includeInAll(includeInAll, this))) {
context.allEntries().addText(fieldType.names().fullName(), nullValueAsString, boost); context.allEntries().addText(fieldType.names().fullName(), fieldType().nullValueAsString(), boost);
} }
} else if (parser.currentToken() == XContentParser.Token.START_OBJECT) { } else if (parser.currentToken() == XContentParser.Token.START_OBJECT) {
XContentParser.Token token; XContentParser.Token token;
String currentFieldName = null; String currentFieldName = null;
Double objValue = nullValue; Double objValue = fieldType().nullValue();
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) { if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName(); currentFieldName = parser.currentName();
@ -338,8 +319,9 @@ public class DoubleFieldMapper extends NumberFieldMapper {
return; return;
} }
if (!mergeResult.simulate()) { if (!mergeResult.simulate()) {
this.nullValue = ((DoubleFieldMapper) mergeWith).nullValue; this.fieldType = this.fieldType.clone();
this.nullValueAsString = ((DoubleFieldMapper) mergeWith).nullValueAsString; this.fieldType.setNullValue(((DoubleFieldMapper) mergeWith).fieldType().nullValue());
this.fieldType.freeze();
} }
} }
@ -350,8 +332,8 @@ public class DoubleFieldMapper extends NumberFieldMapper {
if (includeDefaults || fieldType.numericPrecisionStep() != Defaults.PRECISION_STEP_64_BIT) { if (includeDefaults || fieldType.numericPrecisionStep() != Defaults.PRECISION_STEP_64_BIT) {
builder.field("precision_step", fieldType.numericPrecisionStep()); builder.field("precision_step", fieldType.numericPrecisionStep());
} }
if (includeDefaults || nullValue != null) { if (includeDefaults || fieldType().nullValue() != null) {
builder.field("null_value", nullValue); builder.field("null_value", fieldType().nullValue());
} }
if (includeInAll != null) { if (includeInAll != null) {
builder.field("include_in_all", includeInAll); builder.field("include_in_all", includeInAll);

View File

@ -77,28 +77,19 @@ public class FloatFieldMapper extends NumberFieldMapper {
static { static {
FIELD_TYPE.freeze(); FIELD_TYPE.freeze();
} }
public static final Float NULL_VALUE = null;
} }
public static class Builder extends NumberFieldMapper.Builder<Builder, FloatFieldMapper> { public static class Builder extends NumberFieldMapper.Builder<Builder, FloatFieldMapper> {
protected Float nullValue = Defaults.NULL_VALUE;
public Builder(String name) { public Builder(String name) {
super(name, Defaults.FIELD_TYPE, Defaults.PRECISION_STEP_32_BIT); super(name, Defaults.FIELD_TYPE, Defaults.PRECISION_STEP_32_BIT);
builder = this; builder = this;
} }
public Builder nullValue(float nullValue) {
this.nullValue = nullValue;
return this;
}
@Override @Override
public FloatFieldMapper build(BuilderContext context) { public FloatFieldMapper build(BuilderContext context) {
setupFieldType(context); setupFieldType(context);
FloatFieldMapper fieldMapper = new FloatFieldMapper(fieldType, docValues, nullValue, ignoreMalformed(context), coerce(context), FloatFieldMapper fieldMapper = new FloatFieldMapper(fieldType, docValues, ignoreMalformed(context), coerce(context),
fieldDataSettings, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); fieldDataSettings, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo);
fieldMapper.includeInAll(includeInAll); fieldMapper.includeInAll(includeInAll);
return fieldMapper; return fieldMapper;
@ -149,6 +140,11 @@ public class FloatFieldMapper extends NumberFieldMapper {
return new FloatFieldType(this); return new FloatFieldType(this);
} }
@Override
public Float nullValue() {
return (Float)super.nullValue();
}
@Override @Override
public Float value(Object value) { public Float value(Object value) {
if (value == null) { if (value == null) {
@ -199,16 +195,15 @@ public class FloatFieldMapper extends NumberFieldMapper {
} }
} }
private Float nullValue;
private String nullValueAsString;
protected FloatFieldMapper(MappedFieldType fieldType, Boolean docValues, protected FloatFieldMapper(MappedFieldType fieldType, Boolean docValues,
Float nullValue, Explicit<Boolean> ignoreMalformed, Explicit<Boolean> coerce, Explicit<Boolean> ignoreMalformed, Explicit<Boolean> coerce,
@Nullable Settings fieldDataSettings, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { @Nullable Settings fieldDataSettings, Settings indexSettings, MultiFields multiFields, CopyTo copyTo) {
super(fieldType, docValues, ignoreMalformed, coerce, fieldDataSettings, indexSettings, multiFields, copyTo); super(fieldType, docValues, ignoreMalformed, coerce, fieldDataSettings, indexSettings, multiFields, copyTo);
this.nullValue = nullValue; }
this.nullValueAsString = nullValue == null ? null : nullValue.toString();
@Override
public FloatFieldType fieldType() {
return (FloatFieldType)fieldType;
} }
@Override @Override
@ -231,14 +226,6 @@ public class FloatFieldMapper extends NumberFieldMapper {
return Float.parseFloat(value.toString()); return Float.parseFloat(value.toString());
} }
@Override
public Query nullValueFilter() {
if (nullValue == null) {
return null;
}
return new ConstantScoreQuery(termQuery(nullValue, null));
}
@Override @Override
protected boolean customBoost() { protected boolean customBoost() {
return true; return true;
@ -251,17 +238,17 @@ public class FloatFieldMapper extends NumberFieldMapper {
if (context.externalValueSet()) { if (context.externalValueSet()) {
Object externalValue = context.externalValue(); Object externalValue = context.externalValue();
if (externalValue == null) { if (externalValue == null) {
if (nullValue == null) { if (fieldType().nullValue() == null) {
return; return;
} }
value = nullValue; value = fieldType().nullValue();
} else if (externalValue instanceof String) { } else if (externalValue instanceof String) {
String sExternalValue = (String) externalValue; String sExternalValue = (String) externalValue;
if (sExternalValue.length() == 0) { if (sExternalValue.length() == 0) {
if (nullValue == null) { if (fieldType().nullValue() == null) {
return; return;
} }
value = nullValue; value = fieldType().nullValue();
} else { } else {
value = Float.parseFloat(sExternalValue); value = Float.parseFloat(sExternalValue);
} }
@ -275,17 +262,17 @@ public class FloatFieldMapper extends NumberFieldMapper {
XContentParser parser = context.parser(); XContentParser parser = context.parser();
if (parser.currentToken() == XContentParser.Token.VALUE_NULL || if (parser.currentToken() == XContentParser.Token.VALUE_NULL ||
(parser.currentToken() == XContentParser.Token.VALUE_STRING && parser.textLength() == 0)) { (parser.currentToken() == XContentParser.Token.VALUE_STRING && parser.textLength() == 0)) {
if (nullValue == null) { if (fieldType().nullValue() == null) {
return; return;
} }
value = nullValue; value = fieldType().nullValue();
if (nullValueAsString != null && (context.includeInAll(includeInAll, this))) { if (fieldType().nullValueAsString() != null && (context.includeInAll(includeInAll, this))) {
context.allEntries().addText(fieldType.names().fullName(), nullValueAsString, boost); context.allEntries().addText(fieldType.names().fullName(), fieldType().nullValueAsString(), boost);
} }
} else if (parser.currentToken() == XContentParser.Token.START_OBJECT) { } else if (parser.currentToken() == XContentParser.Token.START_OBJECT) {
XContentParser.Token token; XContentParser.Token token;
String currentFieldName = null; String currentFieldName = null;
Float objValue = nullValue; Float objValue = fieldType().nullValue();
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) { if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName(); currentFieldName = parser.currentName();
@ -346,8 +333,9 @@ public class FloatFieldMapper extends NumberFieldMapper {
return; return;
} }
if (!mergeResult.simulate()) { if (!mergeResult.simulate()) {
this.nullValue = ((FloatFieldMapper) mergeWith).nullValue; this.fieldType = this.fieldType.clone();
this.nullValueAsString = ((FloatFieldMapper) mergeWith).nullValueAsString; this.fieldType.setNullValue(((FloatFieldMapper) mergeWith).fieldType().nullValue());
this.fieldType.freeze();
} }
} }
@ -359,8 +347,8 @@ public class FloatFieldMapper extends NumberFieldMapper {
if (includeDefaults || fieldType.numericPrecisionStep() != Defaults.PRECISION_STEP_32_BIT) { if (includeDefaults || fieldType.numericPrecisionStep() != Defaults.PRECISION_STEP_32_BIT) {
builder.field("precision_step", fieldType.numericPrecisionStep()); builder.field("precision_step", fieldType.numericPrecisionStep());
} }
if (includeDefaults || nullValue != null) { if (includeDefaults || fieldType().nullValue() != null) {
builder.field("null_value", nullValue); builder.field("null_value", fieldType().nullValue());
} }
if (includeInAll != null) { if (includeInAll != null) {
builder.field("include_in_all", includeInAll); builder.field("include_in_all", includeInAll);

View File

@ -42,6 +42,7 @@ import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.analysis.NamedAnalyzer; import org.elasticsearch.index.analysis.NamedAnalyzer;
import org.elasticsearch.index.analysis.NumericIntegerAnalyzer; import org.elasticsearch.index.analysis.NumericIntegerAnalyzer;
import org.elasticsearch.index.fielddata.FieldDataType; import org.elasticsearch.index.fielddata.FieldDataType;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.Mapper; import org.elasticsearch.index.mapper.Mapper;
import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.index.mapper.MapperParsingException;
@ -72,21 +73,17 @@ public class IntegerFieldMapper extends NumberFieldMapper {
static { static {
FIELD_TYPE.freeze(); FIELD_TYPE.freeze();
} }
public static final Integer NULL_VALUE = null;
} }
public static class Builder extends NumberFieldMapper.Builder<Builder, IntegerFieldMapper> { public static class Builder extends NumberFieldMapper.Builder<Builder, IntegerFieldMapper> {
protected Integer nullValue = Defaults.NULL_VALUE;
public Builder(String name) { public Builder(String name) {
super(name, Defaults.FIELD_TYPE, Defaults.PRECISION_STEP_32_BIT); super(name, Defaults.FIELD_TYPE, Defaults.PRECISION_STEP_32_BIT);
builder = this; builder = this;
} }
public Builder nullValue(int nullValue) { public Builder nullValue(int nullValue) {
this.nullValue = nullValue; this.fieldType.setNullValue(nullValue);
return this; return this;
} }
@ -94,7 +91,7 @@ public class IntegerFieldMapper extends NumberFieldMapper {
public IntegerFieldMapper build(BuilderContext context) { public IntegerFieldMapper build(BuilderContext context) {
setupFieldType(context); setupFieldType(context);
IntegerFieldMapper fieldMapper = new IntegerFieldMapper(fieldType, docValues, IntegerFieldMapper fieldMapper = new IntegerFieldMapper(fieldType, docValues,
nullValue, ignoreMalformed(context), coerce(context), fieldDataSettings, ignoreMalformed(context), coerce(context), fieldDataSettings,
context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo);
fieldMapper.includeInAll(includeInAll); fieldMapper.includeInAll(includeInAll);
return fieldMapper; return fieldMapper;
@ -132,7 +129,7 @@ public class IntegerFieldMapper extends NumberFieldMapper {
} }
} }
static final class IntegerFieldType extends NumberFieldType { public static final class IntegerFieldType extends NumberFieldType {
public IntegerFieldType() {} public IntegerFieldType() {}
@ -145,6 +142,11 @@ public class IntegerFieldMapper extends NumberFieldMapper {
return new IntegerFieldType(this); return new IntegerFieldType(this);
} }
@Override
public Integer nullValue() {
return (Integer)super.nullValue();
}
@Override @Override
public Integer value(Object value) { public Integer value(Object value) {
if (value == null) { if (value == null) {
@ -194,17 +196,16 @@ public class IntegerFieldMapper extends NumberFieldMapper {
} }
} }
private Integer nullValue;
private String nullValueAsString;
protected IntegerFieldMapper(MappedFieldType fieldType, Boolean docValues, protected IntegerFieldMapper(MappedFieldType fieldType, Boolean docValues,
Integer nullValue, Explicit<Boolean> ignoreMalformed, Explicit<Boolean> coerce, Explicit<Boolean> ignoreMalformed, Explicit<Boolean> coerce,
@Nullable Settings fieldDataSettings, @Nullable Settings fieldDataSettings,
Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { Settings indexSettings, MultiFields multiFields, CopyTo copyTo) {
super(fieldType, docValues, ignoreMalformed, coerce, fieldDataSettings, indexSettings, multiFields, copyTo); super(fieldType, docValues, ignoreMalformed, coerce, fieldDataSettings, indexSettings, multiFields, copyTo);
this.nullValue = nullValue; }
this.nullValueAsString = nullValue == null ? null : nullValue.toString();
@Override
public IntegerFieldType fieldType() {
return (IntegerFieldType)fieldType;
} }
@Override @Override
@ -217,8 +218,6 @@ public class IntegerFieldMapper extends NumberFieldMapper {
return new FieldDataType("int"); return new FieldDataType("int");
} }
private static int parseValue(Object value) { private static int parseValue(Object value) {
if (value instanceof Number) { if (value instanceof Number) {
return ((Number) value).intValue(); return ((Number) value).intValue();
@ -229,14 +228,6 @@ public class IntegerFieldMapper extends NumberFieldMapper {
return Integer.parseInt(value.toString()); return Integer.parseInt(value.toString());
} }
@Override
public Query nullValueFilter() {
if (nullValue == null) {
return null;
}
return new ConstantScoreQuery(termQuery(nullValue, null));
}
@Override @Override
protected boolean customBoost() { protected boolean customBoost() {
return true; return true;
@ -249,17 +240,17 @@ public class IntegerFieldMapper extends NumberFieldMapper {
if (context.externalValueSet()) { if (context.externalValueSet()) {
Object externalValue = context.externalValue(); Object externalValue = context.externalValue();
if (externalValue == null) { if (externalValue == null) {
if (nullValue == null) { if (fieldType.nullValue() == null) {
return; return;
} }
value = nullValue; value = fieldType().nullValue();
} else if (externalValue instanceof String) { } else if (externalValue instanceof String) {
String sExternalValue = (String) externalValue; String sExternalValue = (String) externalValue;
if (sExternalValue.length() == 0) { if (sExternalValue.length() == 0) {
if (nullValue == null) { if (fieldType().nullValue() == null) {
return; return;
} }
value = nullValue; value = fieldType().nullValue();
} else { } else {
value = Integer.parseInt(sExternalValue); value = Integer.parseInt(sExternalValue);
} }
@ -273,17 +264,17 @@ public class IntegerFieldMapper extends NumberFieldMapper {
XContentParser parser = context.parser(); XContentParser parser = context.parser();
if (parser.currentToken() == XContentParser.Token.VALUE_NULL || if (parser.currentToken() == XContentParser.Token.VALUE_NULL ||
(parser.currentToken() == XContentParser.Token.VALUE_STRING && parser.textLength() == 0)) { (parser.currentToken() == XContentParser.Token.VALUE_STRING && parser.textLength() == 0)) {
if (nullValue == null) { if (fieldType.nullValue() == null) {
return; return;
} }
value = nullValue; value = fieldType().nullValue();
if (nullValueAsString != null && (context.includeInAll(includeInAll, this))) { if (fieldType.nullValueAsString() != null && (context.includeInAll(includeInAll, this))) {
context.allEntries().addText(fieldType.names().fullName(), nullValueAsString, boost); context.allEntries().addText(fieldType.names().fullName(), fieldType.nullValueAsString(), boost);
} }
} else if (parser.currentToken() == XContentParser.Token.START_OBJECT) { } else if (parser.currentToken() == XContentParser.Token.START_OBJECT) {
XContentParser.Token token; XContentParser.Token token;
String currentFieldName = null; String currentFieldName = null;
Integer objValue = nullValue; Integer objValue = fieldType().nullValue();
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) { if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName(); currentFieldName = parser.currentName();
@ -316,7 +307,7 @@ public class IntegerFieldMapper extends NumberFieldMapper {
protected void addIntegerFields(ParseContext context, List<Field> fields, int value, float boost) { protected void addIntegerFields(ParseContext context, List<Field> fields, int value, float boost) {
if (fieldType.indexOptions() != IndexOptions.NONE || fieldType.stored()) { if (fieldType.indexOptions() != IndexOptions.NONE || fieldType.stored()) {
CustomIntegerNumericField field = new CustomIntegerNumericField(this, value, (NumberFieldType)fieldType); CustomIntegerNumericField field = new CustomIntegerNumericField(this, value, fieldType);
field.setBoost(boost); field.setBoost(boost);
fields.add(field); fields.add(field);
} }
@ -325,10 +316,6 @@ public class IntegerFieldMapper extends NumberFieldMapper {
} }
} }
protected Integer nullValue() {
return nullValue;
}
@Override @Override
protected String contentType() { protected String contentType() {
return CONTENT_TYPE; return CONTENT_TYPE;
@ -341,8 +328,9 @@ public class IntegerFieldMapper extends NumberFieldMapper {
return; return;
} }
if (!mergeResult.simulate()) { if (!mergeResult.simulate()) {
this.nullValue = ((IntegerFieldMapper) mergeWith).nullValue; this.fieldType = this.fieldType.clone();
this.nullValueAsString = ((IntegerFieldMapper) mergeWith).nullValueAsString; this.fieldType.setNullValue(((FieldMapper)mergeWith).fieldType().nullValue());
this.fieldType.freeze();
} }
} }
@ -353,8 +341,8 @@ public class IntegerFieldMapper extends NumberFieldMapper {
if (includeDefaults || fieldType.numericPrecisionStep() != Defaults.PRECISION_STEP_32_BIT) { if (includeDefaults || fieldType.numericPrecisionStep() != Defaults.PRECISION_STEP_32_BIT) {
builder.field("precision_step", fieldType.numericPrecisionStep()); builder.field("precision_step", fieldType.numericPrecisionStep());
} }
if (includeDefaults || nullValue != null) { if (includeDefaults || fieldType.nullValue() != null) {
builder.field("null_value", nullValue); builder.field("null_value", fieldType.nullValue());
} }
if (includeInAll != null) { if (includeInAll != null) {
builder.field("include_in_all", includeInAll); builder.field("include_in_all", includeInAll);

View File

@ -72,28 +72,24 @@ public class LongFieldMapper extends NumberFieldMapper {
static { static {
FIELD_TYPE.freeze(); FIELD_TYPE.freeze();
} }
public static final Long NULL_VALUE = null;
} }
public static class Builder extends NumberFieldMapper.Builder<Builder, LongFieldMapper> { public static class Builder extends NumberFieldMapper.Builder<Builder, LongFieldMapper> {
protected Long nullValue = Defaults.NULL_VALUE;
public Builder(String name) { public Builder(String name) {
super(name, Defaults.FIELD_TYPE, Defaults.PRECISION_STEP_64_BIT); super(name, Defaults.FIELD_TYPE, Defaults.PRECISION_STEP_64_BIT);
builder = this; builder = this;
} }
public Builder nullValue(long nullValue) { public Builder nullValue(long nullValue) {
this.nullValue = nullValue; this.fieldType.setNullValue(nullValue);
return this; return this;
} }
@Override @Override
public LongFieldMapper build(BuilderContext context) { public LongFieldMapper build(BuilderContext context) {
setupFieldType(context); setupFieldType(context);
LongFieldMapper fieldMapper = new LongFieldMapper(fieldType, docValues, nullValue, LongFieldMapper fieldMapper = new LongFieldMapper(fieldType, docValues,
ignoreMalformed(context), coerce(context), fieldDataSettings, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); ignoreMalformed(context), coerce(context), fieldDataSettings, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo);
fieldMapper.includeInAll(includeInAll); fieldMapper.includeInAll(includeInAll);
return fieldMapper; return fieldMapper;
@ -144,6 +140,11 @@ public class LongFieldMapper extends NumberFieldMapper {
return new LongFieldType(this); return new LongFieldType(this);
} }
@Override
public Long nullValue() {
return (Long)super.nullValue();
}
@Override @Override
public Long value(Object value) { public Long value(Object value) {
if (value == null) { if (value == null) {
@ -193,17 +194,16 @@ public class LongFieldMapper extends NumberFieldMapper {
} }
} }
private Long nullValue;
private String nullValueAsString;
protected LongFieldMapper(MappedFieldType fieldType, Boolean docValues, protected LongFieldMapper(MappedFieldType fieldType, Boolean docValues,
Long nullValue, Explicit<Boolean> ignoreMalformed, Explicit<Boolean> coerce, Explicit<Boolean> ignoreMalformed, Explicit<Boolean> coerce,
@Nullable Settings fieldDataSettings, @Nullable Settings fieldDataSettings,
Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { Settings indexSettings, MultiFields multiFields, CopyTo copyTo) {
super(fieldType, docValues, ignoreMalformed, coerce, fieldDataSettings, indexSettings, multiFields, copyTo); super(fieldType, docValues, ignoreMalformed, coerce, fieldDataSettings, indexSettings, multiFields, copyTo);
this.nullValue = nullValue; }
this.nullValueAsString = nullValue == null ? null : nullValue.toString();
@Override
public LongFieldType fieldType() {
return (LongFieldType)fieldType;
} }
@Override @Override
@ -216,14 +216,6 @@ public class LongFieldMapper extends NumberFieldMapper {
return new FieldDataType("long"); return new FieldDataType("long");
} }
@Override
public Query nullValueFilter() {
if (nullValue == null) {
return null;
}
return new ConstantScoreQuery(termQuery(nullValue, null));
}
@Override @Override
protected boolean customBoost() { protected boolean customBoost() {
return true; return true;
@ -236,17 +228,17 @@ public class LongFieldMapper extends NumberFieldMapper {
if (context.externalValueSet()) { if (context.externalValueSet()) {
Object externalValue = context.externalValue(); Object externalValue = context.externalValue();
if (externalValue == null) { if (externalValue == null) {
if (nullValue == null) { if (fieldType().nullValue() == null) {
return; return;
} }
value = nullValue; value = fieldType().nullValue();
} else if (externalValue instanceof String) { } else if (externalValue instanceof String) {
String sExternalValue = (String) externalValue; String sExternalValue = (String) externalValue;
if (sExternalValue.length() == 0) { if (sExternalValue.length() == 0) {
if (nullValue == null) { if (fieldType().nullValue() == null) {
return; return;
} }
value = nullValue; value = fieldType().nullValue();
} else { } else {
value = Long.parseLong(sExternalValue); value = Long.parseLong(sExternalValue);
} }
@ -260,17 +252,17 @@ public class LongFieldMapper extends NumberFieldMapper {
XContentParser parser = context.parser(); XContentParser parser = context.parser();
if (parser.currentToken() == XContentParser.Token.VALUE_NULL || if (parser.currentToken() == XContentParser.Token.VALUE_NULL ||
(parser.currentToken() == XContentParser.Token.VALUE_STRING && parser.textLength() == 0)) { (parser.currentToken() == XContentParser.Token.VALUE_STRING && parser.textLength() == 0)) {
if (nullValue == null) { if (fieldType().nullValue() == null) {
return; return;
} }
value = nullValue; value = fieldType().nullValue();
if (nullValueAsString != null && (context.includeInAll(includeInAll, this))) { if (fieldType().nullValueAsString() != null && (context.includeInAll(includeInAll, this))) {
context.allEntries().addText(fieldType.names().fullName(), nullValueAsString, boost); context.allEntries().addText(fieldType.names().fullName(), fieldType().nullValueAsString(), boost);
} }
} else if (parser.currentToken() == XContentParser.Token.START_OBJECT) { } else if (parser.currentToken() == XContentParser.Token.START_OBJECT) {
XContentParser.Token token; XContentParser.Token token;
String currentFieldName = null; String currentFieldName = null;
Long objValue = nullValue; Long objValue = fieldType().nullValue();
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) { if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName(); currentFieldName = parser.currentName();
@ -320,8 +312,9 @@ public class LongFieldMapper extends NumberFieldMapper {
return; return;
} }
if (!mergeResult.simulate()) { if (!mergeResult.simulate()) {
this.nullValue = ((LongFieldMapper) mergeWith).nullValue; this.fieldType = this.fieldType.clone();
this.nullValueAsString = ((LongFieldMapper) mergeWith).nullValueAsString; this.fieldType.setNullValue(((LongFieldMapper) mergeWith).fieldType().nullValue());
this.fieldType.freeze();
} }
} }
@ -332,8 +325,8 @@ public class LongFieldMapper extends NumberFieldMapper {
if (includeDefaults || fieldType.numericPrecisionStep() != Defaults.PRECISION_STEP_64_BIT) { if (includeDefaults || fieldType.numericPrecisionStep() != Defaults.PRECISION_STEP_64_BIT) {
builder.field("precision_step", fieldType.numericPrecisionStep()); builder.field("precision_step", fieldType.numericPrecisionStep());
} }
if (includeDefaults || nullValue != null) { if (includeDefaults || fieldType().nullValue() != null) {
builder.field("null_value", nullValue); builder.field("null_value", fieldType().nullValue());
} }
if (includeInAll != null) { if (includeInAll != null) {
builder.field("include_in_all", includeInAll); builder.field("include_in_all", includeInAll);

View File

@ -60,7 +60,7 @@ public class Murmur3FieldMapper extends LongFieldMapper {
@Override @Override
public Murmur3FieldMapper build(BuilderContext context) { public Murmur3FieldMapper build(BuilderContext context) {
setupFieldType(context); setupFieldType(context);
Murmur3FieldMapper fieldMapper = new Murmur3FieldMapper(fieldType, docValues, null, Murmur3FieldMapper fieldMapper = new Murmur3FieldMapper(fieldType, docValues,
ignoreMalformed(context), coerce(context), ignoreMalformed(context), coerce(context),
fieldDataSettings, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); fieldDataSettings, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo);
fieldMapper.includeInAll(includeInAll); fieldMapper.includeInAll(includeInAll);
@ -105,10 +105,10 @@ public class Murmur3FieldMapper extends LongFieldMapper {
} }
protected Murmur3FieldMapper(MappedFieldType fieldType, Boolean docValues, protected Murmur3FieldMapper(MappedFieldType fieldType, Boolean docValues,
Long nullValue, Explicit<Boolean> ignoreMalformed, Explicit<Boolean> coerce, Explicit<Boolean> ignoreMalformed, Explicit<Boolean> coerce,
@Nullable Settings fieldDataSettings, @Nullable Settings fieldDataSettings,
Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { Settings indexSettings, MultiFields multiFields, CopyTo copyTo) {
super(fieldType, docValues, nullValue, ignoreMalformed, coerce, super(fieldType, docValues, ignoreMalformed, coerce,
fieldDataSettings, indexSettings, multiFields, copyTo); fieldDataSettings, indexSettings, multiFields, copyTo);
} }

View File

@ -73,28 +73,19 @@ public class ShortFieldMapper extends NumberFieldMapper {
static { static {
FIELD_TYPE.freeze(); FIELD_TYPE.freeze();
} }
public static final Short NULL_VALUE = null;
} }
public static class Builder extends NumberFieldMapper.Builder<Builder, ShortFieldMapper> { public static class Builder extends NumberFieldMapper.Builder<Builder, ShortFieldMapper> {
protected Short nullValue = Defaults.NULL_VALUE;
public Builder(String name) { public Builder(String name) {
super(name, Defaults.FIELD_TYPE, DEFAULT_PRECISION_STEP); super(name, Defaults.FIELD_TYPE, DEFAULT_PRECISION_STEP);
builder = this; builder = this;
} }
public Builder nullValue(short nullValue) {
this.nullValue = nullValue;
return this;
}
@Override @Override
public ShortFieldMapper build(BuilderContext context) { public ShortFieldMapper build(BuilderContext context) {
setupFieldType(context); setupFieldType(context);
ShortFieldMapper fieldMapper = new ShortFieldMapper(fieldType, docValues, nullValue, ShortFieldMapper fieldMapper = new ShortFieldMapper(fieldType, docValues,
ignoreMalformed(context), coerce(context), fieldDataSettings, ignoreMalformed(context), coerce(context), fieldDataSettings,
context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo);
fieldMapper.includeInAll(includeInAll); fieldMapper.includeInAll(includeInAll);
@ -147,6 +138,11 @@ public class ShortFieldMapper extends NumberFieldMapper {
return new ShortFieldType(this); return new ShortFieldType(this);
} }
@Override
public Short nullValue() {
return (Short)super.nullValue();
}
@Override @Override
public Short value(Object value) { public Short value(Object value) {
if (value == null) { if (value == null) {
@ -196,18 +192,17 @@ public class ShortFieldMapper extends NumberFieldMapper {
} }
} }
private Short nullValue;
private String nullValueAsString;
protected ShortFieldMapper(MappedFieldType fieldType, Boolean docValues, protected ShortFieldMapper(MappedFieldType fieldType, Boolean docValues,
Short nullValue, Explicit<Boolean> ignoreMalformed, Explicit<Boolean> coerce, Explicit<Boolean> ignoreMalformed, Explicit<Boolean> coerce,
@Nullable Settings fieldDataSettings, @Nullable Settings fieldDataSettings,
Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { Settings indexSettings, MultiFields multiFields, CopyTo copyTo) {
super(fieldType, docValues, ignoreMalformed, coerce, super(fieldType, docValues, ignoreMalformed, coerce,
fieldDataSettings, indexSettings, multiFields, copyTo); fieldDataSettings, indexSettings, multiFields, copyTo);
this.nullValue = nullValue; }
this.nullValueAsString = nullValue == null ? null : nullValue.toString();
@Override
public ShortFieldType fieldType() {
return (ShortFieldType)fieldType;
} }
@Override @Override
@ -230,14 +225,6 @@ public class ShortFieldMapper extends NumberFieldMapper {
return Short.parseShort(value.toString()); return Short.parseShort(value.toString());
} }
@Override
public Query nullValueFilter() {
if (nullValue == null) {
return null;
}
return new ConstantScoreQuery(termQuery(nullValue, null));
}
@Override @Override
protected boolean customBoost() { protected boolean customBoost() {
return true; return true;
@ -250,17 +237,17 @@ public class ShortFieldMapper extends NumberFieldMapper {
if (context.externalValueSet()) { if (context.externalValueSet()) {
Object externalValue = context.externalValue(); Object externalValue = context.externalValue();
if (externalValue == null) { if (externalValue == null) {
if (nullValue == null) { if (fieldType().nullValue() == null) {
return; return;
} }
value = nullValue; value = fieldType().nullValue();
} else if (externalValue instanceof String) { } else if (externalValue instanceof String) {
String sExternalValue = (String) externalValue; String sExternalValue = (String) externalValue;
if (sExternalValue.length() == 0) { if (sExternalValue.length() == 0) {
if (nullValue == null) { if (fieldType().nullValue() == null) {
return; return;
} }
value = nullValue; value = fieldType().nullValue();
} else { } else {
value = Short.parseShort(sExternalValue); value = Short.parseShort(sExternalValue);
} }
@ -274,17 +261,17 @@ public class ShortFieldMapper extends NumberFieldMapper {
XContentParser parser = context.parser(); XContentParser parser = context.parser();
if (parser.currentToken() == XContentParser.Token.VALUE_NULL || if (parser.currentToken() == XContentParser.Token.VALUE_NULL ||
(parser.currentToken() == XContentParser.Token.VALUE_STRING && parser.textLength() == 0)) { (parser.currentToken() == XContentParser.Token.VALUE_STRING && parser.textLength() == 0)) {
if (nullValue == null) { if (fieldType().nullValue() == null) {
return; return;
} }
value = nullValue; value = fieldType().nullValue();
if (nullValueAsString != null && (context.includeInAll(includeInAll, this))) { if (fieldType().nullValueAsString() != null && (context.includeInAll(includeInAll, this))) {
context.allEntries().addText(fieldType.names().fullName(), nullValueAsString, boost); context.allEntries().addText(fieldType.names().fullName(), fieldType().nullValueAsString(), boost);
} }
} else if (parser.currentToken() == XContentParser.Token.START_OBJECT) { } else if (parser.currentToken() == XContentParser.Token.START_OBJECT) {
XContentParser.Token token; XContentParser.Token token;
String currentFieldName = null; String currentFieldName = null;
Short objValue = nullValue; Short objValue = fieldType().nullValue();
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) { if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName(); currentFieldName = parser.currentName();
@ -334,8 +321,9 @@ public class ShortFieldMapper extends NumberFieldMapper {
return; return;
} }
if (!mergeResult.simulate()) { if (!mergeResult.simulate()) {
this.nullValue = ((ShortFieldMapper) mergeWith).nullValue; this.fieldType = this.fieldType.clone();
this.nullValueAsString = ((ShortFieldMapper) mergeWith).nullValueAsString; this.fieldType.setNullValue(((ShortFieldMapper) mergeWith).fieldType().nullValue());
this.fieldType.freeze();
} }
} }
@ -346,8 +334,8 @@ public class ShortFieldMapper extends NumberFieldMapper {
if (includeDefaults || fieldType.numericPrecisionStep() != DEFAULT_PRECISION_STEP) { if (includeDefaults || fieldType.numericPrecisionStep() != DEFAULT_PRECISION_STEP) {
builder.field("precision_step", fieldType.numericPrecisionStep()); builder.field("precision_step", fieldType.numericPrecisionStep());
} }
if (includeDefaults || nullValue != null) { if (includeDefaults || fieldType().nullValue() != null) {
builder.field("null_value", nullValue); builder.field("null_value", fieldType().nullValue());
} }
if (includeInAll != null) { if (includeInAll != null) {
builder.field("include_in_all", includeInAll); builder.field("include_in_all", includeInAll);

View File

@ -83,11 +83,6 @@ public class StringFieldMapper extends AbstractFieldMapper implements AllFieldMa
builder = this; builder = this;
} }
public Builder nullValue(String nullValue) {
this.nullValue = nullValue;
return this;
}
@Override @Override
public Builder searchAnalyzer(NamedAnalyzer searchAnalyzer) { public Builder searchAnalyzer(NamedAnalyzer searchAnalyzer) {
super.searchAnalyzer(searchAnalyzer); super.searchAnalyzer(searchAnalyzer);
@ -135,7 +130,7 @@ public class StringFieldMapper extends AbstractFieldMapper implements AllFieldMa
defaultFieldType.freeze(); defaultFieldType.freeze();
setupFieldType(context); setupFieldType(context);
StringFieldMapper fieldMapper = new StringFieldMapper( StringFieldMapper fieldMapper = new StringFieldMapper(
fieldType, defaultFieldType, docValues, nullValue, positionOffsetGap, ignoreAbove, fieldType, defaultFieldType, docValues, positionOffsetGap, ignoreAbove,
fieldDataSettings, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); fieldDataSettings, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo);
fieldMapper.includeInAll(includeInAll); fieldMapper.includeInAll(includeInAll);
return fieldMapper; return fieldMapper;
@ -210,23 +205,29 @@ public class StringFieldMapper extends AbstractFieldMapper implements AllFieldMa
} }
return value.toString(); return value.toString();
} }
@Override
public Query nullValueQuery() {
if (nullValue() == null) {
return null;
}
return termQuery(nullValue(), null);
}
} }
private String nullValue;
private Boolean includeInAll; private Boolean includeInAll;
private int positionOffsetGap; private int positionOffsetGap;
private int ignoreAbove; private int ignoreAbove;
private final MappedFieldType defaultFieldType; private final MappedFieldType defaultFieldType;
protected StringFieldMapper(MappedFieldType fieldType, MappedFieldType defaultFieldType, Boolean docValues, protected StringFieldMapper(MappedFieldType fieldType, MappedFieldType defaultFieldType, Boolean docValues,
String nullValue, int positionOffsetGap, int ignoreAbove, @Nullable Settings fieldDataSettings, int positionOffsetGap, int ignoreAbove, @Nullable Settings fieldDataSettings,
Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { Settings indexSettings, MultiFields multiFields, CopyTo copyTo) {
super(fieldType, docValues, fieldDataSettings, indexSettings, multiFields, copyTo); super(fieldType, docValues, fieldDataSettings, indexSettings, multiFields, copyTo);
if (fieldType.tokenized() && fieldType.indexOptions() != NONE && fieldType().hasDocValues()) { if (fieldType.tokenized() && fieldType.indexOptions() != NONE && fieldType().hasDocValues()) {
throw new MapperParsingException("Field [" + fieldType.names().fullName() + "] cannot be analyzed and have doc values"); throw new MapperParsingException("Field [" + fieldType.names().fullName() + "] cannot be analyzed and have doc values");
} }
this.defaultFieldType = defaultFieldType; this.defaultFieldType = defaultFieldType;
this.nullValue = nullValue;
this.positionOffsetGap = positionOffsetGap; this.positionOffsetGap = positionOffsetGap;
this.ignoreAbove = ignoreAbove; this.ignoreAbove = ignoreAbove;
} }
@ -273,17 +274,9 @@ public class StringFieldMapper extends AbstractFieldMapper implements AllFieldMa
return ignoreAbove; return ignoreAbove;
} }
@Override
public Query nullValueFilter() {
if (nullValue == null) {
return null;
}
return termQuery(nullValue, null);
}
@Override @Override
protected void parseCreateField(ParseContext context, List<Field> fields) throws IOException { protected void parseCreateField(ParseContext context, List<Field> fields) throws IOException {
ValueAndBoost valueAndBoost = parseCreateFieldForString(context, nullValue, fieldType.boost()); ValueAndBoost valueAndBoost = parseCreateFieldForString(context, fieldType().nullValueAsString(), fieldType.boost());
if (valueAndBoost.value() == null) { if (valueAndBoost.value() == null) {
return; return;
} }
@ -359,8 +352,10 @@ public class StringFieldMapper extends AbstractFieldMapper implements AllFieldMa
} }
if (!mergeResult.simulate()) { if (!mergeResult.simulate()) {
this.includeInAll = ((StringFieldMapper) mergeWith).includeInAll; this.includeInAll = ((StringFieldMapper) mergeWith).includeInAll;
this.nullValue = ((StringFieldMapper) mergeWith).nullValue;
this.ignoreAbove = ((StringFieldMapper) mergeWith).ignoreAbove; this.ignoreAbove = ((StringFieldMapper) mergeWith).ignoreAbove;
this.fieldType = this.fieldType.clone();
this.fieldType.setNullValue(((StringFieldMapper) mergeWith).fieldType().nullValue());
this.fieldType.freeze();
} }
} }
@ -368,8 +363,8 @@ public class StringFieldMapper extends AbstractFieldMapper implements AllFieldMa
protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException { protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException {
super.doXContentBody(builder, includeDefaults, params); super.doXContentBody(builder, includeDefaults, params);
if (includeDefaults || nullValue != null) { if (includeDefaults || fieldType().nullValue() != null) {
builder.field("null_value", nullValue); builder.field("null_value", fieldType().nullValue());
} }
if (includeInAll != null) { if (includeInAll != null) {
builder.field("include_in_all", includeInAll); builder.field("include_in_all", includeInAll);

View File

@ -60,7 +60,6 @@ public class TokenCountFieldMapper extends IntegerFieldMapper {
} }
public static class Builder extends NumberFieldMapper.Builder<Builder, TokenCountFieldMapper> { public static class Builder extends NumberFieldMapper.Builder<Builder, TokenCountFieldMapper> {
private Integer nullValue = Defaults.NULL_VALUE;
private NamedAnalyzer analyzer; private NamedAnalyzer analyzer;
public Builder(String name) { public Builder(String name) {
@ -68,11 +67,6 @@ public class TokenCountFieldMapper extends IntegerFieldMapper {
builder = this; builder = this;
} }
public Builder nullValue(int nullValue) {
this.nullValue = nullValue;
return this;
}
public Builder analyzer(NamedAnalyzer analyzer) { public Builder analyzer(NamedAnalyzer analyzer) {
this.analyzer = analyzer; this.analyzer = analyzer;
return this; return this;
@ -85,7 +79,7 @@ public class TokenCountFieldMapper extends IntegerFieldMapper {
@Override @Override
public TokenCountFieldMapper build(BuilderContext context) { public TokenCountFieldMapper build(BuilderContext context) {
setupFieldType(context); setupFieldType(context);
TokenCountFieldMapper fieldMapper = new TokenCountFieldMapper(fieldType, docValues, nullValue, TokenCountFieldMapper fieldMapper = new TokenCountFieldMapper(fieldType, docValues,
ignoreMalformed(context), coerce(context), fieldDataSettings, context.indexSettings(), ignoreMalformed(context), coerce(context), fieldDataSettings, context.indexSettings(),
analyzer, multiFieldsBuilder.build(this, context), copyTo); analyzer, multiFieldsBuilder.build(this, context), copyTo);
fieldMapper.includeInAll(includeInAll); fieldMapper.includeInAll(includeInAll);
@ -134,26 +128,24 @@ public class TokenCountFieldMapper extends IntegerFieldMapper {
private NamedAnalyzer analyzer; private NamedAnalyzer analyzer;
protected TokenCountFieldMapper(MappedFieldType fieldType, Boolean docValues, Integer nullValue, protected TokenCountFieldMapper(MappedFieldType fieldType, Boolean docValues, Explicit<Boolean> ignoreMalformed,
Explicit<Boolean> ignoreMalformed, Explicit<Boolean> coerce, Settings fieldDataSettings, Settings indexSettings, NamedAnalyzer analyzer, Explicit<Boolean> coerce, Settings fieldDataSettings, Settings indexSettings,
MultiFields multiFields, CopyTo copyTo) { NamedAnalyzer analyzer, MultiFields multiFields, CopyTo copyTo) {
super(fieldType, docValues, nullValue, ignoreMalformed, coerce, super(fieldType, docValues, ignoreMalformed, coerce, fieldDataSettings, indexSettings, multiFields, copyTo);
fieldDataSettings, indexSettings, multiFields, copyTo);
this.analyzer = analyzer; this.analyzer = analyzer;
} }
@Override @Override
protected void parseCreateField(ParseContext context, List<Field> fields) throws IOException { protected void parseCreateField(ParseContext context, List<Field> fields) throws IOException {
ValueAndBoost valueAndBoost = StringFieldMapper.parseCreateFieldForString(context, null /* Out null value is an int so we convert*/, fieldType.boost()); ValueAndBoost valueAndBoost = StringFieldMapper.parseCreateFieldForString(context, null /* Out null value is an int so we convert*/, fieldType.boost());
if (valueAndBoost.value() == null && nullValue() == null) { if (valueAndBoost.value() == null && fieldType().nullValue() == null) {
return; return;
} }
if (fieldType.indexOptions() != NONE || fieldType.stored() || fieldType().hasDocValues()) { if (fieldType.indexOptions() != NONE || fieldType.stored() || fieldType().hasDocValues()) {
int count; int count;
if (valueAndBoost.value() == null) { if (valueAndBoost.value() == null) {
count = nullValue(); count = fieldType().nullValue();
} else { } else {
count = countPositions(analyzer.analyzer().tokenStream(fieldType().names().shortName(), valueAndBoost.value())); count = countPositions(analyzer.analyzer().tokenStream(fieldType().names().shortName(), valueAndBoost.value()));
} }

View File

@ -123,7 +123,7 @@ public class SizeFieldMapper extends IntegerFieldMapper implements RootMapper {
} }
public SizeFieldMapper(EnabledAttributeMapper enabled, MappedFieldType fieldType, @Nullable Settings fieldDataSettings, Settings indexSettings) { public SizeFieldMapper(EnabledAttributeMapper enabled, MappedFieldType fieldType, @Nullable Settings fieldDataSettings, Settings indexSettings) {
super(fieldType, false, Defaults.NULL_VALUE, super(fieldType, false,
Defaults.IGNORE_MALFORMED, Defaults.COERCE, fieldDataSettings, Defaults.IGNORE_MALFORMED, Defaults.COERCE, fieldDataSettings,
indexSettings, MultiFields.empty(), null); indexSettings, MultiFields.empty(), null);
this.enabledState = enabled; this.enabledState = enabled;

View File

@ -174,7 +174,7 @@ public class TTLFieldMapper extends LongFieldMapper implements RootMapper {
protected TTLFieldMapper(MappedFieldType fieldType, EnabledAttributeMapper enabled, long defaultTTL, Explicit<Boolean> ignoreMalformed, protected TTLFieldMapper(MappedFieldType fieldType, EnabledAttributeMapper enabled, long defaultTTL, Explicit<Boolean> ignoreMalformed,
Explicit<Boolean> coerce, @Nullable Settings fieldDataSettings, Settings indexSettings) { Explicit<Boolean> coerce, @Nullable Settings fieldDataSettings, Settings indexSettings) {
super(fieldType, false, Defaults.NULL_VALUE, ignoreMalformed, coerce, super(fieldType, false, ignoreMalformed, coerce,
fieldDataSettings, indexSettings, MultiFields.empty(), null); fieldDataSettings, indexSettings, MultiFields.empty(), null);
this.enabledState = enabled; this.enabledState = enabled;
this.defaultTTL = defaultTTL; this.defaultTTL = defaultTTL;

View File

@ -251,7 +251,7 @@ public class TimestampFieldMapper extends DateFieldMapper implements RootMapper
protected TimestampFieldMapper(MappedFieldType fieldType, Boolean docValues, EnabledAttributeMapper enabledState, String path, protected TimestampFieldMapper(MappedFieldType fieldType, Boolean docValues, EnabledAttributeMapper enabledState, String path,
String defaultTimestamp, Boolean ignoreMissing, Explicit<Boolean> ignoreMalformed, Explicit<Boolean> coerce, String defaultTimestamp, Boolean ignoreMissing, Explicit<Boolean> ignoreMalformed, Explicit<Boolean> coerce,
@Nullable Settings fieldDataSettings, Settings indexSettings) { @Nullable Settings fieldDataSettings, Settings indexSettings) {
super(fieldType, docValues, Defaults.NULL_VALUE, ignoreMalformed, coerce, fieldDataSettings, super(fieldType, docValues, ignoreMalformed, coerce, fieldDataSettings,
indexSettings, MultiFields.empty(), null); indexSettings, MultiFields.empty(), null);
this.enabledState = enabledState; this.enabledState = enabledState;
this.path = path; this.path = path;

View File

@ -115,15 +115,10 @@ public class IpFieldMapper extends NumberFieldMapper {
builder = this; builder = this;
} }
public Builder nullValue(String nullValue) {
this.nullValue = nullValue;
return this;
}
@Override @Override
public IpFieldMapper build(BuilderContext context) { public IpFieldMapper build(BuilderContext context) {
setupFieldType(context); setupFieldType(context);
IpFieldMapper fieldMapper = new IpFieldMapper(fieldType, docValues, nullValue, ignoreMalformed(context), coerce(context), IpFieldMapper fieldMapper = new IpFieldMapper(fieldType, docValues, ignoreMalformed(context), coerce(context),
fieldDataSettings, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo); fieldDataSettings, context.indexSettings(), multiFieldsBuilder.build(this, context), copyTo);
fieldMapper.includeInAll(includeInAll); fieldMapper.includeInAll(includeInAll);
return fieldMapper; return fieldMapper;
@ -232,15 +227,12 @@ public class IpFieldMapper extends NumberFieldMapper {
} }
} }
private String nullValue;
protected IpFieldMapper(MappedFieldType fieldType, Boolean docValues, protected IpFieldMapper(MappedFieldType fieldType, Boolean docValues,
String nullValue, Explicit<Boolean> ignoreMalformed, Explicit<Boolean> coerce, Explicit<Boolean> ignoreMalformed, Explicit<Boolean> coerce,
@Nullable Settings fieldDataSettings, @Nullable Settings fieldDataSettings,
Settings indexSettings, MultiFields multiFields, CopyTo copyTo) { Settings indexSettings, MultiFields multiFields, CopyTo copyTo) {
super(fieldType, docValues, ignoreMalformed, coerce, super(fieldType, docValues, ignoreMalformed, coerce,
fieldDataSettings, indexSettings, multiFields, copyTo); fieldDataSettings, indexSettings, multiFields, copyTo);
this.nullValue = nullValue;
} }
@Override @Override
@ -263,25 +255,17 @@ public class IpFieldMapper extends NumberFieldMapper {
return ipToLong(value.toString()); return ipToLong(value.toString());
} }
@Override
public Query nullValueFilter() {
if (nullValue == null) {
return null;
}
return new ConstantScoreQuery(termQuery(nullValue, null));
}
@Override @Override
protected void innerParseCreateField(ParseContext context, List<Field> fields) throws IOException { protected void innerParseCreateField(ParseContext context, List<Field> fields) throws IOException {
String ipAsString; String ipAsString;
if (context.externalValueSet()) { if (context.externalValueSet()) {
ipAsString = (String) context.externalValue(); ipAsString = (String) context.externalValue();
if (ipAsString == null) { if (ipAsString == null) {
ipAsString = nullValue; ipAsString = fieldType().nullValueAsString();
} }
} else { } else {
if (context.parser().currentToken() == XContentParser.Token.VALUE_NULL) { if (context.parser().currentToken() == XContentParser.Token.VALUE_NULL) {
ipAsString = nullValue; ipAsString = fieldType().nullValueAsString();
} else { } else {
ipAsString = context.parser().text(); ipAsString = context.parser().text();
} }
@ -317,7 +301,8 @@ public class IpFieldMapper extends NumberFieldMapper {
return; return;
} }
if (!mergeResult.simulate()) { if (!mergeResult.simulate()) {
this.nullValue = ((IpFieldMapper) mergeWith).nullValue; this.fieldType = this.fieldType.clone();
this.fieldType.setNullValue(((IpFieldMapper) mergeWith).fieldType().nullValue());
} }
} }
@ -328,8 +313,8 @@ public class IpFieldMapper extends NumberFieldMapper {
if (includeDefaults || fieldType.numericPrecisionStep() != Defaults.PRECISION_STEP_64_BIT) { if (includeDefaults || fieldType.numericPrecisionStep() != Defaults.PRECISION_STEP_64_BIT) {
builder.field("precision_step", fieldType.numericPrecisionStep()); builder.field("precision_step", fieldType.numericPrecisionStep());
} }
if (includeDefaults || nullValue != null) { if (includeDefaults || fieldType().nullValueAsString() != null) {
builder.field("null_value", nullValue); builder.field("null_value", fieldType().nullValueAsString());
} }
if (includeInAll != null) { if (includeInAll != null) {
builder.field("include_in_all", includeInAll); builder.field("include_in_all", includeInAll);