Apache Solr open-source search software
SOLR-1270: make sure that plain numeric types produce correct transfer syntax for JSON, Python, Ruby, and other TextWriter fields
Yonik Seeleye918778c4853
diff --git a/src/java/org/apache/solr/schema/ByteField.java b/src/java/org/apache/solr/schema/ByteField.javaindex e442642050a..ecb4d37966c 100644--- a/src/java/org/apache/solr/schema/ByteField.java+++ b/src/java/org/apache/solr/schema/ByteField.java@@ -28,7 +28,7 @@ import java.io.IOException;import java.util.Map;/**- * @version $Id: LongField.java 555343 2007-07-11 17:46:25Z hossman $+ * @version $Id:$*/public class ByteField extends FieldType {protected void init(IndexSchema schema, Map<String, String> args) {@@ -50,7 +50,27 @@ public class ByteField extends FieldType {}public void write(TextResponseWriter writer, String name, Fieldable f) throws IOException {- writer.writeByte(name, f.stringValue());+ String s = f.stringValue();++ // these values may be from a legacy lucene index, which may+ // not be properly formatted in some output formats, or may+ // incorrectly have a zero length.++ if (s.length()==0) {+ // zero length value means someone mistakenly indexed the value+ // instead of simply leaving it out. Write a null value instead of a numeric.+ writer.writeNull(name);+ return;+ }++ try {+ byte val = Byte.parseByte(s);+ writer.writeByte(name, val);+ } catch (NumberFormatException e){+ // can't parse - write out the contents as a string so nothing is lost and+ // clients don't get a parse error.+ writer.writeStr(name, s, true);+ }}@Overridediff --git a/src/java/org/apache/solr/schema/DoubleField.java b/src/java/org/apache/solr/schema/DoubleField.javaindex 7104742d4b8..832ada3d3a2 100644--- a/src/java/org/apache/solr/schema/DoubleField.java+++ b/src/java/org/apache/solr/schema/DoubleField.java@@ -50,7 +50,27 @@ public class DoubleField extends FieldType {}public void write(TextResponseWriter writer, String name, Fieldable f) throws IOException {- writer.writeDouble(name, f.stringValue());+ String s = f.stringValue();++ // these values may be from a legacy lucene index, which may+ // not be properly formatted in some output formats, or may+ // incorrectly have a zero length.++ if (s.length()==0) {+ // zero length value means someone mistakenly indexed the value+ // instead of simply leaving it out. Write a null value instead of a numeric.+ writer.writeNull(name);+ return;+ }++ try {+ double val = Double.parseDouble(s);+ writer.writeDouble(name, val);+ } catch (NumberFormatException e){+ // can't parse - write out the contents as a string so nothing is lost and+ // clients don't get a parse error.+ writer.writeStr(name, s, true);+ }}diff --git a/src/java/org/apache/solr/schema/IntField.java b/src/java/org/apache/solr/schema/IntField.javaindex 73bcda01893..15d2e3a95c1 100644--- a/src/java/org/apache/solr/schema/IntField.java+++ b/src/java/org/apache/solr/schema/IntField.java@@ -48,22 +48,26 @@ public class IntField extends FieldType {public void write(TextResponseWriter writer, String name, Fieldable f) throws IOException {String s = f.stringValue();- int len = s.length();- // these values may be from a legacy lucene index, which may contain- // integer values padded with zeros, or a zero length value.- if (len>=2) {- char ch = s.charAt(0);- if ((ch=='0') || (ch=='-' && s.charAt(1)=='0')) {- s = Integer.toString(Integer.parseInt(s));- }- } else if (len == 0) {++ // these values may be from a legacy lucene index, which may+ // not be properly formatted in some output formats, or may+ // incorrectly have a zero length.++ if (s.length()==0) {// zero length value means someone mistakenly indexed the value- // instead of simply leaving it out. Write a null value instead- // of an integer value in this case.+ // instead of simply leaving it out. Write a null value instead of a numeric.writer.writeNull(name);return;}- writer.writeInt(name, s);++ try {+ int val = Integer.parseInt(s);+ writer.writeInt(name, val);+ } catch (NumberFormatException e){+ // can't parse - write out the contents as a string so nothing is lost and+ // clients don't get a parse error.+ writer.writeStr(name, s, true);+ }}@Overridediff --git a/src/java/org/apache/solr/schema/LongField.java b/src/java/org/apache/solr/schema/LongField.javaindex fe8441e9ee4..3f3317770d7 100644--- a/src/java/org/apache/solr/schema/LongField.java+++ b/src/java/org/apache/solr/schema/LongField.java@@ -55,22 +55,26 @@ public class LongField extends FieldType {@Overridepublic void write(TextResponseWriter writer, String name, Fieldable f) throws IOException {String s = f.stringValue();- int len = s.length();- // these values may be from a legacy lucene index, which may contain- // integer values padded with zeros, or a zero length value.- if (len>=2) {- char ch = s.charAt(0);- if ((ch=='0') || (ch=='-' && s.charAt(1)=='0')) {- s = Long.toString(Long.parseLong(s));- }- } else if (len == 0) {++ // these values may be from a legacy lucene index, which may+ // not be properly formatted in some output formats, or may+ // incorrectly have a zero length.++ if (s.length()==0) {// zero length value means someone mistakenly indexed the value- // instead of simply leaving it out. Write a null value instead- // of an integer value in this case.+ // instead of simply leaving it out. Write a null value instead of a numeric.writer.writeNull(name);return;}- writer.writeLong(name, s);++ try {+ long val = Long.parseLong(s);+ writer.writeLong(name, val);+ } catch (NumberFormatException e){+ // can't parse - write out the contents as a string so nothing is lost and+ // clients don't get a parse error.+ writer.writeStr(name, s, true);+ }}@Overridediff --git a/src/java/org/apache/solr/schema/ShortField.java b/src/java/org/apache/solr/schema/ShortField.javaindex af9c8a2cd4d..7451455c919 100644--- a/src/java/org/apache/solr/schema/ShortField.java+++ b/src/java/org/apache/solr/schema/ShortField.java@@ -56,7 +56,27 @@ public class ShortField extends FieldType {@Overridepublic void write(TextResponseWriter writer, String name, Fieldable f) throws IOException {- writer.writeShort(name, f.stringValue());+ String s = f.stringValue();++ // these values may be from a legacy lucene index, which may+ // not be properly formatted in some output formats, or may+ // incorrectly have a zero length.++ if (s.length()==0) {+ // zero length value means someone mistakenly indexed the value+ // instead of simply leaving it out. Write a null value instead of a numeric.+ writer.writeNull(name);+ return;+ }++ try {+ short val = Short.parseShort(s);+ writer.writeShort(name, val);+ } catch (NumberFormatException e){+ // can't parse - write out the contents as a string so nothing is lost and+ // clients don't get a parse error.+ writer.writeStr(name, s, true);+ }}@Override