Allow empty Strings to be null for Number's and don't autodetect empty string fields as string types, closes #1473.

This commit is contained in:
Shay Banon 2011-11-21 18:02:12 +02:00
parent fbb03c611a
commit bff980c797
7 changed files with 77 additions and 6 deletions

View File

@ -189,6 +189,16 @@ public class ByteFieldMapper extends NumberFieldMapper<Byte> {
return null;
}
value = nullValue;
} else if (externalValue instanceof String) {
String sExternalValue = (String) externalValue;
if (sExternalValue.length() == 0) {
if (nullValue == null) {
return null;
}
value = nullValue;
} else {
value = Byte.parseByte(sExternalValue);
}
} else {
value = ((Number) externalValue).byteValue();
}
@ -197,7 +207,8 @@ public class ByteFieldMapper extends NumberFieldMapper<Byte> {
}
} else {
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)) {
if (nullValue == null) {
return null;
}

View File

@ -189,6 +189,16 @@ public class DoubleFieldMapper extends NumberFieldMapper<Double> {
return null;
}
value = nullValue;
} else if (externalValue instanceof String) {
String sExternalValue = (String) externalValue;
if (sExternalValue.length() == 0) {
if (nullValue == null) {
return null;
}
value = nullValue;
} else {
value = Double.parseDouble(sExternalValue);
}
} else {
value = ((Number) externalValue).doubleValue();
}
@ -197,7 +207,8 @@ public class DoubleFieldMapper extends NumberFieldMapper<Double> {
}
} else {
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)) {
if (nullValue == null) {
return null;
}

View File

@ -184,6 +184,16 @@ public class FloatFieldMapper extends NumberFieldMapper<Float> {
return null;
}
value = nullValue;
} else if (externalValue instanceof String) {
String sExternalValue = (String) externalValue;
if (sExternalValue.length() == 0) {
if (nullValue == null) {
return null;
}
value = nullValue;
} else {
value = Float.parseFloat(sExternalValue);
}
} else {
value = ((Number) externalValue).floatValue();
}
@ -192,7 +202,8 @@ public class FloatFieldMapper extends NumberFieldMapper<Float> {
}
} else {
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)) {
if (nullValue == null) {
return null;
}

View File

@ -189,6 +189,16 @@ public class IntegerFieldMapper extends NumberFieldMapper<Integer> {
return null;
}
value = nullValue;
} else if (externalValue instanceof String) {
String sExternalValue = (String) externalValue;
if (sExternalValue.length() == 0) {
if (nullValue == null) {
return null;
}
value = nullValue;
} else {
value = Integer.parseInt(sExternalValue);
}
} else {
value = ((Number) externalValue).intValue();
}
@ -197,7 +207,8 @@ public class IntegerFieldMapper extends NumberFieldMapper<Integer> {
}
} else {
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)) {
if (nullValue == null) {
return null;
}

View File

@ -189,6 +189,16 @@ public class LongFieldMapper extends NumberFieldMapper<Long> {
return null;
}
value = nullValue;
} else if (externalValue instanceof String) {
String sExternalValue = (String) externalValue;
if (sExternalValue.length() == 0) {
if (nullValue == null) {
return null;
}
value = nullValue;
} else {
value = Long.parseLong(sExternalValue);
}
} else {
value = ((Number) externalValue).longValue();
}
@ -197,7 +207,8 @@ public class LongFieldMapper extends NumberFieldMapper<Long> {
}
} else {
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)) {
if (nullValue == null) {
return null;
}

View File

@ -189,6 +189,16 @@ public class ShortFieldMapper extends NumberFieldMapper<Short> {
return null;
}
value = nullValue;
} else if (externalValue instanceof String) {
String sExternalValue = (String) externalValue;
if (sExternalValue.length() == 0) {
if (nullValue == null) {
return null;
}
value = nullValue;
} else {
value = Short.parseShort(sExternalValue);
}
} else {
value = ((Number) externalValue).shortValue();
}
@ -197,7 +207,8 @@ public class ShortFieldMapper extends NumberFieldMapper<Short> {
}
} else {
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)) {
if (nullValue == null) {
return null;
}

View File

@ -610,6 +610,11 @@ public class ObjectMapper implements Mapper, AllFieldMapper.IncludeInAll {
}
}
if (!resolved && context.parser().textLength() == 0) {
// empty string with no mapping, treat it like null value
return;
}
if (!resolved && context.root().dateDetection()) {
String text = context.parser().text();
// a safe check since "1" gets parsed as well