From e918778c48531ebac835da135ca6463e97739ccb Mon Sep 17 00:00:00 2001 From: Yonik Seeley Date: Tue, 4 Aug 2009 20:51:12 +0000 Subject: [PATCH] SOLR-1270: make sure that plain numeric types produce correct transfer syntax for JSON, Python, Ruby, and other TextWriter fields git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@800966 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/solr/schema/ByteField.java | 24 ++++++++++++++-- .../org/apache/solr/schema/DoubleField.java | 22 ++++++++++++++- src/java/org/apache/solr/schema/IntField.java | 28 +++++++++++-------- .../org/apache/solr/schema/LongField.java | 28 +++++++++++-------- .../org/apache/solr/schema/ShortField.java | 22 ++++++++++++++- 5 files changed, 96 insertions(+), 28 deletions(-) diff --git a/src/java/org/apache/solr/schema/ByteField.java b/src/java/org/apache/solr/schema/ByteField.java index 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 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); + } } @Override diff --git a/src/java/org/apache/solr/schema/DoubleField.java b/src/java/org/apache/solr/schema/DoubleField.java index 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.java index 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); + } } @Override diff --git a/src/java/org/apache/solr/schema/LongField.java b/src/java/org/apache/solr/schema/LongField.java index 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 { @Override 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 = 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); + } } @Override diff --git a/src/java/org/apache/solr/schema/ShortField.java b/src/java/org/apache/solr/schema/ShortField.java index 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 { @Override public 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