From bff980c79785461d667097550b7cb8eaf86b803a Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Mon, 21 Nov 2011 18:02:12 +0200 Subject: [PATCH] Allow empty Strings to be null for Number's and don't autodetect empty string fields as string types, closes #1473. --- .../index/mapper/core/ByteFieldMapper.java | 13 ++++++++++++- .../index/mapper/core/DoubleFieldMapper.java | 13 ++++++++++++- .../index/mapper/core/FloatFieldMapper.java | 13 ++++++++++++- .../index/mapper/core/IntegerFieldMapper.java | 13 ++++++++++++- .../index/mapper/core/LongFieldMapper.java | 13 ++++++++++++- .../index/mapper/core/ShortFieldMapper.java | 13 ++++++++++++- .../index/mapper/object/ObjectMapper.java | 5 +++++ 7 files changed, 77 insertions(+), 6 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/core/ByteFieldMapper.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/core/ByteFieldMapper.java index 529f97a4cbf..cb90c341aa0 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/core/ByteFieldMapper.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/core/ByteFieldMapper.java @@ -189,6 +189,16 @@ public class ByteFieldMapper extends NumberFieldMapper { 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 { } } 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; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/core/DoubleFieldMapper.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/core/DoubleFieldMapper.java index 2542071c8ce..419f275ee44 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/core/DoubleFieldMapper.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/core/DoubleFieldMapper.java @@ -189,6 +189,16 @@ public class DoubleFieldMapper extends NumberFieldMapper { 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 { } } 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; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/core/FloatFieldMapper.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/core/FloatFieldMapper.java index ee3726bc9e5..f57c221f1c0 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/core/FloatFieldMapper.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/core/FloatFieldMapper.java @@ -184,6 +184,16 @@ public class FloatFieldMapper extends NumberFieldMapper { 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 { } } 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; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/core/IntegerFieldMapper.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/core/IntegerFieldMapper.java index 0b7fca922eb..c87db76efc7 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/core/IntegerFieldMapper.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/core/IntegerFieldMapper.java @@ -189,6 +189,16 @@ public class IntegerFieldMapper extends NumberFieldMapper { 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 { } } 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; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/core/LongFieldMapper.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/core/LongFieldMapper.java index 8b5929d3724..aac5b9d5568 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/core/LongFieldMapper.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/core/LongFieldMapper.java @@ -189,6 +189,16 @@ public class LongFieldMapper extends NumberFieldMapper { 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 { } } 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; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/core/ShortFieldMapper.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/core/ShortFieldMapper.java index 99e4f37c924..59a81624c93 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/core/ShortFieldMapper.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/core/ShortFieldMapper.java @@ -189,6 +189,16 @@ public class ShortFieldMapper extends NumberFieldMapper { 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 { } } 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; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/object/ObjectMapper.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/object/ObjectMapper.java index 1a5a67bf4f7..d4f0c5ac4bf 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/object/ObjectMapper.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/object/ObjectMapper.java @@ -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